2013-07-15 20:21:24 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-11-21 18:24:00 +08:00
|
|
|
Bootstrap helps you to test pyFAI scripts without installing them
|
2014-11-28 01:42:28 +08:00
|
|
|
by patching your PYTHONPATH on the fly
|
2013-07-15 20:21:24 +08:00
|
|
|
|
|
|
|
example: ./bootstrap.py pyFAI-integrate test/testimages/Pilatus1M.edf
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__authors__ = ["Frédéric-Emmanuel Picca", "Jérôme Kieffer"]
|
|
|
|
__contact__ = "jerome.kieffer@esrf.eu"
|
|
|
|
__license__ = "GPLv3+"
|
2016-08-02 18:32:55 +08:00
|
|
|
__date__ = "02/08/2016"
|
2014-11-28 01:42:28 +08:00
|
|
|
|
2013-07-15 20:21:24 +08:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import distutils.util
|
2016-06-23 20:30:01 +08:00
|
|
|
import distutils.dir_util
|
2013-07-15 20:21:24 +08:00
|
|
|
import subprocess
|
2015-12-10 18:18:31 +08:00
|
|
|
import logging
|
2015-11-21 18:24:00 +08:00
|
|
|
|
|
|
|
|
2013-07-15 20:21:24 +08:00
|
|
|
def _distutils_dir_name(dname="lib"):
|
|
|
|
"""
|
|
|
|
Returns the name of a distutils build directory
|
|
|
|
"""
|
|
|
|
platform = distutils.util.get_platform()
|
|
|
|
architecture = "%s.%s-%i.%i" % (dname, platform,
|
|
|
|
sys.version_info[0], sys.version_info[1])
|
|
|
|
return architecture
|
|
|
|
|
|
|
|
|
|
|
|
def _distutils_scripts_name():
|
|
|
|
"""Return the name of the distrutils scripts sirectory"""
|
|
|
|
f = "scripts-{version[0]}.{version[1]}"
|
|
|
|
return f.format(version=sys.version_info)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_available_scripts(path):
|
|
|
|
res = []
|
|
|
|
try:
|
|
|
|
res = " ".join([s.rstrip('.py') for s in os.listdir(path)])
|
|
|
|
except OSError:
|
|
|
|
res = ["no script available, did you ran "
|
|
|
|
"'python setup.py build' before bootstrapping ?"]
|
|
|
|
return res
|
|
|
|
|
2014-11-27 20:56:37 +08:00
|
|
|
|
2016-03-22 16:41:41 +08:00
|
|
|
if sys.version_info[0] >= 3: # Python3
|
|
|
|
def execfile(fullpath):
|
|
|
|
"Python3 implementation for execfile"
|
|
|
|
with open(fullpath) as f:
|
|
|
|
code = compile(f.read(), fullpath, 'exec')
|
|
|
|
exec(code)
|
|
|
|
|
2015-01-23 22:07:12 +08:00
|
|
|
|
|
|
|
def runfile(fname):
|
|
|
|
try:
|
|
|
|
execfile(fname)
|
2016-06-16 23:23:36 +08:00
|
|
|
except (SyntaxError, NameError) as error:
|
2016-04-05 22:07:48 +08:00
|
|
|
print(error)
|
2015-01-23 22:07:12 +08:00
|
|
|
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)
|
|
|
|
run.wait()
|
|
|
|
|
2016-06-23 20:30:01 +08:00
|
|
|
def get_project_name(root_dir):
|
|
|
|
"""Retrieve project name by running python setup.py --name in root_dir.
|
|
|
|
|
|
|
|
:param str root_dir: Directory where to run the command.
|
|
|
|
:return: The name of the project stored in root_dir
|
|
|
|
"""
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.debug("Getting project name in %s", root_dir)
|
2016-06-23 20:30:01 +08:00
|
|
|
p = subprocess.Popen([sys.executable, "setup.py", "--name"],
|
|
|
|
shell=False, cwd=root_dir, stdout=subprocess.PIPE)
|
|
|
|
name, _stderr_data = p.communicate()
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.debug("subprocess ended with rc= %s", p.returncode)
|
2016-06-23 20:30:01 +08:00
|
|
|
return name.split()[-1].decode('ascii')
|
|
|
|
|
2016-06-15 22:10:23 +08:00
|
|
|
logging.basicConfig()
|
2016-06-15 22:08:41 +08:00
|
|
|
logger = logging.getLogger("bootstrap")
|
2016-06-23 20:30:01 +08:00
|
|
|
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
PROJECT_NAME = get_project_name(PROJECT_DIR)
|
2016-06-15 22:08:41 +08:00
|
|
|
|
2014-04-29 14:39:14 +08:00
|
|
|
home = os.path.dirname(os.path.abspath(__file__))
|
2016-06-23 20:30:01 +08:00
|
|
|
SCRIPTSPATH = os.path.join(home, 'build', _distutils_scripts_name())
|
|
|
|
LIBPATH = os.path.join(home, 'build', _distutils_dir_name('lib'))
|
2016-03-22 16:41:41 +08:00
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir(home)
|
|
|
|
build = subprocess.Popen([sys.executable, "setup.py", "build"],
|
|
|
|
shell=False, cwd=os.path.dirname(os.path.abspath(__file__)))
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.info("Build process ended with rc= %s", build.wait())
|
2016-06-23 20:30:01 +08:00
|
|
|
distutils.dir_util.copy_tree("pyFAI/resources", os.path.join(LIBPATH, PROJECT_NAME, "resources"), update=1)
|
|
|
|
|
2016-03-22 16:41:41 +08:00
|
|
|
os.chdir(cwd)
|
2013-07-15 20:21:24 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
2015-12-10 18:18:31 +08:00
|
|
|
logging.warning("usage: ./bootstrap.py <script>\n")
|
|
|
|
logging.warning("Available scripts : %s\n" %
|
|
|
|
_get_available_scripts(SCRIPTSPATH))
|
|
|
|
script = None
|
|
|
|
else:
|
|
|
|
script = sys.argv[1]
|
|
|
|
|
|
|
|
if script:
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.info("Executing %s from source checkout", script)
|
2015-12-10 18:18:31 +08:00
|
|
|
else:
|
|
|
|
logging.info("Running iPython by default")
|
2013-07-15 20:21:24 +08:00
|
|
|
sys.path.insert(0, LIBPATH)
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.info("01. Patched sys.path with %s", LIBPATH)
|
2013-07-15 20:21:24 +08:00
|
|
|
|
|
|
|
sys.path.insert(0, SCRIPTSPATH)
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.info("02. Patched sys.path with %s", SCRIPTSPATH)
|
2015-12-10 18:18:31 +08:00
|
|
|
|
|
|
|
if script:
|
|
|
|
sys.argv = sys.argv[1:]
|
|
|
|
logger.info("03. patch the sys.argv : ", sys.argv)
|
2016-08-02 18:32:55 +08:00
|
|
|
logger.info("04. Executing %s.main()", script)
|
2015-12-10 18:18:31 +08:00
|
|
|
fullpath = os.path.join(SCRIPTSPATH, script)
|
|
|
|
if os.path.exists(fullpath):
|
2016-06-15 21:59:47 +08:00
|
|
|
exec_file = fullpath
|
2015-12-10 18:18:31 +08:00
|
|
|
else:
|
|
|
|
if os.path.exists(script):
|
2016-06-15 21:59:47 +08:00
|
|
|
exec_file = script
|
2015-12-10 18:18:31 +08:00
|
|
|
else:
|
|
|
|
for dirname in os.environ.get("PATH", "").split(os.pathsep):
|
|
|
|
fullpath = os.path.join(dirname, script)
|
|
|
|
if os.path.exists(fullpath):
|
2016-06-15 21:59:47 +08:00
|
|
|
exec_file = fullpath
|
2015-12-10 18:18:31 +08:00
|
|
|
break
|
2016-06-15 21:59:47 +08:00
|
|
|
else:
|
|
|
|
exec_file = None
|
|
|
|
if exec_file is not None:
|
|
|
|
runfile(exec_file)
|
|
|
|
else:
|
|
|
|
logger.error("Script not found")
|
2014-04-29 14:39:14 +08:00
|
|
|
else:
|
2015-12-10 18:18:31 +08:00
|
|
|
logger.info("03. patch the sys.argv : ", sys.argv)
|
|
|
|
sys.path.insert(2, "")
|
|
|
|
try:
|
|
|
|
from IPython import embed
|
|
|
|
except Exception as err:
|
|
|
|
logger.error("Unable to execute iPython, using normal Python")
|
|
|
|
logger.error(err)
|
|
|
|
import code
|
|
|
|
code.interact()
|
2014-04-29 14:39:14 +08:00
|
|
|
else:
|
2015-12-10 18:18:31 +08:00
|
|
|
embed()
|