binman: Drop unnecessary debug handling
[oweals/u-boot.git] / tools / binman / binman.py
1 #!/usr/bin/env python2
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 __future__ import print_function
13
14 from distutils.sysconfig import get_python_lib
15 import glob
16 import multiprocessing
17 import os
18 import site
19 import sys
20 import traceback
21 import unittest
22
23 # Bring in the patman and dtoc libraries
24 our_path = os.path.dirname(os.path.realpath(__file__))
25 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
26     sys.path.insert(0, os.path.join(our_path, dirname))
27
28 # Bring in the libfdt module
29 sys.path.insert(0, 'scripts/dtc/pylibfdt')
30 sys.path.insert(0, os.path.join(our_path,
31                 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
32
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())
38
39 import cmdline
40 import command
41 use_concurrent = True
42 try:
43     from concurrencytest import ConcurrentTestSuite, fork_for_tests
44 except:
45     use_concurrent = False
46 import control
47 import test_util
48
49 def RunTests(debug, processes, args):
50     """Run the functional tests and any embedded doctests
51
52     Args:
53         debug: True to enable debugging, which shows a full stack trace on error
54         args: List of positional args provided to binman. This can hold a test
55             name to execute (as in 'binman -t testSections', for example)
56         processes: Number of processes to use to run tests (None=same as #CPUs)
57     """
58     import elf_test
59     import entry_test
60     import fdt_test
61     import ftest
62     import image_test
63     import test
64     import doctest
65
66     result = unittest.TestResult()
67     for module in []:
68         suite = doctest.DocTestSuite(module)
69         suite.run(result)
70
71     sys.argv = [sys.argv[0]]
72     if debug:
73         sys.argv.append('-D')
74
75     # Run the entry tests first ,since these need to be the first to import the
76     # 'entry' module.
77     test_name = args and args[0] or None
78     suite = unittest.TestSuite()
79     loader = unittest.TestLoader()
80     for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
81                    elf_test.TestElf, image_test.TestImage):
82         if test_name:
83             try:
84                 suite.addTests(loader.loadTestsFromName(test_name, module))
85             except AttributeError:
86                 continue
87         else:
88             suite.addTests(loader.loadTestsFromTestCase(module))
89     if use_concurrent and processes != 1:
90         concurrent_suite = ConcurrentTestSuite(suite,
91                 fork_for_tests(processes or multiprocessing.cpu_count()))
92         concurrent_suite.run(result)
93     else:
94         suite.run(result)
95
96     # Remove errors which just indicate a missing test. Since Python v3.5 If an
97     # ImportError or AttributeError occurs while traversing name then a
98     # synthetic test that raises that error when run will be returned. These
99     # errors are included in the errors accumulated by result.errors.
100     if test_name:
101         errors = []
102         for test, err in result.errors:
103             if ("has no attribute '%s'" % test_name) not in err:
104                 errors.append((test, err))
105             result.testsRun -= 1
106         result.errors = errors
107
108     print(result)
109     for test, err in result.errors:
110         print(test.id(), err)
111     for test, err in result.failures:
112         print(err, result.failures)
113     if result.skipped:
114         print('%d binman test%s SKIPPED:' %
115               (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
116         for skip_info in result.skipped:
117             print('%s: %s' % (skip_info[0], skip_info[1]))
118     if result.errors or result.failures:
119         print('binman tests FAILED')
120         return 1
121     return 0
122
123 def GetEntryModules(include_testing=True):
124     """Get a set of entry class implementations
125
126     Returns:
127         Set of paths to entry class filenames
128     """
129     glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
130     return set([os.path.splitext(os.path.basename(item))[0]
131                 for item in glob_list
132                 if include_testing or '_testing' not in item])
133
134 def RunTestCoverage():
135     """Run the tests and check that we get 100% coverage"""
136     glob_list = GetEntryModules(False)
137     all_set = set([os.path.splitext(os.path.basename(item))[0]
138                    for item in glob_list if '_testing' not in item])
139     test_util.RunTestCoverage('tools/binman/binman.py', None,
140             ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
141             options.build_dir, all_set)
142
143 def RunBinman(options, args):
144     """Main entry point to binman once arguments are parsed
145
146     Args:
147         options: Command-line options
148         args: Non-option arguments
149     """
150     ret_code = 0
151
152     if not options.debug:
153         sys.tracebacklimit = 0
154
155     if options.test:
156         ret_code = RunTests(options.debug, options.processes, args[1:])
157
158     elif options.test_coverage:
159         RunTestCoverage()
160
161     elif options.entry_docs:
162         control.WriteEntryDocs(GetEntryModules())
163
164     else:
165         try:
166             ret_code = control.Binman(options, args)
167         except Exception as e:
168             print('binman: %s' % e)
169             if options.debug:
170                 print()
171                 traceback.print_exc()
172             ret_code = 1
173     return ret_code
174
175
176 if __name__ == "__main__":
177     (options, args) = cmdline.ParseArgs(sys.argv)
178     ret_code = RunBinman(options, args)
179     sys.exit(ret_code)