Пример #1
0
def get_dyn_lib_dir(use_default=True):
    """
    Return the shared lib directory
    if use_default : return default directory if not defined
    """
    if is_conda_env():
        return get_default_dyn_lib()

    bdir = get_base_dir("openalea.deploy")
    up_dir = os.path.abspath(join(bdir, os.path.pardir))

    try:
        f = open(join(up_dir, "shared-lib.pth"), 'r')
        lib_dir = f.read()
        print('Reading shared-lib.pth found in %s' % lib_dir)
        f.close()

    except Exception:

        if (use_default):
            lib_dir = get_default_dyn_lib()
        else:
            lib_dir = None

    return lib_dir
Пример #2
0
def get_dyn_lib_dir(use_default=True):
    """
    Return the shared lib directory
    if use_default : return default directory if not defined
    """
    if is_conda_env():
        return get_default_dyn_lib()

    bdir = get_base_dir("openalea.deploy")
    up_dir = os.path.abspath(join(bdir, os.path.pardir))

    try:
        f = open(join(up_dir, "shared-lib.pth"), 'r')
        lib_dir = f.read()
        print(('Reading shared-lib.pth found in %s' % lib_dir))
        f.close()

    except Exception:

        if (use_default):
            lib_dir = get_default_dyn_lib()
        else:
            lib_dir = None

    return lib_dir
Пример #3
0
def get_default_dyn_lib():
    """Return the default path for dynamic library."""
    basedir = get_python_lib()

    # Virtual environment
    if (is_virtual_env()):
        if ("posix" in os.name):
            return os.path.abspath(os.path.join(basedir, '../../lib'))
        else:
            return os.path.join(basedir, "shared_libs")

    # Conda environment
    if is_conda_env():
        return get_conda_dyn_lib()

    # Standard environment
    if ("posix" in os.name):
        return "/usr/local/lib"
    else:
        basedir = get_python_lib()
        return os.path.join(basedir, "shared_libs")
Пример #4
0
def get_default_dyn_lib():
    """Return the default path for dynamic library."""
    basedir = get_python_lib()

    # Virtual environment
    if (is_virtual_env()):
        if ("posix" in os.name):
            return os.path.abspath(
                os.path.join(basedir, '../../lib'))
        else:
            return os.path.join(basedir, "shared_libs")

    # Conda environment
    if is_conda_env():
        return get_conda_dyn_lib()

    # Standard environment
    if ("posix" in os.name):
        return "/usr/local/lib"
    else:
        basedir = get_python_lib()
        return os.path.join(basedir, "shared_libs")
