recipeutils.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. # Utility functions for reading and modifying recipes
  2. #
  3. # Some code borrowed from the OE layer index
  4. #
  5. # Copyright (C) 2013-2016 Intel Corporation
  6. #
  7. import sys
  8. import os
  9. import os.path
  10. import tempfile
  11. import textwrap
  12. import difflib
  13. from . import utils
  14. import shutil
  15. import re
  16. import fnmatch
  17. import glob
  18. from collections import OrderedDict, defaultdict
  19. # Help us to find places to insert values
  20. recipe_progression = ['SUMMARY', 'DESCRIPTION', 'HOMEPAGE', 'BUGTRACKER', 'SECTION', 'LICENSE', 'LICENSE_FLAGS', 'LIC_FILES_CHKSUM', 'PROVIDES', 'DEPENDS', 'PR', 'PV', 'SRCREV', 'SRCPV', 'SRC_URI', 'S', 'do_fetch()', 'do_unpack()', 'do_patch()', 'EXTRA_OECONF', 'EXTRA_OECMAKE', 'EXTRA_OESCONS', 'do_configure()', 'EXTRA_OEMAKE', 'do_compile()', 'do_install()', 'do_populate_sysroot()', 'INITSCRIPT', 'USERADD', 'GROUPADD', 'PACKAGES', 'FILES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RPROVIDES', 'RREPLACES', 'RCONFLICTS', 'ALLOW_EMPTY', 'populate_packages()', 'do_package()', 'do_deploy()']
  21. # Variables that sometimes are a bit long but shouldn't be wrapped
  22. nowrap_vars = ['SUMMARY', 'HOMEPAGE', 'BUGTRACKER', 'SRC_URI[md5sum]', 'SRC_URI[sha256sum]']
  23. list_vars = ['SRC_URI', 'LIC_FILES_CHKSUM']
  24. meta_vars = ['SUMMARY', 'DESCRIPTION', 'HOMEPAGE', 'BUGTRACKER', 'SECTION']
  25. def pn_to_recipe(cooker, pn, mc=''):
  26. """Convert a recipe name (PN) to the path to the recipe file"""
  27. best = cooker.findBestProvider(pn, mc)
  28. return best[3]
  29. def get_unavailable_reasons(cooker, pn):
  30. """If a recipe could not be found, find out why if possible"""
  31. import bb.taskdata
  32. taskdata = bb.taskdata.TaskData(None, skiplist=cooker.skiplist)
  33. return taskdata.get_reasons(pn)
  34. def parse_recipe(cooker, fn, appendfiles):
  35. """
  36. Parse an individual recipe file, optionally with a list of
  37. bbappend files.
  38. """
  39. import bb.cache
  40. parser = bb.cache.NoCache(cooker.databuilder)
  41. envdata = parser.loadDataFull(fn, appendfiles)
  42. return envdata
  43. def parse_recipe_simple(cooker, pn, d, appends=True):
  44. """
  45. Parse a recipe and optionally all bbappends that apply to it
  46. in the current configuration.
  47. """
  48. import bb.providers
  49. recipefile = pn_to_recipe(cooker, pn)
  50. if not recipefile:
  51. skipreasons = get_unavailable_reasons(cooker, pn)
  52. # We may as well re-use bb.providers.NoProvider here
  53. if skipreasons:
  54. raise bb.providers.NoProvider(skipreasons)
  55. else:
  56. raise bb.providers.NoProvider('Unable to find any recipe file matching %s' % pn)
  57. if appends:
  58. appendfiles = cooker.collection.get_file_appends(recipefile)
  59. else:
  60. appendfiles = None
  61. return parse_recipe(cooker, recipefile, appendfiles)
  62. def get_var_files(fn, varlist, d):
  63. """Find the file in which each of a list of variables is set.
  64. Note: requires variable history to be enabled when parsing.
  65. """
  66. varfiles = {}
  67. for v in varlist:
  68. history = d.varhistory.variable(v)
  69. files = []
  70. for event in history:
  71. if 'file' in event and not 'flag' in event:
  72. files.append(event['file'])
  73. if files:
  74. actualfile = files[-1]
  75. else:
  76. actualfile = None
  77. varfiles[v] = actualfile
  78. return varfiles
  79. def split_var_value(value, assignment=True):
  80. """
  81. Split a space-separated variable's value into a list of items,
  82. taking into account that some of the items might be made up of
  83. expressions containing spaces that should not be split.
  84. Parameters:
  85. value:
  86. The string value to split
  87. assignment:
  88. True to assume that the value represents an assignment
  89. statement, False otherwise. If True, and an assignment
  90. statement is passed in the first item in
  91. the returned list will be the part of the assignment
  92. statement up to and including the opening quote character,
  93. and the last item will be the closing quote.
  94. """
  95. inexpr = 0
  96. lastchar = None
  97. out = []
  98. buf = ''
  99. for char in value:
  100. if char == '{':
  101. if lastchar == '$':
  102. inexpr += 1
  103. elif char == '}':
  104. inexpr -= 1
  105. elif assignment and char in '"\'' and inexpr == 0:
  106. if buf:
  107. out.append(buf)
  108. out.append(char)
  109. char = ''
  110. buf = ''
  111. elif char.isspace() and inexpr == 0:
  112. char = ''
  113. if buf:
  114. out.append(buf)
  115. buf = ''
  116. buf += char
  117. lastchar = char
  118. if buf:
  119. out.append(buf)
  120. # Join together assignment statement and opening quote
  121. outlist = out
  122. if assignment:
  123. assigfound = False
  124. for idx, item in enumerate(out):
  125. if '=' in item:
  126. assigfound = True
  127. if assigfound:
  128. if '"' in item or "'" in item:
  129. outlist = [' '.join(out[:idx+1])]
  130. outlist.extend(out[idx+1:])
  131. break
  132. return outlist
  133. def patch_recipe_lines(fromlines, values, trailing_newline=True):
  134. """Update or insert variable values into lines from a recipe.
  135. Note that some manual inspection/intervention may be required
  136. since this cannot handle all situations.
  137. """
  138. import bb.utils
  139. if trailing_newline:
  140. newline = '\n'
  141. else:
  142. newline = ''
  143. recipe_progression_res = []
  144. recipe_progression_restrs = []
  145. for item in recipe_progression:
  146. if item.endswith('()'):
  147. key = item[:-2]
  148. else:
  149. key = item
  150. restr = '%s(_[a-zA-Z0-9-_$(){}]+|\[[^\]]*\])?' % key
  151. if item.endswith('()'):
  152. recipe_progression_restrs.append(restr + '()')
  153. else:
  154. recipe_progression_restrs.append(restr)
  155. recipe_progression_res.append(re.compile('^%s$' % restr))
  156. def get_recipe_pos(variable):
  157. for i, p in enumerate(recipe_progression_res):
  158. if p.match(variable):
  159. return i
  160. return -1
  161. remainingnames = {}
  162. for k in values.keys():
  163. remainingnames[k] = get_recipe_pos(k)
  164. remainingnames = OrderedDict(sorted(remainingnames.items(), key=lambda x: x[1]))
  165. modifying = False
  166. def outputvalue(name, lines, rewindcomments=False):
  167. if values[name] is None:
  168. return
  169. rawtext = '%s = "%s"%s' % (name, values[name], newline)
  170. addlines = []
  171. if name in nowrap_vars:
  172. addlines.append(rawtext)
  173. elif name in list_vars:
  174. splitvalue = split_var_value(values[name], assignment=False)
  175. if len(splitvalue) > 1:
  176. linesplit = ' \\\n' + (' ' * (len(name) + 4))
  177. addlines.append('%s = "%s%s"%s' % (name, linesplit.join(splitvalue), linesplit, newline))
  178. else:
  179. addlines.append(rawtext)
  180. else:
  181. wrapped = textwrap.wrap(rawtext)
  182. for wrapline in wrapped[:-1]:
  183. addlines.append('%s \\%s' % (wrapline, newline))
  184. addlines.append('%s%s' % (wrapped[-1], newline))
  185. if rewindcomments:
  186. # Ensure we insert the lines before any leading comments
  187. # (that we'd want to ensure remain leading the next value)
  188. for i, ln in reversed(list(enumerate(lines))):
  189. if not ln.startswith('#'):
  190. lines[i+1:i+1] = addlines
  191. break
  192. else:
  193. lines.extend(addlines)
  194. else:
  195. lines.extend(addlines)
  196. existingnames = []
  197. def patch_recipe_varfunc(varname, origvalue, op, newlines):
  198. if modifying:
  199. # Insert anything that should come before this variable
  200. pos = get_recipe_pos(varname)
  201. for k in list(remainingnames):
  202. if remainingnames[k] > -1 and pos >= remainingnames[k] and not k in existingnames:
  203. outputvalue(k, newlines, rewindcomments=True)
  204. del remainingnames[k]
  205. # Now change this variable, if it needs to be changed
  206. if varname in existingnames and op in ['+=', '=', '=+']:
  207. if varname in remainingnames:
  208. outputvalue(varname, newlines)
  209. del remainingnames[varname]
  210. return None, None, 0, True
  211. else:
  212. if varname in values:
  213. existingnames.append(varname)
  214. return origvalue, None, 0, True
  215. # First run - establish which values we want to set are already in the file
  216. varlist = [re.escape(item) for item in values.keys()]
  217. bb.utils.edit_metadata(fromlines, varlist, patch_recipe_varfunc)
  218. # Second run - actually set everything
  219. modifying = True
  220. varlist.extend(recipe_progression_restrs)
  221. changed, tolines = bb.utils.edit_metadata(fromlines, varlist, patch_recipe_varfunc, match_overrides=True)
  222. if remainingnames:
  223. if tolines and tolines[-1].strip() != '':
  224. tolines.append('\n')
  225. for k in remainingnames.keys():
  226. outputvalue(k, tolines)
  227. return changed, tolines
  228. def patch_recipe_file(fn, values, patch=False, relpath=''):
  229. """Update or insert variable values into a recipe file (assuming you
  230. have already identified the exact file you want to update.)
  231. Note that some manual inspection/intervention may be required
  232. since this cannot handle all situations.
  233. """
  234. with open(fn, 'r') as f:
  235. fromlines = f.readlines()
  236. _, tolines = patch_recipe_lines(fromlines, values)
  237. if patch:
  238. relfn = os.path.relpath(fn, relpath)
  239. diff = difflib.unified_diff(fromlines, tolines, 'a/%s' % relfn, 'b/%s' % relfn)
  240. return diff
  241. else:
  242. with open(fn, 'w') as f:
  243. f.writelines(tolines)
  244. return None
  245. def localise_file_vars(fn, varfiles, varlist):
  246. """Given a list of variables and variable history (fetched with get_var_files())
  247. find where each variable should be set/changed. This handles for example where a
  248. recipe includes an inc file where variables might be changed - in most cases
  249. we want to update the inc file when changing the variable value rather than adding
  250. it to the recipe itself.
  251. """
  252. fndir = os.path.dirname(fn) + os.sep
  253. first_meta_file = None
  254. for v in meta_vars:
  255. f = varfiles.get(v, None)
  256. if f:
  257. actualdir = os.path.dirname(f) + os.sep
  258. if actualdir.startswith(fndir):
  259. first_meta_file = f
  260. break
  261. filevars = defaultdict(list)
  262. for v in varlist:
  263. f = varfiles[v]
  264. # Only return files that are in the same directory as the recipe or in some directory below there
  265. # (this excludes bbclass files and common inc files that wouldn't be appropriate to set the variable
  266. # in if we were going to set a value specific to this recipe)
  267. if f:
  268. actualfile = f
  269. else:
  270. # Variable isn't in a file, if it's one of the "meta" vars, use the first file with a meta var in it
  271. if first_meta_file:
  272. actualfile = first_meta_file
  273. else:
  274. actualfile = fn
  275. actualdir = os.path.dirname(actualfile) + os.sep
  276. if not actualdir.startswith(fndir):
  277. actualfile = fn
  278. filevars[actualfile].append(v)
  279. return filevars
  280. def patch_recipe(d, fn, varvalues, patch=False, relpath=''):
  281. """Modify a list of variable values in the specified recipe. Handles inc files if
  282. used by the recipe.
  283. """
  284. varlist = varvalues.keys()
  285. varfiles = get_var_files(fn, varlist, d)
  286. locs = localise_file_vars(fn, varfiles, varlist)
  287. patches = []
  288. for f,v in locs.items():
  289. vals = {k: varvalues[k] for k in v}
  290. patchdata = patch_recipe_file(f, vals, patch, relpath)
  291. if patch:
  292. patches.append(patchdata)
  293. if patch:
  294. return patches
  295. else:
  296. return None
  297. def copy_recipe_files(d, tgt_dir, whole_dir=False, download=True):
  298. """Copy (local) recipe files, including both files included via include/require,
  299. and files referred to in the SRC_URI variable."""
  300. import bb.fetch2
  301. import oe.path
  302. # FIXME need a warning if the unexpanded SRC_URI value contains variable references
  303. uris = (d.getVar('SRC_URI', True) or "").split()
  304. fetch = bb.fetch2.Fetch(uris, d)
  305. if download:
  306. fetch.download()
  307. # Copy local files to target directory and gather any remote files
  308. bb_dir = os.path.dirname(d.getVar('FILE', True)) + os.sep
  309. remotes = []
  310. copied = []
  311. includes = [path for path in d.getVar('BBINCLUDED', True).split() if
  312. path.startswith(bb_dir) and os.path.exists(path)]
  313. for path in fetch.localpaths() + includes:
  314. # Only import files that are under the meta directory
  315. if path.startswith(bb_dir):
  316. if not whole_dir:
  317. relpath = os.path.relpath(path, bb_dir)
  318. subdir = os.path.join(tgt_dir, os.path.dirname(relpath))
  319. if not os.path.exists(subdir):
  320. os.makedirs(subdir)
  321. shutil.copy2(path, os.path.join(tgt_dir, relpath))
  322. copied.append(relpath)
  323. else:
  324. remotes.append(path)
  325. # Simply copy whole meta dir, if requested
  326. if whole_dir:
  327. shutil.copytree(bb_dir, tgt_dir)
  328. return copied, remotes
  329. def get_recipe_local_files(d, patches=False, archives=False):
  330. """Get a list of local files in SRC_URI within a recipe."""
  331. import oe.patch
  332. uris = (d.getVar('SRC_URI', True) or "").split()
  333. fetch = bb.fetch2.Fetch(uris, d)
  334. # FIXME this list should be factored out somewhere else (such as the
  335. # fetcher) though note that this only encompasses actual container formats
  336. # i.e. that can contain multiple files as opposed to those that only
  337. # contain a compressed stream (i.e. .tar.gz as opposed to just .gz)
  338. archive_exts = ['.tar', '.tgz', '.tar.gz', '.tar.Z', '.tbz', '.tbz2', '.tar.bz2', '.tar.xz', '.tar.lz', '.zip', '.jar', '.rpm', '.srpm', '.deb', '.ipk', '.tar.7z', '.7z']
  339. ret = {}
  340. for uri in uris:
  341. if fetch.ud[uri].type == 'file':
  342. if (not patches and
  343. oe.patch.patch_path(uri, fetch, '', expand=False)):
  344. continue
  345. # Skip files that are referenced by absolute path
  346. fname = fetch.ud[uri].basepath
  347. if os.path.isabs(fname):
  348. continue
  349. # Handle subdir=
  350. subdir = fetch.ud[uri].parm.get('subdir', '')
  351. if subdir:
  352. if os.path.isabs(subdir):
  353. continue
  354. fname = os.path.join(subdir, fname)
  355. localpath = fetch.localpath(uri)
  356. if not archives:
  357. # Ignore archives that will be unpacked
  358. if localpath.endswith(tuple(archive_exts)):
  359. unpack = fetch.ud[uri].parm.get('unpack', True)
  360. if unpack:
  361. continue
  362. ret[fname] = localpath
  363. return ret
  364. def get_recipe_patches(d):
  365. """Get a list of the patches included in SRC_URI within a recipe."""
  366. import oe.patch
  367. patches = oe.patch.src_patches(d, expand=False)
  368. patchfiles = []
  369. for patch in patches:
  370. _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
  371. patchfiles.append(local)
  372. return patchfiles
  373. def get_recipe_patched_files(d):
  374. """
  375. Get the list of patches for a recipe along with the files each patch modifies.
  376. Params:
  377. d: the datastore for the recipe
  378. Returns:
  379. a dict mapping patch file path to a list of tuples of changed files and
  380. change mode ('A' for add, 'D' for delete or 'M' for modify)
  381. """
  382. import oe.patch
  383. patches = oe.patch.src_patches(d, expand=False)
  384. patchedfiles = {}
  385. for patch in patches:
  386. _, _, patchfile, _, _, parm = bb.fetch.decodeurl(patch)
  387. striplevel = int(parm['striplevel'])
  388. patchedfiles[patchfile] = oe.patch.PatchSet.getPatchedFiles(patchfile, striplevel, os.path.join(d.getVar('S', True), parm.get('patchdir', '')))
  389. return patchedfiles
  390. def validate_pn(pn):
  391. """Perform validation on a recipe name (PN) for a new recipe."""
  392. reserved_names = ['forcevariable', 'append', 'prepend', 'remove']
  393. if not re.match('^[0-9a-z-.+]+$', pn):
  394. return 'Recipe name "%s" is invalid: only characters 0-9, a-z, -, + and . are allowed' % pn
  395. elif pn in reserved_names:
  396. return 'Recipe name "%s" is invalid: is a reserved keyword' % pn
  397. elif pn.startswith('pn-'):
  398. return 'Recipe name "%s" is invalid: names starting with "pn-" are reserved' % pn
  399. elif pn.endswith(('.bb', '.bbappend', '.bbclass', '.inc', '.conf')):
  400. return 'Recipe name "%s" is invalid: should be just a name, not a file name' % pn
  401. return ''
  402. def get_bbfile_path(d, destdir, extrapathhint=None):
  403. """
  404. Determine the correct path for a recipe within a layer
  405. Parameters:
  406. d: Recipe-specific datastore
  407. destdir: destination directory. Can be the path to the base of the layer or a
  408. partial path somewhere within the layer.
  409. extrapathhint: a path relative to the base of the layer to try
  410. """
  411. import bb.cookerdata
  412. destdir = os.path.abspath(destdir)
  413. destlayerdir = find_layerdir(destdir)
  414. # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf
  415. confdata = d.createCopy()
  416. confdata.setVar('BBFILES', '')
  417. confdata.setVar('LAYERDIR', destlayerdir)
  418. destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf")
  419. confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata)
  420. pn = d.getVar('PN', True)
  421. bbfilespecs = (confdata.getVar('BBFILES', True) or '').split()
  422. if destdir == destlayerdir:
  423. for bbfilespec in bbfilespecs:
  424. if not bbfilespec.endswith('.bbappend'):
  425. for match in glob.glob(bbfilespec):
  426. splitext = os.path.splitext(os.path.basename(match))
  427. if splitext[1] == '.bb':
  428. mpn = splitext[0].split('_')[0]
  429. if mpn == pn:
  430. return os.path.dirname(match)
  431. # Try to make up a path that matches BBFILES
  432. # this is a little crude, but better than nothing
  433. bpn = d.getVar('BPN', True)
  434. recipefn = os.path.basename(d.getVar('FILE', True))
  435. pathoptions = [destdir]
  436. if extrapathhint:
  437. pathoptions.append(os.path.join(destdir, extrapathhint))
  438. if destdir == destlayerdir:
  439. pathoptions.append(os.path.join(destdir, 'recipes-%s' % bpn, bpn))
  440. pathoptions.append(os.path.join(destdir, 'recipes', bpn))
  441. pathoptions.append(os.path.join(destdir, bpn))
  442. elif not destdir.endswith(('/' + pn, '/' + bpn)):
  443. pathoptions.append(os.path.join(destdir, bpn))
  444. closepath = ''
  445. for pathoption in pathoptions:
  446. bbfilepath = os.path.join(pathoption, 'test.bb')
  447. for bbfilespec in bbfilespecs:
  448. if fnmatch.fnmatchcase(bbfilepath, bbfilespec):
  449. return pathoption
  450. return None
  451. def get_bbappend_path(d, destlayerdir, wildcardver=False):
  452. """Determine how a bbappend for a recipe should be named and located within another layer"""
  453. import bb.cookerdata
  454. destlayerdir = os.path.abspath(destlayerdir)
  455. recipefile = d.getVar('FILE', True)
  456. recipefn = os.path.splitext(os.path.basename(recipefile))[0]
  457. if wildcardver and '_' in recipefn:
  458. recipefn = recipefn.split('_', 1)[0] + '_%'
  459. appendfn = recipefn + '.bbappend'
  460. # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf
  461. confdata = d.createCopy()
  462. confdata.setVar('BBFILES', '')
  463. confdata.setVar('LAYERDIR', destlayerdir)
  464. destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf")
  465. confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata)
  466. origlayerdir = find_layerdir(recipefile)
  467. if not origlayerdir:
  468. return (None, False)
  469. # Now join this to the path where the bbappend is going and check if it is covered by BBFILES
  470. appendpath = os.path.join(destlayerdir, os.path.relpath(os.path.dirname(recipefile), origlayerdir), appendfn)
  471. closepath = ''
  472. pathok = True
  473. for bbfilespec in confdata.getVar('BBFILES', True).split():
  474. if fnmatch.fnmatchcase(appendpath, bbfilespec):
  475. # Our append path works, we're done
  476. break
  477. elif bbfilespec.startswith(destlayerdir) and fnmatch.fnmatchcase('test.bbappend', os.path.basename(bbfilespec)):
  478. # Try to find the longest matching path
  479. if len(bbfilespec) > len(closepath):
  480. closepath = bbfilespec
  481. else:
  482. # Unfortunately the bbappend layer and the original recipe's layer don't have the same structure
  483. if closepath:
  484. # bbappend layer's layer.conf at least has a spec that picks up .bbappend files
  485. # Now we just need to substitute out any wildcards
  486. appendsubdir = os.path.relpath(os.path.dirname(closepath), destlayerdir)
  487. if 'recipes-*' in appendsubdir:
  488. # Try to copy this part from the original recipe path
  489. res = re.search('/recipes-[^/]+/', recipefile)
  490. if res:
  491. appendsubdir = appendsubdir.replace('/recipes-*/', res.group(0))
  492. # This is crude, but we have to do something
  493. appendsubdir = appendsubdir.replace('*', recipefn.split('_')[0])
  494. appendsubdir = appendsubdir.replace('?', 'a')
  495. appendpath = os.path.join(destlayerdir, appendsubdir, appendfn)
  496. else:
  497. pathok = False
  498. return (appendpath, pathok)
  499. def bbappend_recipe(rd, destlayerdir, srcfiles, install=None, wildcardver=False, machine=None, extralines=None, removevalues=None):
  500. """
  501. Writes a bbappend file for a recipe
  502. Parameters:
  503. rd: data dictionary for the recipe
  504. destlayerdir: base directory of the layer to place the bbappend in
  505. (subdirectory path from there will be determined automatically)
  506. srcfiles: dict of source files to add to SRC_URI, where the value
  507. is the full path to the file to be added, and the value is the
  508. original filename as it would appear in SRC_URI or None if it
  509. isn't already present. You may pass None for this parameter if
  510. you simply want to specify your own content via the extralines
  511. parameter.
  512. install: dict mapping entries in srcfiles to a tuple of two elements:
  513. install path (*without* ${D} prefix) and permission value (as a
  514. string, e.g. '0644').
  515. wildcardver: True to use a % wildcard in the bbappend filename, or
  516. False to make the bbappend specific to the recipe version.
  517. machine:
  518. If specified, make the changes in the bbappend specific to this
  519. machine. This will also cause PACKAGE_ARCH = "${MACHINE_ARCH}"
  520. to be added to the bbappend.
  521. extralines:
  522. Extra lines to add to the bbappend. This may be a dict of name
  523. value pairs, or simply a list of the lines.
  524. removevalues:
  525. Variable values to remove - a dict of names/values.
  526. """
  527. if not removevalues:
  528. removevalues = {}
  529. # Determine how the bbappend should be named
  530. appendpath, pathok = get_bbappend_path(rd, destlayerdir, wildcardver)
  531. if not appendpath:
  532. bb.error('Unable to determine layer directory containing %s' % recipefile)
  533. return (None, None)
  534. if not pathok:
  535. bb.warn('Unable to determine correct subdirectory path for bbappend file - check that what %s adds to BBFILES also matches .bbappend files. Using %s for now, but until you fix this the bbappend will not be applied.' % (os.path.join(destlayerdir, 'conf', 'layer.conf'), os.path.dirname(appendpath)))
  536. appenddir = os.path.dirname(appendpath)
  537. bb.utils.mkdirhier(appenddir)
  538. # FIXME check if the bbappend doesn't get overridden by a higher priority layer?
  539. layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS', True).split()]
  540. if not os.path.abspath(destlayerdir) in layerdirs:
  541. bb.warn('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active')
  542. bbappendlines = []
  543. if extralines:
  544. if isinstance(extralines, dict):
  545. for name, value in extralines.items():
  546. bbappendlines.append((name, '=', value))
  547. else:
  548. # Do our best to split it
  549. for line in extralines:
  550. if line[-1] == '\n':
  551. line = line[:-1]
  552. splitline = line.split(None, 2)
  553. if len(splitline) == 3:
  554. bbappendlines.append(tuple(splitline))
  555. else:
  556. raise Exception('Invalid extralines value passed')
  557. def popline(varname):
  558. for i in range(0, len(bbappendlines)):
  559. if bbappendlines[i][0] == varname:
  560. line = bbappendlines.pop(i)
  561. return line
  562. return None
  563. def appendline(varname, op, value):
  564. for i in range(0, len(bbappendlines)):
  565. item = bbappendlines[i]
  566. if item[0] == varname:
  567. bbappendlines[i] = (item[0], item[1], item[2] + ' ' + value)
  568. break
  569. else:
  570. bbappendlines.append((varname, op, value))
  571. destsubdir = rd.getVar('PN', True)
  572. if srcfiles:
  573. bbappendlines.append(('FILESEXTRAPATHS_prepend', ':=', '${THISDIR}/${PN}:'))
  574. appendoverride = ''
  575. if machine:
  576. bbappendlines.append(('PACKAGE_ARCH', '=', '${MACHINE_ARCH}'))
  577. appendoverride = '_%s' % machine
  578. copyfiles = {}
  579. if srcfiles:
  580. instfunclines = []
  581. for newfile, origsrcfile in srcfiles.items():
  582. srcfile = origsrcfile
  583. srcurientry = None
  584. if not srcfile:
  585. srcfile = os.path.basename(newfile)
  586. srcurientry = 'file://%s' % srcfile
  587. # Double-check it's not there already
  588. # FIXME do we care if the entry is added by another bbappend that might go away?
  589. if not srcurientry in rd.getVar('SRC_URI', True).split():
  590. if machine:
  591. appendline('SRC_URI_append%s' % appendoverride, '=', ' ' + srcurientry)
  592. else:
  593. appendline('SRC_URI', '+=', srcurientry)
  594. copyfiles[newfile] = srcfile
  595. if install:
  596. institem = install.pop(newfile, None)
  597. if institem:
  598. (destpath, perms) = institem
  599. instdestpath = replace_dir_vars(destpath, rd)
  600. instdirline = 'install -d ${D}%s' % os.path.dirname(instdestpath)
  601. if not instdirline in instfunclines:
  602. instfunclines.append(instdirline)
  603. instfunclines.append('install -m %s ${WORKDIR}/%s ${D}%s' % (perms, os.path.basename(srcfile), instdestpath))
  604. if instfunclines:
  605. bbappendlines.append(('do_install_append%s()' % appendoverride, '', instfunclines))
  606. bb.note('Writing append file %s' % appendpath)
  607. if os.path.exists(appendpath):
  608. # Work around lack of nonlocal in python 2
  609. extvars = {'destsubdir': destsubdir}
  610. def appendfile_varfunc(varname, origvalue, op, newlines):
  611. if varname == 'FILESEXTRAPATHS_prepend':
  612. if origvalue.startswith('${THISDIR}/'):
  613. popline('FILESEXTRAPATHS_prepend')
  614. extvars['destsubdir'] = rd.expand(origvalue.split('${THISDIR}/', 1)[1].rstrip(':'))
  615. elif varname == 'PACKAGE_ARCH':
  616. if machine:
  617. popline('PACKAGE_ARCH')
  618. return (machine, None, 4, False)
  619. elif varname.startswith('do_install_append'):
  620. func = popline(varname)
  621. if func:
  622. instfunclines = [line.strip() for line in origvalue.strip('\n').splitlines()]
  623. for line in func[2]:
  624. if not line in instfunclines:
  625. instfunclines.append(line)
  626. return (instfunclines, None, 4, False)
  627. else:
  628. splitval = split_var_value(origvalue, assignment=False)
  629. changed = False
  630. removevar = varname
  631. if varname in ['SRC_URI', 'SRC_URI_append%s' % appendoverride]:
  632. removevar = 'SRC_URI'
  633. line = popline(varname)
  634. if line:
  635. if line[2] not in splitval:
  636. splitval.append(line[2])
  637. changed = True
  638. else:
  639. line = popline(varname)
  640. if line:
  641. splitval = [line[2]]
  642. changed = True
  643. if removevar in removevalues:
  644. remove = removevalues[removevar]
  645. if isinstance(remove, str):
  646. if remove in splitval:
  647. splitval.remove(remove)
  648. changed = True
  649. else:
  650. for removeitem in remove:
  651. if removeitem in splitval:
  652. splitval.remove(removeitem)
  653. changed = True
  654. if changed:
  655. newvalue = splitval
  656. if len(newvalue) == 1:
  657. # Ensure it's written out as one line
  658. if '_append' in varname:
  659. newvalue = ' ' + newvalue[0]
  660. else:
  661. newvalue = newvalue[0]
  662. if not newvalue and (op in ['+=', '.='] or '_append' in varname):
  663. # There's no point appending nothing
  664. newvalue = None
  665. if varname.endswith('()'):
  666. indent = 4
  667. else:
  668. indent = -1
  669. return (newvalue, None, indent, True)
  670. return (origvalue, None, 4, False)
  671. varnames = [item[0] for item in bbappendlines]
  672. if removevalues:
  673. varnames.extend(list(removevalues.keys()))
  674. with open(appendpath, 'r') as f:
  675. (updated, newlines) = bb.utils.edit_metadata(f, varnames, appendfile_varfunc)
  676. destsubdir = extvars['destsubdir']
  677. else:
  678. updated = False
  679. newlines = []
  680. if bbappendlines:
  681. for line in bbappendlines:
  682. if line[0].endswith('()'):
  683. newlines.append('%s {\n %s\n}\n' % (line[0], '\n '.join(line[2])))
  684. else:
  685. newlines.append('%s %s "%s"\n\n' % line)
  686. updated = True
  687. if updated:
  688. with open(appendpath, 'w') as f:
  689. f.writelines(newlines)
  690. if copyfiles:
  691. if machine:
  692. destsubdir = os.path.join(destsubdir, machine)
  693. for newfile, srcfile in copyfiles.items():
  694. filedest = os.path.join(appenddir, destsubdir, os.path.basename(srcfile))
  695. if os.path.abspath(newfile) != os.path.abspath(filedest):
  696. if newfile.startswith(tempfile.gettempdir()):
  697. newfiledisp = os.path.basename(newfile)
  698. else:
  699. newfiledisp = newfile
  700. bb.note('Copying %s to %s' % (newfiledisp, filedest))
  701. bb.utils.mkdirhier(os.path.dirname(filedest))
  702. shutil.copyfile(newfile, filedest)
  703. return (appendpath, os.path.join(appenddir, destsubdir))
  704. def find_layerdir(fn):
  705. """ Figure out the path to the base of the layer containing a file (e.g. a recipe)"""
  706. pth = fn
  707. layerdir = ''
  708. while pth:
  709. if os.path.exists(os.path.join(pth, 'conf', 'layer.conf')):
  710. layerdir = pth
  711. break
  712. pth = os.path.dirname(pth)
  713. if pth == '/':
  714. return None
  715. return layerdir
  716. def replace_dir_vars(path, d):
  717. """Replace common directory paths with appropriate variable references (e.g. /etc becomes ${sysconfdir})"""
  718. dirvars = {}
  719. # Sort by length so we get the variables we're interested in first
  720. for var in sorted(list(d.keys()), key=len):
  721. if var.endswith('dir') and var.lower() == var:
  722. value = d.getVar(var, True)
  723. if value.startswith('/') and not '\n' in value and value not in dirvars:
  724. dirvars[value] = var
  725. for dirpath in sorted(list(dirvars.keys()), reverse=True):
  726. path = path.replace(dirpath, '${%s}' % dirvars[dirpath])
  727. return path
  728. def get_recipe_pv_without_srcpv(pv, uri_type):
  729. """
  730. Get PV without SRCPV common in SCM's for now only
  731. support git.
  732. Returns tuple with pv, prefix and suffix.
  733. """
  734. pfx = ''
  735. sfx = ''
  736. if uri_type == 'git':
  737. git_regex = re.compile("(?P<pfx>v?)(?P<ver>[^\+]*)((?P<sfx>\+(git)?r?(AUTOINC\+))(?P<rev>.*))?")
  738. m = git_regex.match(pv)
  739. if m:
  740. pv = m.group('ver')
  741. pfx = m.group('pfx')
  742. sfx = m.group('sfx')
  743. else:
  744. regex = re.compile("(?P<pfx>(v|r)?)(?P<ver>.*)")
  745. m = regex.match(pv)
  746. if m:
  747. pv = m.group('ver')
  748. pfx = m.group('pfx')
  749. return (pv, pfx, sfx)
  750. def get_recipe_upstream_version(rd):
  751. """
  752. Get upstream version of recipe using bb.fetch2 methods with support for
  753. http, https, ftp and git.
  754. bb.fetch2 exceptions can be raised,
  755. FetchError when don't have network access or upstream site don't response.
  756. NoMethodError when uri latest_versionstring method isn't implemented.
  757. Returns a dictonary with version, type and datetime.
  758. Type can be A for Automatic, M for Manual and U for Unknown.
  759. """
  760. from bb.fetch2 import decodeurl
  761. from datetime import datetime
  762. ru = {}
  763. ru['version'] = ''
  764. ru['type'] = 'U'
  765. ru['datetime'] = ''
  766. pv = rd.getVar('PV', True)
  767. # XXX: If don't have SRC_URI means that don't have upstream sources so
  768. # returns the current recipe version, so that upstream version check
  769. # declares a match.
  770. src_uris = rd.getVar('SRC_URI', True)
  771. if not src_uris:
  772. ru['version'] = pv
  773. ru['type'] = 'M'
  774. ru['datetime'] = datetime.now()
  775. return ru
  776. # XXX: we suppose that the first entry points to the upstream sources
  777. src_uri = src_uris.split()[0]
  778. uri_type, _, _, _, _, _ = decodeurl(src_uri)
  779. manual_upstream_version = rd.getVar("RECIPE_UPSTREAM_VERSION", True)
  780. if manual_upstream_version:
  781. # manual tracking of upstream version.
  782. ru['version'] = manual_upstream_version
  783. ru['type'] = 'M'
  784. manual_upstream_date = rd.getVar("CHECK_DATE", True)
  785. if manual_upstream_date:
  786. date = datetime.strptime(manual_upstream_date, "%b %d, %Y")
  787. else:
  788. date = datetime.now()
  789. ru['datetime'] = date
  790. elif uri_type == "file":
  791. # files are always up-to-date
  792. ru['version'] = pv
  793. ru['type'] = 'A'
  794. ru['datetime'] = datetime.now()
  795. else:
  796. ud = bb.fetch2.FetchData(src_uri, rd)
  797. pupver = ud.method.latest_versionstring(ud, rd)
  798. (upversion, revision) = pupver
  799. # format git version version+gitAUTOINC+HASH
  800. if uri_type == 'git':
  801. (pv, pfx, sfx) = get_recipe_pv_without_srcpv(pv, uri_type)
  802. # if contains revision but not upversion use current pv
  803. if upversion == '' and revision:
  804. upversion = pv
  805. if upversion:
  806. tmp = upversion
  807. upversion = ''
  808. if pfx:
  809. upversion = pfx + tmp
  810. else:
  811. upversion = tmp
  812. if sfx:
  813. upversion = upversion + sfx + revision[:10]
  814. if upversion:
  815. ru['version'] = upversion
  816. ru['type'] = 'A'
  817. ru['datetime'] = datetime.now()
  818. return ru