]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/workspace_tools/utils.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / workspace_tools / utils.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17 import sys
18 from os import listdir, remove, makedirs
19 from shutil import copyfile
20 from os.path import isdir, join, exists, split, relpath, splitext
21 from subprocess import Popen, PIPE, STDOUT, call
22
23
24 def cmd(l, check=True, verbose=False, shell=False, cwd=None):
25 text = l if shell else ' '.join(l)
26 if verbose:
27 print text
28 rc = call(l, shell=shell, cwd=cwd)
29 if check and rc != 0:
30 raise Exception('ERROR %d: "%s"' % (rc, text))
31
32
33 def run_cmd(command, wd=None, redirect=False):
34 p = Popen(command, stdout=PIPE, stderr=STDOUT if redirect else PIPE, cwd=wd)
35 _stdout, _stderr = p.communicate()
36 return _stdout, _stderr, p.returncode
37
38
39 def run_cmd_ext(command):
40 p = Popen(command, stdout=PIPE, stderr=PIPE)
41 _stdout, _stderr = p.communicate()
42 return _stdout, _stderr, p.returncode
43
44
45 def mkdir(path):
46 if not exists(path):
47 makedirs(path)
48
49
50 def copy_file(src, dst):
51 """ Implement the behaviour of "shutil.copy(src, dst)" without copying the
52 permissions (this was causing errors with directories mounted with samba)
53 """
54 if isdir(dst):
55 _, file = split(src)
56 dst = join(dst, file)
57 copyfile(src, dst)
58
59
60 def delete_dir_files(dir):
61 if not exists(dir):
62 return
63
64 for f in listdir(dir):
65 file = join(dir, f)
66 if not isdir(file):
67 remove(file)
68
69
70 def error(msg):
71 print msg
72 sys.exit(1)
73
74
75 def rel_path(path, base, dot=False):
76 p = relpath(path, base)
77 if dot and not p.startswith('.'):
78 p = './' + p
79 return p
80
81
82 class ToolException(Exception):
83 pass
84
85
86 def split_path(path):
87 base, file = split(path)
88 name, ext = splitext(file)
89 return base, name, ext
90
91
92 def args_error(parser, message):
93 print "\n\n%s\n\n" % message
94 parser.print_help()
95 sys.exit()
96
97
98 def construct_enum(**enums):
99 """ Create your own pseudo-enums """
100 return type('Enum', (), enums)
101
102
103 def check_required_modules(required_modules, verbose=True):
104 """ Function checks for Python modules which should be "importable" (installed)
105 before test suite can be used.
106 @return returns True if all modules are installed already
107 """
108 import imp
109 all_modules_found = True
110 not_installed_modules = []
111 for module_name in required_modules:
112 try:
113 imp.find_module(module_name)
114 except ImportError as e:
115 all_modules_found = False
116 not_installed_modules.append(module_name)
117 if verbose:
118 print "Error: %s"% e
119 if verbose:
120 if not all_modules_found:
121 print "Warning: Module(s) %s not installed. Please install required module(s) before using this script."% (', '.join(not_installed_modules))
122 return all_modules_found
Imprint / Impressum