binman.py 3.1 KB

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