main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # Creates binary images from input files controlled by a description
  7. #
  8. """See README for more information"""
  9. from distutils.sysconfig import get_python_lib
  10. import glob
  11. import os
  12. import site
  13. import sys
  14. import traceback
  15. import unittest
  16. # Bring in the patman and dtoc libraries (but don't override the first path
  17. # in PYTHONPATH)
  18. our_path = os.path.dirname(os.path.realpath(__file__))
  19. sys.path.insert(2, os.path.join(our_path, '..'))
  20. from patman import test_util
  21. # Bring in the libfdt module
  22. sys.path.insert(2, 'scripts/dtc/pylibfdt')
  23. sys.path.insert(2, os.path.join(our_path,
  24. '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
  25. # When running under python-coverage on Ubuntu 16.04, the dist-packages
  26. # directories are dropped from the python path. Add them in so that we can find
  27. # the elffile module. We could use site.getsitepackages() here but unfortunately
  28. # that is not available in a virtualenv.
  29. sys.path.append(get_python_lib())
  30. from binman import cmdline
  31. from binman import control
  32. from patman import test_util
  33. def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
  34. """Run the functional tests and any embedded doctests
  35. Args:
  36. debug: True to enable debugging, which shows a full stack trace on error
  37. verbosity: Verbosity level to use
  38. test_preserve_dirs: True to preserve the input directory used by tests
  39. so that it can be examined afterwards (only useful for debugging
  40. tests). If a single test is selected (in args[0]) it also preserves
  41. the output directory for this test. Both directories are displayed
  42. on the command line.
  43. processes: Number of processes to use to run tests (None=same as #CPUs)
  44. args: List of positional args provided to binman. This can hold a test
  45. name to execute (as in 'binman test testSections', for example)
  46. toolpath: List of paths to use for tools
  47. """
  48. from binman import cbfs_util_test
  49. from binman import elf_test
  50. from binman import entry_test
  51. from binman import fdt_test
  52. from binman import ftest
  53. from binman import image_test
  54. from binman import test
  55. import doctest
  56. result = unittest.TestResult()
  57. test_name = args and args[0] or None
  58. # Run the entry tests first ,since these need to be the first to import the
  59. # 'entry' module.
  60. test_util.RunTestSuites(
  61. result, debug, verbosity, test_preserve_dirs, processes, test_name,
  62. toolpath,
  63. [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
  64. elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
  65. return test_util.ReportResult('binman', test_name, result)
  66. def GetEntryModules(include_testing=True):
  67. """Get a set of entry class implementations
  68. Returns:
  69. Set of paths to entry class filenames
  70. """
  71. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  72. return set([os.path.splitext(os.path.basename(item))[0]
  73. for item in glob_list
  74. if include_testing or '_testing' not in item])
  75. def RunTestCoverage():
  76. """Run the tests and check that we get 100% coverage"""
  77. glob_list = GetEntryModules(False)
  78. all_set = set([os.path.splitext(os.path.basename(item))[0]
  79. for item in glob_list if '_testing' not in item])
  80. test_util.RunTestCoverage('tools/binman/binman', None,
  81. ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
  82. args.build_dir, all_set)
  83. def RunBinman(args):
  84. """Main entry point to binman once arguments are parsed
  85. Args:
  86. args: Command line arguments Namespace object
  87. """
  88. ret_code = 0
  89. if not args.debug:
  90. sys.tracebacklimit = 0
  91. if args.cmd == 'test':
  92. if args.test_coverage:
  93. RunTestCoverage()
  94. else:
  95. ret_code = RunTests(args.debug, args.verbosity, args.processes,
  96. args.test_preserve_dirs, args.tests,
  97. args.toolpath)
  98. elif args.cmd == 'entry-docs':
  99. control.WriteEntryDocs(GetEntryModules())
  100. else:
  101. try:
  102. ret_code = control.Binman(args)
  103. except Exception as e:
  104. print('binman: %s' % e)
  105. if args.debug:
  106. print()
  107. traceback.print_exc()
  108. ret_code = 1
  109. return ret_code
  110. if __name__ == "__main__":
  111. args = cmdline.ParseArgs(sys.argv[1:])
  112. ret_code = RunBinman(args)
  113. sys.exit(ret_code)