binman.py 3.7 KB

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