view SConstruct @ 5:33684971cdb1

Replaced the shell script launcher with a cross-platform C executable. * Replaced the Unix-only shell script launcher with a cross-platform compiled executable written in C. * Added several new configuration variables to the SConsctruct script which allows the user to specify where their Python site-packages and executable are located; * Cleaned up the SConstruct script and made it easier to grok.
author M. George Hansen <technopolitica@gmail.com>
date Tue, 17 May 2011 14:18:25 -0700
parents e2a8e3805b04
children dd4ed4945411
line wrap: on
line source

import sys
import os
import platform
import compileall
import fnmatch
from collections import Sequence
from types import StringType
from multiprocessing import cpu_count

def InstallChmod(env, dest, source, mode):
    targets = env.Install(dest, source)
    for target in targets:
        env.AddPostAction(target, Chmod(target, mode))
    return targets

def InstallExecutable(env, dest, source):
    return env.InstallChmod(dest, source, mode=0755)

def InstallReadOnly(env, dest, source):
    if not isinstance(source, Sequence):
        source = [source]
    targets = []
    for entry in map(Entry, source):
        entry.disambiguate()
        if entry.isdir():
            target = env.InstallChmod(dest, entry, mode=0755)
        elif entry.isfile():
            target = env.InstallChmod(dest, entry, mode=0644)
        else:
            # Something really weird happened and entry is not a Dir or a
            # File... (Note: Yes this can happen!)
            error_message = \
                'expected entry to be a Dir or a File, but got {0!r}'
            raise ValueError(error_message.format(entry))
        targets.append(target)
    return targets

def InstallPyPackages(env, dest, source, compile=True):
    # Remove all existing *.pyc and *.pyo files for a sanitary install
    # environment.
    def _remove_compiled_modules(path):
        for dir_path, dir_names, file_names in os.walk(path):
            for file_name in fnmatch.filter(file_names, '*.py[co]'):
                file_path = os.path.join(dir_path, file_name)
                os.remove(file_path)
    
    def _add_targets(path):
         for dir_path, dir_names, file_names in os.walk(path):
            for file_name in fnmatch.filter(file_names, '*.py[co]'):
                file_path = os.path.join(dir_path, file_name)
                env.Clean(path, file_path)
                source_file_path = file_path.rstrip('oc')
                env.Depends(file_path, source_file_path)

    if not isinstance(source, Sequence) or isinstance(source, StringType):
        source = [source]
    for dir in source:
        _remove_compiled_modules(str(dir))
        if compile:
            compileall.compile_dir(str(dir), ddir=str(dest), force=True)
            _add_targets(str(dir))
    targets = env.InstallReadOnly(dest, source)
    return targets

AddMethod(Environment, InstallChmod)
AddMethod(Environment, InstallExecutable)
AddMethod(Environment, InstallReadOnly)
AddMethod(Environment, InstallPyPackages)

EnsurePythonVersion(2, 6)

AddOption(
    '--stand-alone',
    dest='stand_alone',
    action='store_true',
    default=False,
    help='install the entire program under installation prefix instead of in '
         'various system folders'
)
AddOption(
    '--no-compile',
    dest='compile',
    action='store_false',
    default=True,
    help='don\'t compile any Python modules into .pyc files before installing',
)
SetOption('num_jobs', cpu_count())

def is_abs_path(key, val, env):
    if not os.path.isabs(val):
        error_message = '${key} must be an absolute path'
        raise ValueError(error_message.format(key=key))
    env[key] = os.path.normpath(val)

variables = Variables()
# NOTE M. George Hansen 2011-05-13: Path variables are based on the GNU
#     standards as defined at http://www.gnu.org/prep/standards/html_node/Directory-Variables.html

