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 | # |
| 10 | # This program is free software; you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License version 2 as |
| 12 | # published by the Free Software Foundation. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License along |
| 20 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | |
| 23 | import argparse |
| 24 | import errno |
| 25 | import logging |
| 26 | import os |
| 27 | import re |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | import subprocess |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 29 | import sys |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | import scriptutils |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 31 | |
| 32 | |
| 33 | logger = logging.getLogger('recipetool') |
| 34 | tinfoil = None |
| 35 | |
| 36 | |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 37 | def tinfoil_init(instance): |
| 38 | global tinfoil |
| 39 | tinfoil = instance |
| 40 | |
| 41 | |
| 42 | def _provide_to_pn(cooker, provide): |
| 43 | """Get the name of the preferred recipe for the specified provide.""" |
| 44 | import bb.providers |
| 45 | filenames = cooker.recipecache.providers[provide] |
| 46 | eligible, foundUnique = bb.providers.filterProviders(filenames, provide, cooker.expanded_data, cooker.recipecache) |
| 47 | filename = eligible[0] |
| 48 | pn = cooker.recipecache.pkg_fn[filename] |
| 49 | return pn |
| 50 | |
| 51 | |
| 52 | def _get_recipe_file(cooker, pn): |
| 53 | import oe.recipeutils |
| 54 | recipefile = oe.recipeutils.pn_to_recipe(cooker, pn) |
| 55 | if not recipefile: |
| 56 | skipreasons = oe.recipeutils.get_unavailable_reasons(cooker, pn) |
| 57 | if skipreasons: |
| 58 | logger.error('\n'.join(skipreasons)) |
| 59 | else: |
| 60 | logger.error("Unable to find any recipe file matching %s" % pn) |
| 61 | return recipefile |
| 62 | |
| 63 | |
| 64 | def layer(layerpath): |
| 65 | if not os.path.exists(os.path.join(layerpath, 'conf', 'layer.conf')): |
| 66 | raise argparse.ArgumentTypeError('{0!r} must be a path to a valid layer'.format(layerpath)) |
| 67 | return layerpath |
| 68 | |
| 69 | |
| 70 | def newappend(args): |
| 71 | import oe.recipeutils |
| 72 | |
| 73 | pn = _provide_to_pn(tinfoil.cooker, args.target) |
| 74 | recipe_path = _get_recipe_file(tinfoil.cooker, pn) |
| 75 | |
| 76 | rd = tinfoil.config_data.createCopy() |
| 77 | rd.setVar('FILE', recipe_path) |
| 78 | append_path, path_ok = oe.recipeutils.get_bbappend_path(rd, args.destlayer, args.wildcard_version) |
| 79 | if not append_path: |
| 80 | logger.error('Unable to determine layer directory containing %s', recipe_path) |
| 81 | return 1 |
| 82 | |
| 83 | if not path_ok: |
| 84 | logger.warn('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(destlayerdir, 'conf', 'layer.conf'), os.path.dirname(appendpath)) |
| 85 | |
| 86 | layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS', True).split()] |
| 87 | if not os.path.abspath(args.destlayer) in layerdirs: |
| 88 | logger.warn('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active') |
| 89 | |
| 90 | if not os.path.exists(append_path): |
| 91 | bb.utils.mkdirhier(os.path.dirname(append_path)) |
| 92 | |
| 93 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 94 | open(append_path, 'a').close() |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 95 | except (OSError, IOError) as exc: |
| 96 | logger.critical(str(exc)) |
| 97 | return 1 |
| 98 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 99 | if args.edit: |
| 100 | return scriptutils.run_editor([append_path, recipe_path]) |
| 101 | else: |
| 102 | print(append_path) |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 103 | |
| 104 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | def register_commands(subparsers): |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 106 | parser = subparsers.add_parser('newappend', |
| 107 | help='Create a bbappend for the specified target in the specified layer') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | 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] | 109 | parser.add_argument('-w', '--wildcard-version', help='Use wildcard to make the bbappend apply to any recipe version', action='store_true') |
| 110 | parser.add_argument('destlayer', help='Base directory of the destination layer to write the bbappend to', type=layer) |
| 111 | parser.add_argument('target', help='Target recipe/provide to append') |
| 112 | parser.set_defaults(func=newappend, parserecipes=True) |