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