rpm: only remove patches inside autoupdate markers
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 12 Jan 2012 13:39:29 +0000 (15:39 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 8 May 2012 08:48:27 +0000 (11:48 +0300)
in the spec file, if autoupdate is used.

gbp/rpm/__init__.py
gbp/scripts/buildpackage_rpm.py
gbp/scripts/pq_rpm.py

index a6bc0554381ca97431c6a5413523301c10d40c86..6341bf2e4e11f8a34908a0b5a3931477057e1a20 100644 (file)
@@ -180,7 +180,7 @@ class SrcRpmFile(object):
 class SpecFile(object):
     """Class for parsing/modifying spec files"""
     source_re = re.compile(r'^Source(?P<srcnum>[0-9]+)?:\s*(?P<filename>[^\s].*[^\s])\s*$', flags=re.I)
-    patchfile_re = re.compile(r'^(Patch(?P<patchnum>[0-9]+))?:\s*(?P<filename>.+)\s*$', flags=re.I)
+    patchfile_re = re.compile(r'^Patch(?P<patchnum>[0-9]+)?:\s*(?P<filename>.+)\s*$', flags=re.I)
     applypatch_re = re.compile(r'^%patch(?P<patchnum>[0-9]+)?(\s+(?P<args>.*))?$')
     marker_re = re.compile(r'^#\s+(?P<marker>>>|<<)\s+(?P<what>gbp-[^\s]+)\s*(?P<comment>.*)$')
 
@@ -208,10 +208,11 @@ class SpecFile(object):
         for (name, num, typ) in self.specinfo.sources:
             # only add files of patch type
             if typ == 2:
-                self.patches[num] = {'filename': name, 'strip': '0', 'apply': False}
+                self.patches[num] = {'filename': name, 'strip': '0', 'apply': False, 'autoupdate': False}
 
         # Parse info from spec file
         f = file(self.specfile)
+        autoupdate = False
         for line in f:
             m = self.applypatch_re.match(line)
             if m:
@@ -229,6 +230,24 @@ class SpecFile(object):
                 self.patches[patchnum]['apply'] = True
                 continue
 
+            # Find patch tags inside autoupdate markers
+            m = self.marker_re.match(line)
+            if m:
+                if m.group('what') == "gbp-patch-tags":
+                    if m.group('marker') == '>>':
+                        autoupdate = True
+                    else:
+                        autoupdate = False
+                continue
+            m = self.patchfile_re.match(line)
+            if m:
+                if m.group('patchnum'):
+                    patchnum = int(m.group('patchnum'))
+                else:
+                    patchnum = 0
+                self.patches[patchnum]['autoupdate'] = autoupdate
+                continue
+
         f.close()
 
     # RPMTODO: complete this
@@ -319,6 +338,13 @@ class SpecFile(object):
         tmpffd, tmpfpath = tempfile.mkstemp(suffix='.spec', dir='.')
         tmpf = os.fdopen(tmpffd, 'w')
 
+        # Check the max patchnumber of non-autoupdate patches
+        start_patch_tag_num = 0
+        for n, p in self.patches.iteritems():
+            if (not p['autoupdate']) and (n >= start_patch_tag_num):
+                start_patch_tag_num = n + 1
+        gbp.log.debug("Starting autoupdate patch macro numbering from %s" % start_patch_tag_num)
+
         autoupdate = False
         for line in f:
             m = self.marker_re.match(line)
@@ -337,12 +363,14 @@ class SpecFile(object):
 
                         if autoupdate == 'gbp-patch-tags':
                             for i in range(len(patchfilenames)):
+                                tag_num = start_patch_tag_num + i
                                 # "PatchXYZ:" text 12 chars wide, left aligned
-                                tmpf.write("%-12s%s\n" % ("Patch%d:" % i, patchfilenames[i]))
+                                tmpf.write("%-12s%s\n" % ("Patch%d:" % tag_num, patchfilenames[i]))
                         elif autoupdate == 'gbp-apply-patches':
                             for i in range(len(patchfilenames)):
+                                tag_num = start_patch_tag_num + i
                                 tmpf.write("# %s\n" % patchfilenames[i])
-                                tmpf.write("%%patch%d -p1\n" % i)
+                                tmpf.write("%%patch%d -p1\n" % tag_num)
                         else:
                             # Unknown autoupdate marker, we shouldn't end up here
                             gbp.log.warn("Hmm, found a bug - don't know what to do with marker '%s'" % autoupdate)
@@ -420,8 +448,8 @@ class SpecFile(object):
         gbp.log.debug("Orig file: %s" % self.orig_file)
 
         for n, p in sorted(self.patches.iteritems()):
-            gbp.log.debug("Patch %s: %s, strip: %s, apply: %s" %
-                          (n, p['filename'], p['strip'], p['apply']))
+            gbp.log.debug("Patch %s: %s, strip: %s, apply: %s, autoupdate: %s" %
+                          (n, p['filename'], p['strip'], p['apply'], p['autoupdate']))
 
 
 def parse_srpm(srpmfile):
index 6c68d0c57ec6f963155d99a01a1057cd3ced4c74..9936c2e5731a72470ba82cd38ebae257bf0667bd 100755 (executable)
@@ -266,15 +266,16 @@ def gen_patches(repo, spec, totree, options):
 
     # Remove all old patches
     for n, p in spec.patches.iteritems():
-        f = spec.specdir+"/"+p['filename']
-        gbp.log.debug("Removing '%s'" % f)
-        try:
-            os.unlink(f)
-        except OSError, (e, msg):
-            if e != errno.ENOENT:
-                raise GbpError, "Failed to remove patch: %s" % msg
-            else:
-                gbp.log.debug("%s does not exist." % f)
+        if p['autoupdate']:
+            f = spec.specdir+"/"+p['filename']
+            gbp.log.debug("Removing '%s'" % f)
+            try:
+                os.unlink(f)
+            except OSError, (e, msg):
+                if e != errno.ENOENT:
+                    raise GbpError, "Failed to remove patch: %s" % msg
+                else:
+                    gbp.log.debug("%s does not exist." % f)
 
     gbp.log.info("Generating patches from git (%s..%s)" % (upstream_tree, totree))
     if repo.get_obj_type(totree) in ['tag', 'commit']:
index ee415bf9e50ad06a3380dccfa772433d997fbeef..527aca909496a8d5ffd632b720ed79193101d8bc 100755 (executable)
@@ -62,15 +62,16 @@ def export_patches(repo, branch, options):
         raise GbpError, ("Couldn't find upstream version %s. Don't know on what base to import." % spec.version)
 
     for n, p in spec.patches.iteritems():
-        f = options.packaging_dir+"/"+p['filename']
-        gbp.log.debug("Removing '%s'" % f) 
-        try:
-            os.unlink(f)
-        except OSError, (e, msg):
-            if e != errno.ENOENT:
-                raise GbpError, "Failed to remove patch: %s" % msg
-            else:
-                gbp.log.debug("%s does not exist." % f)
+        if p['autoupdate']:
+            f = options.packaging_dir+"/"+p['filename']
+            gbp.log.debug("Removing '%s'" % f) 
+            try:
+                os.unlink(f)
+            except OSError, (e, msg):
+                if e != errno.ENOENT:
+                    raise GbpError, "Failed to remove patch: %s" % msg
+                else:
+                    gbp.log.debug("%s does not exist." % f)
 
     gbp.log.info("Exporting patches from git (%s..%s)" % (upstream_commit, pq_branch))
     patches = repo.format_patches(upstream_commit, pq_branch, options.packaging_dir,