xref: /openbmc/openbmc/poky/scripts/recipetool (revision c342db35)
1#!/usr/bin/env python3
2
3# Recipe creation tool
4#
5# Copyright (C) 2014 Intel Corporation
6#
7# SPDX-License-Identifier: GPL-2.0-only
8#
9
10import sys
11import os
12import argparse
13import glob
14import logging
15
16scripts_path = os.path.dirname(os.path.realpath(__file__))
17lib_path = scripts_path + '/lib'
18sys.path = sys.path + [lib_path]
19import scriptutils
20import argparse_oe
21logger = scriptutils.logger_create('recipetool')
22
23plugins = []
24
25def tinfoil_init(parserecipes):
26    import bb.tinfoil
27    import logging
28    tinfoil = bb.tinfoil.Tinfoil(tracking=True)
29    tinfoil.logger.setLevel(logger.getEffectiveLevel())
30    tinfoil.prepare(not parserecipes)
31    return tinfoil
32
33def 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
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")
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>')
53    subparsers.required = True
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)
66    scriptpath.add_oe_lib_path()
67
68    scriptutils.logger_setup_color(logger, global_args.color)
69
70    tinfoil = tinfoil_init(False)
71    try:
72        for path in (tinfoil.config_data.getVar('BBPATH').split(':')
73                     + [scripts_path]):
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()
105
106    return ret
107
108
109if __name__ == "__main__":
110    try:
111        ret = main()
112    except Exception:
113        ret = 1
114        import traceback
115        traceback.print_exc()
116    sys.exit(ret)
117