Jörg Krause | cd6d46e | 2017-03-06 21:07:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 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 | |
| 13 | import os |
| 14 | import sys |
| 15 | import traceback |
| 16 | import unittest |
| 17 | |
| 18 | # Bring in the patman and dtoc libraries |
| 19 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | dde3e71 | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 20 | for dirname in ['../patman', '../dtoc', '..']: |
| 21 | sys.path.insert(0, os.path.join(our_path, dirname)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 22 | |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 23 | # Bring in the libfdt module |
Masahiro Yamada | 47ae539 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 24 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 25 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 26 | # Also allow entry-type modules to be brought in from the etype directory. |
Simon Glass | dde3e71 | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 27 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 28 | |
| 29 | import cmdline |
| 30 | import command |
| 31 | import control |
| 32 | |
| 33 | def RunTests(): |
| 34 | """Run the functional tests and any embedded doctests""" |
| 35 | import entry_test |
| 36 | import fdt_test |
Simon Glass | 076e63b | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 37 | import ftest |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 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]] |
Simon Glass | 076e63b | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 47 | for module in (ftest.TestFunctional, fdt_test.TestFdt, |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 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 | |
| 58 | def 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 |
Simon Glass | c303b9a | 2017-11-12 21:52:19 -0700 | [diff] [blame^] | 61 | cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run ' |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 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 | |
| 73 | def 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 | |
| 114 | if __name__ == "__main__": |
| 115 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 116 | ret_code = RunBinman(options, args) |
| 117 | sys.exit(ret_code) |