From 1095ab823ebf863656bad7332bc90e06071311f6 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 11 Jan 2013 16:28:05 +0200 Subject: rpm helpers: implement _set/delete_special_macro() methods Currently intended for updating '%patch' macros. Signed-off-by: Markus Lehtonen --- gbp/rpm/__init__.py | 44 ++++++++++++++++++++++ tests/test_rpm.py | 12 +++++- .../specs/gbp-test-updates-reference.spec | 3 ++ tests/test_rpm_data/specs/gbp-test-updates.spec | 6 ++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/gbp/rpm/__init__.py b/gbp/rpm/__init__.py index 61cf13be..2bad49b2 100644 --- a/gbp/rpm/__init__.py +++ b/gbp/rpm/__init__.py @@ -516,6 +516,50 @@ class SpecFile(object): else: raise GbpError("Setting '%s:' tag not supported" % tagname) + def _delete_special_macro(self, name, identifier): + """Delete a special macro line in spec file content""" + if name != 'patch': + raise GbpError("Deleting '%s:' macro not supported" % name) + + key = name.lower() + fullname = '%%%s%s' % (name, identifier) + sparedlines = [] + prev = None + for line in self._special_directives[key]: + if line['id'] == identifier: + gbp.log.debug("Removing '%s' macro from spec" % fullname) + prev = self._content.delete(line['line']) + else: + sparedlines.append(line) + self._special_directives[key] = sparedlines + if not prev: + gbp.log.warn("Tried to delete non-existent macro '%s'" % fullname) + return prev + + def _set_special_macro(self, name, identifier, args, insertafter): + """Update a special macro line in spec file content""" + key = name.lower() + fullname = '%%%s%s' % (name, identifier) + if key != 'patch': + raise GbpError("Setting '%s' macro not supported" % name) + + updated = 0 + text = "%%%s%d %s\n" % (name, identifier, args) + for line in self._special_directives[key]: + if line['id'] == identifier: + gbp.log.debug("Updating '%s' macro in spec" % fullname) + line['args'] = args + line['line'].set_data(text) + ret = line['line'] + updated += 1 + if not updated: + gbp.log.debug("Adding '%s' macro after '%s...' line in spec" % + (fullname, str(insertafter)[0:20])) + ret = self._content.insert_after(insertafter, text) + linerec = {'line': ret, 'id': identifier, 'args': args} + self._special_directives[key].append(linerec) + return ret + def update_patches(self, patchfilenames): """Update spec with new patch tags and patch macros""" # Remove non-ignored patches diff --git a/tests/test_rpm.py b/tests/test_rpm.py index 88eb571f..ec2c834a 100644 --- a/tests/test_rpm.py +++ b/tests/test_rpm.py @@ -197,8 +197,8 @@ class TestSpecFile(object): prev = spec.protected('_delete_tag')('Vendor', None) spec.protected('_set_tag')('License', None, 'new license', prev) spec.protected('_delete_tag')('source', 0) + spec.protected('_delete_tag')('patch', 1) spec.protected('_delete_tag')('patch', 0) - spec.protected('_delete_tag')('patch', -1) prev = spec.protected('_delete_tag')('invalidtag', None) with assert_raises(GbpError): @@ -208,6 +208,16 @@ class TestSpecFile(object): # Check that setting invalid tag with public method fails spec.set_tag('invalidtag', None, 'value') + # Mangle macros + prev = spec.protected('_delete_special_macro')('patch', 0) + spec.protected('_delete_special_macro')('patch', 123) + spec.protected('_set_special_macro')('patch', 1, 'my new args', prev) + with assert_raises(GbpError): + spec.protected('_delete_special_macro')('invalidmacro', 0) + with assert_raises(GbpError): + spec.protected('_set_special_macro')('invalidmacro', 0, 'args', + prev) + # Check resulting spec file spec.write_spec_file() assert filecmp.cmp(tmp_spec, reference_spec) is True diff --git a/tests/test_rpm_data/specs/gbp-test-updates-reference.spec b/tests/test_rpm_data/specs/gbp-test-updates-reference.spec index 557f8b77..e6e6846c 100644 --- a/tests/test_rpm_data/specs/gbp-test-updates-reference.spec +++ b/tests/test_rpm_data/specs/gbp-test-updates-reference.spec @@ -33,6 +33,9 @@ Collections: my_collections Package for testing GBP. %prep +%setup -n my_prefix + +%patch1 my new args %build diff --git a/tests/test_rpm_data/specs/gbp-test-updates.spec b/tests/test_rpm_data/specs/gbp-test-updates.spec index 1cdcb212..19912442 100644 --- a/tests/test_rpm_data/specs/gbp-test-updates.spec +++ b/tests/test_rpm_data/specs/gbp-test-updates.spec @@ -18,7 +18,7 @@ Url: my_url Vcs: my_vcs Source: my_source Patch: my_%patch_fn_base -Patch0: my_%{patch_fn_base}0 +Patch1: my_%{patch_fn_base}1 BuildRoot: my_buildroot Provides: my_provides Requires: my_requires @@ -37,6 +37,10 @@ Collections: my_collections Package for testing GBP. %prep +%setup -n my_prefix + +%patch -b my_patch +%patch -P1 -b my_patch0 %build -- cgit v1.2.3