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