5.2.1 - Updated I2C files
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
tools/test.py@0:098463de4c5d, 2017-01-25 (annotated)
- Committer:
- group-onsemi
- Date:
- Wed Jan 25 20:34:15 2017 +0000
- Revision:
- 0:098463de4c5d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-onsemi | 0:098463de4c5d | 1 | #! /usr/bin/env python2 |
group-onsemi | 0:098463de4c5d | 2 | """ |
group-onsemi | 0:098463de4c5d | 3 | mbed SDK |
group-onsemi | 0:098463de4c5d | 4 | Copyright (c) 2011-2013 ARM Limited |
group-onsemi | 0:098463de4c5d | 5 | |
group-onsemi | 0:098463de4c5d | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
group-onsemi | 0:098463de4c5d | 7 | you may not use this file except in compliance with the License. |
group-onsemi | 0:098463de4c5d | 8 | You may obtain a copy of the License at |
group-onsemi | 0:098463de4c5d | 9 | |
group-onsemi | 0:098463de4c5d | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
group-onsemi | 0:098463de4c5d | 11 | |
group-onsemi | 0:098463de4c5d | 12 | Unless required by applicable law or agreed to in writing, software |
group-onsemi | 0:098463de4c5d | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
group-onsemi | 0:098463de4c5d | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-onsemi | 0:098463de4c5d | 15 | See the License for the specific language governing permissions and |
group-onsemi | 0:098463de4c5d | 16 | limitations under the License. |
group-onsemi | 0:098463de4c5d | 17 | |
group-onsemi | 0:098463de4c5d | 18 | |
group-onsemi | 0:098463de4c5d | 19 | TEST BUILD & RUN |
group-onsemi | 0:098463de4c5d | 20 | """ |
group-onsemi | 0:098463de4c5d | 21 | import sys |
group-onsemi | 0:098463de4c5d | 22 | import os |
group-onsemi | 0:098463de4c5d | 23 | import json |
group-onsemi | 0:098463de4c5d | 24 | import fnmatch |
group-onsemi | 0:098463de4c5d | 25 | |
group-onsemi | 0:098463de4c5d | 26 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
group-onsemi | 0:098463de4c5d | 27 | sys.path.insert(0, ROOT) |
group-onsemi | 0:098463de4c5d | 28 | |
group-onsemi | 0:098463de4c5d | 29 | from tools.config import ConfigException |
group-onsemi | 0:098463de4c5d | 30 | from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds |
group-onsemi | 0:098463de4c5d | 31 | from tools.options import get_default_options_parser, extract_profile |
group-onsemi | 0:098463de4c5d | 32 | from tools.build_api import build_project, build_library |
group-onsemi | 0:098463de4c5d | 33 | from tools.build_api import print_build_memory_usage |
group-onsemi | 0:098463de4c5d | 34 | from tools.targets import TARGET_MAP |
group-onsemi | 0:098463de4c5d | 35 | from tools.utils import mkdir, ToolException, NotSupportedException, args_error |
group-onsemi | 0:098463de4c5d | 36 | from tools.test_exporters import ReportExporter, ResultExporterType |
group-onsemi | 0:098463de4c5d | 37 | from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many |
group-onsemi | 0:098463de4c5d | 38 | from utils import argparse_dir_not_parent |
group-onsemi | 0:098463de4c5d | 39 | from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS, TOOLCHAIN_CLASSES |
group-onsemi | 0:098463de4c5d | 40 | from tools.settings import CLI_COLOR_MAP |
group-onsemi | 0:098463de4c5d | 41 | |
group-onsemi | 0:098463de4c5d | 42 | if __name__ == '__main__': |
group-onsemi | 0:098463de4c5d | 43 | try: |
group-onsemi | 0:098463de4c5d | 44 | # Parse Options |
group-onsemi | 0:098463de4c5d | 45 | parser = get_default_options_parser(add_app_config=True) |
group-onsemi | 0:098463de4c5d | 46 | |
group-onsemi | 0:098463de4c5d | 47 | parser.add_argument("-D", |
group-onsemi | 0:098463de4c5d | 48 | action="append", |
group-onsemi | 0:098463de4c5d | 49 | dest="macros", |
group-onsemi | 0:098463de4c5d | 50 | help="Add a macro definition") |
group-onsemi | 0:098463de4c5d | 51 | |
group-onsemi | 0:098463de4c5d | 52 | parser.add_argument("-j", "--jobs", |
group-onsemi | 0:098463de4c5d | 53 | type=int, |
group-onsemi | 0:098463de4c5d | 54 | dest="jobs", |
group-onsemi | 0:098463de4c5d | 55 | default=0, |
group-onsemi | 0:098463de4c5d | 56 | help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") |
group-onsemi | 0:098463de4c5d | 57 | |
group-onsemi | 0:098463de4c5d | 58 | parser.add_argument("--source", dest="source_dir", |
group-onsemi | 0:098463de4c5d | 59 | type=argparse_filestring_type, |
group-onsemi | 0:098463de4c5d | 60 | default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") |
group-onsemi | 0:098463de4c5d | 61 | |
group-onsemi | 0:098463de4c5d | 62 | parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT), |
group-onsemi | 0:098463de4c5d | 63 | default=None, help="The build (output) directory") |
group-onsemi | 0:098463de4c5d | 64 | |
group-onsemi | 0:098463de4c5d | 65 | parser.add_argument("-l", "--list", action="store_true", dest="list", |
group-onsemi | 0:098463de4c5d | 66 | default=False, help="List (recursively) available tests in order and exit") |
group-onsemi | 0:098463de4c5d | 67 | |
group-onsemi | 0:098463de4c5d | 68 | parser.add_argument("-p", "--paths", dest="paths", |
group-onsemi | 0:098463de4c5d | 69 | type=argparse_many(argparse_filestring_type), |
group-onsemi | 0:098463de4c5d | 70 | default=None, help="Limit the tests to those within the specified comma separated list of paths") |
group-onsemi | 0:098463de4c5d | 71 | |
group-onsemi | 0:098463de4c5d | 72 | format_choices = ["list", "json"] |
group-onsemi | 0:098463de4c5d | 73 | format_default_choice = "list" |
group-onsemi | 0:098463de4c5d | 74 | format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) |
group-onsemi | 0:098463de4c5d | 75 | parser.add_argument("-f", "--format", dest="format", |
group-onsemi | 0:098463de4c5d | 76 | type=argparse_lowercase_type(format_choices, "format"), |
group-onsemi | 0:098463de4c5d | 77 | default=format_default_choice, help=format_help) |
group-onsemi | 0:098463de4c5d | 78 | |
group-onsemi | 0:098463de4c5d | 79 | parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail", |
group-onsemi | 0:098463de4c5d | 80 | default=None, help="Continue trying to build all tests if a build failure occurs") |
group-onsemi | 0:098463de4c5d | 81 | |
group-onsemi | 0:098463de4c5d | 82 | #TODO validate the names instead of just passing through str |
group-onsemi | 0:098463de4c5d | 83 | parser.add_argument("-n", "--names", dest="names", type=argparse_many(str), |
group-onsemi | 0:098463de4c5d | 84 | default=None, help="Limit the tests to a comma separated list of names") |
group-onsemi | 0:098463de4c5d | 85 | |
group-onsemi | 0:098463de4c5d | 86 | parser.add_argument("--test-spec", dest="test_spec", |
group-onsemi | 0:098463de4c5d | 87 | default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") |
group-onsemi | 0:098463de4c5d | 88 | |
group-onsemi | 0:098463de4c5d | 89 | parser.add_argument("--build-report-junit", dest="build_report_junit", |
group-onsemi | 0:098463de4c5d | 90 | default=None, help="Destination path for a build report in the JUnit xml format") |
group-onsemi | 0:098463de4c5d | 91 | |
group-onsemi | 0:098463de4c5d | 92 | parser.add_argument("-v", "--verbose", |
group-onsemi | 0:098463de4c5d | 93 | action="store_true", |
group-onsemi | 0:098463de4c5d | 94 | dest="verbose", |
group-onsemi | 0:098463de4c5d | 95 | default=False, |
group-onsemi | 0:098463de4c5d | 96 | help="Verbose diagnostic output") |
group-onsemi | 0:098463de4c5d | 97 | |
group-onsemi | 0:098463de4c5d | 98 | options = parser.parse_args() |
group-onsemi | 0:098463de4c5d | 99 | |
group-onsemi | 0:098463de4c5d | 100 | # Filter tests by path if specified |
group-onsemi | 0:098463de4c5d | 101 | if options.paths: |
group-onsemi | 0:098463de4c5d | 102 | all_paths = options.paths |
group-onsemi | 0:098463de4c5d | 103 | else: |
group-onsemi | 0:098463de4c5d | 104 | all_paths = ["."] |
group-onsemi | 0:098463de4c5d | 105 | |
group-onsemi | 0:098463de4c5d | 106 | all_tests = {} |
group-onsemi | 0:098463de4c5d | 107 | tests = {} |
group-onsemi | 0:098463de4c5d | 108 | |
group-onsemi | 0:098463de4c5d | 109 | # Target |
group-onsemi | 0:098463de4c5d | 110 | if options.mcu is None : |
group-onsemi | 0:098463de4c5d | 111 | args_error(parser, "argument -m/--mcu is required") |
group-onsemi | 0:098463de4c5d | 112 | mcu = options.mcu[0] |
group-onsemi | 0:098463de4c5d | 113 | |
group-onsemi | 0:098463de4c5d | 114 | # Toolchain |
group-onsemi | 0:098463de4c5d | 115 | if options.tool is None: |
group-onsemi | 0:098463de4c5d | 116 | args_error(parser, "argument -t/--tool is required") |
group-onsemi | 0:098463de4c5d | 117 | toolchain = options.tool[0] |
group-onsemi | 0:098463de4c5d | 118 | |
group-onsemi | 0:098463de4c5d | 119 | if not TOOLCHAIN_CLASSES[toolchain].check_executable(): |
group-onsemi | 0:098463de4c5d | 120 | search_path = TOOLCHAIN_PATHS[toolchain] or "No path set" |
group-onsemi | 0:098463de4c5d | 121 | args_error(parser, "Could not find executable for %s.\n" |
group-onsemi | 0:098463de4c5d | 122 | "Currently set search path: %s" |
group-onsemi | 0:098463de4c5d | 123 | % (toolchain, search_path)) |
group-onsemi | 0:098463de4c5d | 124 | |
group-onsemi | 0:098463de4c5d | 125 | # Find all tests in the relevant paths |
group-onsemi | 0:098463de4c5d | 126 | for path in all_paths: |
group-onsemi | 0:098463de4c5d | 127 | all_tests.update(find_tests(path, mcu, toolchain, |
group-onsemi | 0:098463de4c5d | 128 | app_config=options.app_config)) |
group-onsemi | 0:098463de4c5d | 129 | |
group-onsemi | 0:098463de4c5d | 130 | # Filter tests by name if specified |
group-onsemi | 0:098463de4c5d | 131 | if options.names: |
group-onsemi | 0:098463de4c5d | 132 | all_names = options.names |
group-onsemi | 0:098463de4c5d | 133 | all_names = [x.lower() for x in all_names] |
group-onsemi | 0:098463de4c5d | 134 | |
group-onsemi | 0:098463de4c5d | 135 | for name in all_names: |
group-onsemi | 0:098463de4c5d | 136 | if any(fnmatch.fnmatch(testname, name) for testname in all_tests): |
group-onsemi | 0:098463de4c5d | 137 | for testname, test in all_tests.items(): |
group-onsemi | 0:098463de4c5d | 138 | if fnmatch.fnmatch(testname, name): |
group-onsemi | 0:098463de4c5d | 139 | tests[testname] = test |
group-onsemi | 0:098463de4c5d | 140 | else: |
group-onsemi | 0:098463de4c5d | 141 | print "[Warning] Test with name '%s' was not found in the available tests" % (name) |
group-onsemi | 0:098463de4c5d | 142 | else: |
group-onsemi | 0:098463de4c5d | 143 | tests = all_tests |
group-onsemi | 0:098463de4c5d | 144 | |
group-onsemi | 0:098463de4c5d | 145 | if options.color: |
group-onsemi | 0:098463de4c5d | 146 | # This import happens late to prevent initializing colorization when we don't need it |
group-onsemi | 0:098463de4c5d | 147 | import colorize |
group-onsemi | 0:098463de4c5d | 148 | if options.verbose: |
group-onsemi | 0:098463de4c5d | 149 | notify = mbedToolchain.print_notify_verbose |
group-onsemi | 0:098463de4c5d | 150 | else: |
group-onsemi | 0:098463de4c5d | 151 | notify = mbedToolchain.print_notify |
group-onsemi | 0:098463de4c5d | 152 | notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify) |
group-onsemi | 0:098463de4c5d | 153 | else: |
group-onsemi | 0:098463de4c5d | 154 | notify = None |
group-onsemi | 0:098463de4c5d | 155 | |
group-onsemi | 0:098463de4c5d | 156 | if options.list: |
group-onsemi | 0:098463de4c5d | 157 | # Print available tests in order and exit |
group-onsemi | 0:098463de4c5d | 158 | print_tests(tests, options.format) |
group-onsemi | 0:098463de4c5d | 159 | sys.exit(0) |
group-onsemi | 0:098463de4c5d | 160 | else: |
group-onsemi | 0:098463de4c5d | 161 | # Build all tests |
group-onsemi | 0:098463de4c5d | 162 | if not options.build_dir: |
group-onsemi | 0:098463de4c5d | 163 | args_error(parser, "argument --build is required") |
group-onsemi | 0:098463de4c5d | 164 | |
group-onsemi | 0:098463de4c5d | 165 | base_source_paths = options.source_dir |
group-onsemi | 0:098463de4c5d | 166 | |
group-onsemi | 0:098463de4c5d | 167 | # Default base source path is the current directory |
group-onsemi | 0:098463de4c5d | 168 | if not base_source_paths: |
group-onsemi | 0:098463de4c5d | 169 | base_source_paths = ['.'] |
group-onsemi | 0:098463de4c5d | 170 | |
group-onsemi | 0:098463de4c5d | 171 | build_report = {} |
group-onsemi | 0:098463de4c5d | 172 | build_properties = {} |
group-onsemi | 0:098463de4c5d | 173 | |
group-onsemi | 0:098463de4c5d | 174 | library_build_success = False |
group-onsemi | 0:098463de4c5d | 175 | profile = extract_profile(parser, options, toolchain) |
group-onsemi | 0:098463de4c5d | 176 | try: |
group-onsemi | 0:098463de4c5d | 177 | # Build sources |
group-onsemi | 0:098463de4c5d | 178 | build_library(base_source_paths, options.build_dir, mcu, toolchain, |
group-onsemi | 0:098463de4c5d | 179 | jobs=options.jobs, |
group-onsemi | 0:098463de4c5d | 180 | clean=options.clean, |
group-onsemi | 0:098463de4c5d | 181 | report=build_report, |
group-onsemi | 0:098463de4c5d | 182 | properties=build_properties, |
group-onsemi | 0:098463de4c5d | 183 | name="mbed-build", |
group-onsemi | 0:098463de4c5d | 184 | macros=options.macros, |
group-onsemi | 0:098463de4c5d | 185 | verbose=options.verbose, |
group-onsemi | 0:098463de4c5d | 186 | notify=notify, |
group-onsemi | 0:098463de4c5d | 187 | archive=False, |
group-onsemi | 0:098463de4c5d | 188 | app_config=options.app_config, |
group-onsemi | 0:098463de4c5d | 189 | build_profile=profile) |
group-onsemi | 0:098463de4c5d | 190 | |
group-onsemi | 0:098463de4c5d | 191 | library_build_success = True |
group-onsemi | 0:098463de4c5d | 192 | except ToolException, e: |
group-onsemi | 0:098463de4c5d | 193 | # ToolException output is handled by the build log |
group-onsemi | 0:098463de4c5d | 194 | pass |
group-onsemi | 0:098463de4c5d | 195 | except NotSupportedException, e: |
group-onsemi | 0:098463de4c5d | 196 | # NotSupportedException is handled by the build log |
group-onsemi | 0:098463de4c5d | 197 | pass |
group-onsemi | 0:098463de4c5d | 198 | except Exception, e: |
group-onsemi | 0:098463de4c5d | 199 | # Some other exception occurred, print the error message |
group-onsemi | 0:098463de4c5d | 200 | print e |
group-onsemi | 0:098463de4c5d | 201 | |
group-onsemi | 0:098463de4c5d | 202 | if not library_build_success: |
group-onsemi | 0:098463de4c5d | 203 | print "Failed to build library" |
group-onsemi | 0:098463de4c5d | 204 | else: |
group-onsemi | 0:098463de4c5d | 205 | # Build all the tests |
group-onsemi | 0:098463de4c5d | 206 | |
group-onsemi | 0:098463de4c5d | 207 | test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, mcu, toolchain, |
group-onsemi | 0:098463de4c5d | 208 | clean=options.clean, |
group-onsemi | 0:098463de4c5d | 209 | report=build_report, |
group-onsemi | 0:098463de4c5d | 210 | properties=build_properties, |
group-onsemi | 0:098463de4c5d | 211 | macros=options.macros, |
group-onsemi | 0:098463de4c5d | 212 | verbose=options.verbose, |
group-onsemi | 0:098463de4c5d | 213 | notify=notify, |
group-onsemi | 0:098463de4c5d | 214 | jobs=options.jobs, |
group-onsemi | 0:098463de4c5d | 215 | continue_on_build_fail=options.continue_on_build_fail, |
group-onsemi | 0:098463de4c5d | 216 | app_config=options.app_config, |
group-onsemi | 0:098463de4c5d | 217 | build_profile=profile) |
group-onsemi | 0:098463de4c5d | 218 | |
group-onsemi | 0:098463de4c5d | 219 | # If a path to a test spec is provided, write it to a file |
group-onsemi | 0:098463de4c5d | 220 | if options.test_spec: |
group-onsemi | 0:098463de4c5d | 221 | test_spec_data = test_spec_from_test_builds(test_build) |
group-onsemi | 0:098463de4c5d | 222 | |
group-onsemi | 0:098463de4c5d | 223 | # Create the target dir for the test spec if necessary |
group-onsemi | 0:098463de4c5d | 224 | # mkdir will not create the dir if it already exists |
group-onsemi | 0:098463de4c5d | 225 | test_spec_dir = os.path.dirname(options.test_spec) |
group-onsemi | 0:098463de4c5d | 226 | if test_spec_dir: |
group-onsemi | 0:098463de4c5d | 227 | mkdir(test_spec_dir) |
group-onsemi | 0:098463de4c5d | 228 | |
group-onsemi | 0:098463de4c5d | 229 | try: |
group-onsemi | 0:098463de4c5d | 230 | with open(options.test_spec, 'w') as f: |
group-onsemi | 0:098463de4c5d | 231 | f.write(json.dumps(test_spec_data, indent=2)) |
group-onsemi | 0:098463de4c5d | 232 | except IOError, e: |
group-onsemi | 0:098463de4c5d | 233 | print "[ERROR] Error writing test spec to file" |
group-onsemi | 0:098463de4c5d | 234 | print e |
group-onsemi | 0:098463de4c5d | 235 | |
group-onsemi | 0:098463de4c5d | 236 | # If a path to a JUnit build report spec is provided, write it to a file |
group-onsemi | 0:098463de4c5d | 237 | if options.build_report_junit: |
group-onsemi | 0:098463de4c5d | 238 | report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build") |
group-onsemi | 0:098463de4c5d | 239 | report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties) |
group-onsemi | 0:098463de4c5d | 240 | |
group-onsemi | 0:098463de4c5d | 241 | # Print memory map summary on screen |
group-onsemi | 0:098463de4c5d | 242 | if build_report: |
group-onsemi | 0:098463de4c5d | 243 | |
group-onsemi | 0:098463de4c5d | 244 | print print_build_memory_usage(build_report) |
group-onsemi | 0:098463de4c5d | 245 | |
group-onsemi | 0:098463de4c5d | 246 | print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build") |
group-onsemi | 0:098463de4c5d | 247 | status = print_report_exporter.report(build_report) |
group-onsemi | 0:098463de4c5d | 248 | |
group-onsemi | 0:098463de4c5d | 249 | if status: |
group-onsemi | 0:098463de4c5d | 250 | sys.exit(0) |
group-onsemi | 0:098463de4c5d | 251 | else: |
group-onsemi | 0:098463de4c5d | 252 | sys.exit(1) |
group-onsemi | 0:098463de4c5d | 253 | |
group-onsemi | 0:098463de4c5d | 254 | except KeyboardInterrupt, e: |
group-onsemi | 0:098463de4c5d | 255 | print "\n[CTRL+c] exit" |
group-onsemi | 0:098463de4c5d | 256 | except ConfigException, e: |
group-onsemi | 0:098463de4c5d | 257 | # Catching ConfigException here to prevent a traceback |
group-onsemi | 0:098463de4c5d | 258 | print "[ERROR] %s" % str(e) |
group-onsemi | 0:098463de4c5d | 259 | except Exception,e: |
group-onsemi | 0:098463de4c5d | 260 | import traceback |
group-onsemi | 0:098463de4c5d | 261 | traceback.print_exc(file=sys.stdout) |
group-onsemi | 0:098463de4c5d | 262 | print "[ERROR] %s" % str(e) |
group-onsemi | 0:098463de4c5d | 263 | sys.exit(1) |