__init__.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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}" % (" ".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] != 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 != 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. patchparser.add_option("-p", dest="strip")
  280. patchparser.add_option("-s", dest="silence")
  281. patchparser.add_option("-P", dest="patchnum")
  282. patchparser.add_option("-b", dest="backup")
  283. patchparser.add_option("-E", dest="removeempty")
  284. arglist = args.split()
  285. return patchparser.parse_args(arglist)[0]
  286. @staticmethod
  287. def _setup_macro_opts(args):
  288. """Parse arguments of the '%setup' macro"""
  289. setupparser = OptionParser()
  290. setupparser.add_option("-n", dest="name")
  291. setupparser.add_option("-c", dest="create_dir", action="store_true")
  292. setupparser.add_option("-D", dest="no_delete_dir", action="store_true")
  293. setupparser.add_option("-T", dest="no_unpack_default",
  294. action="store_true")
  295. setupparser.add_option("-b", dest="unpack_before")
  296. setupparser.add_option("-a", dest="unpack_after")
  297. setupparser.add_option("-q", dest="quiet", action="store_true")
  298. arglist = args.split()
  299. return setupparser.parse_args(arglist)[0]
  300. def _parse_directive(self, lineobj):
  301. """Parse special directive/scriptlet/macro lines"""
  302. line = str(lineobj)
  303. matchobj = self.directive_re.match(line)
  304. if not matchobj:
  305. return None
  306. directivename = matchobj.group('name')
  307. # '%patch' macros
  308. directiveid = None
  309. if directivename == 'patch':
  310. opts = self._patch_macro_opts(matchobj.group('args'))
  311. if matchobj.group('num'):
  312. directiveid = int(matchobj.group('num'))
  313. elif opts.patchnum:
  314. directiveid = int(opts.patchnum)
  315. else:
  316. directiveid = -1
  317. # Record special directive/scriptlet/macro locations
  318. if directivename in self.section_identifiers + ('setup', 'patch'):
  319. linerecord = {'line': lineobj,
  320. 'id': directiveid,
  321. 'args': matchobj.group('args')}
  322. self._special_directives[directivename].append(linerecord)
  323. return directivename
  324. def _parse_gbp_tag(self, linenum, lineobj):
  325. """Parse special git-buildpackage tags"""
  326. line = str(lineobj)
  327. matchobj = self.gbptag_re.match(line)
  328. if matchobj:
  329. gbptagname = matchobj.group('name').lower()
  330. if gbptagname not in ('ignore-patches', 'patch-macros'):
  331. gbp.log.info("Found unrecognized Gbp tag on line %s: '%s'" %
  332. (linenum, line))
  333. if matchobj.group('args'):
  334. args = matchobj.group('args').strip()
  335. else:
  336. args = None
  337. record = {'line': lineobj, 'args': args}
  338. self._gbp_tags[gbptagname].append(record)
  339. return gbptagname
  340. return None
  341. def _parse_content(self):
  342. """
  343. Go through spec file content line-by-line and (re-)parse info from it
  344. """
  345. in_preamble = True
  346. for linenum, lineobj in enumerate(self._content):
  347. matched = False
  348. if in_preamble:
  349. if self._parse_tag(lineobj):
  350. continue
  351. matched = self._parse_directive(lineobj)
  352. if matched:
  353. if matched in self.section_identifiers:
  354. in_preamble = False
  355. continue
  356. self._parse_gbp_tag(linenum, lineobj)
  357. # Update sources info (basically possible macros expanded by rpm)
  358. # And, double-check that we parsed spec content correctly
  359. patches = self._patches()
  360. sources = self._sources()
  361. for name, num, typ in self._specinfo.sources:
  362. # workaround rpm parsing bug
  363. if typ == 1 or typ == 9:
  364. if num in sources:
  365. sources[num]['linevalue'] = name
  366. else:
  367. gbp.log.err("BUG: failed to parse all 'Source' tags!")
  368. elif typ == 2 or typ == 10:
  369. # Patch tag without any number defined is treated by RPM as
  370. # having number (2^31-1), we use number -1
  371. if num >= pow(2,30):
  372. num = -1
  373. if num in patches:
  374. patches[num]['linevalue'] = name
  375. else:
  376. gbp.log.err("BUG: failed to parse all 'Patch' tags!")
  377. def _delete_tag(self, tag, num):
  378. """Delete a tag"""
  379. key = tag.lower()
  380. tagname = '%s%s' % (tag, num) if num is not None else tag
  381. if key not in self._tags:
  382. gbp.log.warn("Trying to delete non-existent tag '%s:'" % tag)
  383. return None
  384. sparedlines = []
  385. prev = None
  386. for line in self._tags[key]['lines']:
  387. if line['num'] == num:
  388. gbp.log.debug("Removing '%s:' tag from spec" % tagname)
  389. prev = self._content.delete(line['line'])
  390. else:
  391. sparedlines.append(line)
  392. self._tags[key]['lines'] = sparedlines
  393. if not self._tags[key]['lines']:
  394. self._tags.pop(key)
  395. return prev
  396. def _set_tag(self, tag, num, value, insertafter):
  397. """Set a tag value"""
  398. key = tag.lower()
  399. tagname = '%s%s' % (tag, num) if num is not None else tag
  400. value = value.strip()
  401. if not value:
  402. raise GbpError("Cannot set empty value to '%s:' tag" % tag)
  403. # Check type of tag, we don't support values for 'multivalue' tags
  404. try:
  405. header = self._specinfo.packages[0].header
  406. tagvalue = header[getattr(librpm, 'RPMTAG_%s' % tagname.upper())]
  407. except AttributeError:
  408. tagvalue = None
  409. tagvalue = None if type(tagvalue) is list else value
  410. # Try to guess the correct indentation from the previous or next tag
  411. indent_re = re.compile(r'^([a-z]+([0-9]+)?\s*:\s*)', flags=re.I)
  412. match = indent_re.match(str(insertafter))
  413. if not match:
  414. match = indent_re.match(str(insertafter.next))
  415. indent = 12 if not match else len(match.group(1))
  416. text = '%-*s%s\n' % (indent, '%s:' % tagname, value)
  417. if key in self._tags:
  418. self._tags[key]['value'] = tagvalue
  419. for line in reversed(self._tags[key]['lines']):
  420. if line['num'] == num:
  421. gbp.log.debug("Updating '%s:' tag in spec" % tagname)
  422. line['line'].set_data(text)
  423. line['linevalue'] = value
  424. return line['line']
  425. gbp.log.debug("Adding '%s:' tag after '%s...' line in spec" %
  426. (tagname, str(insertafter)[0:20]))
  427. line = self._content.insert_after(insertafter, text)
  428. linerec = {'line': line, 'num': num, 'linevalue': value}
  429. if key in self._tags:
  430. self._tags[key]['lines'].append(linerec)
  431. else:
  432. self._tags[key] = {'value': tagvalue, 'lines': [linerec]}
  433. return line
  434. def set_tag(self, tag, num, value, insertafter=None):
  435. """Update a tag in spec file content"""
  436. key = tag.lower()
  437. tagname = '%s%s' % (tag, num) if num is not None else tag
  438. if key in ('patch', 'vcs'):
  439. if key in self._tags:
  440. insertafter = key
  441. elif not insertafter in self._tags:
  442. insertafter = 'name'
  443. after_line = self._tags[insertafter]['lines'][-1]['line']
  444. if value:
  445. self._set_tag(tag, num, value, after_line)
  446. elif key in self._tags:
  447. self._delete_tag(tag, num)
  448. else:
  449. raise GbpError("Setting '%s:' tag not supported" % tagname)
  450. def _delete_special_macro(self, name, identifier):
  451. """Delete a special macro line in spec file content"""
  452. if name != 'patch':
  453. raise GbpError("Deleting '%s:' macro not supported" % name)
  454. key = name.lower()
  455. fullname = '%%%s%s' % (name, identifier)
  456. sparedlines = []
  457. prev = None
  458. for line in self._special_directives[key]:
  459. if line['id'] == identifier:
  460. gbp.log.debug("Removing '%s' macro from spec" % fullname)
  461. prev = self._content.delete(line['line'])
  462. else:
  463. sparedlines.append(line)
  464. self._special_directives[key] = sparedlines
  465. if not prev:
  466. gbp.log.warn("Tried to delete non-existent macro '%s'" % fullname)
  467. return prev
  468. def _set_special_macro(self, name, identifier, args, insertafter):
  469. """Update a special macro line in spec file content"""
  470. key = name.lower()
  471. fullname = '%%%s%s' % (name, identifier)
  472. if key != 'patch':
  473. raise GbpError("Setting '%s' macro not supported" % name)
  474. updated = 0
  475. text = "%%%s%d %s\n" % (name, identifier, args)
  476. for line in self._special_directives[key]:
  477. if line['id'] == identifier:
  478. gbp.log.debug("Updating '%s' macro in spec" % fullname)
  479. line['args'] = args
  480. line['line'].set_data(text)
  481. ret = line['line']
  482. updated += 1
  483. if not updated:
  484. gbp.log.debug("Adding '%s' macro after '%s...' line in spec" %
  485. (fullname, str(insertafter)[0:20]))
  486. ret = self._content.insert_after(insertafter, text)
  487. linerec = {'line': ret, 'id': identifier, 'args': args}
  488. self._special_directives[key].append(linerec)
  489. return ret
  490. def _set_section(self, name, text):
  491. """Update/create a complete section in spec file."""
  492. if name not in self.section_identifiers:
  493. raise GbpError("Not a valid section directive: '%s'" % name)
  494. # Delete section, if it exists
  495. if name in self._special_directives:
  496. if len(self._special_directives[name]) > 1:
  497. raise GbpError("Multiple %%%s sections found, don't know "
  498. "which to update" % name)
  499. line = self._special_directives[name][0]['line']
  500. gbp.log.debug("Removing content of %s section" % name)
  501. while line.next:
  502. match = self.directive_re.match(str(line.next))
  503. if match and match.group('name') in self.section_identifiers:
  504. break
  505. self._content.delete(line.next)
  506. else:
  507. gbp.log.debug("Adding %s section to the end of spec file" % name)
  508. line = self._content.append('%%%s\n' % name)
  509. linerec = {'line': line, 'id': None, 'args': None}
  510. self._special_directives[name] = [linerec]
  511. # Add new lines
  512. gbp.log.debug("Updating content of %s section" % name)
  513. for linetext in text.splitlines():
  514. line = self._content.insert_after(line, linetext + '\n')
  515. def set_changelog(self, text):
  516. """Update or create the %changelog section"""
  517. self._set_section('changelog', text)
  518. def get_changelog(self):
  519. """Get the %changelog section"""
  520. text = ''
  521. if 'changelog' in self._special_directives:
  522. line = self._special_directives['changelog'][0]['line']
  523. while line.next:
  524. line = line.next
  525. match = self.directive_re.match(str(line))
  526. if match and match.group('name') in self.section_identifiers:
  527. break
  528. text += str(line)
  529. return text
  530. def update_patches(self, patches, commands):
  531. """Update spec with new patch tags and patch macros"""
  532. # Remove non-ignored patches
  533. tag_prev = None
  534. macro_prev = None
  535. ignored = self.ignorepatches
  536. # Remove 'Patch:̈́' tags
  537. for tag in self._patches().values():
  538. if not tag['num'] in ignored:
  539. tag_prev = self._delete_tag('patch', tag['num'])
  540. # Remove a preceding comment if it seems to originate from GBP
  541. if re.match("^\s*#.*patch.*auto-generated",
  542. str(tag_prev), flags=re.I):
  543. tag_prev = self._content.delete(tag_prev)
  544. # Remove '%patch:' macros
  545. for macro in self._special_directives['patch']:
  546. if not macro['id'] in ignored:
  547. macro_prev = self._delete_special_macro('patch', macro['id'])
  548. # Remove surrounding if-else
  549. macro_next = macro_prev.next
  550. if (str(macro_prev).startswith('%if') and
  551. str(macro_next).startswith('%endif')):
  552. self._content.delete(macro_next)
  553. macro_prev = self._content.delete(macro_prev)
  554. # Remove a preceding comment line if it ends with '.patch' or
  555. # '.diff' plus an optional compression suffix
  556. if re.match("^\s*#.+(patch|diff)(\.(gz|bz2|xz|lzma))?\s*$",
  557. str(macro_prev), flags=re.I):
  558. macro_prev = self._content.delete(macro_prev)
  559. if len(patches) == 0:
  560. return
  561. # Determine where to add Patch tag lines
  562. if tag_prev:
  563. gbp.log.debug("Adding 'Patch' tags in place of the removed tags")
  564. tag_line = tag_prev
  565. elif 'patch' in self._tags:
  566. gbp.log.debug("Adding new 'Patch' tags after the last 'Patch' tag")
  567. tag_line = self._tags['patch']['lines'][-1]['line']
  568. elif 'source' in self._tags:
  569. gbp.log.debug("Didn't find any old 'Patch' tags, adding new "
  570. "patches after the last 'Source' tag.")
  571. tag_line = self._tags['source']['lines'][-1]['line']
  572. else:
  573. gbp.log.debug("Didn't find any old 'Patch' or 'Source' tags, "
  574. "adding new patches after the last 'Name' tag.")
  575. tag_line = self._tags['name']['lines'][-1]['line']
  576. # Determine where to add %patch macro lines
  577. if 'patch-macros' in self._gbp_tags:
  578. gbp.log.debug("Adding '%patch' macros after the start marker")
  579. macro_line = self._gbp_tags['patch-macros'][-1]['line']
  580. elif macro_prev:
  581. gbp.log.debug("Adding '%patch' macros in place of the removed "
  582. "macros")
  583. macro_line = macro_prev
  584. elif self._special_directives['patch']:
  585. gbp.log.debug("Adding new '%patch' macros after the last existing"
  586. "'%patch' macro")
  587. macro_line = self._special_directives['patch'][-1]['line']
  588. elif self._special_directives['setup']:
  589. gbp.log.debug("Didn't find any old '%patch' macros, adding new "
  590. "patches after the last '%setup' macro")
  591. macro_line = self._special_directives['setup'][-1]['line']
  592. elif self._special_directives['prep']:
  593. gbp.log.warn("Didn't find any old '%patch' or '%setup' macros, "
  594. "adding new patches directly after '%prep' directive")
  595. macro_line = self._special_directives['prep'][-1]['line']
  596. else:
  597. raise GbpError("Couldn't determine where to add '%patch' macros")
  598. startnum = sorted(ignored)[-1] + 1 if ignored else 0
  599. gbp.log.debug("Starting autoupdate patch numbering from %s" % startnum)
  600. # Add a comment indicating gbp generated patch tags
  601. comment_text = "# Patches auto-generated by git-buildpackage:\n"
  602. tag_line = self._content.insert_after(tag_line, comment_text)
  603. for ind, patch in enumerate(patches):
  604. cmds = commands[patch] if patch in commands else {}
  605. patchnum = startnum + ind
  606. tag_line = self._set_tag("Patch", patchnum, patch, tag_line)
  607. # Add '%patch' macro and a preceding comment line
  608. comment_text = "# %s\n" % patch
  609. macro_line = self._content.insert_after(macro_line, comment_text)
  610. macro_line = self._set_special_macro('patch', patchnum, '-p1',
  611. macro_line)
  612. for cmd, args in six.iteritems(cmds):
  613. if cmd in ('if', 'ifarch'):
  614. self._content.insert_before(macro_line, '%%%s %s\n' %
  615. (cmd, args))
  616. macro_line = self._content.insert_after(macro_line,
  617. '%endif\n')
  618. # We only support one command per patch, for now
  619. break
  620. def patchseries(self, unapplied=False, ignored=False):
  621. """Return non-ignored patches of the RPM as a gbp patchseries"""
  622. series = PatchSeries()
  623. if 'patch' in self._tags:
  624. tags = self._patches()
  625. applied = []
  626. for macro in self._special_directives['patch']:
  627. if macro['id'] in tags:
  628. applied.append((macro['id'], macro['args']))
  629. ignored = set() if ignored else set(self.ignorepatches)
  630. # Put all patches that are applied first in the series
  631. for num, args in applied:
  632. if num not in ignored:
  633. opts = self._patch_macro_opts(args)
  634. strip = int(opts.strip) if opts.strip else 0
  635. filename = os.path.basename(tags[num]['linevalue'])
  636. series.append(Patch(os.path.join(self.specdir, filename),
  637. strip=strip))
  638. # Finally, append all unapplied patches to the series, if requested
  639. if unapplied:
  640. applied_nums = set([num for num, _args in applied])
  641. unapplied = set(tags.keys()).difference(applied_nums)
  642. for num in sorted(unapplied):
  643. if num not in ignored:
  644. filename = os.path.basename(tags[num]['linevalue'])
  645. series.append(Patch(os.path.join(self.specdir,
  646. filename), strip=0))
  647. return series
  648. def _guess_orig_prefix(self, orig):
  649. """Guess prefix for the orig file"""
  650. # Make initial guess about the prefix in the archive
  651. filename = orig['filename']
  652. name, version = RpmPkgPolicy.guess_upstream_src_version(filename)
  653. if name and version:
  654. prefix = "%s-%s/" % (name, version)
  655. else:
  656. prefix = orig['filename_base'] + "/"
  657. # Refine our guess about the prefix
  658. for macro in self._special_directives['setup']:
  659. args = macro['args']
  660. opts = self._setup_macro_opts(args)
  661. srcnum = None
  662. if opts.no_unpack_default:
  663. if opts.unpack_before:
  664. srcnum = int(opts.unpack_before)
  665. elif opts.unpack_after:
  666. srcnum = int(opts.unpack_after)
  667. else:
  668. srcnum = 0
  669. if srcnum == orig['num']:
  670. if opts.create_dir:
  671. prefix = ''
  672. elif opts.name:
  673. try:
  674. prefix = self.macro_expand(opts.name) + '/'
  675. except MacroExpandError as err:
  676. gbp.log.warn("Couldn't determine prefix from %%setup "\
  677. "macro (%s). Using filename base as a " \
  678. "fallback" % err)
  679. prefix = orig['filename_base'] + '/'
  680. else:
  681. # RPM default
  682. prefix = "%s-%s/" % (self.name, self.upstreamversion)
  683. break
  684. return prefix
  685. def _guess_orig_file(self):
  686. """
  687. Try to guess the name of the primary upstream/source archive.
  688. Returns a dict with all the relevant information.
  689. """
  690. orig = None
  691. sources = self.sources()
  692. for num, filename in sorted(six.iteritems(sources)):
  693. src = {'num': num, 'filename': os.path.basename(filename),
  694. 'uri': filename}
  695. src['filename_base'], src['archive_fmt'], src['compression'] = \
  696. parse_archive_filename(os.path.basename(filename))
  697. if (src['filename_base'].startswith(self.name) and
  698. src['archive_fmt']):
  699. # Take the first archive that starts with pkg name
  700. orig = src
  701. break
  702. # otherwise we take the first archive
  703. elif not orig and src['archive_fmt']:
  704. orig = src
  705. # else don't accept
  706. if orig:
  707. orig['prefix'] = self._guess_orig_prefix(orig)
  708. return orig
  709. def parse_srpm(srpmfile):
  710. """parse srpm by creating a SrcRpmFile object"""
  711. try:
  712. srcrpm = SrcRpmFile(srpmfile)
  713. except IOError as err:
  714. raise GbpError("Error reading src.rpm file: %s" % err)
  715. except librpm.error as err:
  716. raise GbpError("RPM error while reading src.rpm: %s" % err)
  717. return srcrpm
  718. def guess_spec_fn(file_list, preferred_name=None):
  719. """Guess spec file from a list of filenames"""
  720. specs = []
  721. for filepath in file_list:
  722. filename = os.path.basename(filepath)
  723. # Stop at the first file matching the preferred name
  724. if filename == preferred_name:
  725. gbp.log.debug("Found a preferred spec file %s" % filepath)
  726. specs = [filepath]
  727. break
  728. if filename.endswith(".spec"):
  729. gbp.log.debug("Found spec file %s" % filepath)
  730. specs.append(filepath)
  731. if len(specs) == 0:
  732. raise NoSpecError("No spec file found.")
  733. elif len(specs) > 1:
  734. raise NoSpecError("Multiple spec files found (%s), don't know which "
  735. "to use." % ', '.join(specs))
  736. return specs[0]
  737. def guess_spec(topdir, recursive=True, preferred_name=None):
  738. """Guess a spec file"""
  739. file_list = []
  740. if not topdir:
  741. topdir = '.'
  742. for root, dirs, files in os.walk(topdir):
  743. file_list.extend([os.path.join(root, fname) for fname in files])
  744. if not recursive:
  745. del dirs[:]
  746. # Skip .git dir in any case
  747. if '.git' in dirs:
  748. dirs.remove('.git')
  749. return SpecFile(os.path.abspath(guess_spec_fn(file_list, preferred_name)))
  750. def guess_spec_repo(repo, treeish, topdir='', recursive=True, preferred_name=None):
  751. """
  752. Try to find/parse the spec file from a given git treeish.
  753. """
  754. topdir = topdir.rstrip('/') + ('/') if topdir else ''
  755. try:
  756. file_list = [nam for (mod, typ, sha, nam) in
  757. repo.list_tree(treeish, recursive, topdir) if typ == 'blob']
  758. except GitRepositoryError as err:
  759. raise NoSpecError("Cannot find spec file from treeish %s, Git error: %s"
  760. % (treeish, err))
  761. spec_path = guess_spec_fn(file_list, preferred_name)
  762. return spec_from_repo(repo, treeish, spec_path)
  763. def spec_from_repo(repo, treeish, spec_path):
  764. """Get and parse a spec file from a give Git treeish"""
  765. try:
  766. spec = SpecFile(filedata=repo.show('%s:%s' % (treeish, spec_path)))
  767. spec.specdir = os.path.dirname(spec_path)
  768. spec.specfile = os.path.basename(spec_path)
  769. return spec
  770. except GitRepositoryError as err:
  771. raise NoSpecError("Git error: %s" % err)
  772. def string_to_int(val_str):
  773. """
  774. Convert string of possible unit identifier to int.
  775. @param val_str: value to be converted
  776. @type val_str: C{str}
  777. @return: value as integer
  778. @rtype: C{int}
  779. >>> string_to_int("1234")
  780. 1234
  781. >>> string_to_int("123k")
  782. 125952
  783. >>> string_to_int("1234K")
  784. 1263616
  785. >>> string_to_int("1M")
  786. 1048576
  787. """
  788. units = {'k': 1024,
  789. 'm': 1024**2,
  790. 'g': 1024**3,
  791. 't': 1024**4}
  792. if val_str[-1].lower() in units:
  793. return int(val_str[:-1]) * units[val_str[-1].lower()]
  794. else:
  795. return int(val_str)
  796. def split_version_str(version):
  797. """
  798. Parse full version string and split it into individual "version
  799. components", i.e. upstreamversion, epoch and release
  800. @param version: full version of a package
  801. @type version: C{str}
  802. @return: individual version components
  803. @rtype: C{dict}
  804. >>> sorted(split_version_str("1").items())
  805. [('epoch', None), ('release', None), ('upstreamversion', '1')]
  806. >>> sorted(split_version_str("1.2.3-5.3").items())
  807. [('epoch', None), ('release', '5.3'), ('upstreamversion', '1.2.3')]
  808. >>> sorted(split_version_str("3:1.2.3").items())
  809. [('epoch', '3'), ('release', None), ('upstreamversion', '1.2.3')]
  810. >>> sorted(split_version_str("3:1-0").items())
  811. [('epoch', '3'), ('release', '0'), ('upstreamversion', '1')]
  812. """
  813. ret = {'epoch': None, 'upstreamversion': None, 'release': None}
  814. e_vr = version.split(":", 1)
  815. if len(e_vr) == 1:
  816. v_r = e_vr[0].split("-", 1)
  817. else:
  818. ret['epoch'] = e_vr[0]
  819. v_r = e_vr[1].split("-", 1)
  820. ret['upstreamversion'] = v_r[0]
  821. if len(v_r) > 1:
  822. ret['release'] = v_r[1]
  823. return ret
  824. def compose_version_str(evr):
  825. """
  826. Compose a full version string from individual "version components",
  827. i.e. epoch, version and release
  828. @param evr: dict of version components
  829. @type evr: C{dict} of C{str}
  830. @return: full version
  831. @rtype: C{str}
  832. >>> compose_version_str({'epoch': '', 'upstreamversion': '1.0'})
  833. '1.0'
  834. >>> compose_version_str({'epoch': '2', 'upstreamversion': '1.0', 'release': None})
  835. '2:1.0'
  836. >>> compose_version_str({'epoch': None, 'upstreamversion': '1', 'release': '0'})
  837. '1-0'
  838. >>> compose_version_str({'epoch': '2', 'upstreamversion': '1.0', 'release': '2.3'})
  839. '2:1.0-2.3'
  840. >>> compose_version_str({'epoch': '2', 'upstreamversion': '', 'release': '2.3'})
  841. """
  842. if 'upstreamversion' in evr and evr['upstreamversion']:
  843. version = ""
  844. if 'epoch' in evr and evr['epoch']:
  845. version += "%s:" % evr['epoch']
  846. version += evr['upstreamversion']
  847. if 'release' in evr and evr['release']:
  848. version += "-%s" % evr['release']
  849. if version:
  850. return version
  851. return None
  852. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: