binman.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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. # Also allow entry-type modules to be brought in from the etype directory.
  21. sys.path.insert(0, os.path.join(our_path, 'etype'))
  22. import cmdline
  23. import command
  24. import control
  25. def RunTests():
  26. """Run the functional tests and any embedded doctests"""
  27. import entry_test
  28. import fdt_test
  29. import ftest
  30. import test
  31. import doctest
  32. result = unittest.TestResult()
  33. for module in []:
  34. suite = doctest.DocTestSuite(module)
  35. suite.run(result)
  36. sys.argv = [sys.argv[0]]
  37. for module in (ftest.TestFunctional, fdt_test.TestFdt,
  38. entry_test.TestEntry):
  39. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  40. suite.run(result)
  41. print result
  42. for test, err in result.errors:
  43. print test.id(), err
  44. for test, err in result.failures:
  45. print err
  46. def RunTestCoverage():
  47. """Run the tests and check that we get 100% coverage"""
  48. # This uses the build output from sandbox_spl to get _libfdt.so
  49. cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
  50. '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
  51. 'tools/binman/binman.py -t' % options.build_dir)
  52. os.system(cmd)
  53. stdout = command.Output('coverage', 'report')
  54. coverage = stdout.splitlines()[-1].split(' ')[-1]
  55. if coverage != '100%':
  56. print stdout
  57. print "Type 'coverage html' to get a report in htmlcov/index.html"
  58. raise ValueError('Coverage error: %s, but should be 100%%' % coverage)
  59. def RunBinman(options, args):
  60. """Main entry point to binman once arguments are parsed
  61. Args:
  62. options: Command-line options
  63. args: Non-option arguments
  64. """
  65. ret_code = 0
  66. # For testing: This enables full exception traces.
  67. #options.debug = True
  68. if not options.debug:
  69. sys.tracebacklimit = 0
  70. if options.test:
  71. RunTests()
  72. elif options.test_coverage:
  73. RunTestCoverage()
  74. elif options.full_help:
  75. pager = os.getenv('PAGER')
  76. if not pager:
  77. pager = 'more'
  78. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  79. 'README')
  80. command.Run(pager, fname)
  81. else:
  82. try:
  83. ret_code = control.Binman(options, args)
  84. except Exception as e:
  85. print 'binman: %s' % e
  86. if options.debug:
  87. print
  88. traceback.print_exc()
  89. ret_code = 1
  90. return ret_code
  91. if __name__ == "__main__":
  92. (options, args) = cmdline.ParseArgs(sys.argv)
  93. ret_code = RunBinman(options, args)
  94. sys.exit(ret_code)