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