1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Copyright (c) 2012 The Chromium OS Authors.
5#
6
7"""See README for more information"""
8
9try:
10    import importlib.resources
11except ImportError:
12    # for Python 3.6
13    import importlib_resources
14import os
15import sys
16
17# Bring in the patman libraries
18# pylint: disable=C0413
19our_path = os.path.dirname(os.path.realpath(__file__))
20sys.path.insert(1, os.path.join(our_path, '..'))
21
22# Our modules
23from buildman import bsettings
24from buildman import cmdline
25from buildman import control
26from u_boot_pylib import test_util
27from u_boot_pylib import tools
28from u_boot_pylib import tout
29
30def run_tests(skip_net_tests, debug, verbose, args):
31    """Run the buildman tests
32
33    Args:
34        skip_net_tests (bool): True to skip tests which need the network
35        debug (bool): True to run in debugging mode (full traceback)
36        verbosity (int): Verbosity level to use (0-4)
37        args (list of str): List of tests to run, empty to run all
38    """
39    # These imports are here since tests are not available when buildman is
40    # installed as a Python module
41    # pylint: disable=C0415
42    from buildman import func_test
43    from buildman import test
44
45    test_name = args.terms and args.terms[0] or None
46    if skip_net_tests:
47        test.use_network = False
48
49    # Run the entry tests first ,since these need to be the first to import the
50    # 'entry' module.
51    result = test_util.run_test_suites(
52        'buildman', debug, verbose, False, False, args.threads, test_name, [],
53        [test.TestBuild, func_test.TestFunctional, 'buildman.toolchain'])
54
55    return (0 if result.wasSuccessful() else 1)
56
57def run_test_coverage():
58    """Run the tests and check that we get 100% coverage"""
59    test_util.run_test_coverage(
60        'tools/buildman/buildman', None,
61        ['tools/patman/*.py', 'tools/u_boot_pylib/*', '*test_fdt.py',
62         'tools/buildman/kconfiglib.py', 'tools/buildman/*test*.py',
63         'tools/buildman/main.py'],
64        '/tmp/b', single_thread='-T1')
65
66
67def run_buildman():
68    """Run bulidman
69
70    This is the main program. It collects arguments and runs either the tests or
71    the control module.
72    """
73    args = cmdline.parse_args()
74
75    if not args.debug:
76        sys.tracebacklimit = 0
77
78    # Run our meagre tests
79    if cmdline.HAS_TESTS and args.test:
80        return run_tests(args.skip_net_tests, args.debug, args.verbose, args)
81
82    elif cmdline.HAS_TESTS and args.coverage:
83        run_test_coverage()
84
85    elif args.full_help:
86        if hasattr(importlib.resources, 'files'):
87            dirpath = importlib.resources.files('buildman')
88            tools.print_full_help(str(dirpath.joinpath('README.rst')))
89        else:
90            with importlib.resources.path('buildman', 'README.rst') as readme:
91                tools.print_full_help(str(readme))
92
93
94    # Build selected commits for selected boards
95    else:
96        try:
97            tout.init(tout.INFO if args.verbose else tout.WARNING)
98            bsettings.setup(args.config_file)
99            ret_code = control.do_buildman(args)
100        finally:
101            tout.uninit()
102        return ret_code
103
104
105if __name__ == "__main__":
106    sys.exit(run_buildman())
107