Пример #5
0
def set_win_env(vars):
    """
    Set Windows environment variable persistently by editing the registry

    :param vars: ['VAR1=VAL1', 'VAR2=VAL2', 'PATH=SOMEPATH']
    """
    if is_conda_env():
        return set_conda_env(vars)

    if (not 'win32' in sys.platform):
        return

    for newvar in vars:

        from string import find
        try:
            if sys.version_info.major == 2:
                import six.moves.winreg as winreg
            else:
                import winreg
        except ImportError as e:
            print("!!ERROR: Can not access to Windows registry.")
            return

        def queryValue(qkey, qname):
            qvalue, type_id = winreg.QueryValueEx(qkey, qname)
            return qvalue

        name, value = newvar.split('=')

        regpath = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
        reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
        try:
            key = winreg.OpenKey(reg, regpath, 0, winreg.KEY_ALL_ACCESS)
        except  WindowsError as we:
            print(("Cannot set "+repr(name)+" for all users. Set for current user."))
            winreg.CloseKey(reg)
            regpath = r'Environment'
            reg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
            key = winreg.OpenKey(reg, regpath, 0, winreg.KEY_ALL_ACCESS)

        # Specific treatment for PATH variable
        if name.upper() == 'PATH':
            value = os.path.normpath(value)
            try:
                actualpath = queryValue(key, name)
            except:
                print('No PATH variable found')
                actualpath = ''

            listpath = actualpath.split(';')
            if not (value in listpath):
                value = actualpath + ';' + value
                print(("ADD %s to PATH" % (value,)))
            else:
                value = actualpath

            # TEST SIZE
            if (len(value) >= 8191):
                print("!!ERROR!! : PATH variable cannot contain more than 8191 characters")
                print("!!ERROR!! : Please : remove unused value in your environement")
                value = actualpath

        if (name and value):

            expand = winreg.REG_SZ
            # Expand variable if necessary
            if ("%" in value):
                expand = winreg.REG_EXPAND_SZ

            winreg.SetValueEx(key, name, 0, expand, value)
            # os.environ[name] = value #not necessary

        winreg.CloseKey(key)
        winreg.CloseKey(reg)

    # Refresh Environment
    try:
        HWND_BROADCAST = 0xFFFF
        WM_SETTINGCHANGE = 0x001A
        SMTO_ABORTIFHUNG = 0x0002
        sParam = "Environment"

        import win32gui
        res1, res2 = win32gui.SendMessageTimeout(HWND_BROADCAST,
                                                 WM_SETTINGCHANGE, 0, sParam,
                                                 SMTO_ABORTIFHUNG, 100)
        if not res1:
            print(("result %s, %s from SendMessageTimeout" % (bool(res1), res2)))

    except Exception as e:
        print(e)
Пример #6
0
def set_lsb_env(name, vars):
    """
    Write a sh script in /etc/profile.d which set some environment variable
    LIBRARY_PATH and PATH are processed in order to avoid overwriting

    :param name: file name string without extension
    :param vars: ['VAR1=VAL1', 'VAR2=VAL2', 'LIBRARY_PATH=SOMEPATH']
    """
    if is_conda_env():
        return set_conda_env(vars, name)

    if (not 'posix' in os.name):
        return

    exportstr = get_posix_activate_export_str(vars)

    try:
        filename = '/etc/profile.d/' + name + '.sh'
        filehandle = open(filename, 'w')
    except:
        # On Mac, we set the /etc/profile file (there is not .bashrc file)
        if "darwin" in sys.platform.lower():
            filename = os.path.join(os.path.expanduser('~'), ".profile")
        else:
            filename = os.path.join(os.path.expanduser('~'), ".bashrc")

        print(("Warning : Cannot create /etc/profile.d/%s.sh" % (name)))
        print(("Trying to setup environment in %s" % filename))

        # If profile.d directory is not writable, try to update $HOM/.bashrc
        try:
            script_name = ".%s.sh" % (name)

            filehandle = open(filename, 'r')
            bashrc = filehandle.read()
            filehandle.close()

            # create the string to look for : "source ~/.openalea.sh"
            bashrc_cmd = "source ~/%s" % (script_name,)

            # post processing: remove all commented lines to avoid to consider
            # these lines as valid; in particular :"#source ~/.bashrc
            bashrc_list = bashrc.split('\n')
            bashrc = []
            for line in bashrc_list:
                if not line.startswith('#'):
                    bashrc.append(line)

            # search for the "source ~/.openalea.sh" string
            if not bashrc_cmd in bashrc:
                filehandle = open(filename, 'a+')
                filehandle.write('\n' + bashrc_cmd)
                filehandle.close()

            # create the openalea shell script
            filename = os.path.join(os.path.expanduser('~'), script_name)
            filehandle = open(filename, 'w')

        except Exception as e:
            print(e)
            raise

    print(("Creating %s" % (filename,)))

    filehandle.write(exportstr)

    filehandle.close()
    # cmdstr = "(echo $SHELL|grep bash>/dev/null)&&. %s
    # ||source %s"%(filename,filename)
    cmdstr = ". %s" % (filename,)
    print("To enable new OpenAlea config, open a new shell or type")
    print(('  $ %s' % (bashrc_cmd)))