| 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 | # | 
 | 7 | # This program is free software; you can redistribute it and/or modify | 
 | 8 | # it under the terms of the GNU General Public License version 2 as | 
 | 9 | # published by the Free Software Foundation. | 
 | 10 | # | 
 | 11 | # This program is distributed in the hope that it will be useful, | 
 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 14 | # GNU General Public License for more details. | 
 | 15 | # | 
 | 16 | # You should have received a copy of the GNU General Public License along | 
 | 17 | # with this program; if not, write to the Free Software Foundation, Inc., | 
 | 18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
 | 19 |  | 
 | 20 | import sys | 
 | 21 | import os | 
 | 22 | import argparse | 
 | 23 | import glob | 
 | 24 | import logging | 
 | 25 |  | 
 | 26 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | 
 | 27 | lib_path = scripts_path + '/lib' | 
 | 28 | sys.path = sys.path + [lib_path] | 
 | 29 | import scriptutils | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | import argparse_oe | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | logger = scriptutils.logger_create('recipetool') | 
 | 32 |  | 
 | 33 | plugins = [] | 
 | 34 |  | 
 | 35 | def tinfoil_init(parserecipes): | 
 | 36 |     import bb.tinfoil | 
 | 37 |     import logging | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 38 |     tinfoil = bb.tinfoil.Tinfoil(tracking=True) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 |     tinfoil.prepare(not parserecipes) | 
 | 40 |     tinfoil.logger.setLevel(logger.getEffectiveLevel()) | 
 | 41 |     return tinfoil | 
 | 42 |  | 
 | 43 | def main(): | 
 | 44 |  | 
 | 45 |     if not os.environ.get('BUILDDIR', ''): | 
 | 46 |         logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)") | 
 | 47 |         sys.exit(1) | 
 | 48 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 |     parser = argparse_oe.ArgumentParser(description="OpenEmbedded recipe tool", | 
 | 50 |                                         add_help=False, | 
 | 51 |                                         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] | 52 |     parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') | 
 | 53 |     parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') | 
 | 54 |     parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR') | 
 | 55 |  | 
 | 56 |     global_args, unparsed_args = parser.parse_known_args() | 
 | 57 |  | 
 | 58 |     # Help is added here rather than via add_help=True, as we don't want it to | 
 | 59 |     # be handled by parse_known_args() | 
 | 60 |     parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, | 
 | 61 |                         help='show this help message and exit') | 
 | 62 |     subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 |     subparsers.required = True | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 |  | 
 | 65 |     if global_args.debug: | 
 | 66 |         logger.setLevel(logging.DEBUG) | 
 | 67 |     elif global_args.quiet: | 
 | 68 |         logger.setLevel(logging.ERROR) | 
 | 69 |  | 
 | 70 |     import scriptpath | 
 | 71 |     bitbakepath = scriptpath.add_bitbake_lib_path() | 
 | 72 |     if not bitbakepath: | 
 | 73 |         logger.error("Unable to find bitbake by searching parent directory of this script or PATH") | 
 | 74 |         sys.exit(1) | 
 | 75 |     logger.debug('Found bitbake path: %s' % bitbakepath) | 
 | 76 |  | 
 | 77 |     scriptutils.logger_setup_color(logger, global_args.color) | 
 | 78 |  | 
 | 79 |     tinfoil = tinfoil_init(False) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 |     try: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 81 |         for path in ([scripts_path] + | 
 | 82 |                     tinfoil.config_data.getVar('BBPATH', True).split(':')): | 
 | 83 |             pluginpath = os.path.join(path, 'lib', 'recipetool') | 
 | 84 |             scriptutils.load_plugins(logger, plugins, pluginpath) | 
 | 85 |  | 
 | 86 |         registered = False | 
 | 87 |         for plugin in plugins: | 
 | 88 |             if hasattr(plugin, 'register_commands'): | 
 | 89 |                 registered = True | 
 | 90 |                 plugin.register_commands(subparsers) | 
 | 91 |             elif hasattr(plugin, 'register_command'): | 
 | 92 |                 # Legacy function name | 
 | 93 |                 registered = True | 
 | 94 |                 plugin.register_command(subparsers) | 
 | 95 |             if hasattr(plugin, 'tinfoil_init'): | 
 | 96 |                 plugin.tinfoil_init(tinfoil) | 
 | 97 |  | 
 | 98 |         if not registered: | 
 | 99 |             logger.error("No commands registered - missing plugins?") | 
 | 100 |             sys.exit(1) | 
 | 101 |  | 
 | 102 |         args = parser.parse_args(unparsed_args, namespace=global_args) | 
 | 103 |  | 
 | 104 |         try: | 
 | 105 |             if getattr(args, 'parserecipes', False): | 
 | 106 |                 tinfoil.config_data.disableTracking() | 
 | 107 |                 tinfoil.parseRecipes() | 
 | 108 |                 tinfoil.config_data.enableTracking() | 
 | 109 |             ret = args.func(args) | 
 | 110 |         except bb.BBHandledException: | 
 | 111 |             ret = 1 | 
 | 112 |     finally: | 
 | 113 |         tinfoil.shutdown() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 |  | 
 | 115 |     return ret | 
 | 116 |  | 
 | 117 |  | 
 | 118 | if __name__ == "__main__": | 
 | 119 |     try: | 
 | 120 |         ret = main() | 
 | 121 |     except Exception: | 
 | 122 |         ret = 1 | 
 | 123 |         import traceback | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 |         traceback.print_exc() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 |     sys.exit(ret) |