rpm: make SpecFile.update_patches() use new tag/macro methods
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 4 Jan 2013 12:42:25 +0000 (14:42 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Jun 2014 11:20:06 +0000 (14:20 +0300)
Use new set/delete methods for updating the patches in the spec file.
The internal patches structure is not updated and now only used when
importing patches.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/rpm/__init__.py

index c01b44c5551f8a83c1f1c9e88d17b061da134559..1000302b2e892a44bb5a9ab784579f6bd5e51ae2 100644 (file)
@@ -580,99 +580,83 @@ class SpecFile(object):
     def update_patches(self, patchfilenames):
         """Update spec with new patch tags and patch macros"""
         # Remove non-ignored patches
-        last_removed_tag_line = None
-        last_removed_macro_line = None
-        for num, patch in self.patches.items():
-            if patch['autoupdate']:
+        tag_prev = None
+        macro_prev = None
+        ignored = self.ignorepatches
+        # Remove 'Patch:̈́' tags
+        for tag in self._tags['patch']['lines']:
+            if not tag['num'] in ignored:
+                tag_prev = self._delete_tag('patch', tag['num'])
                 # Remove a preceding comment if it seems to originate from GBP
-                prev_line = patch['tag_line'].prev
                 if re.match("^\s*#.*patch.*auto-generated",
-                            str(prev_line), flags=re.I):
-                    self._content.delete(prev_line)
-                last_removed_tag_line = patch['tag_line'].prev
-                self._content.delete(patch['tag_line'])
-                if patch['macro_line']:
-                    # Remove a preceding comment line if it ends with
-                    # '.patch' or '.diff' plus an optional compression suffix
-                    prev_line = patch['macro_line'].prev
-                    if re.match("^\s*#.+(patch|diff)(\.(gz|bz2|xz|lzma))?\s*$",
-                                str(prev_line), flags=re.I):
-                        self._content.delete(prev_line)
-                    last_removed_macro_line = patch['macro_line'].prev
-                    self._content.delete(patch['macro_line'])
-                # Remove from the patch list
-                self.patches.pop(num)
+                            str(tag_prev), flags=re.I):
+                    tag_prev = self._content.delete(tag_prev)
+
+        # Remove '%patch:' macros
+        for macro in self._special_directives['patch']:
+            if not macro['id'] in ignored:
+                # Remove a preceding comment line if it ends with '.patch' or
+                # '.diff' plus an optional compression suffix
+                macro_prev = self._delete_special_macro('patch', macro['id'])
+                if re.match("^\s*#.+(patch|diff)(\.(gz|bz2|xz|lzma))?\s*$",
+                            str(macro_prev), flags=re.I):
+                    macro_prev = self._content.delete(macro_prev)
 
         if len(patchfilenames) == 0:
             return
 
-        # Add new patches to the patch list
-        patchnum = sorted(self.patches.keys())[-1] + 1 if self.patches else 0
-        gbp.log.debug("Starting autoupdate patch numbering from %s" % patchnum)
-        for ind, name in enumerate(patchfilenames):
-            self.patches[patchnum + ind] = {
-                'name': name, 'filename': name, 'apply': True, 'strip': '1',
-                'macro_line': None, 'autoupdate': True, 'tag_line': None}
-
         # Determine where to add Patch tag lines
-        if last_removed_tag_line:
+        if tag_prev:
             gbp.log.debug("Adding 'Patch' tags in place of the removed tags")
-            line = last_removed_tag_line
+            tag_line = tag_prev
         elif 'patch' in self._tags:
             gbp.log.debug("Adding new 'Patch' tags after the last 'Patch' tag")
-            line = self._tags['patch']['lines'][-1]['line']
+            tag_line = self._tags['patch']['lines'][-1]['line']
         elif 'source' in self._tags:
             gbp.log.debug("Didn't find any old 'Patch' tags, adding new "
                           "patches after the last 'Source' tag.")
-            line = self._tags['source']['lines'][-1]['line']
+            tag_line = self._tags['source']['lines'][-1]['line']
         else:
             gbp.log.debug("Didn't find any old 'Patch' or 'Source' tags, "
                           "adding new patches after the last 'Name' tag.")
-            line = self._tags['name']['lines'][-1]['line']
-
-        # Add all patch tag lines to content, in reversed order
-        for n in reversed(sorted(self.patches.keys())):
-            patch = self.patches[n]
-            if patch['autoupdate']:
-                # "PatchXYZ:" text 12 chars wide, left aligned
-                text = "%-12s%s\n" % ("Patch%d:" % n, patch['name'])
-                patch['tag_line'] = self._content.insert_after(line, text)
-        # Finally, add a comment indicating gbp generated patches
-        self._content.insert_after(line, "# Patches auto-generated by "
-                                        "git-buildpackage:\n")
+            tag_line = self._tags['name']['lines'][-1]['line']
 
         # Determine where to add %patch macro lines
         if 'patch-macros' in self._gbp_tags:
             gbp.log.debug("Adding '%patch' macros after the start marker")
-            line = self._gbp_tags['patch-macros'][-1]['line']
-        elif last_removed_macro_line:
+            macro_line = self._gbp_tags['patch-macros'][-1]['line']
+        elif macro_prev:
             gbp.log.debug("Adding '%patch' macros in place of the removed "
                           "macros")
-            line = last_removed_macro_line
+            macro_line = macro_prev
         elif self._special_directives['patch']:
             gbp.log.debug("Adding new '%patch' macros after the last existing"
                           "'%patch' macro")
-            line = self._special_directives['patch'][-1]['line']
+            macro_line = self._special_directives['patch'][-1]['line']
         elif self._special_directives['setup']:
             gbp.log.debug("Didn't find any old '%patch' macros, adding new "
                           "patches after the last '%setup' macro")
-            line = self._special_directives['setup'][-1]['line']
+            macro_line = self._special_directives['setup'][-1]['line']
         elif self._special_directives['prep']:
             gbp.log.warn("Didn't find any old '%patch' or '%setup' macros, "
                          "adding new patches directly after '%prep' directive")
-            line = self._special_directives['prep'][-1]['line']
+            macro_line = self._special_directives['prep'][-1]['line']
         else:
             raise GbpError("Couldn't determine where to add '%patch' macros")
 
-        # Add all patch macro lines to content, in reversed order
-        for n in reversed(sorted(self.patches.keys())):
-            patch = self.patches[n]
-            if patch['autoupdate'] and patch['apply']:
-                # We're adding from bottom to top...
-                text = "%%patch%d -p%s\n" % (n, patch['strip'])
-                patch['macro_line'] = self._content.insert_after(line, text)
-                # Use 'name', that is filename with macros not expanded
-                self._content.insert_after(line, "# %s\n" % patch['name'])
+        startnum = sorted(ignored)[-1] + 1 if ignored else 0
+        gbp.log.debug("Starting autoupdate patch numbering from %s" % startnum)
+        # Add a comment indicating gbp generated patch tags
+        comment_text = "# Patches auto-generated by git-buildpackage:\n"
+        tag_line = self._content.insert_after(tag_line, comment_text)
+        for ind, name in enumerate(patchfilenames):
+            patchnum = startnum + ind
+            tag_line = self._set_tag("Patch", patchnum, name, tag_line)
+            # Add '%patch' macro and a preceding comment line
+            comment_text = "# %s\n" % name
+            macro_line = self._content.insert_after(macro_line, comment_text)
+            macro_line = self._set_special_macro('patch', patchnum, '-p1',
+                                                 macro_line)
 
     def patchseries(self):
         """