main.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 os
  11. import site
  12. import sys
  13. import traceback
  14. import unittest
  15. # Bring in the patman and dtoc libraries (but don't override the first path
  16. # in PYTHONPATH)
  17. our_path = os.path.dirname(os.path.realpath(__file__))
  18. sys.path.insert(2, os.path.join(our_path, '..'))
  19. from patman import test_util
  20. # Bring in the libfdt module
  21. sys.path.insert(2, 'scripts/dtc/pylibfdt')
  22. sys.path.insert(2, os.path.join(our_path, '../../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. import doctest
  55. result = unittest.TestResult()
  56. test_name = args and args[0] or None
  57. # Run the entry tests first ,since these need to be the first to import the
  58. # 'entry' module.
  59. test_util.RunTestSuites(
  60. result, debug, verbosity, test_preserve_dirs, processes, test_name,
  61. toolpath,
  62. [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
  63. elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
  64. return test_util.ReportResult('binman', test_name, result)
  65. def RunTestCoverage(toolpath):
  66. """Run the tests and check that we get 100% coverage"""
  67. glob_list = control.GetEntryModules(False)
  68. all_set = set([os.path.splitext(os.path.basename(item))[0]
  69. for item in glob_list if '_testing' not in item])
  70. extra_args = ''
  71. if toolpath:
  72. for path in toolpath:
  73. extra_args += ' --toolpath %s' % path
  74. test_util.RunTestCoverage('tools/binman/binman', None,
  75. ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
  76. args.build_dir, all_set, extra_args or None)
  77. def RunBinman(args):
  78. """Main entry point to binman once arguments are parsed
  79. Args:
  80. args: Command line arguments Namespace object
  81. """
  82. ret_code = 0
  83. if not args.debug:
  84. sys.tracebacklimit = 0
  85. # Provide a default toolpath in the hope of finding a mkimage built from
  86. # current source
  87. if not args.toolpath:
  88. args.toolpath = ['./tools', 'build-sandbox/tools']
  89. if args.cmd == 'test':
  90. if args.test_coverage:
  91. RunTestCoverage(args.toolpath)
  92. else:
  93. ret_code = RunTests(args.debug, args.verbosity, args.processes,
  94. args.test_preserve_dirs, args.tests,
  95. args.toolpath)
  96. elif args.cmd == 'entry-docs':
  97. control.WriteEntryDocs(control.GetEntryModules())
  98. else:
  99. try:
  100. ret_code = control.Binman(args)
  101. except Exception as e:
  102. print('binman: %s' % e, file=sys.stderr)
  103. if args.debug:
  104. print()
  105. traceback.print_exc()
  106. ret_code = 1
  107. return ret_code
  108. if __name__ == "__main__":
  109. args = cmdline.ParseArgs(sys.argv[1:])
  110. ret_code = RunBinman(args)
  111. sys.exit(ret_code)