Esempio n. 1
0
def _hwaf_get_runtime_env(ctx):
    """return an environment suitably modified to run locally built programs
    """
    import os
    cwd = os.getcwd()
    root = os.path.realpath(ctx.env.PREFIX)
    root = os.path.abspath(os.path.realpath(ctx.env.INSTALL_AREA))
    msg.debug("hwaf: :::root:::"+root)
    if ctx.env.DESTDIR:
        root = ctx.env.DESTDIR + os.sep + ctx.env.INSTALL_AREA
        pass
    bindir = os.path.join(root, 'bin')
    libdir = os.path.join(root, 'lib')
    pydir  = os.path.join(root, 'python')

    env = dict(os.environ)

    def _env_prepend(k, args):
        old_v = env.get(k, '').split(os.pathsep)
        if isinstance(args, (list,tuple)):
            env[k] = os.pathsep.join(args)
        else:
            env[k] = args
            pass
        if old_v:
            env[k] = os.pathsep.join([env[k]]+old_v)
            pass
        return

    def _clean_env_path(env, k):
        paths = env.get(k, '').split(os.pathsep)
        o = []
        for p in paths:
            if osp.exists(p):
                o.append(p)
                pass
            #else: msg.info("discarding: %s" % p)
            pass
        env[k] = os.pathsep.join(o)
        return
    
    for k in ctx.env.keys():
        v = ctx.env[k]
        if not k in ctx.env.HWAF_RUNTIME_ENVVARS:
            continue
        if k in ctx.env.HWAF_RUNTIME_ENVVARS:
            if isinstance(v, (list,tuple)):
                if len(v) == 1: v = v[0]
                else:
                    if k.endswith('PATH'): v = os.pathsep.join(v)
                    else:                  v = " ".join(v)
                pass
            # FIXME: we should have an API to decide...
            if k.endswith('PATH'): _env_prepend(k, osp.abspath(v))
            else:                  env[k]=v
            continue
        # reject invalid values (for an environment)
        if isinstance(v, (list,tuple)):
            continue
        env[k] = str(v)
        pass

    ## handle the shell flavours...
    if ctx.is_linux():
        shell = os.environ.get("SHELL", "/bin/sh")
    elif ctx.is_darwin():
        shell = os.environ.get("SHELL", "/bin/sh")
        shell = shell.strip()
        if shell.startswith('-'):
            shell = shell[1:]
    elif ctx.is_windows():
        ## FIXME: ???
        shell = None
    else:
        shell = None
        pass

    # catch-all
    if not shell or "(deleted)" in shell:
        # fallback on the *login* shell
        shell = os.environ.get("SHELL", "/bin/sh")
        pass

    env['SHELL'] = shell
    
        
    for k in env:
        v = env[k]
        if not isinstance(v, str):
            msg.warning('hwaf: env[%s]=%r (%s)' % (k,v,type(v)))
            del env[k]

    for k in (
        'PATH',
        'LD_LIBRARY_PATH',
        'DYLD_LIBRARY_PATH',
        'PYTHONPATH',
        ):
        _clean_env_path(env, k)
        pass

    return env