# Path: quilt/cli/parser.py
# class Argument(object):
# 
#     """
#     A class to declare positional arguments at a class
# 
#     Usage:
#         class MyParser(Parser):
# 
#             arg1 = Argument(help="A first string argument")
#             arg2 = Argument(type=int)
#             arg3 = Argument(nargs=2)
#     """
# 
#     creation_counter = 0
# 
#     def __init__(self, *args, **kwargs):
#         """
#         Constructs an Argument instance
# 
#         args and kwargs are passed directly to
#         argparse.ArgumentParser.add_argument
#         """
#         self.args = args
#         self.kwargs = kwargs
#         self.creation_counter = Argument.creation_counter
#         Argument.creation_counter += 1
# 
#     def _get_kwargs(self):
#         if self.args:
#             return self.kwargs
# 
#         kwargs = {"dest": self.name}
#         kwargs.update(self.kwargs)
#         return kwargs
# 
#     def _get_args(self):
#         return self.args
# 
#     def add_to_parser(self, parser):
#         """
#         Adds the argument to an argparse.ArgumentParser instance
# 
#         @param parser An argparse.ArgumentParser instance
#         """
#         kwargs = self._get_kwargs()
#         args = self._get_args()
#         parser.add_argument(*args, **kwargs)
# 
#     def set_name(self, name):
#         """
#         Sets the name of this Argument.
#         Normally this method should not be called directly.
#         It is used by the ArgumentsCollectorMetaClass.
# 
#         @param name A string for a name
#         """
#         self.name = name
# 
# class OptionArgument(Argument):
# 
#     """
#     A class to declare (optional) arguments at a class
# 
#     Usage:
#         class MyParser(Parser):
# 
#             arg1 = OptionArgument(help="A first string argument")
#             arg2 = OptionArgument(type=int)
#             arg3 = OptionArgument(nargs=2)
#             arg4 = OptionArgument(required=True)
#     
#     By default, the option name is taken from the attribute name and prefixed
#     with two dashes, or one dash if the name is a single character:
#     
#     verbose = OptionArgument(action="store_true")  # usage: [--verbose]
#     v = OptionArgument(action="store_true")  # usage: [-v]
#     """
# 
#     prefix_chars = "--"
# 
#     def _get_args(self):
#         args = self.args
#         if not args:
#             if self.prefix_chars == "--" and len(self.name) == 1:
#                 args = ("-" + self.name,)
#             else:
#                 args = (self.prefix_chars + self.name,)
#         return args
# 
# Path: quilt/pop.py
# class Pop(Command):
# 
#     unapplying = Signal()
#     unapplied = Signal()
#     unapplied_patch = Signal()
#     empty_patch = Signal()
# 
#     def __init__(self, cwd, quilt_pc):
#         super(Pop, self).__init__(cwd)
#         self.quilt_pc = Directory(quilt_pc)
#         self.db = Db(quilt_pc)
# 
#     def _check(self, force=False):
#         if not self.db.exists() or not self.db.patches():
#             raise NoAppliedPatch(self.db)
#         if not force:
#             patch = self.db.top_patch()
#             pc_dir = self.quilt_pc + patch.get_name()
#             refresh = File(pc_dir.get_name() + "~refresh")
#             if refresh.exists():
#                 raise QuiltError("Patch %s needs to be refreshed first." %
#                                  patch.get_name())
# 
#     def _unapply_patch(self, patch):
#         self.unapplying(patch)
# 
#         patch_name = patch.get_name()
#         pc_dir = self.quilt_pc + patch_name
#         timestamp = pc_dir + File(".timestamp")
#         timestamp.delete_if_exists()
# 
#         if pc_dir.is_empty():
#             pc_dir.delete()
#             self.empty_patch(patch)
#         else:
#             unpatch = RollbackPatch(self.cwd, pc_dir)
#             unpatch.rollback()
#             unpatch.delete_backup()
# 
#         self.db.remove_patch(patch)
# 
#         refresh = File(pc_dir.get_name() + "~refresh")
#         refresh.delete_if_exists()
# 
#         self.unapplied_patch(patch)
# 
#     def unapply_patch(self, patch_name, force=False):
#         """ Unapply patches up to patch_name. patch_name will end up as top
#             patch """
#         self._check(force)
# 
#         patches = self.db.patches_after(Patch(patch_name))
#         for patch in reversed(patches):
#             self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())
# 
#     def unapply_top_patch(self, force=False):
#         """ Unapply top patch """
#         self._check(force)
# 
#         patch = self.db.top_patch()
#         self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())
# 
#     def unapply_all(self, force=False):
#         """ Unapply all patches """
#         self._check(force)
# 
#         for patch in reversed(self.db.applied_patches()):
#             self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())


import os
from quilt.cli.meta import Command
from quilt.cli.parser import Argument, OptionArgument
from quilt.pop import Pop
# vim: fileencoding=utf-8 et sw=4 ts=4 tw=80:

# python-quilt - A Python implementation of the quilt patch system
#
# Copyright (C) 2012 - 2017 Björn Ricks <bjoern.ricks@gmail.com>
#
# See LICENSE comming with the source of python-quilt for details.




class PopCommand(Command):

    name = "pop"
    help = "Remove patch(es) from the stack of applied patches."

    all = OptionArgument("-a", "--all", dest="all", action="store_true",
                         help="remove all applied patches")

    patch = Argument(nargs="?")

    def run(self, args):
        pop = Pop(os.getcwd(), self.get_pc_dir())
        pop.unapplying.connect(self.unapplying)