# Platform-specific variable defaults.
# FIXME M. George Hansen 2011-05-12: Define the MacOS-specific
#     environmental variables (and check version...)
platform_name = platform.system()
if platform_name in ['Linux', 'MacOS']:
    if GetOption('stand_alone'):
        PREFIX_DEFAULT = '/opt'
    else:
        PREFIX_DEFAULT = '/usr/local'
    BIN_DIR_DEFAULT = '$EXEC_PREFIX/bin'
    DATA_ROOT_DIR_DEFAULT = '$PREFIX/share/$PROJECT_NAME'
    SYS_CONF_DIR_DEFAULT = '$PREFIX/etc/$PROJECT_NAME'
    INCLUDE_DIR_DEFAULT = '$PREFIX/include/$PROJECT_NAME'
    DOC_DIR_DEFAULT = '$DATA_ROOT_DIR/doc/$PROJECT_NAME'
    LIB_DIR_DEFAULT = '$EXEC_PREFIX/lib' 
    dist_name, dist_version, dist_id = \
        platform.linux_distribution(full_distribution_name=False)
    if dist_name in ['debian', 'Ubuntu']:
        # Debian uses dist-packages instead of site-packages for Python
        #     versions > 2.5.
        PY_PACKAGES_DIR_DEFAULT = '$EXEC_PREFIX/lib/python$PY_VERSION_SHORT/' \
                                 'dist-packages'
    else:
        PY_PACKAGES_DIR_DEFAULT = '$EXEC_PREFIX/lib/python$PY_VERSION_SHORT/' \
                                 'site-packages'
    PY_HEADERS_DIR_DEFAULT = '/usr/include/python$PY_VERSION_SHORT'
    PY_LIB_DIR_DEFAULT = '/usr/lib'
    PY_LIB_NAME = 'python$PY_VERSION_SHORT'
elif platform_name == 'Windows':
    try:
        PREFIX_DEFAULT = os.environ['PROGRAMFILES'] + r'\$PROGRAM_NAME'
    except KeyError:
        PREFIX_DEFAULT = ''
        error_message = '%PROGRAMFILES% environmental variable is not ' \
                        'set, unable to determine path to Program Files ' \
                        'folder'
        raise SConfWarning(error_message)
    BIN_DIR_DEFAULT = '$EXEC_PREFIX'
    DATA_ROOT_DIR_DEFAULT = '$PREFIX/data'
    SYS_CONF_DIR_DEFAULT = '$PREFIX/config'
    INCLUDE_DIR_DEFAULT = '$PREFIX/include'
    DOC_DIR_DEFAULT = '$PREFIX/doc/'
    LIB_DIR_DEFAULT = '$EXEC_PREFIX/lib'
    PY_LIB_DIR_DEFAULT = '$EXEC_PREFIX/lib'
    # FIXME M. George Hansen 2011-05-12: Does sys.prefix include the
    #     PythonX.Y part on Windows?
    python_prefix = sys.prefix
    if GetOption('stand_alone'):
        PY_PACKAGES_DIR_DEFAULT = '$PREFIX/lib'
    else:
        PY_PACKAGES_DIR_DEFAULT = \
            os.path.join(python_prefix, 'Lib', 'site-packages')
    PY_HEADERS_DIR_DEFAULT = os.path.join(python_prefix, 'include')
    try:
        PY_LIB_DIR_DEFAULT = os.environ['SYSTEMROOT']
    except KeyError:
        PY_LIB_DIR_DEFAULT = ''
        error_message = '%SYSTEMROOT% environmental variable is not set, ' \
                        'unable to determine path to Windows system folder'
        raise SConfWarning(error_message)
    PY_LIB_NAME = 'python$PY_VERSION_MAJOR$PY_VERSION_MINOR'

# Platform-independant variables:
variables.AddVariables(
    PathVariable(
        'PREFIX',
        'directory under which most or all of the program components should '
            'be installed',
        PREFIX_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'EXEC_PREFIX',
        'directory under which machine-specific compiled libraries and '
            'objects should be installed',
        '$PREFIX',
        is_abs_path,
    ),
    PathVariable(
        'BIN_DIR',
        'directory where program executables should be installed',
        BIN_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'DATA_ROOT_DIR',
        'directory under which read-only, architecture-independant data files '
            'should be installed',
        DATA_ROOT_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'DATA_DIR',
        'directory where read-only, architecture-independant data files '
            'should be installed',
        '$DATA_ROOT_DIR',
        is_abs_path,
    ),
    PathVariable(
        'SYS_CONF_DIR',
        'directory where read-only, machine-specific data files should be '
            'installed',
        SYS_CONF_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'INCLUDE_DIR',
        'directory where C/C++ header files should be installed',
        INCLUDE_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'DOC_DIR',
        'directory where program documentation should be installed',
        DOC_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'LIB_DIR',
        'directory where platform-dependant, compiled library and object '
            'files should be installed',
        LIB_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'PY_PACKAGES_DIR',
        'directory where pure Python modules and packages should be installed',
        PY_PACKAGES_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'INCLUDE_DIR',
        'directory where C/C++ header files should be installed',
        INCLUDE_DIR_DEFAULT,
        is_abs_path,
    ),
    PathVariable(
        'PY_HEADERS_DIR',
        'directory where Python.h can be found',
        PY_HEADERS_DIR_DEFAULT,
    ),
    PathVariable(
        'PY_LIB_DIR',
        'directory where the Python shared library can be found',
        PY_LIB_DIR_DEFAULT,
    ),
    BoolVariable(
        'DEBUG',
        'if True, compile the program launcher executable with debugging '
            'symbols',
        False,
    ),
)

