+ (stdout, _) = bb.process.run('git rev-parse HEAD', cwd=srctree)
+ initial_rev = stdout.rstrip()
+
+ if args.no_patch:
+ patches = oe.recipeutils.get_recipe_patches(crd)
+ if len(patches):
+ logger.warn('By user choice, the following (%s_%s)
patches will NOT be applied into' %(pn,pv))
+ for patch in patches:
+ logger.warn("\t%s" % os.path.basename(patch))
+ else:
+ try:
+ bb.process.run('git rebase devtool-patched',
cwd=srctree)
+ except bb.process.ExecutionError as e:
+ # this is the only case where we do not propagate the
error
+ # user will have the change to correct merges
+ logger.error('Command \'%s\' failed:\n%s' %
(e.command, e.stdout))
+ bb.process.run('git tag -f devtool-patched_%s' %
args.version, cwd=srctree)
+ except bb.process.ExecutionError as e:
+ raise DevtoolError('Command \'%s\' failed:\n%s' % (e.command,
e.stdout))
+ finally:
+ if args.keep_temp:
+ logger.info('Preserving temporary directory %s' %
tmpsrcbasetree)
+ else:
+ shutil.rmtree(tmpsrcbasetree)
+ return initial_rev, recipefile
+
+def _create_upgrade_recipe(args, config, recipetmp, config_data, d):
+ """Creates the new recipe under workspace
+
+ Returns the full path on the new created recipe
+ """
+ import oe.recipeutils
+
+ def _get_checksums(recipefile):
+ import re
+ checksums = {}
+ with open(recipefile) as rf:
+ for line in rf:
+ for cs in ['md5sum', 'sha256sum']:
+ m = re.match("^SRC_URI\[%s\].*=.*\"(.*)\"" % cs,
line)
+ if m:
+ checksums[cs] = m.group(1)
+ return checksums
+
+ def _replace_checksums(recipefile, checksums):
+ import re
+ with open(recipefile + ".tmp", "w+") as tmprf:
+ with open(recipefile) as rf:
+ for line in rf:
+ m = None
+ for cs in ['md5sum', 'sha256sum']:
+ m = re.match("^SRC_URI\[%s\].*=.*\"(.*)\"" %
cs, line)
+ if m:
+ if cs in checksums:
+ oldcheck = m.group(1)
+ newcheck = checksums[cs]
+ line = line.replace(oldcheck, newcheck)
+ break
+ tmprf.write(line)
+
+ os.rename(recipefile + ".tmp", recipefile)
+
+ def _rename_patch_dirs(recipefolder, oldpv, newpv):
+ for root, dirs, files in os.walk(recipefolder):
+ for olddir in dirs:
+ if olddir.find(oldpv) != -1:
+ newdir = olddir.replace(oldpv, newpv)
+ bb.process.run('mv %s %s' % (olddir, newdir))
+
+ def _remove_patch_dirs(recipefolder):
+ for root, dirs, files in os.walk(recipefolder):
+ for d in dirs:
+ shutil.rmtree(os.path.join(root,d))
+
+ def _recipe_contains(recipefile, var):
+ import re
+ found = False
+ with open(recipefile) as rf:
+ for line in rf:
+ if re.match("^%s.*=.*" % var, line):
+ found = True
+ break
+ return found
+
+ if not os.path.exists(recipetmp):
+ raise DevtoolError("Temporal recipe %s does not exist" %
recipetmp)
+
+ # Copy current recipe into workspace
+ pn = d.getVar('PN', True)
+ recipepath = os.path.join(config.workspace_path, 'recipes', pn)
+ oe.recipeutils.copy_recipe_files(d, recipepath)
+
+ # Rename recipe
+ pv = d.getVar('PV', True)
+ recipename = "%s_%s.bb" % (pn, pv)
+ newrecipename = "%s_%s.bb" % (pn, args.version)
+ if os.path.isfile(os.path.join(recipepath, recipename)):
+ bb.process.run('mv %s %s' % (recipename, newrecipename),
cwd=recipepath)
+ else:
+ # Check if it is a git recipe
+ recipename = newrecipename = "%s_git.bb" % pn
+ if not os.path.isfile(os.path.join(recipepath, recipename)):
+ raise DevtoolError("Original recipe not found on workspace")
+
+ # Rename folders
+ _rename_patch_dirs(recipepath, pv, args.version)
+
+ # Update PV, just in case it is present
+ if _recipe_contains(os.path.join(recipepath, newrecipename), 'PV'):
+ oe.recipeutils.patch_recipe(d, os.path.join(recipepath,
newrecipename), {'PV':args.version})
+
+ # Update checksums
+ checksums = _get_checksums(os.path.join(recipepath, recipetmp))
+ _replace_checksums(os.path.join(recipepath, newrecipename),
checksums)
+
+ # Remove recipe created by the recipe-tool
+ bb.process.run('rm %s' % recipetmp)
+
+ return os.path.join(recipepath,newrecipename)
+
+def upgrade(args, config, basepath, workspace):
+ """Entry point for the devtool 'upgrade' subcommand"""
+ import bb
+ import oe.recipeutils
+
+ if args.recipename in workspace:
+ raise DevtoolError("recipe %s is already in your workspace" %
+ args.recipename)
+ if not args.version:
+ raise DevtoolError("Provide a version through the parameter
--version/-V")
+
+ reason = oe.recipeutils.validate_pn(args.recipename)
+ if reason:
+ raise DevtoolError(reason)
+
+ tinfoil = setup_tinfoil()
+
+ rd = standard._parse_recipe(config, tinfoil, args.recipename, True)
+ if not rd:
+ return 1
+
+ pn = rd.getVar('PV', True)
+ if pn == args.version:
+ raise DevtoolError("Current and upgrade versions are the same
%s" % pn)
+
+ srctree = os.path.abspath(args.srctree)
+
+ try:
+ # Extract source from current recipe
+ initial_rev_base = standard._extract_source(srctree, False,
args.branch, rd)
+
+ # We need to shutdown tinfoil temporally because recipetool
will be used
+ tinfoil.shutdown()
+
+ # Extract new source code
+ branch_upgrade = "%s_%s" % (args.branch, args.version)
+ initial_rev_upgrade, recipe_file_tmp =
_extract_upgrade_source(args, branch_upgrade, config, basepath, rd)
+ # Start again tinfoil
+ tinfoil = setup_tinfoil()
+
+ recipe_file = _create_upgrade_recipe(args, config,
recipe_file_tmp, tinfoil.config_data, rd)
+
+ except DevtoolError as e:
+ logger.error(e)
+ # remove workspace recipe and srctree
+ pn = rd.getVar('PN', True)
+ recipespath = os.path.join(config.workspace_path, 'recipes')
+ recipepath = os.path.join(recipespath, pn)
+ if os.path.exists(recipepath):
+ shutil.rmtree(recipepath)
+ if not len(os.listdir(recipespath)):
+ os.rmdir(recipespath)
+ # remove srctree
+ if os.path.exists(srctree):
+ shutil.rmtree(srctree)
+ raise DevtoolError(e)
+
+ appendpath = os.path.join(config.workspace_path, 'appends')
+ if not os.path.exists(appendpath):
+ os.makedirs(appendpath)
+
+ recipe_file_base =
os.path.basename(os.path.splitext(recipe_file)[0])
+ appendfile = os.path.join(appendpath, '%s.bbappend' %
recipe_file_base)
+ with open(appendfile, 'w') as f:
+ f.write('inherit externalsrc\n')
+ f.write('EXTERNALSRC = "%s"\n' % srctree)
+ f.write('EXTERNALSRC_BUILD = "%s"\n' % srctree)
+ f.write('\n# initial_rev: %s\n' % initial_rev_upgrade)
+
+ standard._add_md5(config, args.recipename, appendfile)
+ logger.info('Source tree extracted to %s' % srctree)
+ return 0
+
+def register_commands(subparsers, context):
+ """Register devtool subcommands from this plugin"""
+ parser_upgrade = subparsers.add_parser('upgrade', help='Upgrade
an existing recipe',
+ description='Upgrades an
existing recipe',
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+ parser_upgrade.add_argument('recipename', help='Name for recipe
to extract the source for')
+ parser_upgrade.add_argument('srctree', help='Path to where to
extract the source tree')
+ parser_upgrade.add_argument('--version', '-V', help='Version to
upgrade (PV)')
+ parser_upgrade.add_argument('--branch', '-b', default="devtool",
help='Name for development branch to checkout')
+ parser_upgrade.add_argument('--keep-temp', action="store_true",
help='Keep temporary directory (for debugging)')
+ parser_upgrade.add_argument('--no-patch', action="store_true",
help='Do not patch the new source code')
+ parser_upgrade.set_defaults(func=upgrade)