Ejemplo n.º 1
0
 def test_unreverted(self):
     """ Test when the patch modifies unreverted files """
     with tmp_series() as [dir, series]:
         old_dir = os.getcwd()
         try:
             os.chdir(dir)
             db = Db(dir)
             db.add_patch(Patch("patch"))
             db.save()
             originals = os.path.join(db.dirname, "patch")
             os.mkdir(originals)
             make_file(b"unreverted original\n", originals, "unreverted")
             make_file(b"reverted original\n", originals, "reverted")
             make_file(b"unreverted patched\n", dir, "unreverted")
             make_file(b"reverted patched\n", dir, "reverted")
             Refresh(dir, db.dirname, series.dirname).refresh()
             make_file(b"unreverted change\n", dir, "unreverted")
             make_file(b"reverted change\n", dir, "reverted")
             cmd = quilt.revert.Revert(dir, db.dirname, series.dirname)
             cmd.revert_file("reverted")
             with open(os.path.join(dir, "reverted"), "rb") as file:
                 self.assertEqual(file.read(), b"reverted patched\n")
             with open(os.path.join(dir, "unreverted"), "rb") as file:
                 self.assertEqual(file.read(), b"unreverted change\n")
         finally:
             os.chdir(old_dir)
Ejemplo n.º 2
0
 def test_unreverted(self):
     """ Test when the patch modifies unreverted files """
     with tmp_series() as [dir, series]:
         old_dir = os.getcwd()
         try:
             os.chdir(dir)
             db = Db(dir)
             db.add_patch(Patch("patch"))
             db.save()
             originals = os.path.join(db.dirname, "patch")
             os.mkdir(originals)
             make_file(b"unreverted original\n", originals, "unreverted")
             make_file(b"reverted original\n", originals, "reverted")
             make_file(b"unreverted patched\n", dir, "unreverted")
             make_file(b"reverted patched\n", dir, "reverted")
             Refresh(dir, db.dirname, series.dirname).refresh()
             make_file(b"unreverted change\n", dir, "unreverted")
             make_file(b"reverted change\n", dir, "reverted")
             cmd = quilt.revert.Revert(dir, db.dirname, series.dirname)
             cmd.revert_file("reverted")
             with open(os.path.join(dir, "reverted"), "rb") as file:
                 self.assertEqual(file.read(), b"reverted patched\n")
             with open(os.path.join(dir, "unreverted"), "rb") as file:
                 self.assertEqual(file.read(), b"unreverted change\n")
         finally:
             os.chdir(old_dir)
Ejemplo n.º 3
0
 def test_unrefreshed(self):
     with TmpDirectory() as dir:
         db = Db(dir.get_name())
         db.add_patch(Patch("unrefreshed.patch"))
         db.save()
         make_file(b"", db.dirname, "unrefreshed.patch~refresh")
         cmd = Pop(dir.get_name(), db.dirname)
         with six.assertRaisesRegex(self, QuiltError,
                 r"needs to be refreshed"):
             cmd.unapply_top_patch()
Ejemplo n.º 4
0
 def test_next_after(self):
     """ Delete the successor to the topmost patch """
     with tmp_series() as [dir, series]:
         series.add_patch(Patch("topmost"))
         series.add_patch(Patch("unapplied"))
         series.save()
         db = Db(dir)
         db.add_patch(Patch("topmost"))
         db.save()
         cmd = Delete(dir, db.dirname, series.dirname)
         cmd.delete_next()
         series.read()
         [patch] = series.patches()
         self.assertEqual(patch, Patch("topmost"))
Ejemplo n.º 5
0
 def test_series_v(self):
     with tmp_series() as [dir, series]:
         applied = Db(dir)
         applied.add_patch(Patch("applied.patch"))
         applied.add_patch(Patch("topmost.patch"))
         applied.save()
         series.add_patches(applied.applied_patches())
         series.add_patch(Patch("unapplied.patch"))
         series.save()
         output = run_cli(SeriesCommand, dict(v=True),
             series.dirname, applied.dirname)
     self.assertMultiLineEqual(output,
         "+ applied.patch\n"
         "= topmost.patch\n"
         "  unapplied.patch\n")
