Merge pull request #1095 from Pithikos/hotfix

Fixes for premature error checking
pull/1107/head
Martin Kojtal 2015-05-14 07:44:46 +02:00
commit 31db62f4e9
1 changed files with 56 additions and 8 deletions

View File

@ -15,6 +15,8 @@ See the License for the specific language governing permissions and
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
@ -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)
@ -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)
@ -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