binman.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python2
  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. import glob
  10. import multiprocessing
  11. import os
  12. import sys
  13. import traceback
  14. import unittest
  15. # Bring in the patman and dtoc libraries
  16. our_path = os.path.dirname(os.path.realpath(__file__))
  17. for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
  18. sys.path.insert(0, os.path.join(our_path, dirname))
  19. # Bring in the libfdt module
  20. sys.path.insert(0, 'scripts/dtc/pylibfdt')
  21. sys.path.insert(0, os.path.join(our_path,
  22. '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
  23. import cmdline
  24. import command
  25. use_concurrent = True
  26. try:
  27. from concurrencytest import ConcurrentTestSuite, fork_for_tests
  28. except:
  29. use_concurrent = False
  30. import control
  31. import test_util
  32. def RunTests(debug, processes, args):
  33. """Run the functional tests and any embedded doctests
  34. Args:
  35. debug: True to enable debugging, which shows a full stack trace on error
  36. args: List of positional args provided to binman. This can hold a test
  37. name to execute (as in 'binman -t testSections', for example)
  38. processes: Number of processes to use to run tests (None=same as #CPUs)
  39. """
  40. import elf_test
  41. import entry_test
  42. import fdt_test
  43. import ftest
  44. import image_test
  45. import test
  46. import doctest
  47. result = unittest.TestResult()
  48. for module in []:
  49. suite = doctest.DocTestSuite(module)
  50. suite.run(result)
  51. sys.argv = [sys.argv[0]]
  52. if debug:
  53. sys.argv.append('-D')
  54. if debug:
  55. sys.argv.append('-D')
  56. # Run the entry tests first ,since these need to be the first to import the
  57. # 'entry' module.
  58. test_name = args and args[0] or None
  59. suite = unittest.TestSuite()
  60. loader = unittest.TestLoader()
  61. for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
  62. elf_test.TestElf, image_test.TestImage):
  63. if test_name:
  64. try:
  65. suite.addTests(loader.loadTestsFromName(test_name, module))
  66. except AttributeError:
  67. continue
  68. else:
  69. suite.addTests(loader.loadTestsFromTestCase(module))
  70. if use_concurrent and processes != 1:
  71. concurrent_suite = ConcurrentTestSuite(suite,
  72. fork_for_tests(processes or multiprocessing.cpu_count()))
  73. concurrent_suite.run(result)
  74. else:
  75. suite.run(result)
  76. print result
  77. for test, err in result.errors:
  78. print test.id(), err
  79. for test, err in result.failures:
  80. print err, result.failures
  81. if result.errors or result.failures:
  82. print 'binman tests FAILED'
  83. return 1
  84. return 0
  85. def GetEntryModules(include_testing=True):
  86. """Get a set of entry class implementations
  87. Returns:
  88. Set of paths to entry class filenames
  89. """
  90. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  91. return set([os.path.splitext(os.path.basename(item))[0]
  92. for item in glob_list
  93. if include_testing or '_testing' not in item])
  94. def RunTestCoverage():
  95. """Run the tests and check that we get 100% coverage"""
  96. glob_list = GetEntryModules(False)
  97. all_set = set([os.path.splitext(os.path.basename(item))[0]
  98. for item in glob_list if '_testing' not in item])
  99. test_util.RunTestCoverage('tools/binman/binman.py', None,
  100. ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
  101. options.build_dir, all_set)
  102. def RunBinman(options, args):
  103. """Main entry point to binman once arguments are parsed
  104. Args:
  105. options: Command-line options
  106. args: Non-option arguments
  107. """
  108. ret_code = 0
  109. # For testing: This enables full exception traces.
  110. #options.debug = True
  111. if not options.debug:
  112. sys.tracebacklimit = 0
  113. if options.test:
  114. ret_code = RunTests(options.debug, options.processes, args[1:])
  115. elif options.test_coverage:
  116. RunTestCoverage()
  117. elif options.entry_docs:
  118. control.WriteEntryDocs(GetEntryModules())
  119. else:
  120. try:
  121. ret_code = control.Binman(options, args)
  122. except Exception as e:
  123. print 'binman: %s' % e
  124. if options.debug:
  125. print
  126. traceback.print_exc()
  127. ret_code = 1
  128. return ret_code
  129. if __name__ == "__main__":
  130. (options, args) = cmdline.ParseArgs(sys.argv)
  131. ret_code = RunBinman(options, args)
  132. sys.exit(ret_code)