upgrade.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. # Development tool - upgrade command plugin
  2. #
  3. # Copyright (C) 2014-2017 Intel Corporation
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. """Devtool upgrade plugin"""
  8. import os
  9. import sys
  10. import re
  11. import shutil
  12. import tempfile
  13. import logging
  14. import argparse
  15. import scriptutils
  16. import errno
  17. import bb
  18. devtool_path = os.path.dirname(os.path.realpath(__file__)) + '/../../../meta/lib'
  19. sys.path = sys.path + [devtool_path]
  20. import oe.recipeutils
  21. from devtool import standard
  22. from devtool import exec_build_env_command, setup_tinfoil, DevtoolError, parse_recipe, use_external_build, update_unlockedsigs, check_prerelease_version
  23. logger = logging.getLogger('devtool')
  24. def _run(cmd, cwd=''):
  25. logger.debug("Running command %s> %s" % (cwd,cmd))
  26. return bb.process.run('%s' % cmd, cwd=cwd)
  27. def _get_srctree(tmpdir):
  28. srctree = tmpdir
  29. dirs = os.listdir(tmpdir)
  30. if len(dirs) == 1:
  31. srctree = os.path.join(tmpdir, dirs[0])
  32. else:
  33. raise DevtoolError("Cannot determine where the source tree is after unpacking in {}: {}".format(tmpdir,dirs))
  34. return srctree
  35. def _copy_source_code(orig, dest):
  36. for path in standard._ls_tree(orig):
  37. dest_dir = os.path.join(dest, os.path.dirname(path))
  38. bb.utils.mkdirhier(dest_dir)
  39. dest_path = os.path.join(dest, path)
  40. shutil.move(os.path.join(orig, path), dest_path)
  41. def _remove_patch_dirs(recipefolder):
  42. for root, dirs, files in os.walk(recipefolder):
  43. for d in dirs:
  44. shutil.rmtree(os.path.join(root,d))
  45. def _recipe_contains(rd, var):
  46. rf = rd.getVar('FILE')
  47. varfiles = oe.recipeutils.get_var_files(rf, [var], rd)
  48. for var, fn in varfiles.items():
  49. if fn and fn.startswith(os.path.dirname(rf) + os.sep):
  50. return True
  51. return False
  52. def _rename_recipe_dirs(oldpv, newpv, path):
  53. for root, dirs, files in os.walk(path):
  54. # Rename directories with the version in their name
  55. for olddir in dirs:
  56. if olddir.find(oldpv) != -1:
  57. newdir = olddir.replace(oldpv, newpv)
  58. if olddir != newdir:
  59. shutil.move(os.path.join(path, olddir), os.path.join(path, newdir))
  60. # Rename any inc files with the version in their name (unusual, but possible)
  61. for oldfile in files:
  62. if oldfile.endswith('.inc'):
  63. if oldfile.find(oldpv) != -1:
  64. newfile = oldfile.replace(oldpv, newpv)
  65. if oldfile != newfile:
  66. bb.utils.rename(os.path.join(path, oldfile),
  67. os.path.join(path, newfile))
  68. def _rename_recipe_file(oldrecipe, pn, oldpv, newpv, path):
  69. oldrecipe = os.path.basename(oldrecipe)
  70. if oldrecipe.endswith('_%s.bb' % oldpv):
  71. newrecipe = '%s_%s.bb' % (pn, newpv)
  72. if oldrecipe != newrecipe:
  73. shutil.move(os.path.join(path, oldrecipe), os.path.join(path, newrecipe))
  74. else:
  75. newrecipe = oldrecipe
  76. return os.path.join(path, newrecipe)
  77. def _rename_recipe_files(oldrecipe, pn, oldpv, newpv, path):
  78. _rename_recipe_dirs(oldpv, newpv, path)
  79. return _rename_recipe_file(oldrecipe, pn, oldpv, newpv, path)
  80. def _write_append(rc, srctreebase, srctree, same_dir, no_same_dir, revs, copied, workspace, d):
  81. """Writes an append file"""
  82. if not os.path.exists(rc):
  83. raise DevtoolError("bbappend not created because %s does not exist" % rc)
  84. appendpath = os.path.join(workspace, 'appends')
  85. if not os.path.exists(appendpath):
  86. bb.utils.mkdirhier(appendpath)
  87. brf = os.path.basename(os.path.splitext(rc)[0]) # rc basename
  88. srctree = os.path.abspath(srctree)
  89. pn = d.getVar('PN')
  90. af = os.path.join(appendpath, '%s.bbappend' % brf)
  91. with open(af, 'w') as f:
  92. f.write('FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n\n')
  93. # Local files can be modified/tracked in separate subdir under srctree
  94. # Mostly useful for packages with S != WORKDIR
  95. f.write('FILESPATH:prepend := "%s:"\n' %
  96. os.path.join(srctreebase, 'oe-local-files'))
  97. f.write('# srctreebase: %s\n' % srctreebase)
  98. f.write('inherit externalsrc\n')
  99. f.write(('# NOTE: We use pn- overrides here to avoid affecting'
  100. 'multiple variants in the case where the recipe uses BBCLASSEXTEND\n'))
  101. f.write('EXTERNALSRC:pn-%s = "%s"\n' % (pn, srctree))
  102. b_is_s = use_external_build(same_dir, no_same_dir, d)
  103. if b_is_s:
  104. f.write('EXTERNALSRC_BUILD:pn-%s = "%s"\n' % (pn, srctree))
  105. f.write('\n')
  106. if revs:
  107. for name, rev in revs.items():
  108. f.write('# initial_rev %s: %s\n' % (name, rev))
  109. if copied:
  110. f.write('# original_path: %s\n' % os.path.dirname(d.getVar('FILE')))
  111. f.write('# original_files: %s\n' % ' '.join(copied))
  112. return af
  113. def _cleanup_on_error(rd, srctree):
  114. if os.path.exists(rd):
  115. shutil.rmtree(rd)
  116. srctree = os.path.abspath(srctree)
  117. if os.path.exists(srctree):
  118. shutil.rmtree(srctree)
  119. def _upgrade_error(e, rd, srctree, keep_failure=False, extramsg=None):
  120. if not keep_failure:
  121. _cleanup_on_error(rd, srctree)
  122. logger.error(e)
  123. if extramsg:
  124. logger.error(extramsg)
  125. if keep_failure:
  126. logger.info('Preserving failed upgrade files (--keep-failure)')
  127. sys.exit(1)
  128. def _get_uri(rd):
  129. srcuris = rd.getVar('SRC_URI').split()
  130. if not len(srcuris):
  131. raise DevtoolError('SRC_URI not found on recipe')
  132. # Get first non-local entry in SRC_URI - usually by convention it's
  133. # the first entry, but not always!
  134. srcuri = None
  135. for entry in srcuris:
  136. if not entry.startswith('file://'):
  137. srcuri = entry
  138. break
  139. if not srcuri:
  140. raise DevtoolError('Unable to find non-local entry in SRC_URI')
  141. srcrev = '${AUTOREV}'
  142. if '://' in srcuri:
  143. # Fetch a URL
  144. rev_re = re.compile(';rev=([^;]+)')
  145. res = rev_re.search(srcuri)
  146. if res:
  147. srcrev = res.group(1)
  148. srcuri = rev_re.sub('', srcuri)
  149. return srcuri, srcrev
  150. def _extract_new_source(newpv, srctree, no_patch, srcrev, srcbranch, branch, keep_temp, tinfoil, rd):
  151. """Extract sources of a recipe with a new version"""
  152. def __run(cmd):
  153. """Simple wrapper which calls _run with srctree as cwd"""
  154. return _run(cmd, srctree)
  155. crd = rd.createCopy()
  156. pv = crd.getVar('PV')
  157. crd.setVar('PV', newpv)
  158. tmpsrctree = None
  159. uri, rev = _get_uri(crd)
  160. if srcrev:
  161. rev = srcrev
  162. paths = [srctree]
  163. if uri.startswith('git://') or uri.startswith('gitsm://'):
  164. __run('git fetch')
  165. __run('git checkout %s' % rev)
  166. __run('git tag -f devtool-base-new')
  167. __run('git submodule update --recursive')
  168. __run('git submodule foreach \'git tag -f devtool-base-new\'')
  169. (stdout, _) = __run('git submodule --quiet foreach \'echo $sm_path\'')
  170. paths += [os.path.join(srctree, p) for p in stdout.splitlines()]
  171. checksums = {}
  172. _, _, _, _, _, params = bb.fetch2.decodeurl(uri)
  173. srcsubdir_rel = params.get('destsuffix', 'git')
  174. if not srcbranch:
  175. check_branch, check_branch_err = __run('git branch -r --contains %s' % srcrev)
  176. get_branch = [x.strip() for x in check_branch.splitlines()]
  177. # Remove HEAD reference point and drop remote prefix
  178. get_branch = [x.split('/', 1)[1] for x in get_branch if not x.startswith('origin/HEAD')]
  179. if len(get_branch) == 1:
  180. # If srcrev is on only ONE branch, then use that branch
  181. srcbranch = get_branch[0]
  182. elif 'main' in get_branch:
  183. # If srcrev is on multiple branches, then choose 'main' if it is one of them
  184. srcbranch = 'main'
  185. elif 'master' in get_branch:
  186. # Otherwise choose 'master' if it is one of the branches
  187. srcbranch = 'master'
  188. else:
  189. # If get_branch contains more than one objects, then display error and exit.
  190. mbrch = '\n ' + '\n '.join(get_branch)
  191. raise DevtoolError('Revision %s was found on multiple branches: %s\nPlease provide the correct branch in the devtool command with "--srcbranch" or "-B" option.' % (srcrev, mbrch))
  192. else:
  193. __run('git checkout devtool-base -b devtool-%s' % newpv)
  194. tmpdir = tempfile.mkdtemp(prefix='devtool')
  195. try:
  196. checksums, ftmpdir = scriptutils.fetch_url(tinfoil, uri, rev, tmpdir, logger, preserve_tmp=keep_temp)
  197. except scriptutils.FetchUrlFailure as e:
  198. raise DevtoolError(e)
  199. if ftmpdir and keep_temp:
  200. logger.info('Fetch temp directory is %s' % ftmpdir)
  201. tmpsrctree = _get_srctree(tmpdir)
  202. srctree = os.path.abspath(srctree)
  203. srcsubdir_rel = os.path.relpath(tmpsrctree, tmpdir)
  204. # Delete all sources so we ensure no stray files are left over
  205. for item in os.listdir(srctree):
  206. if item in ['.git', 'oe-local-files']:
  207. continue
  208. itempath = os.path.join(srctree, item)
  209. if os.path.isdir(itempath):
  210. shutil.rmtree(itempath)
  211. else:
  212. os.remove(itempath)
  213. # Copy in new ones
  214. _copy_source_code(tmpsrctree, srctree)
  215. (stdout,_) = __run('git ls-files --modified --others')
  216. filelist = stdout.splitlines()
  217. pbar = bb.ui.knotty.BBProgress('Adding changed files', len(filelist))
  218. pbar.start()
  219. batchsize = 100
  220. for i in range(0, len(filelist), batchsize):
  221. batch = filelist[i:i+batchsize]
  222. __run('git add -f -A %s' % ' '.join(['"%s"' % item for item in batch]))
  223. pbar.update(i)
  224. pbar.finish()
  225. useroptions = []
  226. oe.patch.GitApplyTree.gitCommandUserOptions(useroptions, d=rd)
  227. __run('git %s commit -q -m "Commit of upstream changes at version %s" --allow-empty' % (' '.join(useroptions), newpv))
  228. __run('git tag -f devtool-base-%s' % newpv)
  229. revs = {}
  230. for path in paths:
  231. (stdout, _) = _run('git rev-parse HEAD', cwd=path)
  232. revs[os.path.relpath(path, srctree)] = stdout.rstrip()
  233. if no_patch:
  234. patches = oe.recipeutils.get_recipe_patches(crd)
  235. if patches:
  236. logger.warning('By user choice, the following patches will NOT be applied to the new source tree:\n %s' % '\n '.join([os.path.basename(patch) for patch in patches]))
  237. else:
  238. for path in paths:
  239. _run('git checkout devtool-patched -b %s' % branch, cwd=path)
  240. (stdout, _) = _run('git branch --list devtool-override-*', cwd=path)
  241. branches_to_rebase = [branch] + stdout.split()
  242. target_branch = revs[os.path.relpath(path, srctree)]
  243. # There is a bug (or feature?) in git rebase where if a commit with
  244. # a note is fully rebased away by being part of an old commit, the
  245. # note is still attached to the old commit. Avoid this by making
  246. # sure all old devtool related commits have a note attached to them
  247. # (this assumes git config notes.rewriteMode is set to ignore).
  248. (stdout, _) = __run('git rev-list devtool-base..%s' % target_branch)
  249. for rev in stdout.splitlines():
  250. if not oe.patch.GitApplyTree.getNotes(path, rev):
  251. oe.patch.GitApplyTree.addNote(path, rev, "dummy")
  252. for b in branches_to_rebase:
  253. logger.info("Rebasing {} onto {}".format(b, target_branch))
  254. _run('git checkout %s' % b, cwd=path)
  255. try:
  256. _run('git rebase %s' % target_branch, cwd=path)
  257. except bb.process.ExecutionError as e:
  258. if 'conflict' in e.stdout:
  259. logger.warning('Command \'%s\' failed:\n%s\n\nYou will need to resolve conflicts in order to complete the upgrade.' % (e.command, e.stdout.rstrip()))
  260. _run('git rebase --abort', cwd=path)
  261. else:
  262. logger.warning('Command \'%s\' failed:\n%s' % (e.command, e.stdout))
  263. # Remove any dummy notes added above.
  264. (stdout, _) = __run('git rev-list devtool-base..%s' % target_branch)
  265. for rev in stdout.splitlines():
  266. oe.patch.GitApplyTree.removeNote(path, rev, "dummy")
  267. _run('git checkout %s' % branch, cwd=path)
  268. if tmpsrctree:
  269. if keep_temp:
  270. logger.info('Preserving temporary directory %s' % tmpsrctree)
  271. else:
  272. shutil.rmtree(tmpsrctree)
  273. if tmpdir != tmpsrctree:
  274. shutil.rmtree(tmpdir)
  275. return (revs, checksums, srcbranch, srcsubdir_rel)
  276. def _add_license_diff_to_recipe(path, diff):
  277. notice_text = """# FIXME: the LIC_FILES_CHKSUM values have been updated by 'devtool upgrade'.
  278. # The following is the difference between the old and the new license text.
  279. # Please update the LICENSE value if needed, and summarize the changes in
  280. # the commit message via 'License-Update:' tag.
  281. # (example: 'License-Update: copyright years updated.')
  282. #
  283. # The changes:
  284. #
  285. """
  286. commented_diff = "\n".join(["# {}".format(l) for l in diff.split('\n')])
  287. with open(path, 'rb') as f:
  288. orig_content = f.read()
  289. with open(path, 'wb') as f:
  290. f.write(notice_text.encode())
  291. f.write(commented_diff.encode())
  292. f.write("\n#\n\n".encode())
  293. f.write(orig_content)
  294. def _create_new_recipe(newpv, checksums, srcrev, srcbranch, srcsubdir_old, srcsubdir_new, workspace, tinfoil, rd, license_diff, new_licenses, srctree, keep_failure):
  295. """Creates the new recipe under workspace"""
  296. pn = rd.getVar('PN')
  297. path = os.path.join(workspace, 'recipes', pn)
  298. bb.utils.mkdirhier(path)
  299. copied, _ = oe.recipeutils.copy_recipe_files(rd, path, all_variants=True)
  300. if not copied:
  301. raise DevtoolError('Internal error - no files were copied for recipe %s' % pn)
  302. logger.debug('Copied %s to %s' % (copied, path))
  303. oldpv = rd.getVar('PV')
  304. if not newpv:
  305. newpv = oldpv
  306. origpath = rd.getVar('FILE')
  307. fullpath = _rename_recipe_files(origpath, pn, oldpv, newpv, path)
  308. logger.debug('Upgraded %s => %s' % (origpath, fullpath))
  309. newvalues = {}
  310. if _recipe_contains(rd, 'PV') and newpv != oldpv:
  311. newvalues['PV'] = newpv
  312. if srcrev:
  313. newvalues['SRCREV'] = srcrev
  314. if srcbranch:
  315. src_uri = oe.recipeutils.split_var_value(rd.getVar('SRC_URI', False) or '')
  316. changed = False
  317. replacing = True
  318. new_src_uri = []
  319. for entry in src_uri:
  320. try:
  321. scheme, network, path, user, passwd, params = bb.fetch2.decodeurl(entry)
  322. except bb.fetch2.MalformedUrl as e:
  323. raise DevtoolError("Could not decode SRC_URI: {}".format(e))
  324. if replacing and scheme in ['git', 'gitsm']:
  325. branch = params.get('branch', 'master')
  326. if rd.expand(branch) != srcbranch:
  327. # Handle case where branch is set through a variable
  328. res = re.match(r'\$\{([^}@]+)\}', branch)
  329. if res:
  330. newvalues[res.group(1)] = srcbranch
  331. # We know we won't change SRC_URI now, so break out
  332. break
  333. else:
  334. params['branch'] = srcbranch
  335. entry = bb.fetch2.encodeurl((scheme, network, path, user, passwd, params))
  336. changed = True
  337. replacing = False
  338. new_src_uri.append(entry)
  339. if changed:
  340. newvalues['SRC_URI'] = ' '.join(new_src_uri)
  341. newvalues['PR'] = None
  342. # Work out which SRC_URI entries have changed in case the entry uses a name
  343. crd = rd.createCopy()
  344. crd.setVar('PV', newpv)
  345. for var, value in newvalues.items():
  346. crd.setVar(var, value)
  347. old_src_uri = (rd.getVar('SRC_URI') or '').split()
  348. new_src_uri = (crd.getVar('SRC_URI') or '').split()
  349. newnames = []
  350. addnames = []
  351. for newentry in new_src_uri:
  352. _, _, _, _, _, params = bb.fetch2.decodeurl(newentry)
  353. if 'name' in params:
  354. newnames.append(params['name'])
  355. if newentry not in old_src_uri:
  356. addnames.append(params['name'])
  357. # Find what's been set in the original recipe
  358. oldnames = []
  359. oldsums = []
  360. noname = False
  361. for varflag in rd.getVarFlags('SRC_URI'):
  362. for checksum in checksums:
  363. if varflag.endswith('.' + checksum):
  364. name = varflag.rsplit('.', 1)[0]
  365. if name not in oldnames:
  366. oldnames.append(name)
  367. oldsums.append(checksum)
  368. elif varflag == checksum:
  369. noname = True
  370. oldsums.append(checksum)
  371. # Even if SRC_URI has named entries it doesn't have to actually use the name
  372. if noname and addnames and addnames[0] not in oldnames:
  373. addnames = []
  374. # Drop any old names (the name actually might include ${PV})
  375. for name in oldnames:
  376. if name not in newnames:
  377. for checksum in oldsums:
  378. newvalues['SRC_URI[%s.%s]' % (name, checksum)] = None
  379. nameprefix = '%s.' % addnames[0] if addnames else ''
  380. # md5sum is deprecated, remove any traces of it. If it was the only old
  381. # checksum, then replace it with the default checksums.
  382. if 'md5sum' in oldsums:
  383. newvalues['SRC_URI[%smd5sum]' % nameprefix] = None
  384. oldsums.remove('md5sum')
  385. if not oldsums:
  386. oldsums = ["%ssum" % s for s in bb.fetch2.SHOWN_CHECKSUM_LIST]
  387. for checksum in oldsums:
  388. newvalues['SRC_URI[%s%s]' % (nameprefix, checksum)] = checksums[checksum]
  389. if srcsubdir_new != srcsubdir_old:
  390. s_subdir_old = os.path.relpath(os.path.abspath(rd.getVar('S')), rd.getVar('WORKDIR'))
  391. s_subdir_new = os.path.relpath(os.path.abspath(crd.getVar('S')), crd.getVar('WORKDIR'))
  392. if srcsubdir_old == s_subdir_old and srcsubdir_new != s_subdir_new:
  393. # Subdir for old extracted source matches what S points to (it should!)
  394. # but subdir for new extracted source doesn't match what S will be
  395. newvalues['S'] = '${WORKDIR}/%s' % srcsubdir_new.replace(newpv, '${PV}')
  396. if crd.expand(newvalues['S']) == crd.expand('${WORKDIR}/${BP}'):
  397. # It's the default, drop it
  398. # FIXME what if S is being set in a .inc?
  399. newvalues['S'] = None
  400. logger.info('Source subdirectory has changed, dropping S value since it now matches the default ("${WORKDIR}/${BP}")')
  401. else:
  402. logger.info('Source subdirectory has changed, updating S value')
  403. if license_diff:
  404. newlicchksum = " ".join(["file://{}".format(l['path']) +
  405. (";beginline={}".format(l['beginline']) if l['beginline'] else "") +
  406. (";endline={}".format(l['endline']) if l['endline'] else "") +
  407. (";md5={}".format(l['actual_md5'])) for l in new_licenses])
  408. newvalues["LIC_FILES_CHKSUM"] = newlicchksum
  409. _add_license_diff_to_recipe(fullpath, license_diff)
  410. tinfoil.modified_files()
  411. try:
  412. rd = tinfoil.parse_recipe_file(fullpath, False)
  413. except bb.tinfoil.TinfoilCommandFailed as e:
  414. _upgrade_error(e, os.path.dirname(fullpath), srctree, keep_failure, 'Parsing of upgraded recipe failed')
  415. oe.recipeutils.patch_recipe(rd, fullpath, newvalues)
  416. return fullpath, copied
  417. def _check_git_config():
  418. def getconfig(name):
  419. try:
  420. value = bb.process.run('git config %s' % name)[0].strip()
  421. except bb.process.ExecutionError as e:
  422. if e.exitcode == 1:
  423. value = None
  424. else:
  425. raise
  426. return value
  427. username = getconfig('user.name')
  428. useremail = getconfig('user.email')
  429. configerr = []
  430. if not username:
  431. configerr.append('Please set your name using:\n git config --global user.name')
  432. if not useremail:
  433. configerr.append('Please set your email using:\n git config --global user.email')
  434. if configerr:
  435. raise DevtoolError('Your git configuration is incomplete which will prevent rebases from working:\n' + '\n'.join(configerr))
  436. def _extract_licenses(srcpath, recipe_licenses):
  437. licenses = []
  438. for url in recipe_licenses.split():
  439. license = {}
  440. (type, host, path, user, pswd, parm) = bb.fetch.decodeurl(url)
  441. license['path'] = path
  442. license['md5'] = parm.get('md5', '')
  443. license['beginline'], license['endline'] = 0, 0
  444. if 'beginline' in parm:
  445. license['beginline'] = int(parm['beginline'])
  446. if 'endline' in parm:
  447. license['endline'] = int(parm['endline'])
  448. license['text'] = []
  449. with open(os.path.join(srcpath, path), 'rb') as f:
  450. import hashlib
  451. actual_md5 = hashlib.md5()
  452. lineno = 0
  453. for line in f:
  454. lineno += 1
  455. if (lineno >= license['beginline']) and ((lineno <= license['endline']) or not license['endline']):
  456. license['text'].append(line.decode(errors='ignore'))
  457. actual_md5.update(line)
  458. license['actual_md5'] = actual_md5.hexdigest()
  459. licenses.append(license)
  460. return licenses
  461. def _generate_license_diff(old_licenses, new_licenses):
  462. need_diff = False
  463. for l in new_licenses:
  464. if l['md5'] != l['actual_md5']:
  465. need_diff = True
  466. break
  467. if need_diff == False:
  468. return None
  469. import difflib
  470. diff = ''
  471. for old, new in zip(old_licenses, new_licenses):
  472. for line in difflib.unified_diff(old['text'], new['text'], old['path'], new['path']):
  473. diff = diff + line
  474. return diff
  475. def _run_recipe_upgrade_extra_tasks(pn, rd, tinfoil):
  476. tasks = []
  477. for task in (rd.getVar('RECIPE_UPGRADE_EXTRA_TASKS') or '').split():
  478. logger.info('Running extra recipe upgrade task: %s' % task)
  479. res = tinfoil.build_targets(pn, task, handle_events=True)
  480. if not res:
  481. raise DevtoolError('Running extra recipe upgrade task %s for %s failed' % (task, pn))
  482. def upgrade(args, config, basepath, workspace):
  483. """Entry point for the devtool 'upgrade' subcommand"""
  484. if args.recipename in workspace:
  485. raise DevtoolError("recipe %s is already in your workspace" % args.recipename)
  486. if args.srcbranch and not args.srcrev:
  487. raise DevtoolError("If you specify --srcbranch/-B then you must use --srcrev/-S to specify the revision" % args.recipename)
  488. _check_git_config()
  489. tinfoil = setup_tinfoil(basepath=basepath, tracking=True)
  490. try:
  491. rd = parse_recipe(config, tinfoil, args.recipename, True)
  492. if not rd:
  493. return 1
  494. pn = rd.getVar('PN')
  495. if pn != args.recipename:
  496. logger.info('Mapping %s to %s' % (args.recipename, pn))
  497. if pn in workspace:
  498. raise DevtoolError("recipe %s is already in your workspace" % pn)
  499. if args.srctree:
  500. srctree = os.path.abspath(args.srctree)
  501. else:
  502. srctree = standard.get_default_srctree(config, pn)
  503. srctree_s = standard.get_real_srctree(srctree, rd.getVar('S'), rd.getVar('WORKDIR'))
  504. # try to automatically discover latest version and revision if not provided on command line
  505. if not args.version and not args.srcrev:
  506. version_info = oe.recipeutils.get_recipe_upstream_version(rd)
  507. if version_info['version'] and not version_info['version'].endswith("new-commits-available"):
  508. args.version = version_info['version']
  509. if version_info['revision']:
  510. args.srcrev = version_info['revision']
  511. if not args.version and not args.srcrev:
  512. raise DevtoolError("Automatic discovery of latest version/revision failed - you must provide a version using the --version/-V option, or for recipes that fetch from an SCM such as git, the --srcrev/-S option.")
  513. standard._check_compatible_recipe(pn, rd)
  514. old_srcrev = rd.getVar('SRCREV')
  515. if old_srcrev == 'INVALID':
  516. old_srcrev = None
  517. if old_srcrev and not args.srcrev:
  518. raise DevtoolError("Recipe specifies a SRCREV value; you must specify a new one when upgrading")
  519. old_ver = rd.getVar('PV')
  520. if old_ver == args.version and old_srcrev == args.srcrev:
  521. raise DevtoolError("Current and upgrade versions are the same version")
  522. if args.version:
  523. if bb.utils.vercmp_string(args.version, old_ver) < 0:
  524. logger.warning('Upgrade version %s compares as less than the current version %s. If you are using a package feed for on-target upgrades or providing this recipe for general consumption, then you should increment PE in the recipe (or if there is no current PE value set, set it to "1")' % (args.version, old_ver))
  525. check_prerelease_version(args.version, 'devtool upgrade')
  526. rf = None
  527. license_diff = None
  528. try:
  529. logger.info('Extracting current version source...')
  530. rev1, srcsubdir1 = standard._extract_source(srctree, False, 'devtool-orig', False, config, basepath, workspace, args.fixed_setup, rd, tinfoil, no_overrides=args.no_overrides)
  531. old_licenses = _extract_licenses(srctree_s, (rd.getVar('LIC_FILES_CHKSUM') or ""))
  532. logger.info('Extracting upgraded version source...')
  533. rev2, checksums, srcbranch, srcsubdir2 = _extract_new_source(args.version, srctree, args.no_patch,
  534. args.srcrev, args.srcbranch, args.branch, args.keep_temp,
  535. tinfoil, rd)
  536. new_licenses = _extract_licenses(srctree_s, (rd.getVar('LIC_FILES_CHKSUM') or ""))
  537. license_diff = _generate_license_diff(old_licenses, new_licenses)
  538. rf, copied = _create_new_recipe(args.version, checksums, args.srcrev, srcbranch, srcsubdir1, srcsubdir2, config.workspace_path, tinfoil, rd, license_diff, new_licenses, srctree, args.keep_failure)
  539. except (bb.process.CmdError, DevtoolError) as e:
  540. recipedir = os.path.join(config.workspace_path, 'recipes', rd.getVar('PN'))
  541. _upgrade_error(e, recipedir, srctree, args.keep_failure)
  542. standard._add_md5(config, pn, os.path.dirname(rf))
  543. af = _write_append(rf, srctree, srctree_s, args.same_dir, args.no_same_dir, rev2,
  544. copied, config.workspace_path, rd)
  545. standard._add_md5(config, pn, af)
  546. _run_recipe_upgrade_extra_tasks(pn, rd, tinfoil)
  547. update_unlockedsigs(basepath, workspace, args.fixed_setup, [pn])
  548. logger.info('Upgraded source extracted to %s' % srctree)
  549. logger.info('New recipe is %s' % rf)
  550. if license_diff:
  551. logger.info('License checksums have been updated in the new recipe; please refer to it for the difference between the old and the new license texts.')
  552. preferred_version = rd.getVar('PREFERRED_VERSION_%s' % rd.getVar('PN'))
  553. if preferred_version:
  554. logger.warning('Version is pinned to %s via PREFERRED_VERSION; it may need adjustment to match the new version before any further steps are taken' % preferred_version)
  555. finally:
  556. tinfoil.shutdown()
  557. return 0
  558. def latest_version(args, config, basepath, workspace):
  559. """Entry point for the devtool 'latest_version' subcommand"""
  560. tinfoil = setup_tinfoil(basepath=basepath, tracking=True)
  561. try:
  562. rd = parse_recipe(config, tinfoil, args.recipename, True)
  563. if not rd:
  564. return 1
  565. version_info = oe.recipeutils.get_recipe_upstream_version(rd)
  566. # "new-commits-available" is an indication that upstream never issues version tags
  567. if not version_info['version'].endswith("new-commits-available"):
  568. logger.info("Current version: {}".format(version_info['current_version']))
  569. logger.info("Latest version: {}".format(version_info['version']))
  570. if version_info['revision']:
  571. logger.info("Latest version's commit: {}".format(version_info['revision']))
  572. else:
  573. logger.info("Latest commit: {}".format(version_info['revision']))
  574. finally:
  575. tinfoil.shutdown()
  576. return 0
  577. def check_upgrade_status(args, config, basepath, workspace):
  578. def _print_status(recipe):
  579. print("{:25} {:15} {:15} {} {} {}".format( recipe['pn'],
  580. recipe['cur_ver'],
  581. recipe['status'] if recipe['status'] != 'UPDATE' else (recipe['next_ver'] if not recipe['next_ver'].endswith("new-commits-available") else "new commits"),
  582. recipe['maintainer'],
  583. recipe['revision'] if recipe['revision'] != 'N/A' else "",
  584. "cannot be updated due to: %s" %(recipe['no_upgrade_reason']) if recipe['no_upgrade_reason'] else ""))
  585. if not args.recipe:
  586. logger.info("Checking the upstream status for all recipes may take a few minutes")
  587. results = oe.recipeutils.get_recipe_upgrade_status(args.recipe)
  588. for recipegroup in results:
  589. upgrades = [r for r in recipegroup if r['status'] != 'MATCH']
  590. currents = [r for r in recipegroup if r['status'] == 'MATCH']
  591. if len(upgrades) > 1:
  592. print("These recipes need to be upgraded together {")
  593. for r in upgrades:
  594. _print_status(r)
  595. if len(upgrades) > 1:
  596. print("}")
  597. for r in currents:
  598. if args.all:
  599. _print_status(r)
  600. def register_commands(subparsers, context):
  601. """Register devtool subcommands from this plugin"""
  602. defsrctree = standard.get_default_srctree(context.config)
  603. parser_upgrade = subparsers.add_parser('upgrade', help='Upgrade an existing recipe',
  604. description='Upgrades an existing recipe to a new upstream version. Puts the upgraded recipe file into the workspace along with any associated files, and extracts the source tree to a specified location (in case patches need rebasing or adding to as a result of the upgrade).',
  605. group='starting')
  606. parser_upgrade.add_argument('recipename', help='Name of recipe to upgrade (just name - no version, path or extension)')
  607. parser_upgrade.add_argument('srctree', nargs='?', help='Path to where to extract the source tree. If not specified, a subdirectory of %s will be used.' % defsrctree)
  608. parser_upgrade.add_argument('--version', '-V', help='Version to upgrade to (PV). If omitted, latest upstream version will be determined and used, if possible.')
  609. parser_upgrade.add_argument('--srcrev', '-S', help='Source revision to upgrade to (useful when fetching from an SCM such as git)')
  610. parser_upgrade.add_argument('--srcbranch', '-B', help='Branch in source repository containing the revision to use (if fetching from an SCM such as git)')
  611. parser_upgrade.add_argument('--branch', '-b', default="devtool", help='Name for new development branch to checkout (default "%(default)s")')
  612. parser_upgrade.add_argument('--no-patch', action="store_true", help='Do not apply patches from the recipe to the new source code')
  613. parser_upgrade.add_argument('--no-overrides', '-O', action="store_true", help='Do not create branches for other override configurations')
  614. group = parser_upgrade.add_mutually_exclusive_group()
  615. group.add_argument('--same-dir', '-s', help='Build in same directory as source', action="store_true")
  616. group.add_argument('--no-same-dir', help='Force build in a separate build directory', action="store_true")
  617. parser_upgrade.add_argument('--keep-temp', action="store_true", help='Keep temporary directory (for debugging)')
  618. parser_upgrade.add_argument('--keep-failure', action="store_true", help='Keep failed upgrade recipe and associated files (for debugging)')
  619. parser_upgrade.set_defaults(func=upgrade, fixed_setup=context.fixed_setup)
  620. parser_latest_version = subparsers.add_parser('latest-version', help='Report the latest version of an existing recipe',
  621. description='Queries the upstream server for what the latest upstream release is (for git, tags are checked, for tarballs, a list of them is obtained, and one with the highest version number is reported)',
  622. group='info')
  623. parser_latest_version.add_argument('recipename', help='Name of recipe to query (just name - no version, path or extension)')
  624. parser_latest_version.set_defaults(func=latest_version)
  625. parser_check_upgrade_status = subparsers.add_parser('check-upgrade-status', help="Report upgradability for multiple (or all) recipes",
  626. description="Prints a table of recipes together with versions currently provided by recipes, and latest upstream versions, when there is a later version available",
  627. group='info')
  628. parser_check_upgrade_status.add_argument('recipe', help='Name of the recipe to report (omit to report upgrade info for all recipes)', nargs='*')
  629. parser_check_upgrade_status.add_argument('--all', '-a', help='Show all recipes, not just recipes needing upgrade', action="store_true")
  630. parser_check_upgrade_status.set_defaults(func=check_upgrade_status)