Ejemplo n.º 6
0
class New(Command):

    patch_created = Signal()

    """ Creates a new patch in the queue """
    def __init__(self, cwd, quilt_pc, quilt_patches):
        super(New, self).__init__(cwd)
        self.quilt_pc = Directory(quilt_pc)
        self.quilt_patches = Directory(quilt_patches)
        self.db = Db(quilt_pc)
        self.series = Series(quilt_patches)

    def create(self, patchname):
        """ Adds a new patch with patchname to the queue

        The new patch will be added as the topmost applied patch.
        """
        patch = Patch(patchname)
        if self.series.is_patch(patch):
            raise PatchAlreadyExists(self.series, patchname)

        patch_dir = self.quilt_patches
        patch_dir.create()
        patchfile = patch_dir + File(patchname)
        patchfile.touch()

        pc_dir = self.quilt_pc + patchname
        if pc_dir.exists():
            # be sure that the directory is clear
            pc_dir.delete()

        # create empty .pc/<patchname> directory as quilt does too
        pc_dir.create()

        top = self.db.top_patch()
        # add new patch after the current topmost applied patch
        self.series.add_patches([patch], top)
        # "apply" patch
        self.db.add_patch(patch)

        # create patches/series files
        self.series.save()
        # create .pc/.version and .pc/applied-patches files
        self.db.save()

        self.patch_created(patch)
Ejemplo n.º 7
0
class New(Command):

    patch_created = Signal()
    """ Creates a new patch in the queue """
    def __init__(self, cwd, quilt_pc, quilt_patches):
        super(New, self).__init__(cwd)
        self.quilt_pc = Directory(quilt_pc)
        self.quilt_patches = Directory(quilt_patches)
        self.db = Db(quilt_pc)
        self.series = Series(quilt_patches)

    def create(self, patchname):
        """ Adds a new patch with patchname to the queue

        The new patch will be added as the topmost applied patch.
        """
        patch = Patch(patchname)
        if self.series.is_patch(patch):
            raise PatchAlreadyExists(self.series, patchname)

        patch_dir = self.quilt_patches
        patch_dir.create()
        patchfile = patch_dir + File(patchname)
        patchfile.touch()

        pc_dir = self.quilt_pc + patchname
        if pc_dir.exists():
            # be sure that the directory is clear
            pc_dir.delete()

        # create empty .pc/<patchname> directory as quilt does too
        pc_dir.create()

        top = self.db.top_patch()
        # add new patch after the current topmost applied patch
        self.series.add_patches([patch], top)
        # "apply" patch
        self.db.add_patch(patch)

        # create patches/series files
        self.series.save()
        # create .pc/.version and .pc/applied-patches files
        self.db.save()

        self.patch_created(patch)
Ejemplo n.º 8
0
 def test_refresh(self):
     with TmpDirectory() as dir:
         old_dir = os.getcwd()
         try:
             os.chdir(dir.get_name())
             db = Db(".pc")
             db.create()
             backup = os.path.join(".pc", "patch")
             os.mkdir(backup)
             make_file(b"", backup, "file")
             db.add_patch(Patch("patch"))
             db.save()
             make_file(b"", "patch")
             make_file(b"added\n", "file")
             cmd = quilt.refresh.Refresh(".", ".pc", ".")
             cmd.refresh()
             with open("patch", "r") as patch:
                 self.assertTrue(patch.read(30))
         finally:
             os.chdir(old_dir)
Ejemplo n.º 9
0
 def test_refresh(self):
     with TmpDirectory() as dir:
         old_dir = os.getcwd()
         try:
             os.chdir(dir.get_name())
             db = Db(".pc")
             db.create()
             backup = os.path.join(".pc", "patch")
             os.mkdir(backup)
             make_file(b"", backup, "file")
             db.add_patch(Patch("patch"))
             db.save()
             make_file(b"", "patch")
             make_file(b"added\n", "file")
             cmd = quilt.refresh.Refresh(".", ".pc", ".")
             cmd.refresh()
             with open("patch", "r") as patch:
                 self.assertTrue(patch.read(30))
         finally:
             os.chdir(old_dir)
