binman.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # Creates binary images from input files controlled by a description
  7. #
  8. """See README for more information"""
  9. from __future__ import print_function
  10. from distutils.sysconfig import get_python_lib
  11. import glob
  12. import multiprocessing
  13. import os
  14. import site
  15. import sys
  16. import traceback
  17. import unittest
  18. # Bring in the patman and dtoc libraries (but don't override the first path
  19. # in PYTHONPATH)
  20. our_path = os.path.dirname(os.path.realpath(__file__))
  21. for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
  22. sys.path.insert(2, os.path.join(our_path, dirname))
  23. # Bring in the libfdt module
  24. sys.path.insert(2, 'scripts/dtc/pylibfdt')
  25. sys.path.insert(2, os.path.join(our_path,
  26. '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
  27. # When running under python-coverage on Ubuntu 16.04, the dist-packages
  28. # directories are dropped from the python path. Add them in so that we can find
  29. # the elffile module. We could use site.getsitepackages() here but unfortunately
  30. # that is not available in a virtualenv.
  31. sys.path.append(get_python_lib())
  32. import cmdline
  33. import command
  34. use_concurrent = True
  35. try:
  36. from concurrencytest import ConcurrentTestSuite, fork_for_tests
  37. except:
  38. use_concurrent = False
  39. import control
  40. import test_util
  41. def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
  42. """Run the functional tests and any embedded doctests
  43. Args:
  44. debug: True to enable debugging, which shows a full stack trace on error
  45. verbosity: Verbosity level to use
  46. test_preserve_dirs: True to preserve the input directory used by tests
  47. so that it can be examined afterwards (only useful for debugging
  48. tests). If a single test is selected (in args[0]) it also preserves
  49. the output directory for this test. Both directories are displayed
  50. on the command line.
  51. processes: Number of processes to use to run tests (None=same as #CPUs)
  52. args: List of positional args provided to binman. This can hold a test
  53. name to execute (as in 'binman test testSections', for example)
  54. toolpath: List of paths to use for tools
  55. """
  56. import cbfs_util_test
  57. import elf_test
  58. import entry_test
  59. import fdt_test
  60. import ftest
  61. import image_test
  62. import test
  63. import doctest
  64. result = unittest.TestResult()
  65. for module in []:
  66. suite = doctest.DocTestSuite(module)
  67. suite.run(result)
  68. sys.argv = [sys.argv[0]]
  69. if debug:
  70. sys.argv.append('-D')
  71. if verbosity:
  72. sys.argv.append('-v%d' % verbosity)
  73. if toolpath:
  74. for path in toolpath:
  75. sys.argv += ['--toolpath', path]
  76. # Run the entry tests first ,since these need to be the first to import the
  77. # 'entry' module.
  78. test_name = args and args[0] or None
  79. suite = unittest.TestSuite()
  80. loader = unittest.TestLoader()
  81. for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
  82. elf_test.TestElf, image_test.TestImage,
  83. cbfs_util_test.TestCbfs):
  84. # Test the test module about our arguments, if it is interested
  85. if hasattr(module, 'setup_test_args'):
  86. setup_test_args = getattr(module, 'setup_test_args')
  87. setup_test_args(preserve_indir=test_preserve_dirs,
  88. preserve_outdirs=test_preserve_dirs and test_name is not None,
  89. toolpath=toolpath, verbosity=verbosity)
  90. if test_name:
  91. try:
  92. suite.addTests(loader.loadTestsFromName(test_name, module))
  93. except AttributeError:
  94. continue
  95. else:
  96. suite.addTests(loader.loadTestsFromTestCase(module))
  97. if use_concurrent and processes != 1:
  98. concurrent_suite = ConcurrentTestSuite(suite,
  99. fork_for_tests(processes or multiprocessing.cpu_count()))
  100. concurrent_suite.run(result)
  101. else:
  102. suite.run(result)
  103. # Remove errors which just indicate a missing test. Since Python v3.5 If an
  104. # ImportError or AttributeError occurs while traversing name then a
  105. # synthetic test that raises that error when run will be returned. These
  106. # errors are included in the errors accumulated by result.errors.
  107. if test_name:
  108. errors = []
  109. for test, err in result.errors:
  110. if ("has no attribute '%s'" % test_name) not in err:
  111. errors.append((test, err))
  112. result.testsRun -= 1
  113. result.errors = errors
  114. print(result)
  115. for test, err in result.errors:
  116. print(test.id(), err)
  117. for test, err in result.failures:
  118. print(err, result.failures)
  119. if result.skipped:
  120. print('%d binman test%s SKIPPED:' %
  121. (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
  122. for skip_info in result.skipped:
  123. print('%s: %s' % (skip_info[0], skip_info[1]))
  124. if result.errors or result.failures:
  125. print('binman tests FAILED')
  126. return 1
  127. return 0
  128. def GetEntryModules(include_testing=True):
  129. """Get a set of entry class implementations
  130. Returns:
  131. Set of paths to entry class filenames
  132. """
  133. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  134. return set([os.path.splitext(os.path.basename(item))[0]
  135. for item in glob_list
  136. if include_testing or '_testing' not in item])
  137. def RunTestCoverage():
  138. """Run the tests and check that we get 100% coverage"""
  139. glob_list = GetEntryModules(False)
  140. all_set = set([os.path.splitext(os.path.basename(item))[0]
  141. for item in glob_list if '_testing' not in item])
  142. test_util.RunTestCoverage('tools/binman/binman.py', None,
  143. ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
  144. args.build_dir, all_set)
  145. def RunBinman(args):
  146. """Main entry point to binman once arguments are parsed
  147. Args:
  148. args: Command line arguments Namespace object
  149. """
  150. ret_code = 0
  151. if not args.debug:
  152. sys.tracebacklimit = 0
  153. if args.cmd == 'test':
  154. if args.test_coverage:
  155. RunTestCoverage()
  156. else:
  157. ret_code = RunTests(args.debug, args.verbosity, args.processes,
  158. args.test_preserve_dirs, args.tests,
  159. args.toolpath)
  160. elif args.cmd == 'entry-docs':
  161. control.WriteEntryDocs(GetEntryModules())
  162. else:
  163. try:
  164. ret_code = control.Binman(args)
  165. except Exception as e:
  166. print('binman: %s' % e)
  167. if args.debug:
  168. print()
  169. traceback.print_exc()
  170. ret_code = 1
  171. return ret_code
  172. if __name__ == "__main__":
  173. args = cmdline.ParseArgs(sys.argv[1:])
  174. ret_code = RunBinman(args)
  175. sys.exit(ret_code)