def test_replace_prefix_in_path_1(self): activator = Activator('posix') original_path = tuple(activator._get_starting_path_list()) new_prefix = join(os.getcwd(), 'mytestpath-new') new_paths = activator.path_conversion(activator._get_path_dirs(new_prefix)) if isinstance(new_paths, string_types): new_paths = new_paths, keep_path = activator.path_conversion('/keep/this/path') final_path = (keep_path,) + new_paths + original_path final_path = activator.path_conversion(final_path) replace_prefix = join(os.getcwd(), 'mytestpath') replace_paths = tuple(activator._get_path_dirs(replace_prefix)) prefix_added_path = (keep_path,) + replace_paths + original_path new_path = activator._replace_prefix_in_path(replace_prefix, new_prefix, prefix_added_path) assert final_path == new_path
def test_remove_prefix_from_path_1(self): activator = Activator('posix') original_path = tuple(activator._get_starting_path_list()) keep_path = activator.path_conversion('/keep/this/path') final_path = (keep_path,) + original_path final_path = activator.path_conversion(final_path) test_prefix = join(os.getcwd(), 'mytestpath') new_paths = tuple(activator._get_path_dirs(test_prefix)) prefix_added_path = (keep_path,) + new_paths + original_path new_path = activator._remove_prefix_from_path(test_prefix, prefix_added_path) assert final_path == new_path
def test_add_prefix_to_path(self): activator = Activator('posix') path_dirs = activator.path_conversion(['/path1/bin', '/path2/bin', '/usr/local/bin', '/usr/bin', '/bin']) assert len(path_dirs) == 5 test_prefix = '/usr/mytest/prefix' added_paths = activator.path_conversion(activator._get_path_dirs(test_prefix)) if isinstance(added_paths, string_types): added_paths = added_paths, new_path = activator._add_prefix_to_path(test_prefix, path_dirs) assert new_path == added_paths + path_dirs
class InteractiveShell(object): activator = None init_command = None print_env_var = None shells = { 'posix': { 'activator': 'posix', 'init_command': 'set -u && . shell/etc/profile.d/conda.sh', 'print_env_var': 'echo $%s', }, 'bash': { 'base_shell': 'posix', # inheritance implemented in __init__ }, 'dash': { 'base_shell': 'posix', # inheritance implemented in __init__ }, 'zsh': { 'base_shell': 'posix', # inheritance implemented in __init__ }, 'cmd.exe': { 'activator': 'cmd.exe', 'init_command': None, 'print_env_var': '@echo %%%s%%', }, } def __init__(self, shell_name): self.shell_name = shell_name base_shell = self.shells[shell_name].get('base_shell') shell_vals = self.shells.get(base_shell, {}) shell_vals.update(self.shells[shell_name]) for key, value in iteritems(shell_vals): setattr(self, key, value) self.activator = Activator(shell_vals['activator']) def __enter__(self): from pexpect.popen_spawn import PopenSpawn cwd = os.getcwd() env = os.environ.copy() env['PATH'] = self.activator.pathsep_join(self.activator.path_conversion(concatv( self.activator._get_path_dirs(join(cwd, 'shell')), (dirname(sys.executable),), self.activator._get_starting_path_list(), ))) env['PYTHONPATH'] = CONDA_PACKAGE_ROOT env = {str(k): str(v) for k, v in iteritems(env)} p = PopenSpawn(self.shell_name, timeout=6, maxread=2000, searchwindowsize=None, logfile=sys.stdout, cwd=cwd, env=env, encoding=None, codec_errors='strict') if self.init_command: p.sendline(self.init_command) self.p = p return self def __exit__(self, exc_type, exc_val, exc_tb): if self.p: import signal self.p.kill(signal.SIGINT) def sendline(self, s): return self.p.sendline(s) def expect(self, pattern, timeout=-1, searchwindowsize=-1, async=False): return self.p.expect(pattern, timeout, searchwindowsize, async)