binman.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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():
  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 test
  33. import doctest
  34. result = unittest.TestResult()
  35. for module in []:
  36. suite = doctest.DocTestSuite(module)
  37. suite.run(result)
  38. sys.argv = [sys.argv[0]]
  39. # Run the entry tests first ,since these need to be the first to import the
  40. # 'entry' module.
  41. suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
  42. suite.run(result)
  43. for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf):
  44. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  45. suite.run(result)
  46. print result
  47. for test, err in result.errors:
  48. print test.id(), err
  49. for test, err in result.failures:
  50. print err, result.failures
  51. if result.errors or result.failures:
  52. print 'binman tests FAILED'
  53. return 1
  54. return 0
  55. def RunTestCoverage():
  56. """Run the tests and check that we get 100% coverage"""
  57. # This uses the build output from sandbox_spl to get _libfdt.so
  58. cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
  59. '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
  60. 'tools/binman/binman.py -t' % options.build_dir)
  61. os.system(cmd)
  62. stdout = command.Output('coverage', 'report')
  63. lines = stdout.splitlines()
  64. test_set= set([os.path.basename(line.split()[0])
  65. for line in lines if '/etype/' in line])
  66. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  67. all_set = set([os.path.basename(item) for item in glob_list])
  68. missing_list = all_set
  69. missing_list.difference_update(test_set)
  70. missing_list.remove('_testing.py')
  71. coverage = lines[-1].split(' ')[-1]
  72. ok = True
  73. if missing_list:
  74. print 'Missing tests for %s' % (', '.join(missing_list))
  75. ok = False
  76. if coverage != '100%':
  77. print stdout
  78. print "Type 'coverage html' to get a report in htmlcov/index.html"
  79. print 'Coverage error: %s, but should be 100%%' % coverage
  80. ok = False
  81. if not ok:
  82. raise ValueError('Test coverage failure')
  83. def RunBinman(options, args):
  84. """Main entry point to binman once arguments are parsed
  85. Args:
  86. options: Command-line options
  87. args: Non-option arguments
  88. """
  89. ret_code = 0
  90. # For testing: This enables full exception traces.
  91. #options.debug = True
  92. if not options.debug:
  93. sys.tracebacklimit = 0
  94. if options.test:
  95. ret_code = RunTests()
  96. elif options.test_coverage:
  97. RunTestCoverage()
  98. elif options.full_help:
  99. pager = os.getenv('PAGER')
  100. if not pager:
  101. pager = 'more'
  102. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  103. 'README')
  104. command.Run(pager, fname)
  105. else:
  106. try:
  107. ret_code = control.Binman(options, args)
  108. except Exception as e:
  109. print 'binman: %s' % e
  110. if options.debug:
  111. print
  112. traceback.print_exc()
  113. ret_code = 1
  114. return ret_code
  115. if __name__ == "__main__":
  116. (options, args) = cmdline.ParseArgs(sys.argv)
  117. ret_code = RunBinman(options, args)
  118. sys.exit(ret_code)