1*4882a593Smuzhiyun# Recipe creation tool - newappend plugin 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# This sub-command creates a bbappend for the specified target and prints the 4*4882a593Smuzhiyun# path to the bbappend. 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# Example: recipetool newappend meta-mylayer busybox 7*4882a593Smuzhiyun# 8*4882a593Smuzhiyun# Copyright (C) 2015 Christopher Larson <kergoth@gmail.com> 9*4882a593Smuzhiyun# 10*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 11*4882a593Smuzhiyun# 12*4882a593Smuzhiyun 13*4882a593Smuzhiyunimport argparse 14*4882a593Smuzhiyunimport errno 15*4882a593Smuzhiyunimport logging 16*4882a593Smuzhiyunimport os 17*4882a593Smuzhiyunimport re 18*4882a593Smuzhiyunimport subprocess 19*4882a593Smuzhiyunimport sys 20*4882a593Smuzhiyunimport scriptutils 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun 23*4882a593Smuzhiyunlogger = logging.getLogger('recipetool') 24*4882a593Smuzhiyuntinfoil = None 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun 27*4882a593Smuzhiyundef tinfoil_init(instance): 28*4882a593Smuzhiyun global tinfoil 29*4882a593Smuzhiyun tinfoil = instance 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun 32*4882a593Smuzhiyundef layer(layerpath): 33*4882a593Smuzhiyun if not os.path.exists(os.path.join(layerpath, 'conf', 'layer.conf')): 34*4882a593Smuzhiyun raise argparse.ArgumentTypeError('{0!r} must be a path to a valid layer'.format(layerpath)) 35*4882a593Smuzhiyun return layerpath 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun 38*4882a593Smuzhiyundef newappend(args): 39*4882a593Smuzhiyun import oe.recipeutils 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun recipe_path = tinfoil.get_recipe_file(args.target) 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun rd = tinfoil.config_data.createCopy() 44*4882a593Smuzhiyun rd.setVar('FILE', recipe_path) 45*4882a593Smuzhiyun append_path, path_ok = oe.recipeutils.get_bbappend_path(rd, args.destlayer, args.wildcard_version) 46*4882a593Smuzhiyun if not append_path: 47*4882a593Smuzhiyun logger.error('Unable to determine layer directory containing %s', recipe_path) 48*4882a593Smuzhiyun return 1 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun if not path_ok: 51*4882a593Smuzhiyun logger.warning('Unable to determine correct subdirectory path for bbappend file - check that what %s adds to BBFILES also matches .bbappend files. Using %s for now, but until you fix this the bbappend will not be applied.', os.path.join(args.destlayer, 'conf', 'layer.conf'), os.path.dirname(append_path)) 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS').split()] 54*4882a593Smuzhiyun if not os.path.abspath(args.destlayer) in layerdirs: 55*4882a593Smuzhiyun logger.warning('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active') 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun if not os.path.exists(append_path): 58*4882a593Smuzhiyun bb.utils.mkdirhier(os.path.dirname(append_path)) 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun try: 61*4882a593Smuzhiyun open(append_path, 'a').close() 62*4882a593Smuzhiyun except (OSError, IOError) as exc: 63*4882a593Smuzhiyun logger.critical(str(exc)) 64*4882a593Smuzhiyun return 1 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun if args.edit: 67*4882a593Smuzhiyun return scriptutils.run_editor([append_path, recipe_path], logger) 68*4882a593Smuzhiyun else: 69*4882a593Smuzhiyun print(append_path) 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun 72*4882a593Smuzhiyundef register_commands(subparsers): 73*4882a593Smuzhiyun parser = subparsers.add_parser('newappend', 74*4882a593Smuzhiyun help='Create a bbappend for the specified target in the specified layer') 75*4882a593Smuzhiyun parser.add_argument('-e', '--edit', help='Edit the new append. This obeys $VISUAL if set, otherwise $EDITOR, otherwise vi.', action='store_true') 76*4882a593Smuzhiyun parser.add_argument('-w', '--wildcard-version', help='Use wildcard to make the bbappend apply to any recipe version', action='store_true') 77*4882a593Smuzhiyun parser.add_argument('destlayer', help='Base directory of the destination layer to write the bbappend to', type=layer) 78*4882a593Smuzhiyun parser.add_argument('target', help='Target recipe/provide to append') 79*4882a593Smuzhiyun parser.set_defaults(func=newappend, parserecipes=True) 80