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 | # This script has subcommands which operate against your bitbake layers, either |
| 4 | # displaying useful information, or acting against them. |
| 5 | # See the help output for details on available commands. |
| 6 | |
| 7 | # Copyright (C) 2011 Mentor Graphics Corporation |
| 8 | # Copyright (C) 2011-2015 Intel Corporation |
| 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 logging |
| 24 | import os |
| 25 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | import argparse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | bindir = os.path.dirname(__file__) |
| 29 | topdir = os.path.dirname(bindir) |
| 30 | sys.path[0:0] = [os.path.join(topdir, 'lib')] |
| 31 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | import bb.tinfoil |
| 33 | |
| 34 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 35 | def tinfoil_init(parserecipes): |
| 36 | import bb.tinfoil |
| 37 | tinfoil = bb.tinfoil.Tinfoil(tracking=True) |
| 38 | tinfoil.prepare(not parserecipes) |
| 39 | tinfoil.logger.setLevel(logger.getEffectiveLevel()) |
| 40 | return tinfoil |
| 41 | |
| 42 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | def logger_create(name, output=sys.stderr): |
| 44 | logger = logging.getLogger(name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | loggerhandler = logging.StreamHandler(output) |
| 46 | loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
| 47 | logger.addHandler(loggerhandler) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | logger.setLevel(logging.INFO) |
| 49 | return logger |
| 50 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 51 | def logger_setup_color(logger, color='auto'): |
| 52 | from bb.msg import BBLogFormatter |
| 53 | console = logging.StreamHandler(sys.stdout) |
| 54 | formatter = BBLogFormatter("%(levelname)s: %(message)s") |
| 55 | console.setFormatter(formatter) |
| 56 | logger.handlers = [console] |
| 57 | if color == 'always' or (color == 'auto' and console.stream.isatty()): |
| 58 | formatter.enable_color() |
| 59 | |
| 60 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | logger = logger_create('bitbake-layers', sys.stdout) |
| 62 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | def main(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 64 | parser = argparse.ArgumentParser( |
| 65 | description="BitBake layers utility", |
| 66 | epilog="Use %(prog)s <subcommand> --help to get help on a specific command", |
| 67 | add_help=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') |
| 69 | parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR') |
| 71 | |
| 72 | global_args, unparsed_args = parser.parse_known_args() |
| 73 | |
| 74 | # Help is added here rather than via add_help=True, as we don't want it to |
| 75 | # be handled by parse_known_args() |
| 76 | parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, |
| 77 | help='show this help message and exit') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 79 | subparsers.required = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 81 | if global_args.debug: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | logger.setLevel(logging.DEBUG) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 83 | elif global_args.quiet: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | logger.setLevel(logging.ERROR) |
| 85 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 86 | logger_setup_color(logger, global_args.color) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 88 | plugins = [] |
| 89 | tinfoil = tinfoil_init(False) |
| 90 | try: |
| 91 | for path in ([topdir] + |
| 92 | tinfoil.config_data.getVar('BBPATH', True).split(':')): |
| 93 | pluginpath = os.path.join(path, 'lib', 'bblayers') |
| 94 | bb.utils.load_plugins(logger, plugins, pluginpath) |
| 95 | |
| 96 | registered = False |
| 97 | for plugin in plugins: |
| 98 | if hasattr(plugin, 'register_commands'): |
| 99 | registered = True |
| 100 | plugin.register_commands(subparsers) |
| 101 | if hasattr(plugin, 'tinfoil_init'): |
| 102 | plugin.tinfoil_init(tinfoil) |
| 103 | |
| 104 | if not registered: |
| 105 | logger.error("No commands registered - missing plugins?") |
| 106 | sys.exit(1) |
| 107 | |
| 108 | args = parser.parse_args(unparsed_args, namespace=global_args) |
| 109 | |
| 110 | if getattr(args, 'parserecipes', False): |
| 111 | tinfoil.config_data.disableTracking() |
| 112 | tinfoil.parseRecipes() |
| 113 | tinfoil.config_data.enableTracking() |
| 114 | |
| 115 | return args.func(args) |
| 116 | finally: |
| 117 | tinfoil.shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | |
| 119 | |
| 120 | if __name__ == "__main__": |
| 121 | try: |
| 122 | ret = main() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 123 | except bb.BBHandledException: |
| 124 | ret = 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | except Exception: |
| 126 | ret = 1 |
| 127 | import traceback |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 128 | traceback.print_exc() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | sys.exit(ret) |