Commit 9c5efc13 authored by Thomas Vincent's avatar Thomas Vincent
Browse files

Sync bootstrap.py with that of silx

parent b30abfe8
......@@ -10,14 +10,17 @@ example: ./bootstrap.py ipython
__authors__ = ["Frédéric-Emmanuel Picca", "Jérôme Kieffer"]
__contact__ = "jerome.kieffer@esrf.eu"
__license__ = "MIT"
__date__ = "02/03/2018"
__date__ = "26/07/2018"
import sys
import os
import argparse
import distutils.util
import subprocess
import logging
import os
import subprocess
import sys
import tempfile
logging.basicConfig()
logger = logging.getLogger("bootstrap")
......@@ -121,7 +124,7 @@ def run_entry_point(entry_point, argv):
(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)
(NAME = PACKAGE.MODULE:FUNCTION [EXTRA])
"""
import importlib
elements = entry_point.split("=")
......@@ -187,56 +190,100 @@ def find_executable(target):
return None, None
if __name__ == "__main__":
home = os.path.dirname(os.path.abspath(__file__))
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__)))
build_rc = build.wait()
if not os.path.exists(LIBPATH):
logger.warning("`lib` directory does not exist, trying common Python3 lib")
LIBPATH = os.path.join(os.path.split(LIBPATH)[0], "lib")
os.chdir(cwd)
def main(argv):
parser = argparse.ArgumentParser(
prog="bootstrap", usage="./bootstrap.py <script>", description=__doc__)
parser.add_argument("script", nargs=argparse.REMAINDER)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-m", nargs=argparse.REMAINDER, dest='module',
help="run library module as a script (terminates option list)")
group.add_argument(
"-j", "--jupyter", action='store_true',
help="Start jupyter notebook rather than IPython console")
options = parser.parse_args()
if build_rc == 0:
logger.info("Build process ended.")
else:
logger.error("Build process ended with rc=%s", build_rc)
sys.exit(-1)
if options.jupyter:
if options.script:
logger.error("-j, --jupyter is mutually exclusive with other options")
parser.print_help()
return
if len(sys.argv) < 2:
logger.warning("usage: ./bootstrap.py <script>\n")
script = None
else:
script = sys.argv[1]
logger.info("Start Jupyter notebook")
from notebook.notebookapp import main as notebook_main
os.environ["PYTHONPATH"] = LIBPATH + os.pathsep + os.environ.get("PYTHONPATH", "")
if script:
logger.info("Executing %s from source checkout", script)
else:
logging.info("Running iPython by default")
sys.path.insert(0, LIBPATH)
logger.info("Patched sys.path with %s", LIBPATH)
filename = os.path.join(LIBPATH, '.__bootstrap_pythonstartup.py')
with open(filename, 'w') as fp:
fp.write('import sys; sys.path.pop(0)')
os.environ["PYTHONSTARTUP"] = filename
notebook_main(argv=[])
if script:
argv = sys.argv[2:]
try:
os.remove(filename)
except:
logger.error("Cannot delete temporary file: %s", filename)
elif options.script:
logger.info("Executing %s from source checkout", options.script)
script = options.script[0]
argv = options.script[1:]
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)
logger.error("Script %s not found", options.script)
elif options.module:
logging.info("Running module %s", options.module)
import runpy
module = options.module[0]
try:
old = sys.argv
sys.argv = [None] + options.module[1:]
runpy.run_module(module, run_name="__main__", alter_sys=True)
finally:
sys.argv = old
else:
logging.info("Running IPython by default")
logger.info("Patch the sys.argv: %s", sys.argv)
sys.path.insert(2, "")
try:
from IPython import embed
from IPython import start_ipython
except Exception as err:
logger.error("Unable to execute iPython, using normal Python")
logger.error(err)
import code
code.interact()
else:
embed()
start_ipython(argv=[])
if __name__ == "__main__":
home = os.path.dirname(os.path.abspath(__file__))
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)
build_rc = build.wait()
if not os.path.exists(LIBPATH):
logger.warning("`lib` directory does not exist, trying common Python3 lib")
LIBPATH = os.path.join(os.path.split(LIBPATH)[0], "lib")
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)
sys.path.insert(0, LIBPATH)
logger.info("Patched sys.path with %s", LIBPATH)
main(sys.argv)
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