platform_name = platform.system()
python_version_tuple = platform.python_version_tuple()

environment = Environment(
    tools=['default', 'cc', 'textfile', 'packaging'],
    variables=variables,
    PROJECT_NAME='parpg',
    PROJECT_VERSION_MAJOR=0,
    PROJECT_VERSION_MINOR=2,
    PROJECT_VERSION_PATCH=0,
    PROJECT_VERSION_SHORT='${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MAJOR}',
    PROJECT_VERSION_LONG='${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.'
                         '${PROJECT_VERSION_PATCH}',
    PYTHON=sys.executable,
    PY_VERSION_MAJOR=python_version_tuple[0],
    PY_VERSION_MINOR=python_version_tuple[1],
    PY_VERSION_PATCH=python_version_tuple[2],
    PY_VERSION_SHORT='${PY_VERSION_MAJOR}.${PY_VERSION_MINOR}',
    PY_VERSION_LONG='${PY_VERSION_MAJOR}.${PY_VERSION_MINOR}.'
                    '${PY_VERSION_PATCH}',
    PY_LIB_NAME=PY_LIB_NAME,
    PY_PACKAGES=[],
    CONFIG_FILES=[],
    DATA_FILES=[],
    EXECUTABLES=[],
)
if environment['DEBUG']:
    environment.AppendUnique(CCFLAGS=['-gdwarf-2', '-g3'])

Help(variables.GenerateHelpText(environment))

config_dict = [('@{0}@'.format(key), environment.Dictionary()[key]) for key in
               ('PREFIX', 'LIB_DIR', 'PY_PACKAGES_DIR', 'BIN_DIR',
                'SYS_CONF_DIR', 'DATA_DIR', 'PYTHON')]

install_py_packages = environment.InstallPyPackages(
    '$PY_PACKAGES_DIR',
    'src/parpg',
    compile=GetOption('compile'),
)

subst_config_file = environment.Substfile(
    'system.cfg.in',
    SUBST_DICT=config_dict
)
install_config_files = environment.InstallChmod(
    '$SYS_CONF_DIR',
    subst_config_file,
    mode=0755
)
Requires(install_config_files, subst_config_file)

install_data = environment.InstallReadOnly(
    '$DATA_DIR',
    Glob('data/*'),
)

executable_source = environment.Substfile(
    'bin/parpg.c.in',
    SUBST_DICT=config_dict,
)
build_executable = environment.Program(
    executable_source,
    LIBS=['$PY_LIB_NAME'],
    LIBPATH='$PY_LIB_DIR',
    CPPPATH=['$PY_HEADERS_DIR'],
)
# FIXME M. George Hansen 2011-05-17: Should the executable target be hard-coded
#     like this?
install_executable = environment.InstallExecutable(
    '$BIN_DIR',
    'bin/parpg',
)

# TODO M. George Hansen 2011-05-12: Implement package builder.
#package = environment.Package(
#    NAME='parpg',
#    VERSION='0.2.0',
#    PACKAGEVERSION=0,
#    LICENSE='gpl',
#    SUMMARY='',
#    DESCRIPTION='',
#    X_RPM_GROUP='Application/parpg',
#)

build = Alias('build', [build_executable])
install = Alias('install', [build, install_executable, install_py_packages,
                install_config_files, install_data])

Default(install)