3 Copyright (c) 2011-2013 ARM Limited
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 def cmd(l, check=True, verbose=False, shell=False, cwd=None):
25 text = l if shell else ' '.join(l)
28 rc = call(l, shell=shell, cwd=cwd)
30 raise Exception('ERROR %d: "%s"' % (rc, text))
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
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
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)
60 def delete_dir_files(dir):
64 for f in listdir(dir):
75 def rel_path(path, base, dot=False):
76 p = relpath(path, base)
77 if dot and not p.startswith('.'):
82 class ToolException(Exception):
87 base, file = split(path)
88 name, ext = splitext(file)
89 return base, name, ext
92 def args_error(parser, message):
93 print "\n\n%s\n\n" % message
98 def construct_enum(**enums):
99 """ Create your own pseudo-enums """
100 return type('Enum', (), enums)
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
109 all_modules_found = True
110 not_installed_modules = []
111 for module_name in required_modules:
113 imp.find_module(module_name)
114 except ImportError as e:
115 all_modules_found = False
116 not_installed_modules.append(module_name)
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