binman.py 3.1 KB

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