Beispiel #1
0
def setEnv():
    """
        This function loads the pkgInfo.py of each dependency package.
        If environment settings are found there they will be loaded into
        the environment of the current Python process.

        Example:
            The pkgInfo.py of Matlab states MATLAB_ROOT, PATH and
            LD_LIBRARY_PATH settings in its pkgInfo.py. Before compiling
            such variables must be set.

        On Linux, alternatively, this can be achieved by sourcing the
        BashSrc files. Nevertheless this is not possible in all cases
        (especially Matlab because multiple versions are available)
        when used within CIA.

        With some modifications this setEnv() approach conceptually
        potentially could also work on Windows.

        Attention: This function should only be called once (at least not
                   repeatedly when compiling the same package again from
                   within Python) to not unnecessarily increase the length
                   of PATH, LD_LIBRARY_PATH etc.
    """
    try:
        p = PackageDetector()
    except AssertionError:
        # XIF packages are generated on-the-fly during configure-phase.
        # We don't consider such packages for now (experimental code).
        return

    p.retrieveMakefileInfo()

    for package in p.dependencies + p.buildDependencies:
        try:
            envVars = PkgInfo.getPkgInfoContent( SIT.stripSIT(package) )['envVars']
        except ( AssertionError, KeyError ):
            envVars = []                         # no envVars specified
        except ( IOError, OSError, SyntaxError ) as details:
            logging.error( details )
            raise RuntimeError( 'unable to read pkgInfo.py of %s' % package )

        if envVars:
            logging.debug( 'found environment settings:' )
            logging.debug( envVars )

            for varName, varValue in envVars:
                FastScript.setEnv_withExpansion( varName, varValue )
def source(package):
    """
        Python equivalent of "source BashSrc" from SIT, in order to setup
        PATH, LD_LIBRARY_PATH,... within the Python process.

        @anchor ProcessEnv_source
    """
    ProjectProperties.requireIsCanonicalPath(package)

    sourced = FastScript.getEnv('TOOLBOSCORE_SOURCED')
    Any.requireMsg(sourced, '$TOOLBOSCORE_SOURCED must not be empty')

    # avoid double-sourcing
    if package in sourced:
        return True

    ProjectProperties.requireIsInstalled(package)

    logging.debug('source %s/pkgInfo.py', package)
    sourced = '%s %s' % (package, sourced)
    FastScript.setEnv('TOOLBOSCORE_SOURCED', sourced)

    # load pkgInfo.py (if exists)
    try:
        content = getPkgInfoContent(project=package)
    except AssertionError:
        return True  # no such file, this is OK
    except (IOError, OSError) as details:
        logging.error(details)
        return False

    # setup environment of this package
    try:
        envVars = content['envVars']
    except KeyError:
        envVars = []  # no such setting, this is OK

    sitPath = SIT.getPath()

    for name, value in envVars:

        # replace known placeholdes
        value = value.replace('${INSTALL_ROOT}',
                              os.path.join(sitPath, package))

        FastScript.setEnv_withExpansion(name, value)

    # source dependent packages
    try:
        # TODO: eventually extend to sourcing recommended/suggested packages
        depList = content['depends']
    except (AssertionError, KeyError):
        depList = []  # no such setting, this is OK

    for dep in depList:
        if not dep.startswith('deb://'):
            source(SIT.strip(dep))

    # special treatment of PYTHONPATH:
    # After sourcing add new entries in PYTHONPATH to sys.path
    _expandSysPath()

    return True