示例#1
0
def path_cleanup(scripts_dir, shell_type='bash'):
    ''' Removes SIMNIBS from PATH '''
    if sys.platform in ['linux', 'darwin']:
        if shell_type == 'bash':
            bashrc, backup_file = _get_bashrc()
        if shell_type == 'zsh':
            bashrc = os.path.expanduser('~/.zprofile')
            backup_file = os.path.expanduser('~/.zprofile_simnibs_bk')
        if not os.path.isfile(bashrc):
            print('Could not find bashrc file')
            return

        print(f'Removing SimNIBS install from {shell_type} PATH')
        print(f'Backing up the bashrc file at {backup_file}')
        _copy_and_log(bashrc, backup_file)
        with open(backup_file, 'r') as fin:
            with open(bashrc, 'w') as fout:
                for line in fin:
                    if not re.search('simnibs', line, re.IGNORECASE):
                        fout.write(line)
    else:
        simnibs_env_vars = _get_win_simnibs_env_vars()
        for key, value in simnibs_env_vars.items():
            # Remove environments variables with SimNIBS in their names.
            # These are leftovers from previous (3.0, 3.1) installs
            with winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                                'Environment',
                                access=winreg.KEY_WRITE) as reg:
                winreg.DeleteValue(reg, key)

        from simnibs.utils import _system_path
        _system_path.remove_from_system_path(scripts_dir, allusers=False)
        _system_path.broadcast_environment_settings_change()
示例#2
0
def path_setup(scripts_dir, force=False, silent=False, shell_type='bash'):
    ''' Modifies the bash startup path and postpends SimNIBS to the PATH '''
    scripts_dir = os.path.abspath(scripts_dir)
    if sys.platform in ['linux', 'darwin']:
        if shell_type == 'bash':
            bashrc, _ = _get_bashrc()
        elif shell_type == 'zsh':
            bashrc = os.path.expanduser('~/.zprofile')
        else:
            raise OSError('Invalid shell type')
        if os.path.exists(bashrc):
            has_simnibs = (re.search('simnibs',
                                     open(bashrc, 'r').read(), re.IGNORECASE)
                           is not None)
        else:
            has_simnibs = False

    if sys.platform == 'win32':
        has_simnibs = False

    if has_simnibs:
        if force:
            overwrite = True
        else:
            overwrite = _get_input(
                'Found another SimNIBS install, overwite it from the PATH?',
                silent)

        if not overwrite:
            print(
                f'Not Adding the current SimNIBS install to the {shell_type} PATH'
            )
            return False

        path_cleanup(scripts_dir, shell_type=shell_type)

    print(f'Postpending {scripts_dir} to the {shell_type} PATH')
    if sys.platform in ['linux', 'darwin']:
        with open(bashrc, 'a') as f:
            f.write('\n')
            f.write('## Added by SimNIBS\n')
            f.write(f'SIMNIBS_BIN="{scripts_dir}"\n')
            f.write('export PATH=${PATH}:${SIMNIBS_BIN}')

    else:
        from simnibs.utils import _system_path
        _system_path.add_to_system_path(scripts_dir, allusers=False)
        _system_path.broadcast_environment_settings_change()

    return True