def get_source_urls(self): # arcane rpm constants, now in python! sources = filter(lambda x: x[2] == 1, self.rpmspec.sources) if len(sources) == 0: error = "No sources found" raise exception.SpecFileParseError(spec_fn=self.fn, error=error) # OpenStack packages seem to always use only one tarball sources0 = filter(lambda x: x[1] == 0, sources) if len(sources0) == 0: error = "Source0 not found" raise exception.SpecFileParseError(spec_fn=self.fn, error=error) source_url = sources0[0][0] return [source_url]
def load_rpmspec(self): if not RPM_AVAILABLE: raise exception.RpmModuleNotAvailable() rpm.addMacro('_sourcedir', os.path.dirname(os.path.realpath(self.fn))) try: self._rpmspec = rpm.spec(self.fn) except ValueError as e: raise exception.SpecFileParseError(spec_fn=self.fn, error=e.args[0])
def _get_tag_mock(tag, default=None, expand_macros=False): if tag == 'Version': return version elif tag == 'Release': return release elif tag == 'Epoch': if epoch: return epoch raise exception.SpecFileParseError( spec_fn='TESTING', error='Pretending Epoch tag not found') return "MOCKED-OUT-NVR"
def set_new_patches(self, fns): self.wipe_patches() if not fns: return apply_method = self.patches_apply_method() ps = '' pa = '' for i, pfn in enumerate(fns, start=1): ps += "Patch%04d: %s\n" % (i, pfn) if apply_method == 'rpm': pa += "%%patch%04d -p1\n" % i # PatchXXX: lines after Source0 / #patches_base= self._txt, n = re.subn(self.RE_AFTER_PATCHES_BASE, r'\g<1>%s\n' % ps, self.txt, count=1) if n != 1: m = None for m in re.finditer(self.RE_AFTER_SOURCES, self.txt): pass if not m: raise exception.SpecFileParseError( spec_fn=self.fn, error="Failed to append PatchXXXX: lines") i = m.end() startnl, endnl = '', '' if self._txt[i - 2] != '\n': startnl += '\n' if self._txt[i] != '\n': endnl += '\n' self._txt = self._txt[:i] + startnl + ps + endnl + self._txt[i:] # %patchXXX -p1 lines after "%setup" if needed if apply_method == 'rpm': self._txt, n = re.subn(r'((?:^|\n)%setup[^\n]*\n)\s*', r'\g<1>\n%s\n' % pa, self.txt) if n == 0: raise exception.SpecFileParseError( spec_fn=self.fn, error="Failed to append %patchXXXX lines after %setup")
def get_tag(self, tag, default=exception.SpecFileParseError, expand_macros=False): m = re.search('^%s:\s+(\S.*)$' % tag, self.txt, re.M) if not m: if default != exception.SpecFileParseError: return default raise exception.SpecFileParseError(spec_fn=self.fn, error="%s tag not found" % tag) tag = m.group(1).rstrip() if expand_macros and has_macros(tag): # don't parse using rpm unless required tag = self.expand_macro(tag) return tag
def _create_new_patches_base(self, base): self._txt, n = re.subn(self.RE_PATCH, r'\n#\n# patches_base=%s\n#\n\g<1>' % base, self.txt, count=1, flags=re.M) if n != 1: self._txt, n = re.subn(self.RE_AFTER_SOURCES, r'\g<1>#\n# patches_base=%s\n#\n\n' % base, self.txt, count=1, flags=re.M) if n != 1: raise exception.SpecFileParseError( spec_fn=self.fn, error="Unable to create new #patches_base entry.")
def set_patches_base(self, base): v, _ = self.get_patches_base() if re.search("^#\s*patches_ignore\s*=\s*\S+", self.txt, flags=re.M): # This is a temporary hack as patches_ignore currently requires # explicit patches_base. This should be solved with a proper # magic comment parser and using Version in filtration logic # when no patches_base is defined. if not base: base = self.get_tag('Version', expand_macros=True) if base: regex = r'^#\s*patches_base*' if v is None and re.search(regex, self.txt, flags=re.M) is None: self._create_new_patches_base(base) else: lines = self.txt.split('\n') patch_base_regex = re.compile('(#\s*patches_base\s*=\s*)\w*') for idx, line in enumerate(lines): match = patch_base_regex.match(line) if match is not None: out_str = '{0}{1}'.format(match.group(1), base) lines[idx] = out_str break else: raise exception.SpecFileParseError( spec_fn=self.fn, error="Unable to set new #patches_base") self._txt = '\n'.join(lines) else: if v is not None: # Drop magic comment patches_base and following empty comments self._txt = re.sub( r'(?:^#)+\s*patches_base\s*=[^\n]*\n(?:^#\n)*', '', self.txt, flags=re.M)