binman.py 4.0 KB

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