Ejemplo n.º 10
0
class Delete(Command):

    """Command class to delete patches
    """

    deleting_patch = Signal()
    deleted_patch = Signal()

    def __init__(self, cwd, quilt_pc, quilt_patches):
        super(Delete, self).__init__(cwd)
        self.quilt_pc = Directory(quilt_pc)
        self.quilt_patches = Directory(quilt_patches)
        self.db = Db(quilt_pc)
        self.series = Series(quilt_patches)
        self.pop = Pop(cwd, quilt_pc)

    def _delete_patch(self, patch, remove=False, backup=False):
        if self.series.is_empty():
            raise NoPatchesInSeries(self.series)
        if not self.series.is_patch(patch):
            raise UnknownPatch(self.series, patch)

        applied = self.db.top_patch() == patch
        self.deleting_patch(patch, applied)

        if applied:
            self.pop._unapply_patch(patch)
            self.db = self.pop.db
            self.db.save()

        self.series.remove_patch(patch)
        self.series.save()

        patch_file = self.quilt_patches + File(patch.get_name())

        if remove:
            if backup:
                patch_file.copy(File(patch_file.get_name() + "~"))

            patch_file.delete_if_exists()

        self.deleted_patch(patch)

    def delete_next(self, remove=False, backup=False):
        """ Delete next unapplied patch
        If remove is True the patch file will also be removed. If remove and
        backup are True a copy of the deleted patch file will be made.
        """
        patch = self.db.top_patch()
        if patch:
            after = self.series.patch_after(patch)
        else:
            after = self.series.first_patch()
        if not after:
            raise QuiltError("No next patch")

        self._delete_patch(after, remove=remove, backup=backup)

    def delete_patch(self, patch_name=None, remove=False, backup=False):
        """ Delete specified patch from the series
        If remove is True the patch file will also be removed. If remove and
        backup are True a copy of the deleted patch file will be made.
        """
        if patch_name:
            patch = Patch(patch_name)
        else:
            patch = self.db.top_patch()
            if not patch:
                raise NoAppliedPatch(self.db)

        self._delete_patch(patch, remove=remove, backup=backup)
Ejemplo n.º 11
0
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())
Ejemplo n.º 12
0
class Delete(Command):
    """Command class to delete patches
    """

    deleting_patch = Signal()
    deleted_patch = Signal()

    def __init__(self, cwd, quilt_pc, quilt_patches):
        super(Delete, self).__init__(cwd)
        self.quilt_pc = Directory(quilt_pc)
        self.quilt_patches = Directory(quilt_patches)
        self.db = Db(quilt_pc)
        self.series = Series(quilt_patches)
        self.pop = Pop(cwd, quilt_pc)

    def _delete_patch(self, patch, remove=False, backup=False):
        if self.series.is_empty():
            raise NoPatchesInSeries(self.series)
        if not self.series.is_patch(patch):
            raise UnknownPatch(self.series, patch)

        applied = self.db.top_patch() == patch
        self.deleting_patch(patch, applied)

        if applied:
            self.pop._unapply_patch(patch)
            self.db = self.pop.db
            self.db.save()

        self.series.remove_patch(patch)
        self.series.save()

        patch_file = self.quilt_patches + File(patch.get_name())

        if remove:
            if backup:
                patch_file.copy(File(patch_file.get_name() + "~"))

            patch_file.delete_if_exists()

        self.deleted_patch(patch)

    def delete_next(self, remove=False, backup=False):
        """ Delete next unapplied patch
        If remove is True the patch file will also be removed. If remove and
        backup are True a copy of the deleted patch file will be made.
        """
        patch = self.db.top_patch()
        if patch:
            after = self.series.patch_after(patch)
        else:
            after = self.series.first_patch()
        if not after:
            raise QuiltError("No next patch")

        self._delete_patch(after, remove=remove, backup=backup)

    def delete_patch(self, patch_name=None, remove=False, backup=False):
        """ Delete specified patch from the series
        If remove is True the patch file will also be removed. If remove and
        backup are True a copy of the deleted patch file will be made.
        """
        if patch_name:
            patch = Patch(patch_name)
        else:
            patch = self.db.top_patch()
            if not patch:
                raise NoAppliedPatch(self.db)

        self._delete_patch(patch, remove=remove, backup=backup)
