Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a util module for simple detection useful in SConstruct #32

Merged
merged 5 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 2 additions & 69 deletions src/sconsx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/sconsx/tools/multicpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down
Empty file added src/sconsx/util/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions src/sconsx/util/buildprefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


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)

51 changes: 51 additions & 0 deletions src/sconsx/util/env_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#------------------------------------------------------------------------------
# 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

#------------------------------------------------------------------------------
# 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

39 changes: 39 additions & 0 deletions src/sconsx/util/lib_check.py
Original file line number Diff line number Diff line change
@@ -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'

41 changes: 41 additions & 0 deletions src/sconsx/util/qt_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

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

26 changes: 26 additions & 0 deletions src/sconsx/util/versionreader.py
Original file line number Diff line number Diff line change
@@ -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)