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