Skip to content

Commit 31db62f

Browse files
committed
Merge pull request #1095 from Pithikos/hotfix
Fixes for premature error checking
2 parents b788b13 + 6c3cb2c commit 31db62f

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

workspace_tools/utils.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
limitations under the License.
1616
"""
1717
import sys
18+
import inspect
19+
import os
1820
from os import listdir, remove, makedirs
1921
from shutil import copyfile
2022
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):
3133

3234

3335
def run_cmd(command, wd=None, redirect=False):
36+
assert is_cmd_valid(command[0])
3437
p = Popen(command, stdout=PIPE, stderr=STDOUT if redirect else PIPE, cwd=wd)
3538
_stdout, _stderr = p.communicate()
3639
return _stdout, _stderr, p.returncode
3740

3841

3942
def run_cmd_ext(command):
43+
assert is_cmd_valid(command[0])
4044
p = Popen(command, stdout=PIPE, stderr=PIPE)
4145
_stdout, _stderr = p.communicate()
4246
return _stdout, _stderr, p.returncode
4347

4448

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+
4578
def mkdir(path):
4679
if not exists(path):
4780
makedirs(path)
@@ -67,8 +100,16 @@ def delete_dir_files(dir):
67100
remove(file)
68101

69102

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+
70111
def error(msg):
71-
print msg
112+
print("ERROR: %s" % msg)
72113
sys.exit(1)
73114

74115

@@ -106,17 +147,24 @@ def check_required_modules(required_modules, verbose=True):
106147
@return returns True if all modules are installed already
107148
"""
108149
import imp
109-
all_modules_found = True
110150
not_installed_modules = []
111151
for module_name in required_modules:
112152
try:
113153
imp.find_module(module_name)
114154
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+
119163
if verbose:
120-
if not all_modules_found:
164+
if not_installed_modules:
121165
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

Comments
 (0)