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
28
29def run_tests(skip_net_tests, debug, verbose, args):
30    """Run the buildman tests
31
32    Args:
33        skip_net_tests (bool): True to skip tests which need the network
34        debug (bool): True to run in debugging mode (full traceback)
35        verbosity (int): Verbosity level to use (0-4)
36        args (list of str): List of tests to run, empty to run all
37    """
38    # These imports are here since tests are not available when buildman is
39    # installed as a Python module
40    # pylint: disable=C0415
41    from buildman import func_test
42    from buildman import test
43
44    test_name = args.terms and args.terms[0] or None
45    if skip_net_tests:
46        test.use_network = False
47
48    # Run the entry tests first ,since these need to be the first to import the
49    # 'entry' module.
50    result = test_util.run_test_suites(
51        'buildman', debug, verbose, False, args.threads, test_name, [],
52        [test.TestBuild, func_test.TestFunctional,
53         'buildman.toolchain', 'patman.gitutil'])
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        bsettings.setup(args.config_file)
97        ret_code = control.do_buildman(args)
98        return ret_code
99
100
101if __name__ == "__main__":
102    sys.exit(run_buildman())
103