binman.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # SPDX-License-Identifier: GPL-2.0+
  6. #
  7. # Creates binary images from input files controlled by a description
  8. #
  9. """See README for more information"""
  10. import glob
  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', '..']:
  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. # Also allow entry-type modules to be brought in from the etype directory.
  22. sys.path.insert(0, os.path.join(our_path, 'etype'))
  23. import cmdline
  24. import command
  25. import control
  26. def RunTests(debug):
  27. """Run the functional tests and any embedded doctests"""
  28. import elf_test
  29. import entry_test
  30. import fdt_test
  31. import ftest
  32. import image_test
  33. import test
  34. import doctest
  35. result = unittest.TestResult()
  36. for module in []:
  37. suite = doctest.DocTestSuite(module)
  38. suite.run(result)
  39. sys.argv = [sys.argv[0]]
  40. if debug:
  41. sys.argv.append('-D')
  42. # Run the entry tests first ,since these need to be the first to import the
  43. # 'entry' module.
  44. suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
  45. suite.run(result)
  46. for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf,
  47. image_test.TestImage):
  48. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  49. suite.run(result)
  50. print result
  51. for test, err in result.errors:
  52. print test.id(), err
  53. for test, err in result.failures:
  54. print err, result.failures
  55. if result.errors or result.failures:
  56. print 'binman tests FAILED'
  57. return 1
  58. return 0
  59. def RunTestCoverage():
  60. """Run the tests and check that we get 100% coverage"""
  61. # This uses the build output from sandbox_spl to get _libfdt.so
  62. cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
  63. '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
  64. 'tools/binman/binman.py -t' % options.build_dir)
  65. os.system(cmd)
  66. stdout = command.Output('coverage', 'report')
  67. lines = stdout.splitlines()
  68. test_set= set([os.path.basename(line.split()[0])
  69. for line in lines if '/etype/' in line])
  70. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  71. all_set = set([os.path.basename(item) for item in glob_list])
  72. missing_list = all_set
  73. missing_list.difference_update(test_set)
  74. missing_list.remove('_testing.py')
  75. coverage = lines[-1].split(' ')[-1]
  76. ok = True
  77. if missing_list:
  78. print 'Missing tests for %s' % (', '.join(missing_list))
  79. ok = False
  80. if coverage != '100%':
  81. print stdout
  82. print "Type 'coverage html' to get a report in htmlcov/index.html"
  83. print 'Coverage error: %s, but should be 100%%' % coverage
  84. ok = False
  85. if not ok:
  86. raise ValueError('Test coverage failure')
  87. def RunBinman(options, args):
  88. """Main entry point to binman once arguments are parsed
  89. Args:
  90. options: Command-line options
  91. args: Non-option arguments
  92. """
  93. ret_code = 0
  94. # For testing: This enables full exception traces.
  95. #options.debug = True
  96. if not options.debug:
  97. sys.tracebacklimit = 0
  98. if options.test:
  99. ret_code = RunTests(options.debug)
  100. elif options.test_coverage:
  101. RunTestCoverage()
  102. elif options.full_help:
  103. pager = os.getenv('PAGER')
  104. if not pager:
  105. pager = 'more'
  106. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  107. 'README')
  108. command.Run(pager, fname)
  109. else:
  110. try:
  111. ret_code = control.Binman(options, args)
  112. except Exception as e:
  113. print 'binman: %s' % e
  114. if options.debug:
  115. print
  116. traceback.print_exc()
  117. ret_code = 1
  118. return ret_code
  119. if __name__ == "__main__":
  120. (options, args) = cmdline.ParseArgs(sys.argv)
  121. ret_code = RunBinman(options, args)
  122. sys.exit(ret_code)