Skip to content

Fixes for premature error checking #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions workspace_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
limitations under the License.
"""
import sys
import inspect
import os
from os import listdir, remove, makedirs
from shutil import copyfile
from os.path import isdir, join, exists, split, relpath, splitext
Expand All @@ -31,17 +33,48 @@ def cmd(l, check=True, verbose=False, shell=False, cwd=None):


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


def run_cmd_ext(command):
assert is_cmd_valid(command[0])
p = Popen(command, stdout=PIPE, stderr=PIPE)
_stdout, _stderr = p.communicate()
return _stdout, _stderr, p.returncode


def is_cmd_valid(cmd):
caller = get_caller_name()
abspath = find_cmd_abspath(cmd)
if not abspath:
error("%s: Command '%s' can't be found" % (caller, cmd))
if not is_exec(abspath):
error("%s: Command '%s' resolves to file '%s' which is not executable" % (caller, cmd, abspath))
return True


def is_exec(path):
return os.access(path, os.X_OK)


def find_cmd_abspath(cmd):
""" Returns the absolute path to a command.
None is returned if no absolute path was found.
"""
if exists(cmd):
return os.path.abspath(cmd)
if not 'PATH' in os.environ:
raise Exception("Can't find command path for current platform ('%s')" % sys.platform)
PATH=os.environ['PATH']
for path in PATH.split(os.pathsep):
abspath = '%s/%s' % (path, cmd)
if exists(abspath):
return abspath


def mkdir(path):
if not exists(path):
makedirs(path)
Expand All @@ -67,8 +100,16 @@ def delete_dir_files(dir):
remove(file)


def get_caller_name(steps=2):
"""
When called inside a function, it returns the name
of the caller of that function.
"""
return inspect.stack()[steps][3]


def error(msg):
print msg
print("ERROR: %s" % msg)
sys.exit(1)


Expand Down Expand Up @@ -106,17 +147,24 @@ def check_required_modules(required_modules, verbose=True):
@return returns True if all modules are installed already
"""
import imp
all_modules_found = True
not_installed_modules = []
for module_name in required_modules:
try:
imp.find_module(module_name)
except ImportError as e:
all_modules_found = False
not_installed_modules.append(module_name)
if verbose:
print "Error: %s"% e
# We also test against a rare case: module is an egg file
try:
__import__(module_name)
except ImportError as e:
not_installed_modules.append(module_name)
if verbose:
print "Error: %s" % e

if verbose:
if not all_modules_found:
if not_installed_modules:
print "Warning: Module(s) %s not installed. Please install required module(s) before using this script."% (', '.join(not_installed_modules))
return all_modules_found

if not_installed_modules:
return False
else:
return True