# Path: quilt/cli/parser.py
# class Argument(object):
# 
#     """
#     A class to declare positional arguments at a class
# 
#     Usage:
#         class MyParser(Parser):
# 
#             arg1 = Argument(help="A first string argument")
#             arg2 = Argument(type=int)
#             arg3 = Argument(nargs=2)
#     """
# 
#     creation_counter = 0
# 
#     def __init__(self, *args, **kwargs):
#         """
#         Constructs an Argument instance
# 
#         args and kwargs are passed directly to
#         argparse.ArgumentParser.add_argument
#         """
#         self.args = args
#         self.kwargs = kwargs
#         self.creation_counter = Argument.creation_counter
#         Argument.creation_counter += 1
# 
#     def _get_kwargs(self):
#         if self.args:
#             return self.kwargs
# 
#         kwargs = {"dest": self.name}
#         kwargs.update(self.kwargs)
#         return kwargs
# 
#     def _get_args(self):
#         return self.args
# 
#     def add_to_parser(self, parser):
#         """
#         Adds the argument to an argparse.ArgumentParser instance
# 
#         @param parser An argparse.ArgumentParser instance
#         """
#         kwargs = self._get_kwargs()
#         args = self._get_args()
#         parser.add_argument(*args, **kwargs)
# 
#     def set_name(self, name):
#         """
#         Sets the name of this Argument.
#         Normally this method should not be called directly.
#         It is used by the ArgumentsCollectorMetaClass.
# 
#         @param name A string for a name
#         """
#         self.name = name
# 
# class OptionArgument(Argument):
# 
#     """
#     A class to declare (optional) arguments at a class
# 
#     Usage:
#         class MyParser(Parser):
# 
#             arg1 = OptionArgument(help="A first string argument")
#             arg2 = OptionArgument(type=int)
#             arg3 = OptionArgument(nargs=2)
#             arg4 = OptionArgument(required=True)
#     
#     By default, the option name is taken from the attribute name and prefixed
#     with two dashes, or one dash if the name is a single character:
#     
#     verbose = OptionArgument(action="store_true")  # usage: [--verbose]
#     v = OptionArgument(action="store_true")  # usage: [-v]
#     """
# 
#     prefix_chars = "--"
# 
#     def _get_args(self):
#         args = self.args
#         if not args:
#             if self.prefix_chars == "--" and len(self.name) == 1:
#                 args = ("-" + self.name,)
#             else:
#                 args = (self.prefix_chars + self.name,)
#         return args
# 
# Path: quilt/pop.py
# class Pop(Command):
# 
#     unapplying = Signal()
#     unapplied = Signal()
#     unapplied_patch = Signal()
#     empty_patch = Signal()
# 
#     def __init__(self, cwd, quilt_pc):
#         super(Pop, self).__init__(cwd)
#         self.quilt_pc = Directory(quilt_pc)
#         self.db = Db(quilt_pc)
# 
#     def _check(self, force=False):
#         if not self.db.exists() or not self.db.patches():
#             raise NoAppliedPatch(self.db)
#         if not force:
#             patch = self.db.top_patch()
#             pc_dir = self.quilt_pc + patch.get_name()
#             refresh = File(pc_dir.get_name() + "~refresh")
#             if refresh.exists():
#                 raise QuiltError("Patch %s needs to be refreshed first." %
#                                  patch.get_name())
# 
#     def _unapply_patch(self, patch):
#         self.unapplying(patch)
# 
#         patch_name = patch.get_name()
#         pc_dir = self.quilt_pc + patch_name
#         timestamp = pc_dir + File(".timestamp")
#         timestamp.delete_if_exists()
# 
#         if pc_dir.is_empty():
#             pc_dir.delete()
#             self.empty_patch(patch)
#         else:
#             unpatch = RollbackPatch(self.cwd, pc_dir)
#             unpatch.rollback()
#             unpatch.delete_backup()
# 
#         self.db.remove_patch(patch)
# 
#         refresh = File(pc_dir.get_name() + "~refresh")
#         refresh.delete_if_exists()
# 
#         self.unapplied_patch(patch)
# 
#     def unapply_patch(self, patch_name, force=False):
#         """ Unapply patches up to patch_name. patch_name will end up as top
#             patch """
#         self._check(force)
# 
#         patches = self.db.patches_after(Patch(patch_name))
#         for patch in reversed(patches):
#             self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())
# 
#     def unapply_top_patch(self, force=False):
#         """ Unapply top patch """
#         self._check(force)
# 
#         patch = self.db.top_patch()
#         self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())
# 
#     def unapply_all(self, force=False):
#         """ Unapply all patches """
#         self._check(force)
# 
#         for patch in reversed(self.db.applied_patches()):
#             self._unapply_patch(patch)
# 
#         self.db.save()
# 
#         self.unapplied(self.db.top_patch())


# Path: quilt/cli/pop.py
import os
from quilt.cli.meta import Command
from quilt.cli.parser import Argument, OptionArgument
from quilt.pop import Pop

# vim: fileencoding=utf-8 et sw=4 ts=4 tw=80:

# python-quilt - A Python implementation of the quilt patch system
#
# Copyright (C) 2012 - 2017 Björn Ricks <bjoern.ricks@gmail.com>
#
# See LICENSE comming with the source of python-quilt for details.




class PopCommand(Command):

    name = "pop"
    help = "Remove patch(es) from the stack of applied patches."

    all = OptionArgument("-a", "--all", dest="all", action="store_true",
                         help="remove all applied patches")

    patch = Argument(nargs="?")

    def run(self, args):
        pop = Pop(os.getcwd(), self.get_pc_dir())
        pop.unapplying.connect(self.unapplying)

        pop.unapplied.connect(self.unapplied)