2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
7 # Creates binary images from input files controlled by a description
10 """See README for more information"""
12 from distutils.sysconfig import get_python_lib
20 # Bring in the patman and dtoc libraries (but don't override the first path
22 our_path = os.path.dirname(os.path.realpath(__file__))
23 sys.path.insert(2, os.path.join(our_path, '..'))
25 from patman import test_util
27 # Bring in the libfdt module
28 sys.path.insert(2, 'scripts/dtc/pylibfdt')
29 sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
30 sys.path.insert(2, os.path.join(our_path,
31 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
33 # When running under python-coverage on Ubuntu 16.04, the dist-packages
34 # directories are dropped from the python path. Add them in so that we can find
35 # the elffile module. We could use site.getsitepackages() here but unfortunately
36 # that is not available in a virtualenv.
37 sys.path.append(get_python_lib())
39 from binman import cmdline
40 from binman import control
41 from patman import test_util
43 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
44 """Run the functional tests and any embedded doctests
47 debug: True to enable debugging, which shows a full stack trace on error
48 verbosity: Verbosity level to use
49 test_preserve_dirs: True to preserve the input directory used by tests
50 so that it can be examined afterwards (only useful for debugging
51 tests). If a single test is selected (in args[0]) it also preserves
52 the output directory for this test. Both directories are displayed
54 processes: Number of processes to use to run tests (None=same as #CPUs)
55 args: List of positional args provided to binman. This can hold a test
56 name to execute (as in 'binman test testSections', for example)
57 toolpath: List of paths to use for tools
59 from binman import cbfs_util_test
60 from binman import elf_test
61 from binman import entry_test
62 from binman import fdt_test
63 from binman import ftest
64 from binman import image_test
65 from binman import test
68 result = unittest.TestResult()
69 test_name = args and args[0] or None
71 # Run the entry tests first ,since these need to be the first to import the
73 test_util.RunTestSuites(
74 result, debug, verbosity, test_preserve_dirs, processes, test_name,
76 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
77 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
79 return test_util.ReportResult('binman', test_name, result)
81 def GetEntryModules(include_testing=True):
82 """Get a set of entry class implementations
85 Set of paths to entry class filenames
87 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
88 return set([os.path.splitext(os.path.basename(item))[0]
90 if include_testing or '_testing' not in item])
92 def RunTestCoverage():
93 """Run the tests and check that we get 100% coverage"""
94 glob_list = GetEntryModules(False)
95 all_set = set([os.path.splitext(os.path.basename(item))[0]
96 for item in glob_list if '_testing' not in item])
97 test_util.RunTestCoverage('tools/binman/binman', None,
98 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
99 args.build_dir, all_set)
102 """Main entry point to binman once arguments are parsed
105 args: Command line arguments Namespace object
110 sys.tracebacklimit = 0
112 if args.cmd == 'test':
113 if args.test_coverage:
116 ret_code = RunTests(args.debug, args.verbosity, args.processes,
117 args.test_preserve_dirs, args.tests,
120 elif args.cmd == 'entry-docs':
121 control.WriteEntryDocs(GetEntryModules())
125 ret_code = control.Binman(args)
126 except Exception as e:
127 print('binman: %s' % e, file=sys.stderr)
130 traceback.print_exc()
135 if __name__ == "__main__":
136 args = cmdline.ParseArgs(sys.argv[1:])
138 ret_code = RunBinman(args)