blob: f1b3ff0a99751febb59dd51f60b1e37e6d5e9df6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Development tool - upgrade command plugin
2#
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003# Copyright (C) 2014-2017 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004#
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
20import os
21import sys
22import re
23import shutil
24import tempfile
25import logging
26import argparse
27import scriptutils
28import errno
29import bb
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030
31devtool_path = os.path.dirname(os.path.realpath(__file__)) + '/../../../meta/lib'
32sys.path = sys.path + [devtool_path]
33
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034import oe.recipeutils
35from devtool import standard
Brad Bishopd7bf8c12018-02-25 22:55:05 -050036from devtool import exec_build_env_command, setup_tinfoil, DevtoolError, parse_recipe, use_external_build, update_unlockedsigs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38logger = logging.getLogger('devtool')
39
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040def _run(cmd, cwd=''):
41 logger.debug("Running command %s> %s" % (cwd,cmd))
42 return bb.process.run('%s' % cmd, cwd=cwd)
43
44def _get_srctree(tmpdir):
45 srctree = tmpdir
46 dirs = os.listdir(tmpdir)
47 if len(dirs) == 1:
48 srctree = os.path.join(tmpdir, dirs[0])
49 return srctree
50
51def _copy_source_code(orig, dest):
52 for path in standard._ls_tree(orig):
53 dest_dir = os.path.join(dest, os.path.dirname(path))
54 bb.utils.mkdirhier(dest_dir)
55 dest_path = os.path.join(dest, path)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050056 shutil.move(os.path.join(orig, path), dest_path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057
58def _get_checksums(rf):
59 import re
60 checksums = {}
61 with open(rf) as f:
62 for line in f:
63 for cs in ['md5sum', 'sha256sum']:
64 m = re.match("^SRC_URI\[%s\].*=.*\"(.*)\"" % cs, line)
65 if m:
66 checksums[cs] = m.group(1)
67 return checksums
68
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069def _remove_patch_dirs(recipefolder):
70 for root, dirs, files in os.walk(recipefolder):
71 for d in dirs:
72 shutil.rmtree(os.path.join(root,d))
73
Patrick Williamsf1e5d692016-03-30 15:21:19 -050074def _recipe_contains(rd, var):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 rf = rd.getVar('FILE')
Patrick Williamsf1e5d692016-03-30 15:21:19 -050076 varfiles = oe.recipeutils.get_var_files(rf, [var], rd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060077 for var, fn in varfiles.items():
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078 if fn and fn.startswith(os.path.dirname(rf) + os.sep):
79 return True
80 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
82def _rename_recipe_dirs(oldpv, newpv, path):
83 for root, dirs, files in os.walk(path):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084 # Rename directories with the version in their name
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 for olddir in dirs:
86 if olddir.find(oldpv) != -1:
87 newdir = olddir.replace(oldpv, newpv)
88 if olddir != newdir:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050089 shutil.move(os.path.join(path, olddir), os.path.join(path, newdir))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060090 # Rename any inc files with the version in their name (unusual, but possible)
91 for oldfile in files:
92 if oldfile.endswith('.inc'):
93 if oldfile.find(oldpv) != -1:
94 newfile = oldfile.replace(oldpv, newpv)
95 if oldfile != newfile:
96 os.rename(os.path.join(path, oldfile), os.path.join(path, newfile))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098def _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path):
99 oldrecipe = os.path.basename(oldrecipe)
100 if oldrecipe.endswith('_%s.bb' % oldpv):
101 newrecipe = '%s_%s.bb' % (bpn, newpv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 if oldrecipe != newrecipe:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 shutil.move(os.path.join(path, oldrecipe), os.path.join(path, newrecipe))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 newrecipe = oldrecipe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 return os.path.join(path, newrecipe)
107
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500108def _rename_recipe_files(oldrecipe, bpn, oldpv, newpv, path):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 _rename_recipe_dirs(oldpv, newpv, path)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110 return _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600112def _write_append(rc, srctree, same_dir, no_same_dir, rev, copied, workspace, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 """Writes an append file"""
114 if not os.path.exists(rc):
115 raise DevtoolError("bbappend not created because %s does not exist" % rc)
116
117 appendpath = os.path.join(workspace, 'appends')
118 if not os.path.exists(appendpath):
119 bb.utils.mkdirhier(appendpath)
120
121 brf = os.path.basename(os.path.splitext(rc)[0]) # rc basename
122
123 srctree = os.path.abspath(srctree)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 pn = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 af = os.path.join(appendpath, '%s.bbappend' % brf)
126 with open(af, 'w') as f:
127 f.write('FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n\n')
128 f.write('inherit externalsrc\n')
129 f.write(('# NOTE: We use pn- overrides here to avoid affecting'
130 'multiple variants in the case where the recipe uses BBCLASSEXTEND\n'))
131 f.write('EXTERNALSRC_pn-%s = "%s"\n' % (pn, srctree))
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500132 b_is_s = use_external_build(same_dir, no_same_dir, d)
133 if b_is_s:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 f.write('EXTERNALSRC_BUILD_pn-%s = "%s"\n' % (pn, srctree))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600135 f.write('\n')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 if rev:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600137 f.write('# initial_rev: %s\n' % rev)
138 if copied:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 f.write('# original_path: %s\n' % os.path.dirname(d.getVar('FILE')))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600140 f.write('# original_files: %s\n' % ' '.join(copied))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 return af
142
143def _cleanup_on_error(rf, srctree):
144 rfp = os.path.split(rf)[0] # recipe folder
145 rfpp = os.path.split(rfp)[0] # recipes folder
146 if os.path.exists(rfp):
147 shutil.rmtree(b)
148 if not len(os.listdir(rfpp)):
149 os.rmdir(rfpp)
150 srctree = os.path.abspath(srctree)
151 if os.path.exists(srctree):
152 shutil.rmtree(srctree)
153
154def _upgrade_error(e, rf, srctree):
155 if rf:
156 cleanup_on_error(rf, srctree)
157 logger.error(e)
158 raise DevtoolError(e)
159
160def _get_uri(rd):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 srcuris = rd.getVar('SRC_URI').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 if not len(srcuris):
163 raise DevtoolError('SRC_URI not found on recipe')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164 # Get first non-local entry in SRC_URI - usually by convention it's
165 # the first entry, but not always!
166 srcuri = None
167 for entry in srcuris:
168 if not entry.startswith('file://'):
169 srcuri = entry
170 break
171 if not srcuri:
172 raise DevtoolError('Unable to find non-local entry in SRC_URI')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 srcrev = '${AUTOREV}'
174 if '://' in srcuri:
175 # Fetch a URL
176 rev_re = re.compile(';rev=([^;]+)')
177 res = rev_re.search(srcuri)
178 if res:
179 srcrev = res.group(1)
180 srcuri = rev_re.sub('', srcuri)
181 return srcuri, srcrev
182
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500183def _extract_new_source(newpv, srctree, no_patch, srcrev, srcbranch, branch, keep_temp, tinfoil, rd):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 """Extract sources of a recipe with a new version"""
185
186 def __run(cmd):
187 """Simple wrapper which calls _run with srctree as cwd"""
188 return _run(cmd, srctree)
189
190 crd = rd.createCopy()
191
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 pv = crd.getVar('PV')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 crd.setVar('PV', newpv)
194
195 tmpsrctree = None
196 uri, rev = _get_uri(crd)
197 if srcrev:
198 rev = srcrev
199 if uri.startswith('git://'):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500200 __run('git fetch')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 __run('git checkout %s' % rev)
202 __run('git tag -f devtool-base-new')
203 md5 = None
204 sha256 = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500205 if not srcbranch:
206 check_branch, check_branch_err = __run('git branch -r --contains %s' % srcrev)
207 get_branch = [x.strip() for x in check_branch.splitlines()]
208 # Remove HEAD reference point and drop remote prefix
209 get_branch = [x.split('/', 1)[1] for x in get_branch if not x.startswith('origin/HEAD')]
210 if 'master' in get_branch:
211 # If it is master, we do not need to append 'branch=master' as this is default.
212 # Even with the case where get_branch has multiple objects, if 'master' is one
213 # of them, we should default take from 'master'
214 srcbranch = ''
215 elif len(get_branch) == 1:
216 # If 'master' isn't in get_branch and get_branch contains only ONE object, then store result into 'srcbranch'
217 srcbranch = get_branch[0]
218 else:
219 # If get_branch contains more than one objects, then display error and exit.
220 mbrch = '\n ' + '\n '.join(get_branch)
221 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))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500223 __run('git checkout devtool-base -b devtool-%s' % newpv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224
225 tmpdir = tempfile.mkdtemp(prefix='devtool')
226 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500227 checksums, ftmpdir = scriptutils.fetch_url(tinfoil, uri, rev, tmpdir, logger, preserve_tmp=keep_temp)
228 except scriptutils.FetchUrlFailure as e:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 raise DevtoolError(e)
230
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500231 if ftmpdir and keep_temp:
232 logger.info('Fetch temp directory is %s' % ftmpdir)
233
234 md5 = checksums['md5sum']
235 sha256 = checksums['sha256sum']
236
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 tmpsrctree = _get_srctree(tmpdir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500238 srctree = os.path.abspath(srctree)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 # Delete all sources so we ensure no stray files are left over
241 for item in os.listdir(srctree):
242 if item in ['.git', 'oe-local-files']:
243 continue
244 itempath = os.path.join(srctree, item)
245 if os.path.isdir(itempath):
246 shutil.rmtree(itempath)
247 else:
248 os.remove(itempath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500250 # Copy in new ones
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251 _copy_source_code(tmpsrctree, srctree)
252
253 (stdout,_) = __run('git ls-files --modified --others --exclude-standard')
254 for f in stdout.splitlines():
255 __run('git add "%s"' % f)
256
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 useroptions = []
258 oe.patch.GitApplyTree.gitCommandUserOptions(useroptions, d=rd)
259 __run('git %s commit -q -m "Commit of upstream changes at version %s" --allow-empty' % (' '.join(useroptions), newpv))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 __run('git tag -f devtool-base-%s' % newpv)
261
262 (stdout, _) = __run('git rev-parse HEAD')
263 rev = stdout.rstrip()
264
265 if no_patch:
266 patches = oe.recipeutils.get_recipe_patches(crd)
267 if len(patches):
268 logger.warn('By user choice, the following patches will NOT be applied')
269 for patch in patches:
270 logger.warn("%s" % os.path.basename(patch))
271 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600272 __run('git checkout devtool-patched -b %s' % branch)
273 skiptag = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274 try:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275 __run('git rebase %s' % rev)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600276 except bb.process.ExecutionError as e:
277 skiptag = True
278 if 'conflict' in e.stdout:
279 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()))
280 else:
281 logger.warn('Command \'%s\' failed:\n%s' % (e.command, e.stdout))
282 if not skiptag:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500283 if uri.startswith('git://'):
284 suffix = 'new'
285 else:
286 suffix = newpv
287 __run('git tag -f devtool-patched-%s' % suffix)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288
289 if tmpsrctree:
290 if keep_temp:
291 logger.info('Preserving temporary directory %s' % tmpsrctree)
292 else:
293 shutil.rmtree(tmpsrctree)
294
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 return (rev, md5, sha256, srcbranch)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500296
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500297def _create_new_recipe(newpv, md5, sha256, srcrev, srcbranch, workspace, tinfoil, rd):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298 """Creates the new recipe under workspace"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500300 bpn = rd.getVar('BPN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500301 path = os.path.join(workspace, 'recipes', bpn)
302 bb.utils.mkdirhier(path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500303 copied, _ = oe.recipeutils.copy_recipe_files(rd, path, all_variants=True)
304 if not copied:
305 raise DevtoolError('Internal error - no files were copied for recipe %s' % bpn)
306 logger.debug('Copied %s to %s' % (copied, path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500308 oldpv = rd.getVar('PV')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309 if not newpv:
310 newpv = oldpv
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500311 origpath = rd.getVar('FILE')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500312 fullpath = _rename_recipe_files(origpath, bpn, oldpv, newpv, path)
313 logger.debug('Upgraded %s => %s' % (origpath, fullpath))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500315 newvalues = {}
316 if _recipe_contains(rd, 'PV') and newpv != oldpv:
317 newvalues['PV'] = newpv
318
319 if srcrev:
320 newvalues['SRCREV'] = srcrev
321
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500322 if srcbranch:
323 src_uri = oe.recipeutils.split_var_value(rd.getVar('SRC_URI', False) or '')
324 changed = False
325 replacing = True
326 new_src_uri = []
327 for entry in src_uri:
328 scheme, network, path, user, passwd, params = bb.fetch2.decodeurl(entry)
329 if replacing and scheme in ['git', 'gitsm']:
330 branch = params.get('branch', 'master')
331 if rd.expand(branch) != srcbranch:
332 # Handle case where branch is set through a variable
333 res = re.match(r'\$\{([^}@]+)\}', branch)
334 if res:
335 newvalues[res.group(1)] = srcbranch
336 # We know we won't change SRC_URI now, so break out
337 break
338 else:
339 params['branch'] = srcbranch
340 entry = bb.fetch2.encodeurl((scheme, network, path, user, passwd, params))
341 changed = True
342 replacing = False
343 new_src_uri.append(entry)
344 if changed:
345 newvalues['SRC_URI'] = ' '.join(new_src_uri)
346
347 newvalues['PR'] = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500348
349 if md5 and sha256:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500350 newvalues['SRC_URI[md5sum]'] = md5
351 newvalues['SRC_URI[sha256sum]'] = sha256
352
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500353 rd = tinfoil.parse_recipe_file(fullpath, False)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500354 oe.recipeutils.patch_recipe(rd, fullpath, newvalues)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600356 return fullpath, copied
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500357
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500358
359def _check_git_config():
360 def getconfig(name):
361 try:
362 value = bb.process.run('git config --global %s' % name)[0].strip()
363 except bb.process.ExecutionError as e:
364 if e.exitcode == 1:
365 value = None
366 else:
367 raise
368 return value
369
370 username = getconfig('user.name')
371 useremail = getconfig('user.email')
372 configerr = []
373 if not username:
374 configerr.append('Please set your name using:\n git config --global user.name')
375 if not useremail:
376 configerr.append('Please set your email using:\n git config --global user.email')
377 if configerr:
378 raise DevtoolError('Your git configuration is incomplete which will prevent rebases from working:\n' + '\n'.join(configerr))
379
380
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500381def upgrade(args, config, basepath, workspace):
382 """Entry point for the devtool 'upgrade' subcommand"""
383
384 if args.recipename in workspace:
385 raise DevtoolError("recipe %s is already in your workspace" % args.recipename)
386 if not args.version and not args.srcrev:
387 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")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500388 if args.srcbranch and not args.srcrev:
389 raise DevtoolError("If you specify --srcbranch/-B then you must use --srcrev/-S to specify the revision" % args.recipename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500390
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500391 _check_git_config()
392
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500393 tinfoil = setup_tinfoil(basepath=basepath, tracking=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500394 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600395 rd = parse_recipe(config, tinfoil, args.recipename, True)
396 if not rd:
397 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500398
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500399 pn = rd.getVar('PN')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600400 if pn != args.recipename:
401 logger.info('Mapping %s to %s' % (args.recipename, pn))
402 if pn in workspace:
403 raise DevtoolError("recipe %s is already in your workspace" % pn)
404
405 if args.srctree:
406 srctree = os.path.abspath(args.srctree)
407 else:
408 srctree = standard.get_default_srctree(config, pn)
409
410 standard._check_compatible_recipe(pn, rd)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500411 old_srcrev = rd.getVar('SRCREV')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600412 if old_srcrev == 'INVALID':
413 old_srcrev = None
414 if old_srcrev and not args.srcrev:
415 raise DevtoolError("Recipe specifies a SRCREV value; you must specify a new one when upgrading")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500416 if rd.getVar('PV') == args.version and old_srcrev == args.srcrev:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600417 raise DevtoolError("Current and upgrade versions are the same version")
418
419 rf = None
420 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500421 rev1 = standard._extract_source(srctree, False, 'devtool-orig', False, config, basepath, workspace, args.fixed_setup, rd, tinfoil)
422 rev2, md5, sha256, srcbranch = _extract_new_source(args.version, srctree, args.no_patch,
423 args.srcrev, args.srcbranch, args.branch, args.keep_temp,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600424 tinfoil, rd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500425 rf, copied = _create_new_recipe(args.version, md5, sha256, args.srcrev, srcbranch, config.workspace_path, tinfoil, rd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600426 except bb.process.CmdError as e:
427 _upgrade_error(e, rf, srctree)
428 except DevtoolError as e:
429 _upgrade_error(e, rf, srctree)
430 standard._add_md5(config, pn, os.path.dirname(rf))
431
432 af = _write_append(rf, srctree, args.same_dir, args.no_same_dir, rev2,
433 copied, config.workspace_path, rd)
434 standard._add_md5(config, pn, af)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500435
436 update_unlockedsigs(basepath, workspace, [pn], args.fixed_setup)
437
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600438 logger.info('Upgraded source extracted to %s' % srctree)
439 logger.info('New recipe is %s' % rf)
440 finally:
441 tinfoil.shutdown()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500442 return 0
443
444def register_commands(subparsers, context):
445 """Register devtool subcommands from this plugin"""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500446
447 defsrctree = standard.get_default_srctree(context.config)
448
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500449 parser_upgrade = subparsers.add_parser('upgrade', help='Upgrade an existing recipe',
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500450 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).',
451 group='starting')
452 parser_upgrade.add_argument('recipename', help='Name of recipe to upgrade (just name - no version, path or extension)')
453 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500454 parser_upgrade.add_argument('--version', '-V', help='Version to upgrade to (PV)')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600455 parser_upgrade.add_argument('--srcrev', '-S', help='Source revision to upgrade to (required if fetching from an SCM such as git)')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500456 parser_upgrade.add_argument('--srcbranch', '-B', help='Branch in source repository containing the revision to use (if fetching from an SCM such as git)')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500457 parser_upgrade.add_argument('--branch', '-b', default="devtool", help='Name for new development branch to checkout (default "%(default)s")')
458 parser_upgrade.add_argument('--no-patch', action="store_true", help='Do not apply patches from the recipe to the new source code')
459 group = parser_upgrade.add_mutually_exclusive_group()
460 group.add_argument('--same-dir', '-s', help='Build in same directory as source', action="store_true")
461 group.add_argument('--no-same-dir', help='Force build in a separate build directory', action="store_true")
462 parser_upgrade.add_argument('--keep-temp', action="store_true", help='Keep temporary directory (for debugging)')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500463 parser_upgrade.set_defaults(func=upgrade, fixed_setup=context.fixed_setup)