def run_in(command, shell): if shell == 'cmd.exe': cmd_script = tempfile.NamedTemporaryFile(suffix='.bat', mode='wt', delete=False) cmd_script.write(command) cmd_script.close() cmd_bits = [shells[shell]["exe"] ] + shells[shell]["shell_args"] + [cmd_script.name] try: p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() finally: os.unlink(cmd_script.name) elif shell == 'powershell': raise NotImplementedError else: cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [ translate_stream(command, shells[shell]["path_to"]) ] p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() output_translator = shells[shell]["path_from"] streams = [ u"%s" % translate_stream(stream.strip().decode('utf-8').replace('\r\n', '\n'), output_translator) for stream in (stdout, stderr) ] return streams
def run_in(command, shell, cwd=None, env=None): if hasattr(shell, "keys"): shell = shell["exe"] if shell == 'cmd.exe': cmd_script = tempfile.NamedTemporaryFile(suffix='.bat', mode='wt', delete=False) cmd_script.write(command) cmd_script.close() cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [cmd_script.name] try: print(cmd_bits) print(command) p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env) stdout, stderr = p.communicate() finally: os.unlink(cmd_script.name) elif shell == 'powershell': raise NotImplementedError else: cmd_bits = ([shells[shell]["exe"]] + shells[shell]["shell_args"] + [translate_stream(command, shells[shell]["path_to"])]) print(cmd_bits) p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() streams = [u"%s" % stream.decode('utf-8').replace('\r\n', '\n').rstrip("\n") for stream in (stdout, stderr)] return streams
def run_in(command, shell): if shell == 'cmd.exe': cmd_script = tempfile.NamedTemporaryFile(suffix='.bat', mode='wt', delete=False) cmd_script.write(command) cmd_script.close() cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [cmd_script.name] try: p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() finally: os.unlink(cmd_script.name) elif shell == 'powershell': raise NotImplementedError else: cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [translate_stream(command, shells[shell]["path_to"])] p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() output_translator = shells[shell]["path_from"] streams = [u"%s" % translate_stream(stream.strip().decode('utf-8').replace('\r\n', '\n'), output_translator) for stream in (stdout, stderr)] return streams
def main(): import conda.config import conda.install if '-h' in sys.argv or '--help' in sys.argv: help() path = os.getenv("PATH") # This one is because we force Library/bin to be on PATH on windows. Strip it off here. if on_win: path = path.replace( join(sys.prefix, "Library", "bin") + os.pathsep, "", 1) parent_shell = find_parent_shell(path=True) if sys.argv[1] == '..activate': if len(sys.argv) == 2 or sys.argv[2].lower() == "root": binpath = binpath_from_arg("root") rootpath = None elif len(sys.argv) == 3: base_path = sys.argv[2] binpath = binpath_from_arg(base_path) rootpath = os.pathsep.join(binpath_from_arg("root")) else: sys.exit("Error: did not expect more than one argument") sys.stderr.write("prepending %s to PATH\n" % pathlist_to_str(binpath)) path = os.pathsep.join([os.pathsep.join(binpath), path]) if any( [shell in parent_shell for shell in ["cmd.exe", "powershell.exe"]]): path = translate_stream(path, unix_path_to_win) # Clear the root path if it is present if rootpath: path = path.replace( translate_stream(rootpath, unix_path_to_win), "") elif 'cygwin' in parent_shell: # this should be harmless to unix paths, but converts win paths to # unix for bash on win (msys, cygwin) path = translate_stream(path, win_path_to_cygwin) # Clear the root path if it is present if rootpath: path = path.replace( translate_stream(rootpath, win_path_to_cygwin), "") else: if sys.platform == 'win32': path = translate_stream(path, win_path_to_unix) if rootpath: rootpath = translate_stream(rootpath, win_path_to_unix) # Clear the root path if it is present if rootpath: path = path.replace(rootpath, "") elif sys.argv[1] == '..deactivate': path = os.getenv("CONDA_PATH_BACKUP", "") sys.stderr.write("path:") sys.stderr.write(path) if path: sys.stderr.write("Restoring PATH to deactivated state\n") else: path = os.getenv( "PATH" ) # effectively a no-op; just set PATH to what it already is elif sys.argv[1] == '..checkenv': if len(sys.argv) < 3: sys.exit("Error: no environment provided.") if len(sys.argv) > 3: sys.exit("Error: did not expect more than one argument.") if sys.argv[2] == 'root': # no need to check root env and try to install a symlink there sys.exit(0) # this should throw an error and exit if the env or path can't be found. binpath = binpath_from_arg(sys.argv[2]) # Make sure an env always has the conda symlink try: conda.install.symlink_conda(binpath[0], conda.config.root_dir, find_parent_shell()) except (IOError, OSError) as e: if e.errno == errno.EPERM or e.errno == errno.EACCES: msg = ( "Cannot activate environment {0}, not have write access to conda symlink" .format(sys.argv[2])) sys.exit(msg) raise sys.exit(0) elif sys.argv[1] == '..setps1': # path is a bit of a misnomer here. It is the prompt setting. However, it is returned # below by printing. That is why it is named "path" path = sys.argv[3] if not path: if on_win: path = os.getenv("PROMPT", "$P$G") else: # zsh uses prompt. If it exists, prefer it. path = os.getenv("PROMPT") # fall back to bash default if not path: path = os.getenv("PS1") # strip off previous prefix, if any: path = re.sub(".*\(\(.*\)\)\ ", "", path, count=1) env_path = sys.argv[2] if conda.config.changeps1 and env_path: path = "(({})) {}".format(os.path.split(env_path)[-1], path) else: # This means there is a bug in main.py raise ValueError("unexpected command") # This print is actually what sets the PATH or PROMPT variable. The shell # script gets this value, and finishes the job. print(path)
def main(): import conda.config import conda.install if '-h' in sys.argv or '--help' in sys.argv: help() path = os.getenv("PATH") # This one is because we force Library/bin to be on PATH on windows. Strip it off here. if on_win: path = path.replace(join(sys.prefix, "Library", "bin")+os.pathsep, "", 1) parent_shell = find_parent_shell(path=True) if sys.argv[1] == '..activate': if len(sys.argv) == 2 or sys.argv[2].lower() == "root": binpath = binpath_from_arg("root") rootpath = None elif len(sys.argv) == 3: base_path = sys.argv[2] binpath = binpath_from_arg(base_path) rootpath = os.pathsep.join(binpath_from_arg("root")) else: sys.exit("Error: did not expect more than one argument") sys.stderr.write("prepending %s to PATH\n" % pathlist_to_str(binpath)) path = os.pathsep.join([os.pathsep.join(binpath), path]) if any([shell in parent_shell for shell in ["cmd.exe", "powershell.exe"]]): path = translate_stream(path, unix_path_to_win) # Clear the root path if it is present if rootpath: path = path.replace(translate_stream(rootpath, unix_path_to_win), "") elif 'cygwin' in parent_shell: # this should be harmless to unix paths, but converts win paths to unix for bash on win (msys, cygwin) path = translate_stream(path, win_path_to_cygwin) # Clear the root path if it is present if rootpath: path = path.replace(translate_stream(rootpath, win_path_to_cygwin), "") else: if sys.platform == 'win32': path = translate_stream(path, win_path_to_unix) if rootpath: rootpath = translate_stream(rootpath, win_path_to_unix) # Clear the root path if it is present if rootpath: path = path.replace(rootpath, "") elif sys.argv[1] == '..deactivate': path = os.getenv("CONDA_PATH_BACKUP", "") sys.stderr.write("path:") sys.stderr.write(path) if path: sys.stderr.write("Restoring PATH to deactivated state\n") else: path = os.getenv("PATH") # effectively a no-op; just set PATH to what it already is elif sys.argv[1] == '..checkenv': if len(sys.argv) < 3: sys.exit("Error: no environment provided.") if len(sys.argv) > 3: sys.exit("Error: did not expect more than one argument.") if sys.argv[2] == 'root': # no need to check root env and try to install a symlink there sys.exit(0) binpath = binpath_from_arg(sys.argv[2]) # this should throw an error and exit if the env or path can't be found. # Make sure an env always has the conda symlink try: conda.install.symlink_conda(binpath[0], conda.config.root_dir, find_parent_shell()) except (IOError, OSError) as e: if e.errno == errno.EPERM or e.errno == errno.EACCES: sys.exit("Cannot activate environment {}, do not have write access to write conda symlink".format(sys.argv[2])) raise sys.exit(0) elif sys.argv[1] == '..setps1': # path is a bit of a misnomer here. It is the prompt setting. However, it is returned # below by printing. That is why it is named "path" path = sys.argv[3] if not path: if on_win: path = os.getenv("PROMPT", "$P$G") else: # zsh uses prompt. If it exists, prefer it. path = os.getenv("PROMPT") # fall back to bash default if not path: path = os.getenv("PS1") # strip off previous prefix, if any: path = re.sub(".*\(\(.*\)\)\ ", "", path, count=1) env_path = sys.argv[2] if conda.config.changeps1 and env_path: path = "(({})) {}".format(os.path.split(env_path)[-1], path) else: # This means there is a bug in main.py raise ValueError("unexpected command") # This print is actually what sets the PATH or PROMPT variable. The shell script gets this value, and finishes the job. print(path)