Ejemplo n.º 13
0
class Push(Command):

    applying = Signal()
    applying_patch = Signal()
    applied = Signal()
    applied_patch = Signal()
    applied_empty_patch = Signal()

    def __init__(self, cwd, quilt_pc, quilt_patches):
        super(Push, self).__init__(cwd)
        self.quilt_pc = Directory(quilt_pc)
        self.quilt_patches = Directory(quilt_patches)
        self.db = Db(quilt_pc)
        self.series = Series(quilt_patches)

    def _apply_patch(self, patch, force=False, quiet=False):
        patch_name = patch.get_name()
        pc_dir = self.quilt_pc + patch_name
        patch_file = self.quilt_patches + File(patch_name)
        refresh = File(pc_dir.get_name() + "~refresh")

        forced = False
        self.applying_patch(patch)

        if patch_file.exists():
            try:
                patch.run(self.cwd, patch_dir=self.quilt_patches, backup=True,
                          prefix=pc_dir.get_name(), quiet=quiet)
            except SubprocessError as e:
                if not force:
                    patch = RollbackPatch(self.cwd, pc_dir)
                    patch.rollback()
                    patch.delete_backup()
                    raise QuiltError("Patch %s does not apply" % patch_name)
                else:
                    refresh.touch()
                    forced = True

        self.db.add_patch(patch)

        if pc_dir.exists():
            timestamp = pc_dir + File(".timestamp")
            timestamp.touch()
        else:
            pc_dir.create()

        if not patch_file.exists():
            self.applied_empty_patch(patch, False)
        elif pc_dir.is_empty():
            self.applied_empty_patch(patch, True)
        elif forced:
            raise QuiltError("Applied patch %s (forced; needs refresh)" %
                             patch.get_name())
        else:
            self.applied_patch(patch)

    def _check(self):
        if not self.series.exists() or not self.series.patches():
            raise NoPatchesInSeries(self.series)
        
        top = self.db.top_patch()
        if top is not None:
            refresh = top.get_name() + "~refresh"
            refresh = os.path.join(self.quilt_pc.get_name(), refresh)
            if os.path.exists(refresh):
                raise QuiltError("Patch %s needs to be refreshed" % \
                                      top.get_name())

    def apply_patch(self, patch_name, force=False, quiet=False):
        """ Apply all patches up to patch_name """
        self._check()
        patch = Patch(patch_name)
        patches = self.series.patches_until(patch)[:]

        applied = self.db.applied_patches()
        for patch in applied:
            if patch in patches:
                patches.remove(patch)

        if not patches:
            raise AllPatchesApplied(self.series, self.db.top_patch())

        self.applying(patch)

        try:
            for cur_patch in patches:
                self._apply_patch(cur_patch, force, quiet)
        finally:
            self.db.save()

        self.applied(self.db.top_patch())

    def apply_next_patch(self, force=False, quiet=False):
        """ Apply next patch in series file """
        self._check()
        top = self.db.top_patch()
        if not top:
            patch = self.series.first_patch()
        else:
            patch = self.series.patch_after(top)

        if not patch:
            raise AllPatchesApplied(self.series, top)

        self.applying(patch)

        self._apply_patch(patch, force, quiet)

        self.db.save()

        self.applied(self.db.top_patch())

    def apply_all(self, force=False, quiet=False):
        """ Apply all patches in series file """
        self._check()
        top = self.db.top_patch()
        if top:
            patches = self.series.patches_after(top)
        else:
            patches = self.series.patches()

        if not patches:
            raise AllPatchesApplied(self.series, top)

        try:
            for patch in patches:
                self.applying(patch)
                self._apply_patch(patch, force, quiet)
        finally:
            self.db.save()

        self.applied(self.db.top_patch())