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