From b0f0dd4a2bc7c920a956fed67e78df9fe30ed00a Mon Sep 17 00:00:00 2001 From: Frederic Boudon Date: Fri, 12 Jan 2018 14:44:19 +0100 Subject: [PATCH 1/3] create a util branch for simple detection usefull in SConstruct and so --- src/sconsx/config.py | 71 +------------------------------- src/sconsx/util/__init__.py | 0 src/sconsx/util/buildprefix.py | 24 +++++++++++ src/sconsx/util/env_check.py | 35 ++++++++++++++++ src/sconsx/util/lib_check.py | 39 ++++++++++++++++++ src/sconsx/util/qt_check.py | 40 ++++++++++++++++++ src/sconsx/util/versionreader.py | 26 ++++++++++++ 7 files changed, 166 insertions(+), 69 deletions(-) create mode 100644 src/sconsx/util/__init__.py create mode 100644 src/sconsx/util/buildprefix.py create mode 100644 src/sconsx/util/env_check.py create mode 100644 src/sconsx/util/lib_check.py create mode 100644 src/sconsx/util/qt_check.py create mode 100644 src/sconsx/util/versionreader.py diff --git a/src/sconsx/config.py b/src/sconsx/config.py index a055743..4f6c3ec 100644 --- a/src/sconsx/config.py +++ b/src/sconsx/config.py @@ -412,77 +412,10 @@ def ALEASolution(options, tools=[], dir=[]): return env - -# -- locate executables in the path, use with caution -- -def find_executable_path_from_env(exe, strip_bin=True): - paths = os.environ["PATH"].split(os.pathsep) - okPath = None - for p in paths: - if exists(pj(p,exe)): - okPath = p - break - - if okPath is None : return None - bin = okPath[-4:] - if strip_bin and okPath and "bin" in bin and os.sep in bin: - return okPath[:-4] - else: - return okPath - - -def detect_posix_project_installpath(filepattern, potentialdirs = []): - """ Detect the installation of include of lib in the system. - Potential dirs can be added to test on the system. - By default, '/usr','/usr/local','/opt/local' are tested. - If nothing is found, it return the default value /usr - Exemple of use will be: - detect_posix_project_installpath('GL', ['/usr/X11R6']) - """ - from os.path import join, exists - mpotentialdirs = potentialdirs+['/opt/local','/usr/local','/usr'] - if is_conda(): - mpotentialdirs.prepend(conda_library_prefix()) - for potentialdir in mpotentialdirs: - if exists(join(potentialdir,filepattern)) : - return potentialdir - return '/usr' - - -#------------------------------------------------------------------------------ -# Conda detection - -def is_conda(): - """ Check if sconsx is run in a conda environment. """ - return ("CONDA_PREFIX" in os.environ) - -def conda_prefix(): - """ Returns the PREFIX where lib, include are installed. """ - if is_conda(): - if 'CONDA_BUILD' in os.environ: - return os.environ.get('PREFIX', os.environ.get('CONDA_PREFIX')) - else: - return os.environ['CONDA_PREFIX'] - -def conda_library_prefix(): - if os.name == 'posix' : - return conda_prefix() - elif is_conda(): - library_prefix= pj(os.environ.get('CONDA_PREFIX'),'Library') - if 'CONDA_BUILD' in os.environ: - return os.environ.get('LIBRARY_PREFIX', library_prefix) - else: - return library_prefix +from .util.lib_check import * +from .util.env_check import * CONDA_ENV = is_conda() CONDA_PREFIX = conda_prefix() CONDA_LIBRARY_PREFIX = conda_library_prefix() -#------------------------------------------------------------------------------ -# system detection - -def is_32bit_environment(): - return not is_64bit_environment() - -def is_64bit_environment(): - import sys - return sys.maxsize.bit_length() == 63 \ No newline at end of file diff --git a/src/sconsx/util/__init__.py b/src/sconsx/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/sconsx/util/buildprefix.py b/src/sconsx/util/buildprefix.py new file mode 100644 index 0000000..5047ef3 --- /dev/null +++ b/src/sconsx/util/buildprefix.py @@ -0,0 +1,24 @@ + + +def fix_custom_buildprefix(env, standardprefix = 'build-scons'): + """ + A function that create a symlink in case build_prefix is different from standardprefix. + Usefull to make the link with python setup.py configuration. + Args: + env (SCons.environment or str): The actual prefix. + standardprefix (str): The name of the standart prefix used by setup.py. + """ + import os, shutil + if os.name == 'posix' : + if type(env) == str: + prefix = env + else: + # A + prefix = env['build_prefix'] + if os.path.basename(prefix) != standardprefix: + if os.path.exists(standardprefix): + if os.path.isdir(standardprefix) and not os.path.islink(standardprefix): + shutil.rmtree(standardprefix) + else: + os.remove(standardprefix) + os.symlink(prefix, standardprefix) \ No newline at end of file diff --git a/src/sconsx/util/env_check.py b/src/sconsx/util/env_check.py new file mode 100644 index 0000000..1ce91f9 --- /dev/null +++ b/src/sconsx/util/env_check.py @@ -0,0 +1,35 @@ +#------------------------------------------------------------------------------ +# Conda detection +import os + + +def is_conda(): + """ Check if sconsx is run in a conda environment. """ + return ("CONDA_PREFIX" in os.environ) + +def conda_prefix(): + """ Returns the PREFIX where lib, include are installed. """ + if is_conda(): + if 'CONDA_BUILD' in os.environ: + return os.environ.get('PREFIX', os.environ.get('CONDA_PREFIX')) + else: + return os.environ['CONDA_PREFIX'] + +def conda_library_prefix(): + if os.name == 'posix' : + return conda_prefix() + elif is_conda(): + library_prefix= os.path.join(os.environ.get('CONDA_PREFIX'),'Library') + if 'CONDA_BUILD' in os.environ: + return os.environ.get('LIBRARY_PREFIX', library_prefix) + else: + return library_prefix +#------------------------------------------------------------------------------ +# system detection + +def is_32bit_environment(): + return not is_64bit_environment() + +def is_64bit_environment(): + import sys + return sys.maxsize.bit_length() == 63 \ No newline at end of file diff --git a/src/sconsx/util/lib_check.py b/src/sconsx/util/lib_check.py new file mode 100644 index 0000000..4bb2748 --- /dev/null +++ b/src/sconsx/util/lib_check.py @@ -0,0 +1,39 @@ +# -- locate executables in the path, use with caution -- +def find_executable_path_from_env(exe, strip_bin=True): + import os + from os.path import exists, join + + paths = os.environ["PATH"].split(os.pathsep) + okPath = None + for p in paths: + if exists(join(p,exe)): + okPath = p + break + + if okPath is None : return None + bin = okPath[-4:] + if strip_bin and okPath and "bin" in bin and os.sep in bin: + return okPath[:-4] + else: + return okPath + + +def detect_posix_project_installpath(filepattern, potentialdirs = []): + """ Detect the installation of include of lib in the system. + Potential dirs can be added to test on the system. + By default, '/usr','/usr/local','/opt/local' are tested. + If nothing is found, it return the default value /usr + Exemple of use will be: + detect_posix_project_installpath('GL', ['/usr/X11R6']) + """ + from os.path import join, exists + from .env_check import is_conda + + mpotentialdirs = potentialdirs+['/opt/local','/usr/local','/usr'] + if is_conda(): + mpotentialdirs.prepend(conda_library_prefix()) + for potentialdir in mpotentialdirs: + if exists(join(potentialdir,filepattern)) : + return potentialdir + return '/usr' + diff --git a/src/sconsx/util/qt_check.py b/src/sconsx/util/qt_check.py new file mode 100644 index 0000000..8d57468 --- /dev/null +++ b/src/sconsx/util/qt_check.py @@ -0,0 +1,40 @@ + +def detect_qt_includepath(): + from .lib_check import detect_posix_project_installpath, find_executable_path_from_env + import os + from os.path import join, abspath + + defaultdirs = ['/opt/local/libexec'] + qt_dir = os.getenv("QTDIR") + if qt_dir : defaultdirs.append(qt_dir) + qt_dir = find_executable_path_from_env("moc.exe", strip_bin=True) + if qt_dir : defaultdirs.append(abspath(join(qt_dir,os.pardir))) + + for pattern in ['qt4/include','qt5/include','qt/include','include/qt4','include/qt5','include/qt']: + defdir = os.path.join(detect_posix_project_installpath(join(pattern,'QtCore'),defaultdirs),pattern) + if os.path.exists(defdir): + break + return defdir + + +def detect_installed_qt_version(default = 4): + import os + from .versionreader import read_variable, version_from_hex + + QT_VERSION = None + library_inc = detect_qt_includepath() + + qversionconfig = os.path.join(library_inc,'QtCore','qconfig.h') + if os.path.exists(qversionconfig): + variable = 'QT_VERSION_MAJOR' + QT_VERSION = read_variable(variable, qversionconfig) + if QT_VERSION is None: + qversionconfig = os.path.join(library_inc,'QtCore','qglobal.h') + if os.path.exists(qversionconfig): + variable = 'QT_VERSION' + QT_VERSION = read_variable(variable, qversionconfig) + if QT_VERSION is None: + print ('Autodetect qt error in',repr(library_inc)) + QT_VERSION = default + + return QT_VERSION \ No newline at end of file diff --git a/src/sconsx/util/versionreader.py b/src/sconsx/util/versionreader.py new file mode 100644 index 0000000..f4c79fd --- /dev/null +++ b/src/sconsx/util/versionreader.py @@ -0,0 +1,26 @@ + + +def read_variable(varname, fname): + import re + stream = file(fname, 'r') + text = stream.read() + pattern = '#define\s+?'+varname+'\s+?(.+?)\s*?$' + res = re.findall(pattern, text, re.M) + if not res is None and len(res) > 0: + res = res[0] + try: + res = eval(res) + except: + pass + else : return None + return res + +def version_from_hex(hex_version): + major = ((hex_version & 0xff0000) >> 16) + minor = ((hex_version & 0x00ff00) >> 8) + rev = (hex_version & 0x0000ff) + return major, minor, rev + +def strversion_from_hex(hex_version): + major, minor, rev = version_from_hex(hex_version) + return str(major)+'.'+str(minor)+'.'+str(rev) From 518c69b44a7ef35ca3f868bf1c1c2a99316f7a8b Mon Sep 17 00:00:00 2001 From: Frederic Boudon Date: Fri, 12 Jan 2018 14:56:01 +0100 Subject: [PATCH 2/3] add continuous integration info. in ci, use max num of processor. --- src/sconsx/tools/multicpu.py | 6 +++++- src/sconsx/util/env_check.py | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/sconsx/tools/multicpu.py b/src/sconsx/tools/multicpu.py index 9004b58..e0512b8 100644 --- a/src/sconsx/tools/multicpu.py +++ b/src/sconsx/tools/multicpu.py @@ -22,6 +22,7 @@ import os, sys from openalea.sconsx.config import * +from openalea.sconsx.util.env_check import is_continuous_integration import SCons.Script @@ -34,8 +35,11 @@ def __init__(self, config): def option( self, opts): """ Add Options to opts """ + default_num_jobs = 1 + if is_continuous_integration() and 'CPU_COUNT' in os.environ: + default_num_jobs = os.environ['CPU_COUNT'] - opts.AddVariables(('num_jobs', 'Number of jobs', 1),) + opts.AddVariables(('num_jobs', 'Number of jobs', default_num_jobs),) def update(self, env): diff --git a/src/sconsx/util/env_check.py b/src/sconsx/util/env_check.py index 1ce91f9..639c2ab 100644 --- a/src/sconsx/util/env_check.py +++ b/src/sconsx/util/env_check.py @@ -32,4 +32,19 @@ def is_32bit_environment(): def is_64bit_environment(): import sys - return sys.maxsize.bit_length() == 63 \ No newline at end of file + return sys.maxsize.bit_length() == 63 + +#------------------------------------------------------------------------------ +# CI detection + +def is_continuous_integration(): + import os + return 'CI' in os.environ + +def is_on_travis(): + import os + return 'TRAVIS' in os.environ + +def is_on_appveyor(): + import os + return 'APPVEYOR' in os.environ \ No newline at end of file From 6f0d6d802e513a56e043954b672a301f15c60de1 Mon Sep 17 00:00:00 2001 From: Frederic Boudon Date: Fri, 12 Jan 2018 15:00:05 +0100 Subject: [PATCH 3/3] add end file endline --- src/sconsx/util/buildprefix.py | 3 ++- src/sconsx/util/env_check.py | 3 ++- src/sconsx/util/qt_check.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sconsx/util/buildprefix.py b/src/sconsx/util/buildprefix.py index 5047ef3..e14fc66 100644 --- a/src/sconsx/util/buildprefix.py +++ b/src/sconsx/util/buildprefix.py @@ -21,4 +21,5 @@ def fix_custom_buildprefix(env, standardprefix = 'build-scons'): shutil.rmtree(standardprefix) else: os.remove(standardprefix) - os.symlink(prefix, standardprefix) \ No newline at end of file + os.symlink(prefix, standardprefix) + diff --git a/src/sconsx/util/env_check.py b/src/sconsx/util/env_check.py index 639c2ab..694cb59 100644 --- a/src/sconsx/util/env_check.py +++ b/src/sconsx/util/env_check.py @@ -47,4 +47,5 @@ def is_on_travis(): def is_on_appveyor(): import os - return 'APPVEYOR' in os.environ \ No newline at end of file + return 'APPVEYOR' in os.environ + diff --git a/src/sconsx/util/qt_check.py b/src/sconsx/util/qt_check.py index 8d57468..ead4f62 100644 --- a/src/sconsx/util/qt_check.py +++ b/src/sconsx/util/qt_check.py @@ -37,4 +37,5 @@ def detect_installed_qt_version(default = 4): print ('Autodetect qt error in',repr(library_inc)) QT_VERSION = default - return QT_VERSION \ No newline at end of file + return QT_VERSION +