15
15
limitations under the License.
16
16
"""
17
17
import sys
18
+ import inspect
19
+ import os
18
20
from os import listdir , remove , makedirs
19
21
from shutil import copyfile
20
22
from os .path import isdir , join , exists , split , relpath , splitext
@@ -31,17 +33,48 @@ def cmd(l, check=True, verbose=False, shell=False, cwd=None):
31
33
32
34
33
35
def run_cmd (command , wd = None , redirect = False ):
36
+ assert is_cmd_valid (command [0 ])
34
37
p = Popen (command , stdout = PIPE , stderr = STDOUT if redirect else PIPE , cwd = wd )
35
38
_stdout , _stderr = p .communicate ()
36
39
return _stdout , _stderr , p .returncode
37
40
38
41
39
42
def run_cmd_ext (command ):
43
+ assert is_cmd_valid (command [0 ])
40
44
p = Popen (command , stdout = PIPE , stderr = PIPE )
41
45
_stdout , _stderr = p .communicate ()
42
46
return _stdout , _stderr , p .returncode
43
47
44
48
49
+ def is_cmd_valid (cmd ):
50
+ caller = get_caller_name ()
51
+ abspath = find_cmd_abspath (cmd )
52
+ if not abspath :
53
+ error ("%s: Command '%s' can't be found" % (caller , cmd ))
54
+ if not is_exec (abspath ):
55
+ error ("%s: Command '%s' resolves to file '%s' which is not executable" % (caller , cmd , abspath ))
56
+ return True
57
+
58
+
59
+ def is_exec (path ):
60
+ return os .access (path , os .X_OK )
61
+
62
+
63
+ def find_cmd_abspath (cmd ):
64
+ """ Returns the absolute path to a command.
65
+ None is returned if no absolute path was found.
66
+ """
67
+ if exists (cmd ):
68
+ return os .path .abspath (cmd )
69
+ if not 'PATH' in os .environ :
70
+ raise Exception ("Can't find command path for current platform ('%s')" % sys .platform )
71
+ PATH = os .environ ['PATH' ]
72
+ for path in PATH .split (os .pathsep ):
73
+ abspath = '%s/%s' % (path , cmd )
74
+ if exists (abspath ):
75
+ return abspath
76
+
77
+
45
78
def mkdir (path ):
46
79
if not exists (path ):
47
80
makedirs (path )
@@ -67,8 +100,16 @@ def delete_dir_files(dir):
67
100
remove (file )
68
101
69
102
103
+ def get_caller_name (steps = 2 ):
104
+ """
105
+ When called inside a function, it returns the name
106
+ of the caller of that function.
107
+ """
108
+ return inspect .stack ()[steps ][3 ]
109
+
110
+
70
111
def error (msg ):
71
- print msg
112
+ print ( "ERROR: %s" % msg )
72
113
sys .exit (1 )
73
114
74
115
@@ -106,17 +147,24 @@ def check_required_modules(required_modules, verbose=True):
106
147
@return returns True if all modules are installed already
107
148
"""
108
149
import imp
109
- all_modules_found = True
110
150
not_installed_modules = []
111
151
for module_name in required_modules :
112
152
try :
113
153
imp .find_module (module_name )
114
154
except ImportError as e :
115
- all_modules_found = False
116
- not_installed_modules .append (module_name )
117
- if verbose :
118
- print "Error: %s" % e
155
+ # We also test against a rare case: module is an egg file
156
+ try :
157
+ __import__ (module_name )
158
+ except ImportError as e :
159
+ not_installed_modules .append (module_name )
160
+ if verbose :
161
+ print "Error: %s" % e
162
+
119
163
if verbose :
120
- if not all_modules_found :
164
+ if not_installed_modules :
121
165
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
166
+
167
+ if not_installed_modules :
168
+ return False
169
+ else :
170
+ return True
0 commit comments