Commit cbaab0e9 authored by Thomas Vincent's avatar Thomas Vincent

Synchronize bootstrap script with latest version from silx

parent 16fa54fd
......@@ -10,7 +10,7 @@ example: ./bootstrap.py ipython
__authors__ = ["Frédéric-Emmanuel Picca", "Jérôme Kieffer"]
__contact__ = "jerome.kieffer@esrf.eu"
__license__ = "MIT"
__date__ = "07/04/2017"
__date__ = "08/01/2018"
import sys
......@@ -23,6 +23,20 @@ logging.basicConfig()
logger = logging.getLogger("bootstrap")
def is_debug_python():
"""Returns true if the Python interpreter is in debug mode."""
try:
import sysconfig
except ImportError: # pragma nocover
# Python < 2.7
import distutils.sysconfig as sysconfig
if sysconfig.get_config_var("Py_DEBUG"):
return True
return hasattr(sys, "gettotalrefcount")
def _distutils_dir_name(dname="lib"):
"""
Returns the name of a distutils build directory
......@@ -30,6 +44,8 @@ def _distutils_dir_name(dname="lib"):
platform = distutils.util.get_platform()
architecture = "%s.%s-%i.%i" % (dname, platform,
sys.version_info[0], sys.version_info[1])
if is_debug_python():
architecture += "-pydebug"
return architecture
......@@ -83,7 +99,9 @@ def run_file(filename, argv):
logger.info("Patch the sys.argv: %s", sys.argv)
logger.info("Executing %s.main()", filename)
print("########### EXECFILE ###########")
execfile(filename, globals(), globals())
module_globals = globals().copy()
module_globals['__file__'] = filename
execfile(filename, module_globals, module_globals)
finally:
sys.argv = old_argv
except SyntaxError as error:
......@@ -91,53 +109,101 @@ def run_file(filename, argv):
logger.info("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", "")})
"PATH": os.environ.get("PATH", "")})
print("########### SUBPROCESS ###########")
run = subprocess.Popen(full_args, shell=False, env=env)
run.wait()
def find_executable_file(script_name):
def run_entry_point(entry_point, argv):
"""
Execute an entry_point using the current python context
(http://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation)
:param str entry_point: A string identifying a function from a module
(NAME = PACKAGE.MODULE:FUNCTION)
"""
import importlib
elements = entry_point.split("=")
target_name = elements[0].strip()
elements = elements[1].split(":")
module_name = elements[0].strip()
function_name = elements[1].strip()
logger.info("Execute target %s (function %s from module %s) using importlib", target_name, function_name, module_name)
full_args = [target_name]
full_args.extend(argv)
try:
old_argv = sys.argv
sys.argv = full_args
print("########### IMPORTLIB ###########")
module = importlib.import_module(module_name)
if hasattr(module, function_name):
func = getattr(module, function_name)
func()
else:
logger.info("Function %s not found", function_name)
finally:
sys.argv = old_argv
def find_executable(target):
"""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.
- Check the script name as file path,
- Then checks if the name is a target of the setup.py
- 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.
:param str target: Name of the script
:returns: Returns a tuple: kind, name.
"""
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
if os.path.isfile(target):
return ("path", os.path.abspath(target))
# search the file from setup.py
import setup
config = setup.get_project_configuration(dry_run=True)
# scripts from project configuration
if "scripts" in config:
for script_name in config["scripts"]:
if os.path.basename(script) == target:
return ("path", os.path.abspath(script_name))
# entry-points from project configuration
if "entry_points" in config:
for kind in config["entry_points"]:
for entry_point in config["entry_points"][kind]:
elements = entry_point.split("=")
name = elements[0].strip()
if name == target:
return ("entry_point", entry_point)
# search the file from env PATH
for dirname in os.environ.get("PATH", "").split(os.pathsep):
path = os.path.join(dirname, script_name)
path = os.path.join(dirname, target)
if os.path.isfile(path):
return path
return ("path", path)
return None
return None, 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'))
cwd = os.getcwd()
os.chdir(home)
build = subprocess.Popen([sys.executable, "setup.py", "build"],
shell=False, cwd=os.path.dirname(os.path.abspath(__file__)))
logger.info("Build process ended with rc= %s", build.wait())
build_rc = build.wait()
os.chdir(cwd)
if build_rc == 0:
logger.info("Build process ended.")
else:
logger.error("Build process ended with rc=%s", build_rc)
sys.exit(-1)
if __name__ == "__main__":
if len(sys.argv) < 2:
logger.warning("usage: ./bootstrap.py <script>\n")
logger.warning("Available scripts : %s\n" %
_get_available_scripts(SCRIPTSPATH))
script = None
else:
script = sys.argv[1]
......@@ -151,9 +217,11 @@ if __name__ == "__main__":
if script:
argv = sys.argv[2:]
fullpath = find_executable_file(script)
if fullpath is not None:
run_file(fullpath, argv)
kind, target = find_executable(script)
if kind == "path":
run_file(target, argv)
elif kind == "entry_point":
run_entry_point(target, argv)
else:
logger.error("Script %s not found", script)
else:
......
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