__init__.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2006,2007 Guido Guenther <agx@sigxcpu.org>
  4. # (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, please see
  17. # <http://www.gnu.org/licenses/>
  18. """provides some rpm source package related helpers"""
  19. import os
  20. import re
  21. import tempfile
  22. from optparse import OptionParser
  23. from collections import defaultdict
  24. import six
  25. import gbp.command_wrappers as gbpc
  26. from gbp.errors import GbpError
  27. from gbp.git import GitRepositoryError
  28. from gbp.patch_series import (PatchSeries, Patch)
  29. import gbp.log
  30. from gbp.pkg import (UpstreamSource, parse_archive_filename)
  31. from gbp.rpm.policy import RpmPkgPolicy
  32. from gbp.rpm.linkedlist import LinkedList
  33. from gbp.rpm.lib_rpm import librpm, get_librpm_log
  34. class NoSpecError(Exception):
  35. """Spec file parsing error"""
  36. pass
  37. class MacroExpandError(Exception):
  38. """Macro expansion in spec file failed"""
  39. pass
  40. class RpmUpstreamSource(UpstreamSource):
  41. """Upstream source class for RPM packages"""
  42. def __init__(self, name, unpacked=None, **kwargs):
  43. super(RpmUpstreamSource, self).__init__(name,
  44. unpacked,
  45. RpmPkgPolicy,
  46. **kwargs)
  47. class SrcRpmFile(object):
  48. """Keeps all needed data read from a source rpm"""
  49. def __init__(self, srpmfile):
  50. # Do not required signed packages to be able to import
  51. ts_vsflags = (librpm.RPMVSF_NOMD5HEADER | librpm.RPMVSF_NORSAHEADER |
  52. librpm.RPMVSF_NOSHA1HEADER | librpm.RPMVSF_NODSAHEADER |
  53. librpm.RPMVSF_NOMD5 | librpm.RPMVSF_NORSA |
  54. librpm.RPMVSF_NOSHA1 | librpm.RPMVSF_NODSA)
  55. srpmfp = open(srpmfile)
  56. self.rpmhdr = librpm.ts(vsflags=ts_vsflags).hdrFromFdno(srpmfp.fileno())
  57. srpmfp.close()
  58. self.srpmfile = os.path.abspath(srpmfile)
  59. @property
  60. def version(self):
  61. """Get the (downstream) version of the RPM package"""
  62. version = dict(upstreamversion=self.rpmhdr[librpm.RPMTAG_VERSION],
  63. release=self.rpmhdr[librpm.RPMTAG_RELEASE])
  64. if self.rpmhdr[librpm.RPMTAG_EPOCH] is not None:
  65. version['epoch'] = str(self.rpmhdr[librpm.RPMTAG_EPOCH])
  66. return version
  67. @property
  68. def name(self):
  69. """Get the name of the RPM package"""
  70. return self.rpmhdr[librpm.RPMTAG_NAME]
  71. @property
  72. def upstreamversion(self):
  73. """Get the upstream version of the RPM package"""
  74. return self.rpmhdr[librpm.RPMTAG_VERSION]
  75. @property
  76. def packager(self):
  77. """Get the packager of the RPM package"""
  78. return self.rpmhdr[librpm.RPMTAG_PACKAGER]
  79. def unpack(self, dest_dir):
  80. """
  81. Unpack the source rpm to tmpdir.
  82. Leave the cleanup to the caller in case of an error.
  83. """
  84. c = gbpc.RunAtCommand('rpm2cpio',
  85. [self.srpmfile, '|', 'cpio', '-id'],
  86. shell=True, capture_stderr=True)
  87. c.run_error = "'%s' failed: {stderr_or_reason}" % (" ".join([c.cmd] + c.args))
  88. c(dir=dest_dir)
  89. class SpecFile(object):
  90. """Class for parsing/modifying spec files"""
  91. tag_re = re.compile(r'^(?P<name>[a-z]+)(?P<num>[0-9]+)?\s*:\s*'
  92. '(?P<value>\S(.*\S)?)\s*$', flags=re.I)
  93. directive_re = re.compile(r'^%(?P<name>[a-z]+)(?P<num>[0-9]+)?'
  94. '(\s+(?P<args>.*))?$', flags=re.I)
  95. gbptag_re = re.compile(r'^\s*#\s*gbp-(?P<name>[a-z-]+)'
  96. '(\s*:\s*(?P<args>\S.*))?$', flags=re.I)
  97. # Here "sections" stand for all scripts, scriptlets and other directives,
  98. # but not macros
  99. section_identifiers = ('package', 'description', 'prep', 'build', 'install',
  100. 'clean', 'check', 'pre', 'preun', 'post', 'postun', 'verifyscript',
  101. 'files', 'changelog', 'triggerin', 'triggerpostin', 'triggerun',
  102. 'triggerpostun')
  103. def __init__(self, filename=None, filedata=None):
  104. self._content = LinkedList()
  105. # Check args: only filename or filedata can be given, not both
  106. if filename is None and filedata is None:
  107. raise NoSpecError("No filename or raw data given for parsing!")
  108. elif filename and filedata:
  109. raise NoSpecError("Both filename and raw data given, don't know "
  110. "which one to parse!")
  111. elif filename:
  112. # Load spec file into our special data structure
  113. self.specfile = os.path.basename(filename)
  114. self.specdir = os.path.dirname(os.path.abspath(filename))
  115. try:
  116. with open(filename) as spec_file:
  117. for line in spec_file.readlines():
  118. self._content.append(line)
  119. except IOError as err:
  120. raise NoSpecError("Unable to read spec file: %s" % err)
  121. else:
  122. self.specfile = None
  123. self.specdir = None
  124. for line in filedata.splitlines():
  125. self._content.append(line + '\n')
  126. # Use rpm-python to parse the spec file content
  127. self._filtertags = ("excludearch", "excludeos", "exclusivearch",
  128. "exclusiveos", "buildarch")
  129. self._listtags = self._filtertags + ('source', 'patch',
  130. 'requires', 'conflicts', 'recommends',
  131. 'suggests', 'supplements', 'enhances',
  132. 'provides', 'obsoletes', 'buildrequires',
  133. 'buildconflicts', 'buildrecommends',
  134. 'buildsuggests', 'buildsupplements',
  135. 'buildenhances', 'collections',
  136. 'nosource', 'nopatch')
  137. self._specinfo = self._parse_filtered_spec(self._filtertags)
  138. # Other initializations
  139. source_header = self._specinfo.packages[0].header
  140. self.name = source_header[librpm.RPMTAG_NAME]
  141. self.upstreamversion = source_header[librpm.RPMTAG_VERSION]
  142. self.release = source_header[librpm.RPMTAG_RELEASE]
  143. # rpm-python returns epoch as 'long', convert that to string
  144. self.epoch = str(source_header[librpm.RPMTAG_EPOCH]) \
  145. if source_header[librpm.RPMTAG_EPOCH] is not None else None
  146. self.packager = source_header[librpm.RPMTAG_PACKAGER]
  147. self._tags = {}
  148. self._special_directives = defaultdict(list)
  149. self._gbp_tags = defaultdict(list)
  150. # Parse extra info from spec file
  151. self._parse_content()
  152. # Find 'Packager' tag. Needed to circumvent a bug in python-rpm where
  153. # spec.sourceHeader[librpm.RPMTAG_PACKAGER] is not reset when a new spec
  154. # file is parsed
  155. if 'packager' not in self._tags:
  156. self.packager = None
  157. self.orig_src = self._guess_orig_file()
  158. def _parse_filtered_spec(self, skip_tags):
  159. """Parse a filtered spec file in rpm-python"""
  160. skip_tags = [tag.lower() for tag in skip_tags]
  161. with tempfile.NamedTemporaryFile(prefix='gbp') as filtered:
  162. filtered.writelines(str(line) for line in self._content
  163. if str(line).split(":")[0].strip().lower() not in skip_tags)
  164. filtered.flush()
  165. try:
  166. # Parse two times to circumvent a rpm-python problem where
  167. # macros are not expanded if used before their definition
  168. librpm.spec(filtered.name)
  169. return librpm.spec(filtered.name)
  170. except ValueError as err:
  171. rpmlog = get_librpm_log()
  172. gbp.log.debug("librpm log:\n %s" %
  173. "\n ".join(rpmlog))
  174. raise GbpError("RPM error while parsing %s: %s (%s)" %
  175. (self.specfile, err, rpmlog[-1]))
  176. @property
  177. def version(self):
  178. """Get the (downstream) version"""
  179. version = dict(upstreamversion=self.upstreamversion,
  180. release=self.release)
  181. if self.epoch is not None:
  182. version['epoch'] = self.epoch
  183. return version
  184. @property
  185. def specpath(self):
  186. """Get the dir/filename"""
  187. return os.path.join(self.specdir, self.specfile)
  188. @property
  189. def ignorepatches(self):
  190. """Get numbers of ignored patches as a sorted list"""
  191. if 'ignore-patches' in self._gbp_tags:
  192. data = self._gbp_tags['ignore-patches'][-1]['args'].split()
  193. return sorted([int(num) for num in data])
  194. return []
  195. def _patches(self):
  196. """Get all patch tags as a dict"""
  197. if 'patch' not in self._tags:
  198. return {}
  199. return {patch['num']: patch for patch in self._tags['patch']['lines']}
  200. def _sources(self):
  201. """Get all source tags as a dict"""
  202. if 'source' not in self._tags:
  203. return {}
  204. return {src['num']: src for src in self._tags['source']['lines']}
  205. def sources(self):
  206. """Get all source tags as a dict"""
  207. return {src['num']: src['linevalue']
  208. for src in self._sources().values()}
  209. def _macro_replace(self, matchobj):
  210. macro_dict = {'name': self.name,
  211. 'version': self.upstreamversion,
  212. 'release': self.release}
  213. if matchobj.group(2) in macro_dict:
  214. return macro_dict[matchobj.group(2)]
  215. raise MacroExpandError("Unknown macro '%s'" % matchobj.group(0))
  216. def macro_expand(self, text):
  217. """
  218. Expand the rpm macros (that gbp knows of) in the given text.
  219. @param text: text to check for macros
  220. @type text: C{str}
  221. @return: text with macros expanded
  222. @rtype: C{str}
  223. """
  224. # regexp to match '%{macro}' and '%macro'
  225. macro_re = re.compile(r'%({)?(?P<macro_name>[a-z_][a-z0-9_]*)(?(1)})', flags=re.I)
  226. return macro_re.sub(self._macro_replace, text)
  227. def write_spec_file(self):
  228. """
  229. Write, possibly updated, spec to disk
  230. """
  231. with open(os.path.join(self.specdir, self.specfile), 'w') as spec_file:
  232. for line in self._content:
  233. spec_file.write(str(line))
  234. def _parse_tag(self, lineobj):
  235. """Parse tag line"""
  236. line = str(lineobj)
  237. matchobj = self.tag_re.match(line)
  238. if not matchobj:
  239. return False
  240. tagname = matchobj.group('name').lower()
  241. tagnum = int(matchobj.group('num')) if matchobj.group('num') else None
  242. # 'Source:' tags
  243. if tagname == 'source':
  244. tagnum = 0 if tagnum is None else tagnum
  245. # 'Patch:' tags
  246. elif tagname == 'patch':
  247. tagnum = -1 if tagnum is None else tagnum
  248. # Record all tag locations
  249. try:
  250. header = self._specinfo.packages[0].header
  251. tagvalue = header[getattr(librpm, 'RPMTAG_%s' % tagname.upper())]
  252. except AttributeError:
  253. tagvalue = None
  254. # We don't support "multivalue" tags like "Provides:" or "SourceX:"
  255. # Rpm python doesn't support many of these, thus the explicit list
  256. if isinstance(tagvalue, six.integer_types):
  257. tagvalue = str(tagvalue)
  258. elif type(tagvalue) is list or tagname in self._listtags:
  259. tagvalue = None
  260. elif not tagvalue:
  261. # Rpm python doesn't give the following, for reason or another
  262. if tagname not in ('buildroot', 'autoprov', 'autoreq',
  263. 'autoreqprov') + self._filtertags:
  264. gbp.log.warn("BUG: '%s:' tag not found by rpm" % tagname)
  265. tagvalue = matchobj.group('value')
  266. linerecord = {'line': lineobj,
  267. 'num': tagnum,
  268. 'linevalue': matchobj.group('value')}
  269. if tagname in self._tags:
  270. self._tags[tagname]['value'] = tagvalue
  271. self._tags[tagname]['lines'].append(linerecord)
  272. else:
  273. self._tags[tagname] = {'value': tagvalue, 'lines': [linerecord]}
  274. return tagname
  275. @staticmethod
  276. def _patch_macro_opts(args):
  277. """Parse arguments of the '%patch' macro"""
  278. patchparser = OptionParser(
  279. prog="%s internal patch macro opts parser" % __name__,
  280. usage="%prog for " + args)
  281. patchparser.add_option("-p", dest="strip")
  282. patchparser.add_option("-s", dest="silence")
  283. patchparser.add_option("-P", dest="patchnum")
  284. patchparser.add_option("-b", dest="backup")
  285. patchparser.add_option("-E", dest="removeempty")
  286. patchparser.add_option("-F", dest="fuzz")
  287. arglist = args.split()
  288. return patchparser.parse_args(arglist)[0]
  289. @staticmethod
  290. def _setup_macro_opts(args):
  291. """Parse arguments of the '%setup' macro"""
  292. setupparser = OptionParser(
  293. prog="%s internal setup macro opts parser" % __name__,
  294. usage="%prog for " + args)
  295. setupparser.add_option("-n", dest="name")
  296. setupparser.add_option("-c", dest="create_dir", action="store_true")
  297. setupparser.add_option("-D", dest="no_delete_dir", action="store_true")
  298. setupparser.add_option("-T", dest="no_unpack_default",
  299. action="store_true")
  300. setupparser.add_option("-b", dest="unpack_before")
  301. setupparser.add_option("-a", dest="unpack_after")
  302. setupparser.add_option("-q", dest="quiet", action="store_true")
  303. arglist = args.split()
  304. return setupparser.parse_args(arglist)[0]
  305. def _parse_directive(self, lineobj):
  306. """Parse special directive/scriptlet/macro lines"""
  307. line = str(lineobj)
  308. matchobj = self.directive_re.match(line)
  309. if not matchobj:
  310. return None
  311. directivename = matchobj.group('name')
  312. # '%patch' macros
  313. directiveid = None
  314. if directivename == 'patch':
  315. opts = self._patch_macro_opts(matchobj.group('args'))
  316. if matchobj.group('num'):
  317. directiveid = int(matchobj.group('num'))
  318. elif opts.patchnum:
  319. directiveid = int(opts.patchnum)
  320. else:
  321. directiveid = -1
  322. # Record special directive/scriptlet/macro locations
  323. if directivename in self.section_identifiers + ('setup', 'patch'):
  324. linerecord = {'line': lineobj,
  325. 'id': directiveid,
  326. 'args': matchobj.group('args')}
  327. self._special_directives[directivename].append(linerecord)
  328. return directivename
  329. def _parse_gbp_tag(self, linenum, lineobj):
  330. """Parse special git-buildpackage tags"""
  331. line = str(lineobj)
  332. matchobj = self.gbptag_re.match(line)
  333. if matchobj:
  334. gbptagname = matchobj.group('name').lower()
  335. if gbptagname not in ('ignore-patches', 'patch-macros'):
  336. gbp.log.info("Found unrecognized Gbp tag on line %s: '%s'" %
  337. (linenum, line))
  338. if matchobj.group('args'):
  339. args = matchobj.group('args').strip()
  340. else:
  341. args = None
  342. record = {'line': lineobj, 'args': args}
  343. self._gbp_tags[gbptagname].append(record)
  344. return gbptagname
  345. return None
  346. def _parse_content(self):
  347. """
  348. Go through spec file content line-by-line and (re-)parse info from it
  349. """
  350. in_preamble = True
  351. for linenum, lineobj in enumerate(self._content):
  352. matched = False
  353. if in_preamble:
  354. if self._parse_tag(lineobj):
  355. continue
  356. matched = self._parse_directive(lineobj)
  357. if matched:
  358. if matched in self.section_identifiers:
  359. in_preamble = False
  360. continue
  361. self._parse_gbp_tag(linenum, lineobj)
  362. # Update sources info (basically possible macros expanded by rpm)
  363. # And, double-check that we parsed spec content correctly
  364. patches = self._patches()
  365. sources = self._sources()
  366. for name, num, typ in self._specinfo.sources:
  367. # workaround rpm parsing bug
  368. if typ == 1 or typ == 9:
  369. if num in sources:
  370. sources[num]['linevalue'] = name
  371. else:
  372. gbp.log.err("BUG: failed to parse all 'Source' tags!")
  373. elif typ == 2 or typ == 10:
  374. # Patch tag without any number defined is treated by RPM as
  375. # having number (2^31-1), we use number -1
  376. if num >= pow(2, 30):
  377. num = -1
  378. if num in patches:
  379. patches[num]['linevalue'] = name
  380. else:
  381. gbp.log.err("BUG: failed to parse all 'Patch' tags!")
  382. def _delete_tag(self, tag, num):
  383. """Delete a tag"""
  384. key = tag.lower()
  385. tagname = '%s%s' % (tag, num) if num is not None else tag
  386. if key not in self._tags:
  387. gbp.log.warn("Trying to delete non-existent tag '%s:'" % tag)
  388. return None
  389. sparedlines = []
  390. prev = None
  391. for line in self._tags[key]['lines']:
  392. if line['num'] == num:
  393. gbp.log.debug("Removing '%s:' tag from spec" % tagname)
  394. prev = self._content.delete(line['line'])
  395. else:
  396. sparedlines.append(line)
  397. self._tags[key]['lines'] = sparedlines
  398. if not self._tags[key]['lines']:
  399. self._tags.pop(key)
  400. return prev
  401. def _set_tag(self, tag, num, value, insertafter):
  402. """Set a tag value"""
  403. key = tag.lower()
  404. tagname = '%s%s' % (tag, num) if num is not None else tag
  405. value = value.strip()
  406. if not value:
  407. raise GbpError("Cannot set empty value to '%s:' tag" % tag)
  408. # Check type of tag, we don't support values for 'multivalue' tags
  409. try:
  410. header = self._specinfo.packages[0].header
  411. tagvalue = header[getattr(librpm, 'RPMTAG_%s' % tagname.upper())]
  412. except AttributeError:
  413. tagvalue = None
  414. tagvalue = None if type(tagvalue) is list else value
  415. # Try to guess the correct indentation from the previous or next tag
  416. indent_re = re.compile(r'^([a-z]+([0-9]+)?\s*:\s*)', flags=re.I)
  417. match = indent_re.match(str(insertafter))
  418. if not match:
  419. match = indent_re.match(str(insertafter.next))
  420. indent = 12 if not match else len(match.group(1))
  421. text = '%-*s%s\n' % (indent, '%s:' % tagname, value)
  422. if key in self._tags:
  423. self._tags[key]['value'] = tagvalue
  424. for line in reversed(self._tags[key]['lines']):
  425. if line['num'] == num:
  426. gbp.log.debug("Updating '%s:' tag in spec" % tagname)
  427. line['line'].set_data(text)
  428. line['linevalue'] = value
  429. return line['line']
  430. gbp.log.debug("Adding '%s:' tag after '%s...' line in spec" %
  431. (tagname, str(insertafter)[0:20]))
  432. line = self._content.insert_after(insertafter, text)
  433. linerec = {'line': line, 'num': num, 'linevalue': value}
  434. if key in self._tags:
  435. self._tags[key]['lines'].append(linerec)
  436. else:
  437. self._tags[key] = {'value': tagvalue, 'lines': [linerec]}
  438. return line
  439. def set_tag(self, tag, num, value, insertafter=None):
  440. """Update a tag in spec file content"""
  441. key = tag.lower()
  442. tagname = '%s%s' % (tag, num) if num is not None else tag
  443. if key in ('patch', 'vcs'):
  444. if key in self._tags:
  445. insertafter = key
  446. elif insertafter not in self._tags:
  447. insertafter = 'name'
  448. after_line = self._tags[insertafter]['lines'][-1]['line']
  449. if value:
  450. self._set_tag(tag, num, value, after_line)
  451. elif key in self._tags:
  452. self._delete_tag(tag, num)
  453. else:
  454. raise GbpError("Setting '%s:' tag not supported" % tagname)
  455. def _delete_special_macro(self, name, identifier):
  456. """Delete a special macro line in spec file content"""
  457. if name != 'patch':
  458. raise GbpError("Deleting '%s:' macro not supported" % name)
  459. key = name.lower()
  460. fullname = '%%%s%s' % (name, identifier)
  461. sparedlines = []
  462. prev = None
  463. for line in self._special_directives[key]:
  464. if line['id'] == identifier:
  465. gbp.log.debug("Removing '%s' macro from spec" % fullname)
  466. prev = self._content.delete(line['line'])
  467. else:
  468. sparedlines.append(line)
  469. self._special_directives[key] = sparedlines
  470. if not prev:
  471. gbp.log.warn("Tried to delete non-existent macro '%s'" % fullname)
  472. return prev
  473. def _set_special_macro(self, name, identifier, args, insertafter):
  474. """Update a special macro line in spec file content"""
  475. key = name.lower()
  476. fullname = '%%%s%s' % (name, identifier)
  477. if key != 'patch':
  478. raise GbpError("Setting '%s' macro not supported" % name)
  479. updated = 0
  480. text = "%%%s%d %s\n" % (name, identifier, args)
  481. for line in self._special_directives[key]:
  482. if line['id'] == identifier:
  483. gbp.log.debug("Updating '%s' macro in spec" % fullname)
  484. line['args'] = args
  485. line['line'].set_data(text)
  486. ret = line['line']
  487. updated += 1
  488. if not updated:
  489. gbp.log.debug("Adding '%s' macro after '%s...' line in spec" %
  490. (fullname, str(insertafter)[0:20]))
  491. ret = self._content.insert_after(insertafter, text)
  492. linerec = {'line': ret, 'id': identifier, 'args': args}
  493. self._special_directives[key].append(linerec)
  494. return ret
  495. def _set_section(self, name, text):
  496. """Update/create a complete section in spec file."""
  497. if name not in self.section_identifiers:
  498. raise GbpError("Not a valid section directive: '%s'" % name)
  499. # Delete section, if it exists
  500. if name in self._special_directives:
  501. if len(self._special_directives[name]) > 1:
  502. raise GbpError("Multiple %%%s sections found, don't know "
  503. "which to update" % name)
  504. line = self._special_directives[name][0]['line']
  505. gbp.log.debug("Removing content of %s section" % name)
  506. while line.next:
  507. match = self.directive_re.match(str(line.next))
  508. if match and match.group('name') in self.section_identifiers:
  509. break
  510. self._content.delete(line.next)
  511. else:
  512. gbp.log.debug("Adding %s section to the end of spec file" % name)
  513. line = self._content.append('%%%s\n' % name)
  514. linerec = {'line': line, 'id': None, 'args': None}
  515. self._special_directives[name] = [linerec]
  516. # Add new lines
  517. gbp.log.debug("Updating content of %s section" % name)
  518. for linetext in text.splitlines():
  519. line = self._content.insert_after(line, linetext + '\n')
  520. def set_changelog(self, text):
  521. """Update or create the %changelog section"""
  522. self._set_section('changelog', text)
  523. def get_changelog(self):
  524. """Get the %changelog section"""
  525. text = ''
  526. if 'changelog' in self._special_directives:
  527. line = self._special_directives['changelog'][0]['line']
  528. while line.next:
  529. line = line.next
  530. match = self.directive_re.match(str(line))
  531. if match and match.group('name') in self.section_identifiers:
  532. break
  533. text += str(line)
  534. return text
  535. def update_patches(self, patches, commands):
  536. """Update spec with new patch tags and patch macros"""
  537. # Remove non-ignored patches
  538. tag_prev = None
  539. macro_prev = None
  540. ignored = self.ignorepatches
  541. # Remove 'Patch:̈́' tags
  542. for tag in self._patches().values():
  543. if not tag['num'] in ignored:
  544. tag_prev = self._delete_tag('patch', tag['num'])
  545. # Remove a preceding comment if it seems to originate from GBP
  546. if re.match("^\s*#.*patch.*auto-generated",
  547. str(tag_prev), flags=re.I):
  548. tag_prev = self._content.delete(tag_prev)
  549. # Remove '%patch:' macros
  550. for macro in self._special_directives['patch']:
  551. if not macro['id'] in ignored:
  552. macro_prev = self._delete_special_macro('patch', macro['id'])
  553. # Remove surrounding if-else
  554. macro_next = macro_prev.next
  555. if (str(macro_prev).startswith('%if') and
  556. str(macro_next).startswith('%endif')):
  557. self._content.delete(macro_next)
  558. macro_prev = self._content.delete(macro_prev)
  559. # Remove a preceding comment line if it ends with '.patch' or
  560. # '.diff' plus an optional compression suffix
  561. if re.match("^\s*#.+(patch|diff)(\.(gz|bz2|xz|lzma))?\s*$",
  562. str(macro_prev), flags=re.I):
  563. macro_prev = self._content.delete(macro_prev)
  564. if len(patches) == 0:
  565. return
  566. # Determine where to add Patch tag lines
  567. if tag_prev:
  568. gbp.log.debug("Adding 'Patch' tags in place of the removed tags")
  569. tag_line = tag_prev
  570. elif 'patch' in self._tags:
  571. gbp.log.debug("Adding new 'Patch' tags after the last 'Patch' tag")
  572. tag_line = self._tags['patch']['lines'][-1]['line']
  573. elif 'source' in self._tags:
  574. gbp.log.debug("Didn't find any old 'Patch' tags, adding new "
  575. "patches after the last 'Source' tag.")
  576. tag_line = self._tags['source']['lines'][-1]['line']
  577. else:
  578. gbp.log.debug("Didn't find any old 'Patch' or 'Source' tags, "
  579. "adding new patches after the last 'Name' tag.")
  580. tag_line = self._tags['name']['lines'][-1]['line']
  581. # Determine where to add %patch macro lines
  582. if 'patch-macros' in self._gbp_tags:
  583. gbp.log.debug("Adding '%patch' macros after the start marker")
  584. macro_line = self._gbp_tags['patch-macros'][-1]['line']
  585. elif macro_prev:
  586. gbp.log.debug("Adding '%patch' macros in place of the removed "
  587. "macros")
  588. macro_line = macro_prev
  589. elif self._special_directives['patch']:
  590. gbp.log.debug("Adding new '%patch' macros after the last existing"
  591. "'%patch' macro")
  592. macro_line = self._special_directives['patch'][-1]['line']
  593. elif self._special_directives['setup']:
  594. gbp.log.debug("Didn't find any old '%patch' macros, adding new "
  595. "patches after the last '%setup' macro")
  596. macro_line = self._special_directives['setup'][-1]['line']
  597. elif self._special_directives['prep']:
  598. gbp.log.warn("Didn't find any old '%patch' or '%setup' macros, "
  599. "adding new patches directly after '%prep' directive")
  600. macro_line = self._special_directives['prep'][-1]['line']
  601. else:
  602. raise GbpError("Couldn't determine where to add '%patch' macros")
  603. startnum = sorted(ignored)[-1] + 1 if ignored else 0
  604. gbp.log.debug("Starting autoupdate patch numbering from %s" % startnum)
  605. # Add a comment indicating gbp generated patch tags
  606. comment_text = "# Patches auto-generated by git-buildpackage:\n"
  607. tag_line = self._content.insert_after(tag_line, comment_text)
  608. for ind, patch in enumerate(patches):
  609. cmds = commands[patch] if patch in commands else {}
  610. patchnum = startnum + ind
  611. tag_line = self._set_tag("Patch", patchnum, patch, tag_line)
  612. # Add '%patch' macro and a preceding comment line
  613. comment_text = "# %s\n" % patch
  614. macro_line = self._content.insert_after(macro_line, comment_text)
  615. macro_line = self._set_special_macro('patch', patchnum, '-p1',
  616. macro_line)
  617. for cmd, args in six.iteritems(cmds):
  618. if cmd in ('if', 'ifarch'):
  619. self._content.insert_before(macro_line, '%%%s %s\n' %
  620. (cmd, args))
  621. macro_line = self._content.insert_after(macro_line,
  622. '%endif\n')
  623. # We only support one command per patch, for now
  624. break
  625. def patchseries(self, unapplied=False, ignored=False):
  626. """Return non-ignored patches of the RPM as a gbp patchseries"""
  627. series = PatchSeries()
  628. if 'patch' in self._tags:
  629. tags = self._patches()
  630. applied = []
  631. for macro in self._special_directives['patch']:
  632. if macro['id'] in tags:
  633. applied.append((macro['id'], macro['args']))
  634. ignored = set() if ignored else set(self.ignorepatches)
  635. # Put all patches that are applied first in the series
  636. for num, args in applied:
  637. if num not in ignored:
  638. opts = self._patch_macro_opts(args)
  639. strip = int(opts.strip) if opts.strip else 0
  640. filename = os.path.basename(tags[num]['linevalue'])
  641. series.append(Patch(os.path.join(self.specdir, filename),
  642. strip=strip))
  643. # Finally, append all unapplied patches to the series, if requested
  644. if unapplied:
  645. applied_nums = set([num for num, _args in applied])
  646. unapplied = set(tags.keys()).difference(applied_nums)
  647. for num in sorted(unapplied):
  648. if num not in ignored:
  649. filename = os.path.basename(tags[num]['linevalue'])
  650. series.append(Patch(os.path.join(self.specdir,
  651. filename), strip=0))
  652. return series
  653. def _guess_orig_prefix(self, orig):
  654. """Guess prefix for the orig file"""
  655. # Make initial guess about the prefix in the archive
  656. filename = orig['filename']
  657. name, version = RpmPkgPolicy.guess_upstream_src_version(filename)
  658. if name and version:
  659. prefix = "%s-%s/" % (name, version)
  660. else:
  661. prefix = orig['filename_base'] + "/"
  662. # Refine our guess about the prefix
  663. for macro in self._special_directives['setup']:
  664. args = macro['args']
  665. opts = self._setup_macro_opts(args)
  666. srcnum = None
  667. if opts.no_unpack_default:
  668. if opts.unpack_before:
  669. srcnum = int(opts.unpack_before)
  670. elif opts.unpack_after:
  671. srcnum = int(opts.unpack_after)
  672. else:
  673. srcnum = 0
  674. if srcnum == orig['num']:
  675. if opts.create_dir:
  676. prefix = ''
  677. elif opts.name:
  678. try:
  679. prefix = self.macro_expand(opts.name) + '/'
  680. except MacroExpandError as err:
  681. gbp.log.warn("Couldn't determine prefix from %%setup "
  682. "macro (%s). Using filename base as a "
  683. "fallback" % err)
  684. prefix = orig['filename_base'] + '/'
  685. else:
  686. # RPM default
  687. prefix = "%s-%s/" % (self.name, self.upstreamversion)
  688. break
  689. return prefix
  690. def _guess_orig_file(self):
  691. """
  692. Try to guess the name of the primary upstream/source archive.
  693. Returns a dict with all the relevant information.
  694. """
  695. orig = None
  696. sources = self.sources()
  697. for num, filename in sorted(six.iteritems(sources)):
  698. src = {'num': num, 'filename': os.path.basename(filename),
  699. 'uri': filename}
  700. src['filename_base'], src['archive_fmt'], src['compression'] = \
  701. parse_archive_filename(os.path.basename(filename))
  702. if (src['filename_base'].startswith(self.name) and
  703. src['archive_fmt']):
  704. # Take the first archive that starts with pkg name
  705. orig = src
  706. break
  707. # otherwise we take the first archive
  708. elif not orig and src['archive_fmt']:
  709. orig = src
  710. # else don't accept
  711. if orig:
  712. orig['prefix'] = self._guess_orig_prefix(orig)
  713. return orig
  714. def parse_srpm(srpmfile):
  715. """parse srpm by creating a SrcRpmFile object"""
  716. try:
  717. srcrpm = SrcRpmFile(srpmfile)
  718. except IOError as err:
  719. raise GbpError("Error reading src.rpm file: %s" % err)
  720. except librpm.error as err:
  721. raise GbpError("RPM error while reading src.rpm: %s" % err)
  722. return srcrpm
  723. def guess_spec_fn(file_list, preferred_name=None):
  724. """Guess spec file from a list of filenames"""
  725. specs = []
  726. for filepath in file_list:
  727. filename = os.path.basename(filepath)
  728. # Stop at the first file matching the preferred name
  729. if filename == preferred_name:
  730. gbp.log.debug("Found a preferred spec file %s" % filepath)
  731. specs = [filepath]
  732. break
  733. if filename.endswith(".spec"):
  734. gbp.log.debug("Found spec file %s" % filepath)
  735. specs.append(filepath)
  736. if len(specs) == 0:
  737. raise NoSpecError("No spec file found.")
  738. elif len(specs) > 1:
  739. raise NoSpecError("Multiple spec files found (%s), don't know which "
  740. "to use." % ', '.join(specs))
  741. return specs[0]
  742. def guess_spec(topdir, recursive=True, preferred_name=None):
  743. """Guess a spec file"""
  744. file_list = []
  745. if not topdir:
  746. topdir = '.'
  747. for root, dirs, files in os.walk(topdir):
  748. file_list.extend([os.path.join(root, fname) for fname in files])
  749. if not recursive:
  750. del dirs[:]
  751. # Skip .git dir in any case
  752. if '.git' in dirs:
  753. dirs.remove('.git')
  754. return SpecFile(os.path.abspath(guess_spec_fn(file_list, preferred_name)))
  755. def guess_spec_repo(repo, treeish, topdir='', recursive=True, preferred_name=None):
  756. """
  757. Try to find/parse the spec file from a given git treeish.
  758. """
  759. topdir = topdir.rstrip('/') + ('/') if topdir else ''
  760. try:
  761. file_list = [nam for (mod, typ, sha, nam) in
  762. repo.list_tree(treeish, recursive, topdir) if typ == 'blob']
  763. except GitRepositoryError as err:
  764. raise NoSpecError("Cannot find spec file from treeish %s, Git error: %s"
  765. % (treeish, err))
  766. spec_path = guess_spec_fn(file_list, preferred_name)
  767. return spec_from_repo(repo, treeish, spec_path)
  768. def spec_from_repo(repo, treeish, spec_path):
  769. """Get and parse a spec file from a give Git treeish"""
  770. try:
  771. spec = SpecFile(filedata=repo.show('%s:%s' % (treeish, spec_path)))
  772. spec.specdir = os.path.dirname(spec_path)
  773. spec.specfile = os.path.basename(spec_path)
  774. return spec
  775. except GitRepositoryError as err:
  776. raise NoSpecError("Git error: %s" % err)
  777. def string_to_int(val_str):
  778. """
  779. Convert string of possible unit identifier to int.
  780. @param val_str: value to be converted
  781. @type val_str: C{str}
  782. @return: value as integer
  783. @rtype: C{int}
  784. >>> string_to_int("1234")
  785. 1234
  786. >>> string_to_int("123k")
  787. 125952
  788. >>> string_to_int("1234K")
  789. 1263616
  790. >>> string_to_int("1M")
  791. 1048576
  792. """
  793. units = {'k': 1024,
  794. 'm': 1024**2,
  795. 'g': 1024**3,
  796. 't': 1024**4}
  797. if val_str[-1].lower() in units:
  798. return int(val_str[:-1]) * units[val_str[-1].lower()]
  799. else:
  800. return int(val_str)
  801. def split_version_str(version):
  802. """
  803. Parse full version string and split it into individual "version
  804. components", i.e. upstreamversion, epoch and release
  805. @param version: full version of a package
  806. @type version: C{str}
  807. @return: individual version components
  808. @rtype: C{dict}
  809. >>> sorted(split_version_str("1").items())
  810. [('epoch', None), ('release', None), ('upstreamversion', '1')]
  811. >>> sorted(split_version_str("1.2.3-5.3").items())
  812. [('epoch', None), ('release', '5.3'), ('upstreamversion', '1.2.3')]
  813. >>> sorted(split_version_str("3:1.2.3").items())
  814. [('epoch', '3'), ('release', None), ('upstreamversion', '1.2.3')]
  815. >>> sorted(split_version_str("3:1-0").items())
  816. [('epoch', '3'), ('release', '0'), ('upstreamversion', '1')]
  817. """
  818. ret = {'epoch': None, 'upstreamversion': None, 'release': None}
  819. e_vr = version.split(":", 1)
  820. if len(e_vr) == 1:
  821. v_r = e_vr[0].split("-", 1)
  822. else:
  823. ret['epoch'] = e_vr[0]
  824. v_r = e_vr[1].split("-", 1)
  825. ret['upstreamversion'] = v_r[0]
  826. if len(v_r) > 1:
  827. ret['release'] = v_r[1]
  828. return ret
  829. def compose_version_str(evr):
  830. """
  831. Compose a full version string from individual "version components",
  832. i.e. epoch, version and release
  833. @param evr: dict of version components
  834. @type evr: C{dict} of C{str}
  835. @return: full version
  836. @rtype: C{str}
  837. >>> compose_version_str({'epoch': '', 'upstreamversion': '1.0'})
  838. '1.0'
  839. >>> compose_version_str({'epoch': '2', 'upstreamversion': '1.0', 'release': None})
  840. '2:1.0'
  841. >>> compose_version_str({'epoch': None, 'upstreamversion': '1', 'release': '0'})
  842. '1-0'
  843. >>> compose_version_str({'epoch': '2', 'upstreamversion': '1.0', 'release': '2.3'})
  844. '2:1.0-2.3'
  845. >>> compose_version_str({'epoch': '2', 'upstreamversion': '', 'release': '2.3'})
  846. """
  847. if 'upstreamversion' in evr and evr['upstreamversion']:
  848. version = ""
  849. if 'epoch' in evr and evr['epoch']:
  850. version += "%s:" % evr['epoch']
  851. version += evr['upstreamversion']
  852. if 'release' in evr and evr['release']:
  853. version += "-%s" % evr['release']
  854. if version:
  855. return version
  856. return None
  857. def filter_version(evr, *keys):
  858. """
  859. Remove entry from the version dict
  860. @param evr: dict of version components
  861. @type evr: C{dict} of C{str}
  862. @param keys: keys to remove
  863. @type keys: C{str}s
  864. @return: new version dict
  865. @rtype: C{dict} of C{str}
  866. >>> filter_version({'epoch': 'foo', 'upstreamversion': 'bar', 'vendor': 'baz'}, 'vendor').keys()
  867. ['epoch', 'upstreamversion']
  868. >>> filter_version({'epoch': 'foo', 'upstreamversion': 'bar', 'revision': 'baz'}, 'epoch', 'revision').keys()
  869. ['upstreamversion']
  870. """
  871. return {k: evr[k] for k in evr if k not in keys}
  872. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: