Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Development tool - upgrade command plugin |
| 2 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 3 | # Copyright (C) 2014-2017 Intel Corporation |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 20 | import os |
| 21 | import sys |
| 22 | import re |
| 23 | import shutil |
| 24 | import tempfile |
| 25 | import logging |
| 26 | import argparse |
| 27 | import scriptutils |
| 28 | import errno |
| 29 | import bb |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | |
| 31 | devtool_path = os.path.dirname(os.path.realpath(__file__)) + '/../../../meta/lib' |
| 32 | sys.path = sys.path + [devtool_path] |
| 33 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | import oe.recipeutils |
| 35 | from devtool import standard |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 36 | from devtool import exec_build_env_command, setup_tinfoil, DevtoolError, parse_recipe, use_external_build, update_unlockedsigs |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | logger = logging.getLogger('devtool') |
| 39 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | def _run(cmd, cwd=''): |
| 41 | logger.debug("Running command %s> %s" % (cwd,cmd)) |
| 42 | return bb.process.run('%s' % cmd, cwd=cwd) |
| 43 | |
| 44 | def _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 | |
| 51 | def _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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 56 | shutil.move(os.path.join(orig, path), dest_path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
| 58 | def _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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | def _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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 74 | def _recipe_contains(rd, var): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 75 | rf = rd.getVar('FILE') |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 76 | varfiles = oe.recipeutils.get_var_files(rf, [var], rd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | for var, fn in varfiles.items(): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 78 | if fn and fn.startswith(os.path.dirname(rf) + os.sep): |
| 79 | return True |
| 80 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
| 82 | def _rename_recipe_dirs(oldpv, newpv, path): |
| 83 | for root, dirs, files in os.walk(path): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | # Rename directories with the version in their name |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | for olddir in dirs: |
| 86 | if olddir.find(oldpv) != -1: |
| 87 | newdir = olddir.replace(oldpv, newpv) |
| 88 | if olddir != newdir: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 89 | shutil.move(os.path.join(path, olddir), os.path.join(path, newdir)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 98 | def _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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | if oldrecipe != newrecipe: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 103 | shutil.move(os.path.join(path, oldrecipe), os.path.join(path, newrecipe)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | newrecipe = oldrecipe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | return os.path.join(path, newrecipe) |
| 107 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | def _rename_recipe_files(oldrecipe, bpn, oldpv, newpv, path): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | _rename_recipe_dirs(oldpv, newpv, path) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 110 | return _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 112 | def _write_append(rc, srctree, same_dir, no_same_dir, rev, copied, workspace, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | """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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | pn = d.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 132 | b_is_s = use_external_build(same_dir, no_same_dir, d) |
| 133 | if b_is_s: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | f.write('EXTERNALSRC_BUILD_pn-%s = "%s"\n' % (pn, srctree)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 135 | f.write('\n') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | if rev: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 137 | f.write('# initial_rev: %s\n' % rev) |
| 138 | if copied: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | f.write('# original_path: %s\n' % os.path.dirname(d.getVar('FILE'))) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 140 | f.write('# original_files: %s\n' % ' '.join(copied)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | return af |
| 142 | |
| 143 | def _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 | |
| 154 | def _upgrade_error(e, rf, srctree): |
| 155 | if rf: |
| 156 | cleanup_on_error(rf, srctree) |
| 157 | logger.error(e) |
| 158 | raise DevtoolError(e) |
| 159 | |
| 160 | def _get_uri(rd): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 161 | srcuris = rd.getVar('SRC_URI').split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | if not len(srcuris): |
| 163 | raise DevtoolError('SRC_URI not found on recipe') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 164 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 183 | def _extract_new_source(newpv, srctree, no_patch, srcrev, srcbranch, branch, keep_temp, tinfoil, rd): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | """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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 192 | pv = crd.getVar('PV') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 200 | __run('git fetch') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 201 | __run('git checkout %s' % rev) |
| 202 | __run('git tag -f devtool-base-new') |
| 203 | md5 = None |
| 204 | sha256 = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 222 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 223 | __run('git checkout devtool-base -b devtool-%s' % newpv) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | |
| 225 | tmpdir = tempfile.mkdtemp(prefix='devtool') |
| 226 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 227 | checksums, ftmpdir = scriptutils.fetch_url(tinfoil, uri, rev, tmpdir, logger, preserve_tmp=keep_temp) |
| 228 | except scriptutils.FetchUrlFailure as e: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | raise DevtoolError(e) |
| 230 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 231 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | tmpsrctree = _get_srctree(tmpdir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 238 | srctree = os.path.abspath(srctree) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 240 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 250 | # Copy in new ones |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 251 | _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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 257 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | __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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 272 | __run('git checkout devtool-patched -b %s' % branch) |
| 273 | skiptag = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 274 | try: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 275 | __run('git rebase %s' % rev) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 276 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 283 | if uri.startswith('git://'): |
| 284 | suffix = 'new' |
| 285 | else: |
| 286 | suffix = newpv |
| 287 | __run('git tag -f devtool-patched-%s' % suffix) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | |
| 289 | if tmpsrctree: |
| 290 | if keep_temp: |
| 291 | logger.info('Preserving temporary directory %s' % tmpsrctree) |
| 292 | else: |
| 293 | shutil.rmtree(tmpsrctree) |
| 294 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 295 | return (rev, md5, sha256, srcbranch) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 297 | def _create_new_recipe(newpv, md5, sha256, srcrev, srcbranch, workspace, tinfoil, rd): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 298 | """Creates the new recipe under workspace""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 299 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 300 | bpn = rd.getVar('BPN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 301 | path = os.path.join(workspace, 'recipes', bpn) |
| 302 | bb.utils.mkdirhier(path) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 303 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 307 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 308 | oldpv = rd.getVar('PV') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | if not newpv: |
| 310 | newpv = oldpv |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 311 | origpath = rd.getVar('FILE') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 312 | fullpath = _rename_recipe_files(origpath, bpn, oldpv, newpv, path) |
| 313 | logger.debug('Upgraded %s => %s' % (origpath, fullpath)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 314 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 315 | newvalues = {} |
| 316 | if _recipe_contains(rd, 'PV') and newpv != oldpv: |
| 317 | newvalues['PV'] = newpv |
| 318 | |
| 319 | if srcrev: |
| 320 | newvalues['SRCREV'] = srcrev |
| 321 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 322 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 348 | |
| 349 | if md5 and sha256: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 350 | newvalues['SRC_URI[md5sum]'] = md5 |
| 351 | newvalues['SRC_URI[sha256sum]'] = sha256 |
| 352 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 353 | rd = tinfoil.parse_recipe_file(fullpath, False) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 354 | oe.recipeutils.patch_recipe(rd, fullpath, newvalues) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 355 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 356 | return fullpath, copied |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 357 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 358 | |
| 359 | def _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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 381 | def 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 388 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 390 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 391 | _check_git_config() |
| 392 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 393 | tinfoil = setup_tinfoil(basepath=basepath, tracking=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 394 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 395 | rd = parse_recipe(config, tinfoil, args.recipename, True) |
| 396 | if not rd: |
| 397 | return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 398 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 399 | pn = rd.getVar('PN') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 400 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 411 | old_srcrev = rd.getVar('SRCREV') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 412 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 416 | if rd.getVar('PV') == args.version and old_srcrev == args.srcrev: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 417 | raise DevtoolError("Current and upgrade versions are the same version") |
| 418 | |
| 419 | rf = None |
| 420 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 421 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 424 | tinfoil, rd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 425 | rf, copied = _create_new_recipe(args.version, md5, sha256, args.srcrev, srcbranch, config.workspace_path, tinfoil, rd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 426 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 435 | |
| 436 | update_unlockedsigs(basepath, workspace, [pn], args.fixed_setup) |
| 437 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 438 | logger.info('Upgraded source extracted to %s' % srctree) |
| 439 | logger.info('New recipe is %s' % rf) |
| 440 | finally: |
| 441 | tinfoil.shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 442 | return 0 |
| 443 | |
| 444 | def register_commands(subparsers, context): |
| 445 | """Register devtool subcommands from this plugin""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 446 | |
| 447 | defsrctree = standard.get_default_srctree(context.config) |
| 448 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 449 | parser_upgrade = subparsers.add_parser('upgrade', help='Upgrade an existing recipe', |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 450 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 454 | parser_upgrade.add_argument('--version', '-V', help='Version to upgrade to (PV)') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 455 | parser_upgrade.add_argument('--srcrev', '-S', help='Source revision to upgrade to (required if fetching from an SCM such as git)') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 456 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 457 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 463 | parser_upgrade.set_defaults(func=upgrade, fixed_setup=context.fixed_setup) |