upgrade.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. # Development tool - upgrade command plugin
  2. #
  3. # Copyright (C) 2014-2015 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. #
  18. """Devtool upgrade plugin"""
  19. import os
  20. import sys
  21. import re
  22. import shutil
  23. import tempfile
  24. import logging
  25. import argparse
  26. import scriptutils
  27. import errno
  28. import bb
  29. import oe.recipeutils
  30. from devtool import standard
  31. from devtool import exec_build_env_command, setup_tinfoil, DevtoolError, parse_recipe, use_external_build
  32. logger = logging.getLogger('devtool')
  33. def _run(cmd, cwd=''):
  34. logger.debug("Running command %s> %s" % (cwd,cmd))
  35. return bb.process.run('%s' % cmd, cwd=cwd)
  36. def _get_srctree(tmpdir):
  37. srctree = tmpdir
  38. dirs = os.listdir(tmpdir)
  39. if len(dirs) == 1:
  40. srctree = os.path.join(tmpdir, dirs[0])
  41. return srctree
  42. def _copy_source_code(orig, dest):
  43. for path in standard._ls_tree(orig):
  44. dest_dir = os.path.join(dest, os.path.dirname(path))
  45. bb.utils.mkdirhier(dest_dir)
  46. dest_path = os.path.join(dest, path)
  47. shutil.move(os.path.join(orig, path), dest_path)
  48. def _get_checksums(rf):
  49. import re
  50. checksums = {}
  51. with open(rf) as f:
  52. for line in f:
  53. for cs in ['md5sum', 'sha256sum']:
  54. m = re.match("^SRC_URI\[%s\].*=.*\"(.*)\"" % cs, line)
  55. if m:
  56. checksums[cs] = m.group(1)
  57. return checksums
  58. def _remove_patch_dirs(recipefolder):
  59. for root, dirs, files in os.walk(recipefolder):
  60. for d in dirs:
  61. shutil.rmtree(os.path.join(root,d))
  62. def _recipe_contains(rd, var):
  63. rf = rd.getVar('FILE', True)
  64. varfiles = oe.recipeutils.get_var_files(rf, [var], rd)
  65. for var, fn in varfiles.items():
  66. if fn and fn.startswith(os.path.dirname(rf) + os.sep):
  67. return True
  68. return False
  69. def _rename_recipe_dirs(oldpv, newpv, path):
  70. for root, dirs, files in os.walk(path):
  71. # Rename directories with the version in their name
  72. for olddir in dirs:
  73. if olddir.find(oldpv) != -1:
  74. newdir = olddir.replace(oldpv, newpv)
  75. if olddir != newdir:
  76. shutil.move(os.path.join(path, olddir), os.path.join(path, newdir))
  77. # Rename any inc files with the version in their name (unusual, but possible)
  78. for oldfile in files:
  79. if oldfile.endswith('.inc'):
  80. if oldfile.find(oldpv) != -1:
  81. newfile = oldfile.replace(oldpv, newpv)
  82. if oldfile != newfile:
  83. os.rename(os.path.join(path, oldfile), os.path.join(path, newfile))
  84. def _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path):
  85. oldrecipe = os.path.basename(oldrecipe)
  86. if oldrecipe.endswith('_%s.bb' % oldpv):
  87. newrecipe = '%s_%s.bb' % (bpn, newpv)
  88. if oldrecipe != newrecipe:
  89. shutil.move(os.path.join(path, oldrecipe), os.path.join(path, newrecipe))
  90. else:
  91. newrecipe = oldrecipe
  92. return os.path.join(path, newrecipe)
  93. def _rename_recipe_files(oldrecipe, bpn, oldpv, newpv, path):
  94. _rename_recipe_dirs(oldpv, newpv, path)
  95. return _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path)
  96. def _write_append(rc, srctree, same_dir, no_same_dir, rev, copied, workspace, d):
  97. """Writes an append file"""
  98. if not os.path.exists(rc):
  99. raise DevtoolError("bbappend not created because %s does not exist" % rc)
  100. appendpath = os.path.join(workspace, 'appends')
  101. if not os.path.exists(appendpath):
  102. bb.utils.mkdirhier(appendpath)
  103. brf = os.path.basename(os.path.splitext(rc)[0]) # rc basename
  104. srctree = os.path.abspath(srctree)
  105. pn = d.getVar('PN',True)
  106. af = os.path.join(appendpath, '%s.bbappend' % brf)
  107. with open(af, 'w') as f:
  108. f.write('FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n\n')
  109. f.write('inherit externalsrc\n')
  110. f.write(('# NOTE: We use pn- overrides here to avoid affecting'
  111. 'multiple variants in the case where the recipe uses BBCLASSEXTEND\n'))
  112. f.write('EXTERNALSRC_pn-%s = "%s"\n' % (pn, srctree))
  113. b_is_s = use_external_build(same_dir, no_same_dir, d)
  114. if b_is_s:
  115. f.write('EXTERNALSRC_BUILD_pn-%s = "%s"\n' % (pn, srctree))
  116. f.write('\n')
  117. if rev:
  118. f.write('# initial_rev: %s\n' % rev)
  119. if copied:
  120. f.write('# original_path: %s\n' % os.path.dirname(d.getVar('FILE', True)))
  121. f.write('# original_files: %s\n' % ' '.join(copied))
  122. return af
  123. def _cleanup_on_error(rf, srctree):
  124. rfp = os.path.split(rf)[0] # recipe folder
  125. rfpp = os.path.split(rfp)[0] # recipes folder
  126. if os.path.exists(rfp):
  127. shutil.rmtree(b)
  128. if not len(os.listdir(rfpp)):
  129. os.rmdir(rfpp)
  130. srctree = os.path.abspath(srctree)
  131. if os.path.exists(srctree):
  132. shutil.rmtree(srctree)
  133. def _upgrade_error(e, rf, srctree):
  134. if rf:
  135. cleanup_on_error(rf, srctree)
  136. logger.error(e)
  137. raise DevtoolError(e)
  138. def _get_uri(rd):
  139. srcuris = rd.getVar('SRC_URI', True).split()
  140. if not len(srcuris):
  141. raise DevtoolError('SRC_URI not found on recipe')
  142. # Get first non-local entry in SRC_URI - usually by convention it's
  143. # the first entry, but not always!
  144. srcuri = None
  145. for entry in srcuris:
  146. if not entry.startswith('file://'):
  147. srcuri = entry
  148. break
  149. if not srcuri:
  150. raise DevtoolError('Unable to find non-local entry in SRC_URI')
  151. srcrev = '${AUTOREV}'
  152. if '://' in srcuri:
  153. # Fetch a URL
  154. rev_re = re.compile(';rev=([^;]+)')
  155. res = rev_re.search(srcuri)
  156. if res:
  157. srcrev = res.group(1)
  158. srcuri = rev_re.sub('', srcuri)
  159. return srcuri, srcrev
  160. def _extract_new_source(newpv, srctree, no_patch, srcrev, branch, keep_temp, tinfoil, rd):
  161. """Extract sources of a recipe with a new version"""
  162. def __run(cmd):
  163. """Simple wrapper which calls _run with srctree as cwd"""
  164. return _run(cmd, srctree)
  165. crd = rd.createCopy()
  166. pv = crd.getVar('PV', True)
  167. crd.setVar('PV', newpv)
  168. tmpsrctree = None
  169. uri, rev = _get_uri(crd)
  170. if srcrev:
  171. rev = srcrev
  172. if uri.startswith('git://'):
  173. __run('git fetch')
  174. __run('git checkout %s' % rev)
  175. __run('git tag -f devtool-base-new')
  176. md5 = None
  177. sha256 = None
  178. else:
  179. __run('git checkout devtool-base -b devtool-%s' % newpv)
  180. tmpdir = tempfile.mkdtemp(prefix='devtool')
  181. try:
  182. md5, sha256 = scriptutils.fetch_uri(tinfoil.config_data, uri, tmpdir, rev)
  183. except bb.fetch2.FetchError as e:
  184. raise DevtoolError(e)
  185. tmpsrctree = _get_srctree(tmpdir)
  186. srctree = os.path.abspath(srctree)
  187. # Delete all sources so we ensure no stray files are left over
  188. for item in os.listdir(srctree):
  189. if item in ['.git', 'oe-local-files']:
  190. continue
  191. itempath = os.path.join(srctree, item)
  192. if os.path.isdir(itempath):
  193. shutil.rmtree(itempath)
  194. else:
  195. os.remove(itempath)
  196. # Copy in new ones
  197. _copy_source_code(tmpsrctree, srctree)
  198. (stdout,_) = __run('git ls-files --modified --others --exclude-standard')
  199. for f in stdout.splitlines():
  200. __run('git add "%s"' % f)
  201. __run('git commit -q -m "Commit of upstream changes at version %s" --allow-empty' % newpv)
  202. __run('git tag -f devtool-base-%s' % newpv)
  203. (stdout, _) = __run('git rev-parse HEAD')
  204. rev = stdout.rstrip()
  205. if no_patch:
  206. patches = oe.recipeutils.get_recipe_patches(crd)
  207. if len(patches):
  208. logger.warn('By user choice, the following patches will NOT be applied')
  209. for patch in patches:
  210. logger.warn("%s" % os.path.basename(patch))
  211. else:
  212. __run('git checkout devtool-patched -b %s' % branch)
  213. skiptag = False
  214. try:
  215. __run('git rebase %s' % rev)
  216. except bb.process.ExecutionError as e:
  217. skiptag = True
  218. if 'conflict' in e.stdout:
  219. logger.warn('Command \'%s\' failed:\n%s\n\nYou will need to resolve conflicts in order to complete the upgrade.' % (e.command, e.stdout.rstrip()))
  220. else:
  221. logger.warn('Command \'%s\' failed:\n%s' % (e.command, e.stdout))
  222. if not skiptag:
  223. if uri.startswith('git://'):
  224. suffix = 'new'
  225. else:
  226. suffix = newpv
  227. __run('git tag -f devtool-patched-%s' % suffix)
  228. if tmpsrctree:
  229. if keep_temp:
  230. logger.info('Preserving temporary directory %s' % tmpsrctree)
  231. else:
  232. shutil.rmtree(tmpsrctree)
  233. return (rev, md5, sha256)
  234. def _create_new_recipe(newpv, md5, sha256, srcrev, srcbranch, workspace, tinfoil, rd):
  235. """Creates the new recipe under workspace"""
  236. bpn = rd.getVar('BPN', True)
  237. path = os.path.join(workspace, 'recipes', bpn)
  238. bb.utils.mkdirhier(path)
  239. copied, _ = oe.recipeutils.copy_recipe_files(rd, path)
  240. oldpv = rd.getVar('PV', True)
  241. if not newpv:
  242. newpv = oldpv
  243. origpath = rd.getVar('FILE', True)
  244. fullpath = _rename_recipe_files(origpath, bpn, oldpv, newpv, path)
  245. logger.debug('Upgraded %s => %s' % (origpath, fullpath))
  246. newvalues = {}
  247. if _recipe_contains(rd, 'PV') and newpv != oldpv:
  248. newvalues['PV'] = newpv
  249. if srcrev:
  250. newvalues['SRCREV'] = srcrev
  251. if srcbranch:
  252. src_uri = oe.recipeutils.split_var_value(rd.getVar('SRC_URI', False) or '')
  253. changed = False
  254. replacing = True
  255. new_src_uri = []
  256. for entry in src_uri:
  257. scheme, network, path, user, passwd, params = bb.fetch2.decodeurl(entry)
  258. if replacing and scheme in ['git', 'gitsm']:
  259. branch = params.get('branch', 'master')
  260. if rd.expand(branch) != srcbranch:
  261. # Handle case where branch is set through a variable
  262. res = re.match(r'\$\{([^}@]+)\}', branch)
  263. if res:
  264. newvalues[res.group(1)] = srcbranch
  265. # We know we won't change SRC_URI now, so break out
  266. break
  267. else:
  268. params['branch'] = srcbranch
  269. entry = bb.fetch2.encodeurl((scheme, network, path, user, passwd, params))
  270. changed = True
  271. replacing = False
  272. new_src_uri.append(entry)
  273. if changed:
  274. newvalues['SRC_URI'] = ' '.join(new_src_uri)
  275. newvalues['PR'] = None
  276. if md5 and sha256:
  277. newvalues['SRC_URI[md5sum]'] = md5
  278. newvalues['SRC_URI[sha256sum]'] = sha256
  279. rd = oe.recipeutils.parse_recipe(tinfoil.cooker, fullpath, None)
  280. oe.recipeutils.patch_recipe(rd, fullpath, newvalues)
  281. return fullpath, copied
  282. def upgrade(args, config, basepath, workspace):
  283. """Entry point for the devtool 'upgrade' subcommand"""
  284. if args.recipename in workspace:
  285. raise DevtoolError("recipe %s is already in your workspace" % args.recipename)
  286. if not args.version and not args.srcrev:
  287. raise DevtoolError("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")
  288. if args.srcbranch and not args.srcrev:
  289. raise DevtoolError("If you specify --srcbranch/-B then you must use --srcrev/-S to specify the revision" % args.recipename)
  290. tinfoil = setup_tinfoil(basepath=basepath, tracking=True)
  291. rd = parse_recipe(config, tinfoil, args.recipename, True)
  292. if not rd:
  293. return 1
  294. pn = rd.getVar('PN', True)
  295. if pn != args.recipename:
  296. logger.info('Mapping %s to %s' % (args.recipename, pn))
  297. if pn in workspace:
  298. raise DevtoolError("recipe %s is already in your workspace" % pn)
  299. if args.srctree:
  300. srctree = os.path.abspath(args.srctree)
  301. else:
  302. srctree = standard.get_default_srctree(config, pn)
  303. standard._check_compatible_recipe(pn, rd)
  304. old_srcrev = rd.getVar('SRCREV', True)
  305. if old_srcrev == 'INVALID':
  306. old_srcrev = None
  307. if old_srcrev and not args.srcrev:
  308. raise DevtoolError("Recipe specifies a SRCREV value; you must specify a new one when upgrading")
  309. if rd.getVar('PV', True) == args.version and old_srcrev == args.srcrev:
  310. raise DevtoolError("Current and upgrade versions are the same version")
  311. rf = None
  312. try:
  313. rev1 = standard._extract_source(srctree, False, 'devtool-orig', False, rd)
  314. rev2, md5, sha256 = _extract_new_source(args.version, srctree, args.no_patch,
  315. args.srcrev, args.branch, args.keep_temp,
  316. tinfoil, rd)
  317. rf, copied = _create_new_recipe(args.version, md5, sha256, args.srcrev, args.srcbranch, config.workspace_path, tinfoil, rd)
  318. except bb.process.CmdError as e:
  319. _upgrade_error(e, rf, srctree)
  320. except DevtoolError as e:
  321. _upgrade_error(e, rf, srctree)
  322. standard._add_md5(config, pn, os.path.dirname(rf))
  323. af = _write_append(rf, srctree, args.same_dir, args.no_same_dir, rev2,
  324. copied, config.workspace_path, rd)
  325. standard._add_md5(config, pn, af)
  326. logger.info('Upgraded source extracted to %s' % srctree)
  327. logger.info('New recipe is %s' % rf)
  328. return 0
  329. def register_commands(subparsers, context):
  330. """Register devtool subcommands from this plugin"""
  331. defsrctree = standard.get_default_srctree(context.config)
  332. parser_upgrade = subparsers.add_parser('upgrade', help='Upgrade an existing recipe',
  333. 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).',
  334. group='starting')
  335. parser_upgrade.add_argument('recipename', help='Name of recipe to upgrade (just name - no version, path or extension)')
  336. 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)
  337. parser_upgrade.add_argument('--version', '-V', help='Version to upgrade to (PV)')
  338. parser_upgrade.add_argument('--srcrev', '-S', help='Source revision to upgrade to (required if fetching from an SCM such as git)')
  339. parser_upgrade.add_argument('--srcbranch', '-B', help='Branch in source repository containing the revision to use (if fetching from an SCM such as git)')
  340. parser_upgrade.add_argument('--branch', '-b', default="devtool", help='Name for new development branch to checkout (default "%(default)s")')
  341. parser_upgrade.add_argument('--no-patch', action="store_true", help='Do not apply patches from the recipe to the new source code')
  342. group = parser_upgrade.add_mutually_exclusive_group()
  343. group.add_argument('--same-dir', '-s', help='Build in same directory as source', action="store_true")
  344. group.add_argument('--no-same-dir', help='Force build in a separate build directory', action="store_true")
  345. parser_upgrade.add_argument('--keep-temp', action="store_true", help='Keep temporary directory (for debugging)')
  346. parser_upgrade.set_defaults(func=upgrade)