xref: /openbmc/u-boot/tools/binman/binman.py (revision d9b23e26)
1#!/usr/bin/env python2
2
3# Copyright (c) 2016 Google, Inc
4# Written by Simon Glass <sjg@chromium.org>
5#
6# SPDX-License-Identifier:	GPL-2.0+
7#
8# Creates binary images from input files controlled by a description
9#
10
11"""See README for more information"""
12
13import os
14import sys
15import traceback
16import unittest
17
18# Bring in the patman and dtoc libraries
19our_path = os.path.dirname(os.path.realpath(__file__))
20for dirname in ['../patman', '../dtoc', '..']:
21    sys.path.insert(0, os.path.join(our_path, dirname))
22
23# Bring in the libfdt module
24sys.path.insert(0, 'tools')
25
26# Also allow entry-type modules to be brought in from the etype directory.
27sys.path.insert(0, os.path.join(our_path, 'etype'))
28
29import cmdline
30import command
31import control
32
33def RunTests():
34    """Run the functional tests and any embedded doctests"""
35    import entry_test
36    import fdt_test
37    import func_test
38    import test
39    import doctest
40
41    result = unittest.TestResult()
42    for module in []:
43        suite = doctest.DocTestSuite(module)
44        suite.run(result)
45
46    sys.argv = [sys.argv[0]]
47    for module in (func_test.TestFunctional, fdt_test.TestFdt,
48                   entry_test.TestEntry):
49        suite = unittest.TestLoader().loadTestsFromTestCase(module)
50        suite.run(result)
51
52    print result
53    for test, err in result.errors:
54        print test.id(), err
55    for test, err in result.failures:
56        print err
57
58def RunTestCoverage():
59    """Run the tests and check that we get 100% coverage"""
60    # This uses the build output from sandbox_spl to get _libfdt.so
61    cmd = ('PYTHONPATH=%s/sandbox_spl/tools coverage run '
62            '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
63            'tools/binman/binman.py -t' % options.build_dir)
64    os.system(cmd)
65    stdout = command.Output('coverage', 'report')
66    coverage = stdout.splitlines()[-1].split(' ')[-1]
67    if coverage != '100%':
68        print stdout
69        print "Type 'coverage html' to get a report in htmlcov/index.html"
70        raise ValueError('Coverage error: %s, but should be 100%%' % coverage)
71
72
73def RunBinman(options, args):
74    """Main entry point to binman once arguments are parsed
75
76    Args:
77        options: Command-line options
78        args: Non-option arguments
79    """
80    ret_code = 0
81
82    # For testing: This enables full exception traces.
83    #options.debug = True
84
85    if not options.debug:
86        sys.tracebacklimit = 0
87
88    if options.test:
89        RunTests()
90
91    elif options.test_coverage:
92        RunTestCoverage()
93
94    elif options.full_help:
95        pager = os.getenv('PAGER')
96        if not pager:
97            pager = 'more'
98        fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
99                            'README')
100        command.Run(pager, fname)
101
102    else:
103        try:
104            ret_code = control.Binman(options, args)
105        except Exception as e:
106            print 'binman: %s' % e
107            if options.debug:
108                print
109                traceback.print_exc()
110            ret_code = 1
111    return ret_code
112
113
114if __name__ == "__main__":
115    (options, args) = cmdline.ParseArgs(sys.argv)
116    ret_code = RunBinman(options, args)
117    sys.exit(ret_code)
118