5.2.1 - Updated I2C files
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
tools/utils.py@1:f30bdcd2b33b, 2017-02-27 (annotated)
- Committer:
- jacobjohnson
- Date:
- Mon Feb 27 17:45:05 2017 +0000
- Revision:
- 1:f30bdcd2b33b
- Parent:
- 0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c. This will need to be changed later, and accessed from the main level, but for now this allows the adc to read a value from 0 to 3.7V, instead of just up to 1V.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-onsemi | 0:098463de4c5d | 1 | """ |
group-onsemi | 0:098463de4c5d | 2 | mbed SDK |
group-onsemi | 0:098463de4c5d | 3 | Copyright (c) 2011-2013 ARM Limited |
group-onsemi | 0:098463de4c5d | 4 | |
group-onsemi | 0:098463de4c5d | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
group-onsemi | 0:098463de4c5d | 6 | you may not use this file except in compliance with the License. |
group-onsemi | 0:098463de4c5d | 7 | You may obtain a copy of the License at |
group-onsemi | 0:098463de4c5d | 8 | |
group-onsemi | 0:098463de4c5d | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
group-onsemi | 0:098463de4c5d | 10 | |
group-onsemi | 0:098463de4c5d | 11 | Unless required by applicable law or agreed to in writing, software |
group-onsemi | 0:098463de4c5d | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
group-onsemi | 0:098463de4c5d | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-onsemi | 0:098463de4c5d | 14 | See the License for the specific language governing permissions and |
group-onsemi | 0:098463de4c5d | 15 | limitations under the License. |
group-onsemi | 0:098463de4c5d | 16 | """ |
group-onsemi | 0:098463de4c5d | 17 | import sys |
group-onsemi | 0:098463de4c5d | 18 | import inspect |
group-onsemi | 0:098463de4c5d | 19 | import os |
group-onsemi | 0:098463de4c5d | 20 | import argparse |
group-onsemi | 0:098463de4c5d | 21 | import math |
group-onsemi | 0:098463de4c5d | 22 | from os import listdir, remove, makedirs |
group-onsemi | 0:098463de4c5d | 23 | from shutil import copyfile |
group-onsemi | 0:098463de4c5d | 24 | from os.path import isdir, join, exists, split, relpath, splitext, abspath |
group-onsemi | 0:098463de4c5d | 25 | from os.path import commonprefix, normpath, dirname |
group-onsemi | 0:098463de4c5d | 26 | from subprocess import Popen, PIPE, STDOUT, call |
group-onsemi | 0:098463de4c5d | 27 | from math import ceil |
group-onsemi | 0:098463de4c5d | 28 | import json |
group-onsemi | 0:098463de4c5d | 29 | from collections import OrderedDict |
group-onsemi | 0:098463de4c5d | 30 | import logging |
group-onsemi | 0:098463de4c5d | 31 | |
group-onsemi | 0:098463de4c5d | 32 | def remove_if_in(lst, thing): |
group-onsemi | 0:098463de4c5d | 33 | if thing in lst: |
group-onsemi | 0:098463de4c5d | 34 | lst.remove(thing) |
group-onsemi | 0:098463de4c5d | 35 | |
group-onsemi | 0:098463de4c5d | 36 | def compile_worker(job): |
group-onsemi | 0:098463de4c5d | 37 | """Standard task runner used for compiling |
group-onsemi | 0:098463de4c5d | 38 | |
group-onsemi | 0:098463de4c5d | 39 | Positional argumets: |
group-onsemi | 0:098463de4c5d | 40 | job - a dict containing a list of commands and the remaining arguments |
group-onsemi | 0:098463de4c5d | 41 | to run_cmd |
group-onsemi | 0:098463de4c5d | 42 | """ |
group-onsemi | 0:098463de4c5d | 43 | results = [] |
group-onsemi | 0:098463de4c5d | 44 | for command in job['commands']: |
group-onsemi | 0:098463de4c5d | 45 | try: |
group-onsemi | 0:098463de4c5d | 46 | _, _stderr, _rc = run_cmd(command, work_dir=job['work_dir'], |
group-onsemi | 0:098463de4c5d | 47 | chroot=job['chroot']) |
group-onsemi | 0:098463de4c5d | 48 | except KeyboardInterrupt: |
group-onsemi | 0:098463de4c5d | 49 | raise ToolException |
group-onsemi | 0:098463de4c5d | 50 | |
group-onsemi | 0:098463de4c5d | 51 | results.append({ |
group-onsemi | 0:098463de4c5d | 52 | 'code': _rc, |
group-onsemi | 0:098463de4c5d | 53 | 'output': _stderr, |
group-onsemi | 0:098463de4c5d | 54 | 'command': command |
group-onsemi | 0:098463de4c5d | 55 | }) |
group-onsemi | 0:098463de4c5d | 56 | |
group-onsemi | 0:098463de4c5d | 57 | return { |
group-onsemi | 0:098463de4c5d | 58 | 'source': job['source'], |
group-onsemi | 0:098463de4c5d | 59 | 'object': job['object'], |
group-onsemi | 0:098463de4c5d | 60 | 'commands': job['commands'], |
group-onsemi | 0:098463de4c5d | 61 | 'results': results |
group-onsemi | 0:098463de4c5d | 62 | } |
group-onsemi | 0:098463de4c5d | 63 | |
group-onsemi | 0:098463de4c5d | 64 | def cmd(command, check=True, verbose=False, shell=False, cwd=None): |
group-onsemi | 0:098463de4c5d | 65 | """A wrapper to run a command as a blocking job""" |
group-onsemi | 0:098463de4c5d | 66 | text = command if shell else ' '.join(command) |
group-onsemi | 0:098463de4c5d | 67 | if verbose: |
group-onsemi | 0:098463de4c5d | 68 | print text |
group-onsemi | 0:098463de4c5d | 69 | return_code = call(command, shell=shell, cwd=cwd) |
group-onsemi | 0:098463de4c5d | 70 | if check and return_code != 0: |
group-onsemi | 0:098463de4c5d | 71 | raise Exception('ERROR %d: "%s"' % (return_code, text)) |
group-onsemi | 0:098463de4c5d | 72 | |
group-onsemi | 0:098463de4c5d | 73 | |
group-onsemi | 0:098463de4c5d | 74 | def run_cmd(command, work_dir=None, chroot=None, redirect=False): |
group-onsemi | 0:098463de4c5d | 75 | """Run a command in the forground |
group-onsemi | 0:098463de4c5d | 76 | |
group-onsemi | 0:098463de4c5d | 77 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 78 | command - the command to run |
group-onsemi | 0:098463de4c5d | 79 | |
group-onsemi | 0:098463de4c5d | 80 | Keyword arguments: |
group-onsemi | 0:098463de4c5d | 81 | work_dir - the working directory to run the command in |
group-onsemi | 0:098463de4c5d | 82 | chroot - the chroot to run the command in |
group-onsemi | 0:098463de4c5d | 83 | redirect - redirect the stderr to a pipe to be used later |
group-onsemi | 0:098463de4c5d | 84 | """ |
group-onsemi | 0:098463de4c5d | 85 | if chroot: |
group-onsemi | 0:098463de4c5d | 86 | # Conventions managed by the web team for the mbed.org build system |
group-onsemi | 0:098463de4c5d | 87 | chroot_cmd = [ |
group-onsemi | 0:098463de4c5d | 88 | '/usr/sbin/chroot', '--userspec=33:33', chroot |
group-onsemi | 0:098463de4c5d | 89 | ] |
group-onsemi | 0:098463de4c5d | 90 | for element in command: |
group-onsemi | 0:098463de4c5d | 91 | chroot_cmd += [element.replace(chroot, '')] |
group-onsemi | 0:098463de4c5d | 92 | |
group-onsemi | 0:098463de4c5d | 93 | logging.debug("Running command %s", ' '.join(chroot_cmd)) |
group-onsemi | 0:098463de4c5d | 94 | command = chroot_cmd |
group-onsemi | 0:098463de4c5d | 95 | work_dir = None |
group-onsemi | 0:098463de4c5d | 96 | |
group-onsemi | 0:098463de4c5d | 97 | try: |
group-onsemi | 0:098463de4c5d | 98 | process = Popen(command, stdout=PIPE, |
group-onsemi | 0:098463de4c5d | 99 | stderr=STDOUT if redirect else PIPE, cwd=work_dir) |
group-onsemi | 0:098463de4c5d | 100 | _stdout, _stderr = process.communicate() |
group-onsemi | 0:098463de4c5d | 101 | except OSError: |
group-onsemi | 0:098463de4c5d | 102 | print "[OS ERROR] Command: "+(' '.join(command)) |
group-onsemi | 0:098463de4c5d | 103 | raise |
group-onsemi | 0:098463de4c5d | 104 | |
group-onsemi | 0:098463de4c5d | 105 | return _stdout, _stderr, process.returncode |
group-onsemi | 0:098463de4c5d | 106 | |
group-onsemi | 0:098463de4c5d | 107 | |
group-onsemi | 0:098463de4c5d | 108 | def run_cmd_ext(command): |
group-onsemi | 0:098463de4c5d | 109 | """ A version of run command that checks if the command exists befor running |
group-onsemi | 0:098463de4c5d | 110 | |
group-onsemi | 0:098463de4c5d | 111 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 112 | command - the command line you are trying to invoke |
group-onsemi | 0:098463de4c5d | 113 | """ |
group-onsemi | 0:098463de4c5d | 114 | assert is_cmd_valid(command[0]) |
group-onsemi | 0:098463de4c5d | 115 | process = Popen(command, stdout=PIPE, stderr=PIPE) |
group-onsemi | 0:098463de4c5d | 116 | _stdout, _stderr = process.communicate() |
group-onsemi | 0:098463de4c5d | 117 | return _stdout, _stderr, process.returncode |
group-onsemi | 0:098463de4c5d | 118 | |
group-onsemi | 0:098463de4c5d | 119 | |
group-onsemi | 0:098463de4c5d | 120 | def is_cmd_valid(command): |
group-onsemi | 0:098463de4c5d | 121 | """ Verify that a command exists and is executable |
group-onsemi | 0:098463de4c5d | 122 | |
group-onsemi | 0:098463de4c5d | 123 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 124 | command - the command to check |
group-onsemi | 0:098463de4c5d | 125 | """ |
group-onsemi | 0:098463de4c5d | 126 | caller = get_caller_name() |
group-onsemi | 0:098463de4c5d | 127 | cmd_path = find_cmd_abspath(command) |
group-onsemi | 0:098463de4c5d | 128 | if not cmd_path: |
group-onsemi | 0:098463de4c5d | 129 | error("%s: Command '%s' can't be found" % (caller, command)) |
group-onsemi | 0:098463de4c5d | 130 | if not is_exec(cmd_path): |
group-onsemi | 0:098463de4c5d | 131 | error("%s: Command '%s' resolves to file '%s' which is not executable" |
group-onsemi | 0:098463de4c5d | 132 | % (caller, command, cmd_path)) |
group-onsemi | 0:098463de4c5d | 133 | return True |
group-onsemi | 0:098463de4c5d | 134 | |
group-onsemi | 0:098463de4c5d | 135 | |
group-onsemi | 0:098463de4c5d | 136 | def is_exec(path): |
group-onsemi | 0:098463de4c5d | 137 | """A simple check to verify that a path to an executable exists |
group-onsemi | 0:098463de4c5d | 138 | |
group-onsemi | 0:098463de4c5d | 139 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 140 | path - the executable |
group-onsemi | 0:098463de4c5d | 141 | """ |
group-onsemi | 0:098463de4c5d | 142 | return os.access(path, os.X_OK) or os.access(path+'.exe', os.X_OK) |
group-onsemi | 0:098463de4c5d | 143 | |
group-onsemi | 0:098463de4c5d | 144 | |
group-onsemi | 0:098463de4c5d | 145 | def find_cmd_abspath(command): |
group-onsemi | 0:098463de4c5d | 146 | """ Returns the absolute path to a command. |
group-onsemi | 0:098463de4c5d | 147 | None is returned if no absolute path was found. |
group-onsemi | 0:098463de4c5d | 148 | |
group-onsemi | 0:098463de4c5d | 149 | Positional arguhments: |
group-onsemi | 0:098463de4c5d | 150 | command - the command to find the path of |
group-onsemi | 0:098463de4c5d | 151 | """ |
group-onsemi | 0:098463de4c5d | 152 | if exists(command) or exists(command + '.exe'): |
group-onsemi | 0:098463de4c5d | 153 | return os.path.abspath(command) |
group-onsemi | 0:098463de4c5d | 154 | if not 'PATH' in os.environ: |
group-onsemi | 0:098463de4c5d | 155 | raise Exception("Can't find command path for current platform ('%s')" |
group-onsemi | 0:098463de4c5d | 156 | % sys.platform) |
group-onsemi | 0:098463de4c5d | 157 | path_env = os.environ['PATH'] |
group-onsemi | 0:098463de4c5d | 158 | for path in path_env.split(os.pathsep): |
group-onsemi | 0:098463de4c5d | 159 | cmd_path = '%s/%s' % (path, command) |
group-onsemi | 0:098463de4c5d | 160 | if exists(cmd_path) or exists(cmd_path + '.exe'): |
group-onsemi | 0:098463de4c5d | 161 | return cmd_path |
group-onsemi | 0:098463de4c5d | 162 | |
group-onsemi | 0:098463de4c5d | 163 | |
group-onsemi | 0:098463de4c5d | 164 | def mkdir(path): |
group-onsemi | 0:098463de4c5d | 165 | """ a wrapped makedirs that only tries to create a directory if it does not |
group-onsemi | 0:098463de4c5d | 166 | exist already |
group-onsemi | 0:098463de4c5d | 167 | |
group-onsemi | 0:098463de4c5d | 168 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 169 | path - the path to maybe create |
group-onsemi | 0:098463de4c5d | 170 | """ |
group-onsemi | 0:098463de4c5d | 171 | if not exists(path): |
group-onsemi | 0:098463de4c5d | 172 | makedirs(path) |
group-onsemi | 0:098463de4c5d | 173 | |
group-onsemi | 0:098463de4c5d | 174 | |
group-onsemi | 0:098463de4c5d | 175 | def copy_file(src, dst): |
group-onsemi | 0:098463de4c5d | 176 | """ Implement the behaviour of "shutil.copy(src, dst)" without copying the |
group-onsemi | 0:098463de4c5d | 177 | permissions (this was causing errors with directories mounted with samba) |
group-onsemi | 0:098463de4c5d | 178 | |
group-onsemi | 0:098463de4c5d | 179 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 180 | src - the source of the copy operation |
group-onsemi | 0:098463de4c5d | 181 | dst - the destination of the copy operation |
group-onsemi | 0:098463de4c5d | 182 | """ |
group-onsemi | 0:098463de4c5d | 183 | if isdir(dst): |
group-onsemi | 0:098463de4c5d | 184 | _, base = split(src) |
group-onsemi | 0:098463de4c5d | 185 | dst = join(dst, base) |
group-onsemi | 0:098463de4c5d | 186 | copyfile(src, dst) |
group-onsemi | 0:098463de4c5d | 187 | |
group-onsemi | 0:098463de4c5d | 188 | |
group-onsemi | 0:098463de4c5d | 189 | def delete_dir_files(directory): |
group-onsemi | 0:098463de4c5d | 190 | """ A function that does rm -rf |
group-onsemi | 0:098463de4c5d | 191 | |
group-onsemi | 0:098463de4c5d | 192 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 193 | directory - the directory to remove |
group-onsemi | 0:098463de4c5d | 194 | """ |
group-onsemi | 0:098463de4c5d | 195 | if not exists(directory): |
group-onsemi | 0:098463de4c5d | 196 | return |
group-onsemi | 0:098463de4c5d | 197 | |
group-onsemi | 0:098463de4c5d | 198 | for element in listdir(directory): |
group-onsemi | 0:098463de4c5d | 199 | to_remove = join(directory, element) |
group-onsemi | 0:098463de4c5d | 200 | if not isdir(to_remove): |
group-onsemi | 0:098463de4c5d | 201 | remove(file) |
group-onsemi | 0:098463de4c5d | 202 | |
group-onsemi | 0:098463de4c5d | 203 | |
group-onsemi | 0:098463de4c5d | 204 | def get_caller_name(steps=2): |
group-onsemi | 0:098463de4c5d | 205 | """ |
group-onsemi | 0:098463de4c5d | 206 | When called inside a function, it returns the name |
group-onsemi | 0:098463de4c5d | 207 | of the caller of that function. |
group-onsemi | 0:098463de4c5d | 208 | |
group-onsemi | 0:098463de4c5d | 209 | Keyword arguments: |
group-onsemi | 0:098463de4c5d | 210 | steps - the number of steps up the stack the calling function is |
group-onsemi | 0:098463de4c5d | 211 | """ |
group-onsemi | 0:098463de4c5d | 212 | return inspect.stack()[steps][3] |
group-onsemi | 0:098463de4c5d | 213 | |
group-onsemi | 0:098463de4c5d | 214 | |
group-onsemi | 0:098463de4c5d | 215 | def error(msg): |
group-onsemi | 0:098463de4c5d | 216 | """Fatal error, abort hard |
group-onsemi | 0:098463de4c5d | 217 | |
group-onsemi | 0:098463de4c5d | 218 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 219 | msg - the message to print before crashing |
group-onsemi | 0:098463de4c5d | 220 | """ |
group-onsemi | 0:098463de4c5d | 221 | print("ERROR: %s" % msg) |
group-onsemi | 0:098463de4c5d | 222 | sys.exit(1) |
group-onsemi | 0:098463de4c5d | 223 | |
group-onsemi | 0:098463de4c5d | 224 | |
group-onsemi | 0:098463de4c5d | 225 | def rel_path(path, base, dot=False): |
group-onsemi | 0:098463de4c5d | 226 | """Relative path calculation that optionaly always starts with a dot |
group-onsemi | 0:098463de4c5d | 227 | |
group-onsemi | 0:098463de4c5d | 228 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 229 | path - the path to make relative |
group-onsemi | 0:098463de4c5d | 230 | base - what to make the path relative to |
group-onsemi | 0:098463de4c5d | 231 | |
group-onsemi | 0:098463de4c5d | 232 | Keyword arguments: |
group-onsemi | 0:098463de4c5d | 233 | dot - if True, the path will always start with a './' |
group-onsemi | 0:098463de4c5d | 234 | """ |
group-onsemi | 0:098463de4c5d | 235 | final_path = relpath(path, base) |
group-onsemi | 0:098463de4c5d | 236 | if dot and not final_path.startswith('.'): |
group-onsemi | 0:098463de4c5d | 237 | final_path = './' + final_path |
group-onsemi | 0:098463de4c5d | 238 | return final_path |
group-onsemi | 0:098463de4c5d | 239 | |
group-onsemi | 0:098463de4c5d | 240 | |
group-onsemi | 0:098463de4c5d | 241 | class ToolException(Exception): |
group-onsemi | 0:098463de4c5d | 242 | """A class representing an exception throw by the tools""" |
group-onsemi | 0:098463de4c5d | 243 | pass |
group-onsemi | 0:098463de4c5d | 244 | |
group-onsemi | 0:098463de4c5d | 245 | class NotSupportedException(Exception): |
group-onsemi | 0:098463de4c5d | 246 | """A class a toolchain not supporting a particular target""" |
group-onsemi | 0:098463de4c5d | 247 | pass |
group-onsemi | 0:098463de4c5d | 248 | |
group-onsemi | 0:098463de4c5d | 249 | class InvalidReleaseTargetException(Exception): |
group-onsemi | 0:098463de4c5d | 250 | pass |
group-onsemi | 0:098463de4c5d | 251 | |
group-onsemi | 0:098463de4c5d | 252 | def split_path(path): |
group-onsemi | 0:098463de4c5d | 253 | """spilt a file name into it's directory name, base name, and extension |
group-onsemi | 0:098463de4c5d | 254 | |
group-onsemi | 0:098463de4c5d | 255 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 256 | path - the file name to split |
group-onsemi | 0:098463de4c5d | 257 | """ |
group-onsemi | 0:098463de4c5d | 258 | base, has_ext = split(path) |
group-onsemi | 0:098463de4c5d | 259 | name, ext = splitext(has_ext) |
group-onsemi | 0:098463de4c5d | 260 | return base, name, ext |
group-onsemi | 0:098463de4c5d | 261 | |
group-onsemi | 0:098463de4c5d | 262 | |
group-onsemi | 0:098463de4c5d | 263 | def get_path_depth(path): |
group-onsemi | 0:098463de4c5d | 264 | """ Given a path, return the number of directory levels present. |
group-onsemi | 0:098463de4c5d | 265 | This roughly translates to the number of path separators (os.sep) + 1. |
group-onsemi | 0:098463de4c5d | 266 | Ex. Given "path/to/dir", this would return 3 |
group-onsemi | 0:098463de4c5d | 267 | Special cases: "." and "/" return 0 |
group-onsemi | 0:098463de4c5d | 268 | |
group-onsemi | 0:098463de4c5d | 269 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 270 | path - the path to calculate the depth of |
group-onsemi | 0:098463de4c5d | 271 | """ |
group-onsemi | 0:098463de4c5d | 272 | normalized_path = normpath(path) |
group-onsemi | 0:098463de4c5d | 273 | path_depth = 0 |
group-onsemi | 0:098463de4c5d | 274 | head, tail = split(normalized_path) |
group-onsemi | 0:098463de4c5d | 275 | |
group-onsemi | 0:098463de4c5d | 276 | while tail and tail != '.': |
group-onsemi | 0:098463de4c5d | 277 | path_depth += 1 |
group-onsemi | 0:098463de4c5d | 278 | head, tail = split(head) |
group-onsemi | 0:098463de4c5d | 279 | |
group-onsemi | 0:098463de4c5d | 280 | return path_depth |
group-onsemi | 0:098463de4c5d | 281 | |
group-onsemi | 0:098463de4c5d | 282 | |
group-onsemi | 0:098463de4c5d | 283 | def args_error(parser, message): |
group-onsemi | 0:098463de4c5d | 284 | """Abort with an error that was generated by the arguments to a CLI program |
group-onsemi | 0:098463de4c5d | 285 | |
group-onsemi | 0:098463de4c5d | 286 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 287 | parser - the ArgumentParser object that parsed the command line |
group-onsemi | 0:098463de4c5d | 288 | message - what went wrong |
group-onsemi | 0:098463de4c5d | 289 | """ |
group-onsemi | 0:098463de4c5d | 290 | parser.error(message) |
group-onsemi | 0:098463de4c5d | 291 | sys.exit(2) |
group-onsemi | 0:098463de4c5d | 292 | |
group-onsemi | 0:098463de4c5d | 293 | |
group-onsemi | 0:098463de4c5d | 294 | def construct_enum(**enums): |
group-onsemi | 0:098463de4c5d | 295 | """ Create your own pseudo-enums |
group-onsemi | 0:098463de4c5d | 296 | |
group-onsemi | 0:098463de4c5d | 297 | Keyword arguments: |
group-onsemi | 0:098463de4c5d | 298 | * - a member of the Enum you are creating and it's value |
group-onsemi | 0:098463de4c5d | 299 | """ |
group-onsemi | 0:098463de4c5d | 300 | return type('Enum', (), enums) |
group-onsemi | 0:098463de4c5d | 301 | |
group-onsemi | 0:098463de4c5d | 302 | |
group-onsemi | 0:098463de4c5d | 303 | def check_required_modules(required_modules, verbose=True): |
group-onsemi | 0:098463de4c5d | 304 | """ Function checks for Python modules which should be "importable" |
group-onsemi | 0:098463de4c5d | 305 | before test suite can be used. |
group-onsemi | 0:098463de4c5d | 306 | @return returns True if all modules are installed already |
group-onsemi | 0:098463de4c5d | 307 | """ |
group-onsemi | 0:098463de4c5d | 308 | import imp |
group-onsemi | 0:098463de4c5d | 309 | not_installed_modules = [] |
group-onsemi | 0:098463de4c5d | 310 | for module_name in required_modules: |
group-onsemi | 0:098463de4c5d | 311 | try: |
group-onsemi | 0:098463de4c5d | 312 | imp.find_module(module_name) |
group-onsemi | 0:098463de4c5d | 313 | except ImportError: |
group-onsemi | 0:098463de4c5d | 314 | # We also test against a rare case: module is an egg file |
group-onsemi | 0:098463de4c5d | 315 | try: |
group-onsemi | 0:098463de4c5d | 316 | __import__(module_name) |
group-onsemi | 0:098463de4c5d | 317 | except ImportError as exc: |
group-onsemi | 0:098463de4c5d | 318 | not_installed_modules.append(module_name) |
group-onsemi | 0:098463de4c5d | 319 | if verbose: |
group-onsemi | 0:098463de4c5d | 320 | print "Error: %s" % exc |
group-onsemi | 0:098463de4c5d | 321 | |
group-onsemi | 0:098463de4c5d | 322 | if verbose: |
group-onsemi | 0:098463de4c5d | 323 | if not_installed_modules: |
group-onsemi | 0:098463de4c5d | 324 | print ("Warning: Module(s) %s not installed. Please install " + \ |
group-onsemi | 0:098463de4c5d | 325 | "required module(s) before using this script.")\ |
group-onsemi | 0:098463de4c5d | 326 | % (', '.join(not_installed_modules)) |
group-onsemi | 0:098463de4c5d | 327 | |
group-onsemi | 0:098463de4c5d | 328 | if not_installed_modules: |
group-onsemi | 0:098463de4c5d | 329 | return False |
group-onsemi | 0:098463de4c5d | 330 | else: |
group-onsemi | 0:098463de4c5d | 331 | return True |
group-onsemi | 0:098463de4c5d | 332 | |
group-onsemi | 0:098463de4c5d | 333 | def dict_to_ascii(dictionary): |
group-onsemi | 0:098463de4c5d | 334 | """ Utility function: traverse a dictionary and change all the strings in |
group-onsemi | 0:098463de4c5d | 335 | the dictionary to ASCII from Unicode. Useful when reading ASCII JSON data, |
group-onsemi | 0:098463de4c5d | 336 | because the JSON decoder always returns Unicode string. Based on |
group-onsemi | 0:098463de4c5d | 337 | http://stackoverflow.com/a/13105359 |
group-onsemi | 0:098463de4c5d | 338 | |
group-onsemi | 0:098463de4c5d | 339 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 340 | dictionary - The dict that contains some Unicode that should be ASCII |
group-onsemi | 0:098463de4c5d | 341 | """ |
group-onsemi | 0:098463de4c5d | 342 | if isinstance(dictionary, dict): |
group-onsemi | 0:098463de4c5d | 343 | return OrderedDict([(dict_to_ascii(key), dict_to_ascii(value)) |
group-onsemi | 0:098463de4c5d | 344 | for key, value in dictionary.iteritems()]) |
group-onsemi | 0:098463de4c5d | 345 | elif isinstance(dictionary, list): |
group-onsemi | 0:098463de4c5d | 346 | return [dict_to_ascii(element) for element in dictionary] |
group-onsemi | 0:098463de4c5d | 347 | elif isinstance(dictionary, unicode): |
group-onsemi | 0:098463de4c5d | 348 | return dictionary.encode('ascii') |
group-onsemi | 0:098463de4c5d | 349 | else: |
group-onsemi | 0:098463de4c5d | 350 | return dictionary |
group-onsemi | 0:098463de4c5d | 351 | |
group-onsemi | 0:098463de4c5d | 352 | def json_file_to_dict(fname): |
group-onsemi | 0:098463de4c5d | 353 | """ Read a JSON file and return its Python representation, transforming all |
group-onsemi | 0:098463de4c5d | 354 | the strings from Unicode to ASCII. The order of keys in the JSON file is |
group-onsemi | 0:098463de4c5d | 355 | preserved. |
group-onsemi | 0:098463de4c5d | 356 | |
group-onsemi | 0:098463de4c5d | 357 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 358 | fname - the name of the file to parse |
group-onsemi | 0:098463de4c5d | 359 | """ |
group-onsemi | 0:098463de4c5d | 360 | try: |
group-onsemi | 0:098463de4c5d | 361 | with open(fname, "r") as file_obj: |
group-onsemi | 0:098463de4c5d | 362 | return dict_to_ascii(json.load(file_obj, |
group-onsemi | 0:098463de4c5d | 363 | object_pairs_hook=OrderedDict)) |
group-onsemi | 0:098463de4c5d | 364 | except (ValueError, IOError): |
group-onsemi | 0:098463de4c5d | 365 | sys.stderr.write("Error parsing '%s':\n" % fname) |
group-onsemi | 0:098463de4c5d | 366 | raise |
group-onsemi | 0:098463de4c5d | 367 | |
group-onsemi | 0:098463de4c5d | 368 | # Wowza, double closure |
group-onsemi | 0:098463de4c5d | 369 | def argparse_type(casedness, prefer_hyphen=False): |
group-onsemi | 0:098463de4c5d | 370 | def middle(lst, type_name): |
group-onsemi | 0:098463de4c5d | 371 | def parse_type(string): |
group-onsemi | 0:098463de4c5d | 372 | """ validate that an argument passed in (as string) is a member of |
group-onsemi | 0:098463de4c5d | 373 | the list of possible arguments. Offer a suggestion if the case of |
group-onsemi | 0:098463de4c5d | 374 | the string, or the hyphens/underscores do not match the expected |
group-onsemi | 0:098463de4c5d | 375 | style of the argument. |
group-onsemi | 0:098463de4c5d | 376 | """ |
group-onsemi | 0:098463de4c5d | 377 | if prefer_hyphen: |
group-onsemi | 0:098463de4c5d | 378 | newstring = casedness(string).replace("_", "-") |
group-onsemi | 0:098463de4c5d | 379 | else: |
group-onsemi | 0:098463de4c5d | 380 | newstring = casedness(string).replace("-", "_") |
group-onsemi | 0:098463de4c5d | 381 | if string in lst: |
group-onsemi | 0:098463de4c5d | 382 | return string |
group-onsemi | 0:098463de4c5d | 383 | elif string not in lst and newstring in lst: |
group-onsemi | 0:098463de4c5d | 384 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 385 | "{0} is not a supported {1}. Did you mean {2}?".format( |
group-onsemi | 0:098463de4c5d | 386 | string, type_name, newstring)) |
group-onsemi | 0:098463de4c5d | 387 | else: |
group-onsemi | 0:098463de4c5d | 388 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 389 | "{0} is not a supported {1}. Supported {1}s are:\n{2}". |
group-onsemi | 0:098463de4c5d | 390 | format(string, type_name, columnate(lst))) |
group-onsemi | 0:098463de4c5d | 391 | return parse_type |
group-onsemi | 0:098463de4c5d | 392 | return middle |
group-onsemi | 0:098463de4c5d | 393 | |
group-onsemi | 0:098463de4c5d | 394 | # short cuts for the argparse_type versions |
group-onsemi | 0:098463de4c5d | 395 | argparse_uppercase_type = argparse_type(str.upper, False) |
group-onsemi | 0:098463de4c5d | 396 | argparse_lowercase_type = argparse_type(str.lower, False) |
group-onsemi | 0:098463de4c5d | 397 | argparse_uppercase_hyphen_type = argparse_type(str.upper, True) |
group-onsemi | 0:098463de4c5d | 398 | argparse_lowercase_hyphen_type = argparse_type(str.lower, True) |
group-onsemi | 0:098463de4c5d | 399 | |
group-onsemi | 0:098463de4c5d | 400 | def argparse_force_type(case): |
group-onsemi | 0:098463de4c5d | 401 | """ validate that an argument passed in (as string) is a member of the list |
group-onsemi | 0:098463de4c5d | 402 | of possible arguments after converting it's case. |
group-onsemi | 0:098463de4c5d | 403 | """ |
group-onsemi | 0:098463de4c5d | 404 | def middle(lst, type_name): |
group-onsemi | 0:098463de4c5d | 405 | """ The parser type generator""" |
group-onsemi | 0:098463de4c5d | 406 | def parse_type(string): |
group-onsemi | 0:098463de4c5d | 407 | """ The parser type""" |
group-onsemi | 0:098463de4c5d | 408 | for option in lst: |
group-onsemi | 0:098463de4c5d | 409 | if case(string) == case(option): |
group-onsemi | 0:098463de4c5d | 410 | return option |
group-onsemi | 0:098463de4c5d | 411 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 412 | "{0} is not a supported {1}. Supported {1}s are:\n{2}". |
group-onsemi | 0:098463de4c5d | 413 | format(string, type_name, columnate(lst))) |
group-onsemi | 0:098463de4c5d | 414 | return parse_type |
group-onsemi | 0:098463de4c5d | 415 | return middle |
group-onsemi | 0:098463de4c5d | 416 | |
group-onsemi | 0:098463de4c5d | 417 | # these two types convert the case of their arguments _before_ validation |
group-onsemi | 0:098463de4c5d | 418 | argparse_force_uppercase_type = argparse_force_type(str.upper) |
group-onsemi | 0:098463de4c5d | 419 | argparse_force_lowercase_type = argparse_force_type(str.lower) |
group-onsemi | 0:098463de4c5d | 420 | |
group-onsemi | 0:098463de4c5d | 421 | def argparse_many(func): |
group-onsemi | 0:098463de4c5d | 422 | """ An argument parser combinator that takes in an argument parser and |
group-onsemi | 0:098463de4c5d | 423 | creates a new parser that accepts a comma separated list of the same thing. |
group-onsemi | 0:098463de4c5d | 424 | """ |
group-onsemi | 0:098463de4c5d | 425 | def wrap(string): |
group-onsemi | 0:098463de4c5d | 426 | """ The actual parser""" |
group-onsemi | 0:098463de4c5d | 427 | return [func(s) for s in string.split(",")] |
group-onsemi | 0:098463de4c5d | 428 | return wrap |
group-onsemi | 0:098463de4c5d | 429 | |
group-onsemi | 0:098463de4c5d | 430 | def argparse_filestring_type(string): |
group-onsemi | 0:098463de4c5d | 431 | """ An argument parser that verifies that a string passed in corresponds |
group-onsemi | 0:098463de4c5d | 432 | to a file""" |
group-onsemi | 0:098463de4c5d | 433 | if exists(string): |
group-onsemi | 0:098463de4c5d | 434 | return string |
group-onsemi | 0:098463de4c5d | 435 | else: |
group-onsemi | 0:098463de4c5d | 436 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 437 | "{0}"" does not exist in the filesystem.".format(string)) |
group-onsemi | 0:098463de4c5d | 438 | |
group-onsemi | 0:098463de4c5d | 439 | def argparse_profile_filestring_type(string): |
group-onsemi | 0:098463de4c5d | 440 | """ An argument parser that verifies that a string passed in is either |
group-onsemi | 0:098463de4c5d | 441 | absolute path or a file name (expanded to |
group-onsemi | 0:098463de4c5d | 442 | mbed-os/tools/profiles/<fname>.json) of a existing file""" |
group-onsemi | 0:098463de4c5d | 443 | fpath = join(dirname(__file__), "profiles/{}.json".format(string)) |
group-onsemi | 0:098463de4c5d | 444 | if exists(string): |
group-onsemi | 0:098463de4c5d | 445 | return string |
group-onsemi | 0:098463de4c5d | 446 | elif exists(fpath): |
group-onsemi | 0:098463de4c5d | 447 | return fpath |
group-onsemi | 0:098463de4c5d | 448 | else: |
group-onsemi | 0:098463de4c5d | 449 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 450 | "{0} does not exist in the filesystem.".format(string)) |
group-onsemi | 0:098463de4c5d | 451 | |
group-onsemi | 0:098463de4c5d | 452 | def columnate(strings, separator=", ", chars=80): |
group-onsemi | 0:098463de4c5d | 453 | """ render a list of strings as a in a bunch of columns |
group-onsemi | 0:098463de4c5d | 454 | |
group-onsemi | 0:098463de4c5d | 455 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 456 | strings - the strings to columnate |
group-onsemi | 0:098463de4c5d | 457 | |
group-onsemi | 0:098463de4c5d | 458 | Keyword arguments; |
group-onsemi | 0:098463de4c5d | 459 | separator - the separation between the columns |
group-onsemi | 0:098463de4c5d | 460 | chars - the maximum with of a row |
group-onsemi | 0:098463de4c5d | 461 | """ |
group-onsemi | 0:098463de4c5d | 462 | col_width = max(len(s) for s in strings) |
group-onsemi | 0:098463de4c5d | 463 | total_width = col_width + len(separator) |
group-onsemi | 0:098463de4c5d | 464 | columns = math.floor(chars / total_width) |
group-onsemi | 0:098463de4c5d | 465 | output = "" |
group-onsemi | 0:098463de4c5d | 466 | for i, string in zip(range(len(strings)), strings): |
group-onsemi | 0:098463de4c5d | 467 | append = string |
group-onsemi | 0:098463de4c5d | 468 | if i != len(strings) - 1: |
group-onsemi | 0:098463de4c5d | 469 | append += separator |
group-onsemi | 0:098463de4c5d | 470 | if i % columns == columns - 1: |
group-onsemi | 0:098463de4c5d | 471 | append += "\n" |
group-onsemi | 0:098463de4c5d | 472 | else: |
group-onsemi | 0:098463de4c5d | 473 | append = append.ljust(total_width) |
group-onsemi | 0:098463de4c5d | 474 | output += append |
group-onsemi | 0:098463de4c5d | 475 | return output |
group-onsemi | 0:098463de4c5d | 476 | |
group-onsemi | 0:098463de4c5d | 477 | def argparse_dir_not_parent(other): |
group-onsemi | 0:098463de4c5d | 478 | """fail if argument provided is a parent of the specified directory""" |
group-onsemi | 0:098463de4c5d | 479 | def parse_type(not_parent): |
group-onsemi | 0:098463de4c5d | 480 | """The parser type""" |
group-onsemi | 0:098463de4c5d | 481 | abs_other = abspath(other) |
group-onsemi | 0:098463de4c5d | 482 | abs_not_parent = abspath(not_parent) |
group-onsemi | 0:098463de4c5d | 483 | if abs_not_parent == commonprefix([abs_not_parent, abs_other]): |
group-onsemi | 0:098463de4c5d | 484 | raise argparse.ArgumentTypeError( |
group-onsemi | 0:098463de4c5d | 485 | "{0} may not be a parent directory of {1}".format( |
group-onsemi | 0:098463de4c5d | 486 | not_parent, other)) |
group-onsemi | 0:098463de4c5d | 487 | else: |
group-onsemi | 0:098463de4c5d | 488 | return not_parent |
group-onsemi | 0:098463de4c5d | 489 | return parse_type |
group-onsemi | 0:098463de4c5d | 490 | |
group-onsemi | 0:098463de4c5d | 491 | def argparse_deprecate(replacement_message): |
group-onsemi | 0:098463de4c5d | 492 | """fail if argument is provided with deprecation warning""" |
group-onsemi | 0:098463de4c5d | 493 | def parse_type(_): |
group-onsemi | 0:098463de4c5d | 494 | """The parser type""" |
group-onsemi | 0:098463de4c5d | 495 | raise argparse.ArgumentTypeError("Deprecated." + replacement_message) |
group-onsemi | 0:098463de4c5d | 496 | return parse_type |
group-onsemi | 0:098463de4c5d | 497 | |
group-onsemi | 0:098463de4c5d | 498 | def print_large_string(large_string): |
group-onsemi | 0:098463de4c5d | 499 | """ Breaks a string up into smaller pieces before print them |
group-onsemi | 0:098463de4c5d | 500 | |
group-onsemi | 0:098463de4c5d | 501 | This is a limitation within Windows, as detailed here: |
group-onsemi | 0:098463de4c5d | 502 | https://bugs.python.org/issue11395 |
group-onsemi | 0:098463de4c5d | 503 | |
group-onsemi | 0:098463de4c5d | 504 | Positional arguments: |
group-onsemi | 0:098463de4c5d | 505 | large_string - the large string to print |
group-onsemi | 0:098463de4c5d | 506 | """ |
group-onsemi | 0:098463de4c5d | 507 | string_limit = 1000 |
group-onsemi | 0:098463de4c5d | 508 | large_string_len = len(large_string) |
group-onsemi | 0:098463de4c5d | 509 | num_parts = int(ceil(float(large_string_len) / float(string_limit))) |
group-onsemi | 0:098463de4c5d | 510 | for string_part in range(num_parts): |
group-onsemi | 0:098463de4c5d | 511 | start_index = string_part * string_limit |
group-onsemi | 0:098463de4c5d | 512 | if string_part == num_parts - 1: |
group-onsemi | 0:098463de4c5d | 513 | print large_string[start_index:] |
group-onsemi | 0:098463de4c5d | 514 | else: |
group-onsemi | 0:098463de4c5d | 515 | end_index = ((string_part + 1) * string_limit) - 1 |
group-onsemi | 0:098463de4c5d | 516 | print large_string[start_index:end_index], |