| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 |  | 
|  | 3 | # Recipe creation tool | 
|  | 4 | # | 
|  | 5 | # Copyright (C) 2014 Intel Corporation | 
|  | 6 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 |  | 
|  | 10 | import sys | 
|  | 11 | import os | 
|  | 12 | import argparse | 
|  | 13 | import glob | 
|  | 14 | import logging | 
|  | 15 |  | 
|  | 16 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | 
|  | 17 | lib_path = scripts_path + '/lib' | 
|  | 18 | sys.path = sys.path + [lib_path] | 
|  | 19 | import scriptutils | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | import argparse_oe | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | logger = scriptutils.logger_create('recipetool') | 
|  | 22 |  | 
|  | 23 | plugins = [] | 
|  | 24 |  | 
|  | 25 | def tinfoil_init(parserecipes): | 
|  | 26 | import bb.tinfoil | 
|  | 27 | import logging | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | tinfoil = bb.tinfoil.Tinfoil(tracking=True) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | tinfoil.logger.setLevel(logger.getEffectiveLevel()) | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 30 | tinfoil.prepare(not parserecipes) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | return tinfoil | 
|  | 32 |  | 
|  | 33 | def main(): | 
|  | 34 |  | 
|  | 35 | if not os.environ.get('BUILDDIR', ''): | 
|  | 36 | logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)") | 
|  | 37 | sys.exit(1) | 
|  | 38 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 39 | parser = argparse_oe.ArgumentParser(description="OpenEmbedded recipe tool", | 
|  | 40 | add_help=False, | 
|  | 41 | epilog="Use %(prog)s <subcommand> --help to get help on a specific command") | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') | 
|  | 43 | parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') | 
|  | 44 | parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR') | 
|  | 45 |  | 
|  | 46 | global_args, unparsed_args = parser.parse_known_args() | 
|  | 47 |  | 
|  | 48 | # Help is added here rather than via add_help=True, as we don't want it to | 
|  | 49 | # be handled by parse_known_args() | 
|  | 50 | parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, | 
|  | 51 | help='show this help message and exit') | 
|  | 52 | subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | subparsers.required = True | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 |  | 
|  | 55 | if global_args.debug: | 
|  | 56 | logger.setLevel(logging.DEBUG) | 
|  | 57 | elif global_args.quiet: | 
|  | 58 | logger.setLevel(logging.ERROR) | 
|  | 59 |  | 
|  | 60 | import scriptpath | 
|  | 61 | bitbakepath = scriptpath.add_bitbake_lib_path() | 
|  | 62 | if not bitbakepath: | 
|  | 63 | logger.error("Unable to find bitbake by searching parent directory of this script or PATH") | 
|  | 64 | sys.exit(1) | 
|  | 65 | logger.debug('Found bitbake path: %s' % bitbakepath) | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 66 | scriptpath.add_oe_lib_path() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 |  | 
|  | 68 | scriptutils.logger_setup_color(logger, global_args.color) | 
|  | 69 |  | 
|  | 70 | tinfoil = tinfoil_init(False) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | try: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | for path in (tinfoil.config_data.getVar('BBPATH').split(':') | 
|  | 73 | + [scripts_path]): | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 74 | pluginpath = os.path.join(path, 'lib', 'recipetool') | 
|  | 75 | scriptutils.load_plugins(logger, plugins, pluginpath) | 
|  | 76 |  | 
|  | 77 | registered = False | 
|  | 78 | for plugin in plugins: | 
|  | 79 | if hasattr(plugin, 'register_commands'): | 
|  | 80 | registered = True | 
|  | 81 | plugin.register_commands(subparsers) | 
|  | 82 | elif hasattr(plugin, 'register_command'): | 
|  | 83 | # Legacy function name | 
|  | 84 | registered = True | 
|  | 85 | plugin.register_command(subparsers) | 
|  | 86 | if hasattr(plugin, 'tinfoil_init'): | 
|  | 87 | plugin.tinfoil_init(tinfoil) | 
|  | 88 |  | 
|  | 89 | if not registered: | 
|  | 90 | logger.error("No commands registered - missing plugins?") | 
|  | 91 | sys.exit(1) | 
|  | 92 |  | 
|  | 93 | args = parser.parse_args(unparsed_args, namespace=global_args) | 
|  | 94 |  | 
|  | 95 | try: | 
|  | 96 | if getattr(args, 'parserecipes', False): | 
|  | 97 | tinfoil.config_data.disableTracking() | 
|  | 98 | tinfoil.parseRecipes() | 
|  | 99 | tinfoil.config_data.enableTracking() | 
|  | 100 | ret = args.func(args) | 
|  | 101 | except bb.BBHandledException: | 
|  | 102 | ret = 1 | 
|  | 103 | finally: | 
|  | 104 | tinfoil.shutdown() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 |  | 
|  | 106 | return ret | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | if __name__ == "__main__": | 
|  | 110 | try: | 
|  | 111 | ret = main() | 
|  | 112 | except Exception: | 
|  | 113 | ret = 1 | 
|  | 114 | import traceback | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 115 | traceback.print_exc() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | sys.exit(ret) |