Clone of official tools
utils.py@19:3604ee113e2d, 2016-07-15 (annotated)
- Committer:
- screamer
- Date:
- Fri Jul 15 13:11:30 2016 +0100
- Revision:
- 19:3604ee113e2d
- Parent:
- 17:04753e1e329d
- Child:
- 21:4fdf0dd04f6f
Fixed missing import
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
screamer | 0:66f3b5499f7f | 17 | import sys |
screamer | 0:66f3b5499f7f | 18 | import inspect |
screamer | 0:66f3b5499f7f | 19 | import os |
screamer | 0:66f3b5499f7f | 20 | from os import listdir, remove, makedirs |
screamer | 0:66f3b5499f7f | 21 | from shutil import copyfile |
screamer | 0:66f3b5499f7f | 22 | from os.path import isdir, join, exists, split, relpath, splitext |
screamer | 0:66f3b5499f7f | 23 | from subprocess import Popen, PIPE, STDOUT, call |
screamer | 7:5af61d55adbe | 24 | import json |
screamer | 7:5af61d55adbe | 25 | from collections import OrderedDict |
screamer | 19:3604ee113e2d | 26 | import logging |
screamer | 0:66f3b5499f7f | 27 | |
screamer | 0:66f3b5499f7f | 28 | def cmd(l, check=True, verbose=False, shell=False, cwd=None): |
screamer | 0:66f3b5499f7f | 29 | text = l if shell else ' '.join(l) |
screamer | 0:66f3b5499f7f | 30 | if verbose: |
screamer | 0:66f3b5499f7f | 31 | print text |
screamer | 0:66f3b5499f7f | 32 | rc = call(l, shell=shell, cwd=cwd) |
screamer | 0:66f3b5499f7f | 33 | if check and rc != 0: |
screamer | 0:66f3b5499f7f | 34 | raise Exception('ERROR %d: "%s"' % (rc, text)) |
screamer | 0:66f3b5499f7f | 35 | |
screamer | 0:66f3b5499f7f | 36 | |
screamer | 17:04753e1e329d | 37 | def run_cmd(command, work_dir=None, chroot=None, redirect=False): |
screamer | 17:04753e1e329d | 38 | if chroot: |
screamer | 17:04753e1e329d | 39 | # Conventions managed by the web team for the mbed.org build system |
screamer | 17:04753e1e329d | 40 | chroot_cmd = [ |
screamer | 17:04753e1e329d | 41 | '/usr/sbin/chroot', '--userspec=33:33', chroot |
screamer | 17:04753e1e329d | 42 | ] |
screamer | 17:04753e1e329d | 43 | for c in command: |
screamer | 17:04753e1e329d | 44 | chroot_cmd += [c.replace(chroot, '')] |
screamer | 17:04753e1e329d | 45 | |
screamer | 17:04753e1e329d | 46 | logging.debug("Running command %s"%' '.join(chroot_cmd)) |
screamer | 17:04753e1e329d | 47 | command = chroot_cmd |
screamer | 17:04753e1e329d | 48 | work_dir = chroot |
screamer | 17:04753e1e329d | 49 | |
screamer | 0:66f3b5499f7f | 50 | assert is_cmd_valid(command[0]) |
screamer | 0:66f3b5499f7f | 51 | try: |
screamer | 17:04753e1e329d | 52 | p = Popen(command, stdout=PIPE, stderr=STDOUT if redirect else PIPE, cwd=work_dir) |
screamer | 0:66f3b5499f7f | 53 | _stdout, _stderr = p.communicate() |
screamer | 13:ab47a20b66f0 | 54 | except OSError as e: |
screamer | 0:66f3b5499f7f | 55 | print "[OS ERROR] Command: "+(' '.join(command)) |
screamer | 0:66f3b5499f7f | 56 | raise |
screamer | 13:ab47a20b66f0 | 57 | |
screamer | 0:66f3b5499f7f | 58 | return _stdout, _stderr, p.returncode |
screamer | 0:66f3b5499f7f | 59 | |
screamer | 0:66f3b5499f7f | 60 | |
screamer | 0:66f3b5499f7f | 61 | def run_cmd_ext(command): |
screamer | 0:66f3b5499f7f | 62 | assert is_cmd_valid(command[0]) |
screamer | 0:66f3b5499f7f | 63 | p = Popen(command, stdout=PIPE, stderr=PIPE) |
screamer | 0:66f3b5499f7f | 64 | _stdout, _stderr = p.communicate() |
screamer | 0:66f3b5499f7f | 65 | return _stdout, _stderr, p.returncode |
screamer | 0:66f3b5499f7f | 66 | |
screamer | 0:66f3b5499f7f | 67 | |
screamer | 0:66f3b5499f7f | 68 | def is_cmd_valid(cmd): |
screamer | 0:66f3b5499f7f | 69 | caller = get_caller_name() |
screamer | 0:66f3b5499f7f | 70 | abspath = find_cmd_abspath(cmd) |
screamer | 0:66f3b5499f7f | 71 | if not abspath: |
screamer | 0:66f3b5499f7f | 72 | error("%s: Command '%s' can't be found" % (caller, cmd)) |
screamer | 0:66f3b5499f7f | 73 | if not is_exec(abspath): |
screamer | 0:66f3b5499f7f | 74 | error("%s: Command '%s' resolves to file '%s' which is not executable" % (caller, cmd, abspath)) |
screamer | 0:66f3b5499f7f | 75 | return True |
screamer | 0:66f3b5499f7f | 76 | |
screamer | 0:66f3b5499f7f | 77 | |
screamer | 0:66f3b5499f7f | 78 | def is_exec(path): |
screamer | 0:66f3b5499f7f | 79 | return os.access(path, os.X_OK) or os.access(path+'.exe', os.X_OK) |
screamer | 0:66f3b5499f7f | 80 | |
screamer | 0:66f3b5499f7f | 81 | |
screamer | 0:66f3b5499f7f | 82 | def find_cmd_abspath(cmd): |
screamer | 0:66f3b5499f7f | 83 | """ Returns the absolute path to a command. |
screamer | 0:66f3b5499f7f | 84 | None is returned if no absolute path was found. |
screamer | 0:66f3b5499f7f | 85 | """ |
screamer | 0:66f3b5499f7f | 86 | if exists(cmd) or exists(cmd + '.exe'): |
screamer | 0:66f3b5499f7f | 87 | return os.path.abspath(cmd) |
screamer | 0:66f3b5499f7f | 88 | if not 'PATH' in os.environ: |
screamer | 0:66f3b5499f7f | 89 | raise Exception("Can't find command path for current platform ('%s')" % sys.platform) |
screamer | 0:66f3b5499f7f | 90 | PATH=os.environ['PATH'] |
screamer | 0:66f3b5499f7f | 91 | for path in PATH.split(os.pathsep): |
screamer | 0:66f3b5499f7f | 92 | abspath = '%s/%s' % (path, cmd) |
screamer | 0:66f3b5499f7f | 93 | if exists(abspath) or exists(abspath + '.exe'): |
screamer | 0:66f3b5499f7f | 94 | return abspath |
screamer | 0:66f3b5499f7f | 95 | |
screamer | 0:66f3b5499f7f | 96 | |
screamer | 0:66f3b5499f7f | 97 | def mkdir(path): |
screamer | 0:66f3b5499f7f | 98 | if not exists(path): |
screamer | 0:66f3b5499f7f | 99 | makedirs(path) |
screamer | 0:66f3b5499f7f | 100 | |
screamer | 0:66f3b5499f7f | 101 | |
screamer | 0:66f3b5499f7f | 102 | def copy_file(src, dst): |
screamer | 0:66f3b5499f7f | 103 | """ Implement the behaviour of "shutil.copy(src, dst)" without copying the |
screamer | 0:66f3b5499f7f | 104 | permissions (this was causing errors with directories mounted with samba) |
screamer | 0:66f3b5499f7f | 105 | """ |
screamer | 0:66f3b5499f7f | 106 | if isdir(dst): |
screamer | 0:66f3b5499f7f | 107 | _, file = split(src) |
screamer | 0:66f3b5499f7f | 108 | dst = join(dst, file) |
screamer | 0:66f3b5499f7f | 109 | copyfile(src, dst) |
screamer | 0:66f3b5499f7f | 110 | |
screamer | 0:66f3b5499f7f | 111 | |
screamer | 0:66f3b5499f7f | 112 | def delete_dir_files(dir): |
screamer | 0:66f3b5499f7f | 113 | if not exists(dir): |
screamer | 0:66f3b5499f7f | 114 | return |
screamer | 0:66f3b5499f7f | 115 | |
screamer | 0:66f3b5499f7f | 116 | for f in listdir(dir): |
screamer | 0:66f3b5499f7f | 117 | file = join(dir, f) |
screamer | 0:66f3b5499f7f | 118 | if not isdir(file): |
screamer | 0:66f3b5499f7f | 119 | remove(file) |
screamer | 0:66f3b5499f7f | 120 | |
screamer | 0:66f3b5499f7f | 121 | |
screamer | 0:66f3b5499f7f | 122 | def get_caller_name(steps=2): |
screamer | 0:66f3b5499f7f | 123 | """ |
screamer | 0:66f3b5499f7f | 124 | When called inside a function, it returns the name |
screamer | 0:66f3b5499f7f | 125 | of the caller of that function. |
screamer | 0:66f3b5499f7f | 126 | """ |
screamer | 0:66f3b5499f7f | 127 | return inspect.stack()[steps][3] |
screamer | 0:66f3b5499f7f | 128 | |
screamer | 0:66f3b5499f7f | 129 | |
screamer | 0:66f3b5499f7f | 130 | def error(msg): |
screamer | 0:66f3b5499f7f | 131 | print("ERROR: %s" % msg) |
screamer | 0:66f3b5499f7f | 132 | sys.exit(1) |
screamer | 0:66f3b5499f7f | 133 | |
screamer | 0:66f3b5499f7f | 134 | |
screamer | 0:66f3b5499f7f | 135 | def rel_path(path, base, dot=False): |
screamer | 0:66f3b5499f7f | 136 | p = relpath(path, base) |
screamer | 0:66f3b5499f7f | 137 | if dot and not p.startswith('.'): |
screamer | 0:66f3b5499f7f | 138 | p = './' + p |
screamer | 0:66f3b5499f7f | 139 | return p |
screamer | 0:66f3b5499f7f | 140 | |
screamer | 0:66f3b5499f7f | 141 | |
screamer | 0:66f3b5499f7f | 142 | class ToolException(Exception): |
screamer | 0:66f3b5499f7f | 143 | pass |
screamer | 0:66f3b5499f7f | 144 | |
screamer | 0:66f3b5499f7f | 145 | class NotSupportedException(Exception): |
screamer | 0:66f3b5499f7f | 146 | pass |
screamer | 0:66f3b5499f7f | 147 | |
screamer | 0:66f3b5499f7f | 148 | def split_path(path): |
screamer | 0:66f3b5499f7f | 149 | base, file = split(path) |
screamer | 0:66f3b5499f7f | 150 | name, ext = splitext(file) |
screamer | 0:66f3b5499f7f | 151 | return base, name, ext |
screamer | 0:66f3b5499f7f | 152 | |
screamer | 0:66f3b5499f7f | 153 | |
screamer | 0:66f3b5499f7f | 154 | def args_error(parser, message): |
screamer | 0:66f3b5499f7f | 155 | print "\n\n%s\n\n" % message |
screamer | 0:66f3b5499f7f | 156 | parser.print_help() |
screamer | 0:66f3b5499f7f | 157 | sys.exit() |
screamer | 0:66f3b5499f7f | 158 | |
screamer | 0:66f3b5499f7f | 159 | |
screamer | 0:66f3b5499f7f | 160 | def construct_enum(**enums): |
screamer | 0:66f3b5499f7f | 161 | """ Create your own pseudo-enums """ |
screamer | 0:66f3b5499f7f | 162 | return type('Enum', (), enums) |
screamer | 0:66f3b5499f7f | 163 | |
screamer | 0:66f3b5499f7f | 164 | |
screamer | 0:66f3b5499f7f | 165 | def check_required_modules(required_modules, verbose=True): |
screamer | 0:66f3b5499f7f | 166 | """ Function checks for Python modules which should be "importable" (installed) |
screamer | 0:66f3b5499f7f | 167 | before test suite can be used. |
screamer | 0:66f3b5499f7f | 168 | @return returns True if all modules are installed already |
screamer | 0:66f3b5499f7f | 169 | """ |
screamer | 0:66f3b5499f7f | 170 | import imp |
screamer | 0:66f3b5499f7f | 171 | not_installed_modules = [] |
screamer | 0:66f3b5499f7f | 172 | for module_name in required_modules: |
screamer | 0:66f3b5499f7f | 173 | try: |
screamer | 0:66f3b5499f7f | 174 | imp.find_module(module_name) |
screamer | 0:66f3b5499f7f | 175 | except ImportError as e: |
screamer | 0:66f3b5499f7f | 176 | # We also test against a rare case: module is an egg file |
screamer | 0:66f3b5499f7f | 177 | try: |
screamer | 0:66f3b5499f7f | 178 | __import__(module_name) |
screamer | 0:66f3b5499f7f | 179 | except ImportError as e: |
screamer | 0:66f3b5499f7f | 180 | not_installed_modules.append(module_name) |
screamer | 0:66f3b5499f7f | 181 | if verbose: |
screamer | 0:66f3b5499f7f | 182 | print "Error: %s" % e |
screamer | 0:66f3b5499f7f | 183 | |
screamer | 0:66f3b5499f7f | 184 | if verbose: |
screamer | 0:66f3b5499f7f | 185 | if not_installed_modules: |
screamer | 0:66f3b5499f7f | 186 | print "Warning: Module(s) %s not installed. Please install required module(s) before using this script."% (', '.join(not_installed_modules)) |
screamer | 0:66f3b5499f7f | 187 | |
screamer | 0:66f3b5499f7f | 188 | if not_installed_modules: |
screamer | 0:66f3b5499f7f | 189 | return False |
screamer | 0:66f3b5499f7f | 190 | else: |
screamer | 0:66f3b5499f7f | 191 | return True |
screamer | 7:5af61d55adbe | 192 | |
screamer | 7:5af61d55adbe | 193 | # Utility function: traverse a dictionary and change all the strings in the dictionary to |
screamer | 7:5af61d55adbe | 194 | # ASCII from Unicode. Useful when reading ASCII JSON data, because the JSON decoder always |
screamer | 7:5af61d55adbe | 195 | # returns Unicode string. |
screamer | 7:5af61d55adbe | 196 | # Based on http://stackoverflow.com/a/13105359 |
screamer | 7:5af61d55adbe | 197 | def dict_to_ascii(input): |
screamer | 7:5af61d55adbe | 198 | if isinstance(input, dict): |
screamer | 7:5af61d55adbe | 199 | return OrderedDict([(dict_to_ascii(key), dict_to_ascii(value)) for key, value in input.iteritems()]) |
screamer | 7:5af61d55adbe | 200 | elif isinstance(input, list): |
screamer | 7:5af61d55adbe | 201 | return [dict_to_ascii(element) for element in input] |
screamer | 7:5af61d55adbe | 202 | elif isinstance(input, unicode): |
screamer | 7:5af61d55adbe | 203 | return input.encode('ascii') |
screamer | 7:5af61d55adbe | 204 | else: |
screamer | 7:5af61d55adbe | 205 | return input |
screamer | 7:5af61d55adbe | 206 | |
screamer | 7:5af61d55adbe | 207 | # Read a JSON file and return its Python representation, transforming all the strings from Unicode |
screamer | 7:5af61d55adbe | 208 | # to ASCII. The order of keys in the JSON file is preserved. |
screamer | 7:5af61d55adbe | 209 | def json_file_to_dict(fname): |
screamer | 7:5af61d55adbe | 210 | with open(fname, "rt") as f: |
screamer | 7:5af61d55adbe | 211 | return dict_to_ascii(json.load(f, object_pairs_hook=OrderedDict)) |