From: Markus Lehtonen Date: Mon, 25 Feb 2013 10:11:14 +0000 (+0200) Subject: rpm: support conditionals in patch-export X-Git-Tag: tizen/0.6.15-20140828~99 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3a5198aead28e4fe0742035f651736080b20d13f;p=tools%2Fgit-buildpackage.git rpm: support conditionals in patch-export One can now define conditional patches that are enclosed in '%if' or '%ifarch' when the spec file is updated. One can do this by defining 'Gbp-Rpm-If: ' and 'Gbp-Rpm-IfArch: ' in the commit message. Only one conditional per patch is supported, i.e. you cannot define 'IfArch' and 'If' for the same patch. Signed-off-by: Markus Lehtonen --- diff --git a/gbp/rpm/__init__.py b/gbp/rpm/__init__.py index 94297445..b797e98a 100644 --- a/gbp/rpm/__init__.py +++ b/gbp/rpm/__init__.py @@ -545,7 +545,7 @@ class SpecFile(object): self._special_directives[key].append(linerec) return ret - def update_patches(self, patchfilenames): + def update_patches(self, patches, commands): """Update spec with new patch tags and patch macros""" # Remove non-ignored patches tag_prev = None @@ -563,14 +563,21 @@ class SpecFile(object): # Remove '%patch:' macros for macro in self._special_directives['patch']: if not macro['id'] in ignored: + macro_prev = self._delete_special_macro('patch', macro['id']) + # Remove surrounding if-else + macro_next = macro_prev.next + if (str(macro_prev).startswith('%if') and + str(macro_next).startswith('%endif')): + self._content.delete(macro_next) + macro_prev = self._content.delete(macro_prev) + # 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: + if len(patches) == 0: return # Determine where to add Patch tag lines @@ -617,14 +624,23 @@ class SpecFile(object): # 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): + for ind, patch in enumerate(patches): + cmds = commands[patch] if patch in commands else {} patchnum = startnum + ind - tag_line = self._set_tag("Patch", patchnum, name, tag_line) + tag_line = self._set_tag("Patch", patchnum, patch, tag_line) # Add '%patch' macro and a preceding comment line - comment_text = "# %s\n" % name + comment_text = "# %s\n" % patch macro_line = self._content.insert_after(macro_line, comment_text) macro_line = self._set_special_macro('patch', patchnum, '-p1', macro_line) + for cmd, args in cmds.iteritems(): + if cmd in ('if', 'ifarch'): + self._content.insert_before(macro_line, '%%%s %s\n' % + (cmd, args)) + macro_line = self._content.insert_after(macro_line, + '%endif\n') + # We only support one command per patch, for now + break def patchseries(self, unapplied=False, ignored=False): """Return non-ignored patches of the RPM as a gbp patchseries""" diff --git a/gbp/scripts/import_srpm.py b/gbp/scripts/import_srpm.py index 1a120c97..3ef4b110 100755 --- a/gbp/scripts/import_srpm.py +++ b/gbp/scripts/import_srpm.py @@ -152,7 +152,7 @@ def import_spec_patches(repo, spec, dirs): gbp.log.info("Removing imported patch files from spec and packaging dir") rm_patch_files(spec) try: - spec.update_patches([]) + spec.update_patches([], {}) spec.write_spec_file() except GbpError: repo.force_head('HEAD', hard=True) diff --git a/gbp/scripts/pq_rpm.py b/gbp/scripts/pq_rpm.py index d3da13a8..edb327f6 100755 --- a/gbp/scripts/pq_rpm.py +++ b/gbp/scripts/pq_rpm.py @@ -110,7 +110,8 @@ def generate_patches(repo, start, squash, end, outdir, options): # Generate patches for commit in reversed(repo.get_commits(start, end_commit)): info = repo.get_commit_info(commit) - cmds = parse_gbp_commands(info, 'gbp-rpm', ('ignore'), None)[0] + cmds = parse_gbp_commands(info, 'gbp-rpm', ('ignore'), + ('if', 'ifarch'))[0] if not 'ignore' in cmds: patch_fn = format_patch(outdir, repo, info, patches, options.patch_numbers, @@ -164,9 +165,9 @@ def update_patch_series(repo, spec, start, end, options): # Unlink old patch files and generate new patches rm_patch_files(spec) - patches, _commands = generate_patches(repo, start, squash, end, - spec.specdir, options) - spec.update_patches(patches) + patches, commands = generate_patches(repo, start, squash, end, + spec.specdir, options) + spec.update_patches(patches, commands) spec.write_spec_file() diff --git a/tests/test_rpm.py b/tests/test_rpm.py index b065db4d..2542f126 100644 --- a/tests/test_rpm.py +++ b/tests/test_rpm.py @@ -159,7 +159,7 @@ class TestSpecFile(object): reference_spec = os.path.join(SPEC_DIR, 'gbp-test-reference.spec') spec = SpecFile(tmp_spec) - spec.update_patches(['new.patch']) + spec.update_patches(['new.patch'], {}) spec.write_spec_file() assert filecmp.cmp(tmp_spec, reference_spec) is True @@ -176,14 +176,16 @@ class TestSpecFile(object): reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference2.spec') spec = SpecFile(tmp_spec) - spec.update_patches(['1.patch', '2.patch']) + spec.update_patches(['1.patch', '2.patch'], + {'1.patch': {'if': 'true'}, + '2.patch': {'ifarch': '%ix86'}}) spec.set_tag('VCS', None, 'myvcstag') - spec.update_patches(['new.patch']) spec.write_spec_file() assert filecmp.cmp(tmp_spec, reference_spec) is True - # Test removing the VCS tag + # Test updating patches again and removing the VCS tag reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference.spec') + spec.update_patches(['new.patch'], {'new.patch': {'if': '1'}}) spec.set_tag('VCS', None, '') spec.write_spec_file() assert filecmp.cmp(tmp_spec, reference_spec) is True @@ -264,10 +266,10 @@ class TestSpecFile(object): spec = SpecFileTester(spec_filepath) assert len(spec.patchseries()) == 0 - spec.update_patches(['1.patch', '2.patch', '3.patch']) + spec.update_patches(['1.patch', '2.patch', '3.patch'], {}) assert len(spec.patchseries()) == 3 spec.protected('_gbp_tags')['ignore-patches'].append({'args': "0"}) - spec.update_patches(['4.patch']) + spec.update_patches(['4.patch'], {}) assert len(spec.patchseries()) == 1 assert len(spec.patchseries(ignored=True)) == 2 spec.protected('_delete_special_macro')('patch', 0) diff --git a/tests/test_rpm_data/specs/gbp-test2-reference.spec b/tests/test_rpm_data/specs/gbp-test2-reference.spec index e31930d7..dfd4b91f 100644 --- a/tests/test_rpm_data/specs/gbp-test2-reference.spec +++ b/tests/test_rpm_data/specs/gbp-test2-reference.spec @@ -27,7 +27,9 @@ echo "Do things" # Gbp-Patch-Macros # new.patch +%if 1 %patch0 -p1 +%endif %build make diff --git a/tests/test_rpm_data/specs/gbp-test2-reference2.spec b/tests/test_rpm_data/specs/gbp-test2-reference2.spec index 095600d3..0b93f0fa 100644 --- a/tests/test_rpm_data/specs/gbp-test2-reference2.spec +++ b/tests/test_rpm_data/specs/gbp-test2-reference2.spec @@ -11,7 +11,8 @@ Source20: bar.tar.gz # Gbp-Ignore-Patches: -1 Patch: my.patch # Patches auto-generated by git-buildpackage: -Patch0: new.patch +Patch0: 1.patch +Patch1: 2.patch Packager: Markus Lehtonen VCS: myvcstag @@ -27,8 +28,14 @@ Package for testing the RPM functionality of git-buildpackage. echo "Do things" # Gbp-Patch-Macros -# new.patch +# 1.patch +%if true %patch0 -p1 +%endif +# 2.patch +%ifarch %ix86 +%patch1 -p1 +%endif %build make