Commit 94adc71b authored by Damien Naudet's avatar Damien Naudet

Updated bootstrap and run_tests from silx.

parent b993aec4
......@@ -10,25 +10,17 @@ example: ./ ipython
__authors__ = ["Frédéric-Emmanuel Picca", "Jérôme Kieffer"]
__contact__ = ""
__license__ = "MIT"
__date__ = "27/11/2015"
__date__ = "07/04/2017"
import sys
import os
import shutil
import distutils.util
import subprocess
import logging
TARGET = os.path.basename(os.path.dirname(os.path.abspath(__file__))).split("-")[0]
def _copy(infile, outfile):
"link or copy file according to the OS. Nota those are HARD_LINKS"
if "link" in dir(os):, outfile)
shutil.copy(infile, outfile)
logger = logging.getLogger("bootstrap")
def _distutils_dir_name(dname="lib"):
......@@ -57,72 +49,122 @@ def _get_available_scripts(path):
return res
def _copy_files(source, dest, extn):
copy all files with a given extension from source to destination
if sys.version_info[0] >= 3: # Python3
def execfile(fullpath, globals=None, locals=None):
"Python3 implementation for execfile"
with open(fullpath) as f:
data =
except UnicodeDecodeError:
raise SyntaxError("Not a Python script")
code = compile(data, fullpath, 'exec')
exec(code, globals, locals)
def run_file(filename, argv):
if not os.path.isdir(dest):
full_src = os.path.join(os.path.dirname(__file__), source)
for clf in os.listdir(full_src):
if clf.endswith(extn) and clf not in os.listdir(dest):
_copy(os.path.join(full_src, clf), os.path.join(dest, clf))
Execute a script trying first to use execfile, then a subprocess
:param str filename: Script to execute
:param list[str] argv: Arguments passed to the filename
full_args = [filename]
def runfile(fname):
except SyntaxError:"Execute target using exec")
# execfile is considered as a local call.
# Providing globals() as locals will force to feed the file into
# globals() (for examples imports).
# Without this any function call from the executed file loses imports
old_argv = sys.argv
sys.argv = full_args"Patch the sys.argv: %s", sys.argv)"Executing %s.main()", filename)
print("########### EXECFILE ###########")
execfile(filename, globals(), globals())
sys.argv = old_argv
except SyntaxError as error:
logger.error(error)"Execute target using subprocess")
env = os.environ.copy()
env.update({"PYTHONPATH": LIBPATH + os.pathsep + os.environ.get("PYTHONPATH", ""),
"PATH": SCRIPTSPATH + os.pathsep + os.environ.get("PATH", "")})
run = subprocess.Popen(sys.argv, shell=False, env=env)
print("########### SUBPROCESS ###########")
run = subprocess.Popen(full_args, shell=False, env=env)
def find_executable_file(script_name):
"""Find a filename from a script name.
Check the script name as file path, then checks files from the 'scripts'
directory, then search the script from the PATH environment variable.
:param str script_name: Name of the script
:returns: An absolute file path, else None if nothing found.
if os.path.isfile(script_name):
return os.path.abspath(script_name)
# search the file from the script path
path = os.path.join(SCRIPTSPATH, script_name)
if os.path.isfile(path):
return path
# search the file from env PATH
for dirname in os.environ.get("PATH", "").split(os.pathsep):
path = os.path.join(dirname, script_name)
if os.path.isfile(path):
return path
return None
home = os.path.dirname(os.path.abspath(__file__))
SCRIPTSPATH = os.path.join(home,
'build', _distutils_scripts_name())
LIBPATH = (os.path.join(home,
'build', _distutils_dir_name('lib')))
if (not os.path.isdir(SCRIPTSPATH)) or (not os.path.isdir(LIBPATH)):
build = subprocess.Popen([sys.executable, "", "build"],
shell=False, cwd=os.path.dirname(__file__))
print("Build process ended with rc= %s" % build.wait())
# _copy_files("openCL", os.path.join(LIBPATH, TARGET, "openCL"), ".cl")
# _copy_files("gui", os.path.join(LIBPATH, TARGET, "gui"), ".ui")
# _copy_files("calibration", os.path.join(LIBPATH, TARGET, "calibration"), ".D")
SCRIPTSPATH = os.path.join(home, 'build', _distutils_scripts_name())
LIBPATH = os.path.join(home, 'build', _distutils_dir_name('lib'))
cwd = os.getcwd()
build = subprocess.Popen([sys.executable, "", "build"],
shell=False, cwd=os.path.dirname(os.path.abspath(__file__)))"Build process ended with rc= %s", build.wait())
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: ./ <script>\n")
print("Available scripts : %s\n" %
os.system("cd %s;python build; cd -" % home)
print("Executing %s from source checkout" % (sys.argv[1]))
logger.warning("usage: ./ <script>\n")
logger.warning("Available scripts : %s\n" %
script = None
script = sys.argv[1]
if script:"Executing %s from source checkout", script)
else:"Running iPython by default")
sys.path.insert(0, LIBPATH)
print("01. Patched sys.path with %s" % LIBPATH)
sys.path.insert(0, SCRIPTSPATH)
print("02. Patched sys.path with %s" % SCRIPTSPATH)
script = sys.argv[1]
sys.argv = sys.argv[1:]
print("03. patch the sys.argv : ", sys.argv)"Patched sys.path with %s", LIBPATH)
print("04. Executing %s.main()" % (script,))
fullpath = os.path.join(SCRIPTSPATH, script)
if os.path.exists(fullpath):
if script:
argv = sys.argv[2:]
fullpath = find_executable_file(script)
if fullpath is not None:
run_file(fullpath, argv)
logger.error("Script %s not found", script)
if os.path.exists(script):
runfile(script)"Patch the sys.argv: %s", sys.argv)
sys.path.insert(2, "")
from IPython import embed
except Exception as err:
logger.error("Unable to execute iPython, using normal Python")
import code
for dirname in os.environ.get("PATH", "").split(os.pathsep):
fullpath = os.path.join(dirname, script)
if os.path.exists(fullpath):
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment