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