patman: Drop references to __future__
[oweals/u-boot.git] / tools / binman / binman.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0+
3
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
6 #
7 # Creates binary images from input files controlled by a description
8 #
9
10 """See README for more information"""
11
12 from distutils.sysconfig import get_python_lib
13 import glob
14 import multiprocessing
15 import os
16 import site
17 import sys
18 import traceback
19 import unittest
20
21 # Bring in the patman and dtoc libraries (but don't override the first path
22 # in PYTHONPATH)
23 our_path = os.path.dirname(os.path.realpath(__file__))
24 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
25     sys.path.insert(2, os.path.join(our_path, dirname))
26
27 # Bring in the libfdt module
28 sys.path.insert(2, 'scripts/dtc/pylibfdt')
29 sys.path.insert(2, os.path.join(our_path,
30                 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
31
32 # When running under python-coverage on Ubuntu 16.04, the dist-packages
33 # directories are dropped from the python path. Add them in so that we can find
34 # the elffile module. We could use site.getsitepackages() here but unfortunately
35 # that is not available in a virtualenv.
36 sys.path.append(get_python_lib())
37
38 import cmdline
39 import command
40 use_concurrent = True
41 try:
42     from concurrencytest import ConcurrentTestSuite, fork_for_tests
43 except:
44     use_concurrent = False
45 import control
46 import test_util
47
48 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
49     """Run the functional tests and any embedded doctests
50
51     Args:
52         debug: True to enable debugging, which shows a full stack trace on error
53         verbosity: Verbosity level to use
54         test_preserve_dirs: True to preserve the input directory used by tests
55             so that it can be examined afterwards (only useful for debugging
56             tests). If a single test is selected (in args[0]) it also preserves
57             the output directory for this test. Both directories are displayed
58             on the command line.
59         processes: Number of processes to use to run tests (None=same as #CPUs)
60         args: List of positional args provided to binman. This can hold a test
61             name to execute (as in 'binman test testSections', for example)
62         toolpath: List of paths to use for tools
63     """
64     import cbfs_util_test
65     import elf_test
66     import entry_test
67     import fdt_test
68     import ftest
69     import image_test
70     import test
71     import doctest
72
73     result = unittest.TestResult()
74     for module in []:
75         suite = doctest.DocTestSuite(module)
76         suite.run(result)
77
78     sys.argv = [sys.argv[0]]
79     if debug:
80         sys.argv.append('-D')
81     if verbosity:
82         sys.argv.append('-v%d' % verbosity)
83     if toolpath:
84         for path in toolpath:
85             sys.argv += ['--toolpath', path]
86
87     # Run the entry tests first ,since these need to be the first to import the
88     # 'entry' module.
89     test_name = args and args[0] or None
90     suite = unittest.TestSuite()
91     loader = unittest.TestLoader()
92     for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
93                    elf_test.TestElf, image_test.TestImage,
94                    cbfs_util_test.TestCbfs):
95         # Test the test module about our arguments, if it is interested
96         if hasattr(module, 'setup_test_args'):
97             setup_test_args = getattr(module, 'setup_test_args')
98             setup_test_args(preserve_indir=test_preserve_dirs,
99                 preserve_outdirs=test_preserve_dirs and test_name is not None,
100                 toolpath=toolpath, verbosity=verbosity)
101         if test_name:
102             try:
103                 suite.addTests(loader.loadTestsFromName(test_name, module))
104             except AttributeError:
105                 continue
106         else:
107             suite.addTests(loader.loadTestsFromTestCase(module))
108     if use_concurrent and processes != 1:
109         concurrent_suite = ConcurrentTestSuite(suite,
110                 fork_for_tests(processes or multiprocessing.cpu_count()))
111         concurrent_suite.run(result)
112     else:
113         suite.run(result)
114
115     # Remove errors which just indicate a missing test. Since Python v3.5 If an
116     # ImportError or AttributeError occurs while traversing name then a
117     # synthetic test that raises that error when run will be returned. These
118     # errors are included in the errors accumulated by result.errors.
119     if test_name:
120         errors = []
121         for test, err in result.errors:
122             if ("has no attribute '%s'" % test_name) not in err:
123                 errors.append((test, err))
124             result.testsRun -= 1
125         result.errors = errors
126
127     print(result)
128     for test, err in result.errors:
129         print(test.id(), err)
130     for test, err in result.failures:
131         print(err, result.failures)
132     if result.skipped:
133         print('%d binman test%s SKIPPED:' %
134               (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
135         for skip_info in result.skipped:
136             print('%s: %s' % (skip_info[0], skip_info[1]))
137     if result.errors or result.failures:
138         print('binman tests FAILED')
139         return 1
140     return 0
141
142 def GetEntryModules(include_testing=True):
143     """Get a set of entry class implementations
144
145     Returns:
146         Set of paths to entry class filenames
147     """
148     glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
149     return set([os.path.splitext(os.path.basename(item))[0]
150                 for item in glob_list
151                 if include_testing or '_testing' not in item])
152
153 def RunTestCoverage():
154     """Run the tests and check that we get 100% coverage"""
155     glob_list = GetEntryModules(False)
156     all_set = set([os.path.splitext(os.path.basename(item))[0]
157                    for item in glob_list if '_testing' not in item])
158     test_util.RunTestCoverage('tools/binman/binman.py', None,
159             ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
160             args.build_dir, all_set)
161
162 def RunBinman(args):
163     """Main entry point to binman once arguments are parsed
164
165     Args:
166         args: Command line arguments Namespace object
167     """
168     ret_code = 0
169
170     if not args.debug:
171         sys.tracebacklimit = 0
172
173     if args.cmd == 'test':
174         if args.test_coverage:
175             RunTestCoverage()
176         else:
177             ret_code = RunTests(args.debug, args.verbosity, args.processes,
178                                 args.test_preserve_dirs, args.tests,
179                                 args.toolpath)
180
181     elif args.cmd == 'entry-docs':
182         control.WriteEntryDocs(GetEntryModules())
183
184     else:
185         try:
186             ret_code = control.Binman(args)
187         except Exception as e:
188             print('binman: %s' % e)
189             if args.debug:
190                 print()
191                 traceback.print_exc()
192             ret_code = 1
193     return ret_code
194
195
196 if __name__ == "__main__":
197     args = cmdline.ParseArgs(sys.argv[1:])
198
199     ret_code = RunBinman(args)
200     sys.exit(ret_code)