Clone of official tools

Committer:
screamer
Date:
Thu Jul 14 20:21:19 2016 +0100
Revision:
13:ab47a20b66f0
Parent:
0:66f3b5499f7f
Child:
22:9e85236d8716
Apply latest tools

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 #! /usr/bin/env python2
screamer 0:66f3b5499f7f 2 """
screamer 0:66f3b5499f7f 3 mbed SDK
screamer 0:66f3b5499f7f 4 Copyright (c) 2011-2013 ARM Limited
screamer 0:66f3b5499f7f 5
screamer 0:66f3b5499f7f 6 Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:66f3b5499f7f 7 you may not use this file except in compliance with the License.
screamer 0:66f3b5499f7f 8 You may obtain a copy of the License at
screamer 0:66f3b5499f7f 9
screamer 0:66f3b5499f7f 10 http://www.apache.org/licenses/LICENSE-2.0
screamer 0:66f3b5499f7f 11
screamer 0:66f3b5499f7f 12 Unless required by applicable law or agreed to in writing, software
screamer 0:66f3b5499f7f 13 distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:66f3b5499f7f 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:66f3b5499f7f 15 See the License for the specific language governing permissions and
screamer 0:66f3b5499f7f 16 limitations under the License.
screamer 0:66f3b5499f7f 17
screamer 0:66f3b5499f7f 18
screamer 0:66f3b5499f7f 19 TEST BUILD & RUN
screamer 0:66f3b5499f7f 20 """
screamer 0:66f3b5499f7f 21 import sys
screamer 0:66f3b5499f7f 22 import os
screamer 0:66f3b5499f7f 23 import json
screamer 13:ab47a20b66f0 24 import fnmatch
screamer 0:66f3b5499f7f 25
screamer 0:66f3b5499f7f 26 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
screamer 0:66f3b5499f7f 27 sys.path.insert(0, ROOT)
screamer 0:66f3b5499f7f 28
screamer 13:ab47a20b66f0 29 from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds
screamer 0:66f3b5499f7f 30 from tools.options import get_default_options_parser
screamer 0:66f3b5499f7f 31 from tools.build_api import build_project, build_library
screamer 0:66f3b5499f7f 32 from tools.targets import TARGET_MAP
screamer 13:ab47a20b66f0 33 from tools.utils import mkdir, ToolException, NotSupportedException
screamer 13:ab47a20b66f0 34 from tools.test_exporters import ReportExporter, ResultExporterType
screamer 0:66f3b5499f7f 35
screamer 0:66f3b5499f7f 36 if __name__ == '__main__':
screamer 0:66f3b5499f7f 37 try:
screamer 0:66f3b5499f7f 38 # Parse Options
screamer 0:66f3b5499f7f 39 parser = get_default_options_parser()
screamer 0:66f3b5499f7f 40
screamer 13:ab47a20b66f0 41 parser.add_option("-D", "",
screamer 13:ab47a20b66f0 42 action="append",
screamer 13:ab47a20b66f0 43 dest="macros",
screamer 13:ab47a20b66f0 44 help="Add a macro definition")
screamer 13:ab47a20b66f0 45
screamer 0:66f3b5499f7f 46 parser.add_option("-j", "--jobs",
screamer 0:66f3b5499f7f 47 type="int",
screamer 0:66f3b5499f7f 48 dest="jobs",
screamer 13:ab47a20b66f0 49 default=0,
screamer 13:ab47a20b66f0 50 help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
screamer 0:66f3b5499f7f 51
screamer 0:66f3b5499f7f 52 parser.add_option("--source", dest="source_dir",
screamer 0:66f3b5499f7f 53 default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
screamer 0:66f3b5499f7f 54
screamer 0:66f3b5499f7f 55 parser.add_option("--build", dest="build_dir",
screamer 0:66f3b5499f7f 56 default=None, help="The build (output) directory")
screamer 0:66f3b5499f7f 57
screamer 0:66f3b5499f7f 58 parser.add_option("-l", "--list", action="store_true", dest="list",
screamer 0:66f3b5499f7f 59 default=False, help="List (recursively) available tests in order and exit")
screamer 0:66f3b5499f7f 60
screamer 0:66f3b5499f7f 61 parser.add_option("-p", "--paths", dest="paths",
screamer 0:66f3b5499f7f 62 default=None, help="Limit the tests to those within the specified comma separated list of paths")
screamer 0:66f3b5499f7f 63
screamer 0:66f3b5499f7f 64 format_choices = ["list", "json"]
screamer 0:66f3b5499f7f 65 format_default_choice = "list"
screamer 0:66f3b5499f7f 66 format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice)
screamer 0:66f3b5499f7f 67 parser.add_option("-f", "--format", type="choice", dest="format",
screamer 0:66f3b5499f7f 68 choices=format_choices, default=format_default_choice, help=format_help)
screamer 0:66f3b5499f7f 69
screamer 13:ab47a20b66f0 70 parser.add_option("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
screamer 13:ab47a20b66f0 71 default=None, help="Continue trying to build all tests if a build failure occurs")
screamer 13:ab47a20b66f0 72
screamer 0:66f3b5499f7f 73 parser.add_option("-n", "--names", dest="names",
screamer 0:66f3b5499f7f 74 default=None, help="Limit the tests to a comma separated list of names")
screamer 0:66f3b5499f7f 75
screamer 0:66f3b5499f7f 76 parser.add_option("--test-spec", dest="test_spec",
screamer 0:66f3b5499f7f 77 default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
screamer 0:66f3b5499f7f 78
screamer 13:ab47a20b66f0 79 parser.add_option("--build-report-junit", dest="build_report_junit",
screamer 13:ab47a20b66f0 80 default=None, help="Destination path for a build report in the JUnit xml format")
screamer 13:ab47a20b66f0 81
screamer 0:66f3b5499f7f 82 parser.add_option("-v", "--verbose",
screamer 0:66f3b5499f7f 83 action="store_true",
screamer 0:66f3b5499f7f 84 dest="verbose",
screamer 0:66f3b5499f7f 85 default=False,
screamer 0:66f3b5499f7f 86 help="Verbose diagnostic output")
screamer 0:66f3b5499f7f 87
screamer 0:66f3b5499f7f 88 (options, args) = parser.parse_args()
screamer 0:66f3b5499f7f 89
screamer 0:66f3b5499f7f 90 # Filter tests by path if specified
screamer 0:66f3b5499f7f 91 if options.paths:
screamer 0:66f3b5499f7f 92 all_paths = options.paths.split(",")
screamer 0:66f3b5499f7f 93 else:
screamer 0:66f3b5499f7f 94 all_paths = ["."]
screamer 0:66f3b5499f7f 95
screamer 0:66f3b5499f7f 96 all_tests = {}
screamer 0:66f3b5499f7f 97 tests = {}
screamer 0:66f3b5499f7f 98
screamer 0:66f3b5499f7f 99 # Find all tests in the relevant paths
screamer 0:66f3b5499f7f 100 for path in all_paths:
screamer 0:66f3b5499f7f 101 all_tests.update(find_tests(path))
screamer 0:66f3b5499f7f 102
screamer 0:66f3b5499f7f 103 # Filter tests by name if specified
screamer 0:66f3b5499f7f 104 if options.names:
screamer 0:66f3b5499f7f 105 all_names = options.names.split(",")
screamer 13:ab47a20b66f0 106 all_names = [x.lower() for x in all_names]
screamer 0:66f3b5499f7f 107
screamer 0:66f3b5499f7f 108 for name in all_names:
screamer 13:ab47a20b66f0 109 if any(fnmatch.fnmatch(testname, name) for testname in all_tests):
screamer 13:ab47a20b66f0 110 for testname, test in all_tests.items():
screamer 13:ab47a20b66f0 111 if fnmatch.fnmatch(testname, name):
screamer 13:ab47a20b66f0 112 tests[testname] = test
screamer 0:66f3b5499f7f 113 else:
screamer 0:66f3b5499f7f 114 print "[Warning] Test with name '%s' was not found in the available tests" % (name)
screamer 0:66f3b5499f7f 115 else:
screamer 0:66f3b5499f7f 116 tests = all_tests
screamer 0:66f3b5499f7f 117
screamer 0:66f3b5499f7f 118 if options.list:
screamer 0:66f3b5499f7f 119 # Print available tests in order and exit
screamer 0:66f3b5499f7f 120 print_tests(tests, options.format)
screamer 13:ab47a20b66f0 121 sys.exit(0)
screamer 0:66f3b5499f7f 122 else:
screamer 0:66f3b5499f7f 123 # Build all tests
screamer 0:66f3b5499f7f 124 if not options.build_dir:
screamer 0:66f3b5499f7f 125 print "[ERROR] You must specify a build path"
screamer 0:66f3b5499f7f 126 sys.exit(1)
screamer 0:66f3b5499f7f 127
screamer 0:66f3b5499f7f 128 base_source_paths = options.source_dir
screamer 0:66f3b5499f7f 129
screamer 0:66f3b5499f7f 130 # Default base source path is the current directory
screamer 0:66f3b5499f7f 131 if not base_source_paths:
screamer 0:66f3b5499f7f 132 base_source_paths = ['.']
screamer 0:66f3b5499f7f 133
screamer 0:66f3b5499f7f 134
screamer 13:ab47a20b66f0 135 target = options.mcu
screamer 0:66f3b5499f7f 136
screamer 13:ab47a20b66f0 137 build_report = {}
screamer 13:ab47a20b66f0 138 build_properties = {}
screamer 13:ab47a20b66f0 139
screamer 13:ab47a20b66f0 140 library_build_success = False
screamer 13:ab47a20b66f0 141 try:
screamer 13:ab47a20b66f0 142 # Build sources
screamer 13:ab47a20b66f0 143 build_library(base_source_paths, options.build_dir, target, options.tool,
screamer 13:ab47a20b66f0 144 options=options.options,
screamer 13:ab47a20b66f0 145 jobs=options.jobs,
screamer 13:ab47a20b66f0 146 clean=options.clean,
screamer 13:ab47a20b66f0 147 report=build_report,
screamer 13:ab47a20b66f0 148 properties=build_properties,
screamer 13:ab47a20b66f0 149 name="mbed-build",
screamer 13:ab47a20b66f0 150 macros=options.macros,
screamer 13:ab47a20b66f0 151 verbose=options.verbose,
screamer 13:ab47a20b66f0 152 archive=False)
screamer 13:ab47a20b66f0 153
screamer 13:ab47a20b66f0 154 library_build_success = True
screamer 13:ab47a20b66f0 155 except ToolException, e:
screamer 13:ab47a20b66f0 156 # ToolException output is handled by the build log
screamer 13:ab47a20b66f0 157 pass
screamer 13:ab47a20b66f0 158 except NotSupportedException, e:
screamer 13:ab47a20b66f0 159 # NotSupportedException is handled by the build log
screamer 13:ab47a20b66f0 160 pass
screamer 13:ab47a20b66f0 161 except Exception, e:
screamer 13:ab47a20b66f0 162 # Some other exception occurred, print the error message
screamer 13:ab47a20b66f0 163 print e
screamer 0:66f3b5499f7f 164
screamer 13:ab47a20b66f0 165 if not library_build_success:
screamer 13:ab47a20b66f0 166 print "Failed to build library"
screamer 13:ab47a20b66f0 167 else:
screamer 13:ab47a20b66f0 168 # Build all the tests
screamer 13:ab47a20b66f0 169 test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, target, options.tool,
screamer 13:ab47a20b66f0 170 options=options.options,
screamer 13:ab47a20b66f0 171 clean=options.clean,
screamer 13:ab47a20b66f0 172 report=build_report,
screamer 13:ab47a20b66f0 173 properties=build_properties,
screamer 13:ab47a20b66f0 174 macros=options.macros,
screamer 13:ab47a20b66f0 175 verbose=options.verbose,
screamer 13:ab47a20b66f0 176 jobs=options.jobs,
screamer 13:ab47a20b66f0 177 continue_on_build_fail=options.continue_on_build_fail)
screamer 0:66f3b5499f7f 178
screamer 13:ab47a20b66f0 179 # If a path to a test spec is provided, write it to a file
screamer 13:ab47a20b66f0 180 if options.test_spec:
screamer 13:ab47a20b66f0 181 test_spec_data = test_spec_from_test_builds(test_build)
screamer 13:ab47a20b66f0 182
screamer 13:ab47a20b66f0 183 # Create the target dir for the test spec if necessary
screamer 13:ab47a20b66f0 184 # mkdir will not create the dir if it already exists
screamer 13:ab47a20b66f0 185 test_spec_dir = os.path.dirname(options.test_spec)
screamer 13:ab47a20b66f0 186 if test_spec_dir:
screamer 13:ab47a20b66f0 187 mkdir(test_spec_dir)
screamer 13:ab47a20b66f0 188
screamer 13:ab47a20b66f0 189 try:
screamer 13:ab47a20b66f0 190 with open(options.test_spec, 'w') as f:
screamer 13:ab47a20b66f0 191 f.write(json.dumps(test_spec_data, indent=2))
screamer 13:ab47a20b66f0 192 except IOError, e:
screamer 13:ab47a20b66f0 193 print "[ERROR] Error writing test spec to file"
screamer 13:ab47a20b66f0 194 print e
screamer 0:66f3b5499f7f 195
screamer 13:ab47a20b66f0 196 # If a path to a JUnit build report spec is provided, write it to a file
screamer 13:ab47a20b66f0 197 if options.build_report_junit:
screamer 13:ab47a20b66f0 198 report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build")
screamer 13:ab47a20b66f0 199 report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
screamer 13:ab47a20b66f0 200
screamer 13:ab47a20b66f0 201 print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
screamer 13:ab47a20b66f0 202 status = print_report_exporter.report(build_report)
screamer 13:ab47a20b66f0 203
screamer 13:ab47a20b66f0 204 if status:
screamer 13:ab47a20b66f0 205 sys.exit(0)
screamer 13:ab47a20b66f0 206 else:
screamer 13:ab47a20b66f0 207 sys.exit(1)
screamer 0:66f3b5499f7f 208
screamer 0:66f3b5499f7f 209 except KeyboardInterrupt, e:
screamer 0:66f3b5499f7f 210 print "\n[CTRL+c] exit"
screamer 0:66f3b5499f7f 211 except Exception,e:
screamer 0:66f3b5499f7f 212 import traceback
screamer 0:66f3b5499f7f 213 traceback.print_exc(file=sys.stdout)
screamer 0:66f3b5499f7f 214 print "[ERROR] %s" % str(e)
screamer 0:66f3b5499f7f 215 sys.exit(1)