diff options
author | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2012-12-03 19:12:56 +0200 |
---|---|---|
committer | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2013-03-08 14:04:00 +0200 |
commit | 433b5691a87af6c761b28e7da245d9c50534af70 (patch) | |
tree | bba9ff68533fa18a1004b803e34a3f2d8a409053 | |
parent | 5ba37d8dca112347c3c2c56adb6dd6c654278c1f (diff) | |
download | git-buildpackage-433b5691a87af6c761b28e7da245d9c50534af70.tar.gz git-buildpackage-433b5691a87af6c761b28e7da245d9c50534af70.tar.bz2 git-buildpackage-433b5691a87af6c761b28e7da245d9c50534af70.zip |
rpm refactor: move spec file filtering into a separate function
Also, remove the skip_tags option from the init as it's currently not
used anywhere and it'd be better parse the spec file similarly in every
place. The option can be re-added in the future if really needed.
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r-- | gbp/rpm/__init__.py | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/gbp/rpm/__init__.py b/gbp/rpm/__init__.py index 970382ce..3eaf7f90 100644 --- a/gbp/rpm/__init__.py +++ b/gbp/rpm/__init__.py @@ -117,28 +117,23 @@ class SpecFile(object): gbptag_re = re.compile(r'^\s*#\s*gbp-(?P<name>[a-z-]+)' '(\s*:\s*(?P<args>\S.*))?$', flags=re.I) - def __init__(self, specfile, skip_tags=("ExcludeArch", "ExcludeOS", - "ExclusiveArch", "ExclusiveOS", - "BuildArch")): - with tempfile.NamedTemporaryFile(prefix='gbp') as temp: - try: - with open(specfile) as specf: - with open(temp.name, 'w') as filtered: - filtered.writelines(line for line in specf \ - if line.split(":")[0].strip() not in skip_tags) - filtered.flush() - try: - # Parse two times to circumvent a rpm-python - # problem where macros are not expanded if used - # before their definition - rpm.spec(temp.name) - self.specinfo = rpm.spec(temp.name) - except ValueError as err: - raise GbpError("RPM error while parsing spec: %s" % err) - except IOError as err: - raise NoSpecError("Unable to read spec file: %s" % err) - - source_header = self.specinfo.packages[0].header + def __init__(self, specfile): + # Load spec file into our special data structure + self.content = LinkedList() + try: + with open(specfile) as spec_file: + for line in spec_file.readlines(): + self.content.append(line) + except IOError as err: + raise NoSpecError("Unable to read spec file: %s" % err) + + # Use rpm-python to parse the spec file content + self._filtertags = ("excludearch", "excludeos", "exclusivearch", + "exclusiveos","buildarch") + self._specinfo = self._parse_filtered_spec(self._filtertags) + + # Other initializations + source_header = self._specinfo.packages[0].header self.name = source_header[rpm.RPMTAG_NAME] self.upstreamversion = source_header[rpm.RPMTAG_VERSION] self.release = source_header[rpm.RPMTAG_RELEASE] @@ -151,14 +146,9 @@ class SpecFile(object): self.patches = {} self.sources = {} - # Load and parse extra info from spec file - self.content = LinkedList() - with open(self.specfile) as spec_file: - for line in spec_file.readlines(): - self.content.append(line) + # Parse extra info from spec file loc = self.parse_content() - # Find 'Packager' tag. Needed to circumvent a bug in python-rpm where # spec.sourceHeader[rpm.RPMTAG_PACKAGER] is not reset when a new spec # file is parsed @@ -167,7 +157,7 @@ class SpecFile(object): # Update sources info (basically possible macros expanded by spec.__init__() # And, double-check that we parsed spec content correctly - for (name, num, typ) in self.specinfo.sources: + for (name, num, typ) in self._specinfo.sources: # workaround rpm parsing bug if num >= MAX_SOURCE_NUMBER: num = 0 @@ -195,6 +185,22 @@ class SpecFile(object): self.orig_src_num = self.guess_orig_file() + def _parse_filtered_spec(self, skip_tags): + """Parse a filtered spec file in rpm-python""" + skip_tags = [tag.lower() for tag in skip_tags] + with tempfile.NamedTemporaryFile(prefix='gbp') as temp: + with open(temp.name, 'w') as filtered: + filtered.writelines(str(line) for line in self. content + if str(line).split(":")[0].strip().lower() + not in skip_tags) + filtered.flush() + try: + # Parse two times to circumvent a rpm-python problem where + # macros are not expanded if used before their definition + rpm.spec(temp.name) + return rpm.spec(temp.name) + except ValueError as err: + raise GbpError("RPM error while parsing spec: %s" % err) def _get_version(self): """ |