|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import platform |
| 4 | +import threading |
| 5 | +import subprocess |
| 6 | + |
| 7 | +OS_NAME = platform.system() |
| 8 | + |
| 9 | + |
| 10 | +def locate_conda(): |
| 11 | + which = "where" if OS_NAME == "Windows" else "which" |
| 12 | + conda = subprocess.getoutput(f"{which} conda") |
| 13 | + conda = conda.split("\n") |
| 14 | + conda = conda[0].strip() |
| 15 | + |
| 16 | + return conda |
| 17 | + |
| 18 | + |
| 19 | +CONDA = locate_conda() |
| 20 | +print("conda: ", CONDA) |
| 21 | + |
| 22 | + |
| 23 | +def create_environment(env_dir, torch_platform, python_version="3.10"): |
| 24 | + env_dir = os.path.abspath(env_dir) |
| 25 | + print(f"Creating environment with {torch_platform} in {env_dir}..") |
| 26 | + |
| 27 | + # create the conda env |
| 28 | + run([CONDA, "create", "-y", "--prefix", env_dir]) |
| 29 | + |
| 30 | + print("Installing packages..") |
| 31 | + |
| 32 | + # install python and git in the conda env |
| 33 | + run([CONDA, "install", "-y", "--prefix", env_dir, "-c", "conda-forge", f"python={python_version}", "git"]) |
| 34 | + |
| 35 | + # print info |
| 36 | + run_in_conda(env_dir, ["git", "--version"]) |
| 37 | + run_in_conda(env_dir, ["python", "--version"]) |
| 38 | + |
| 39 | + # install the appropriate version of torch using torchruntime |
| 40 | + run_in_conda(env_dir, ["python", "-m", "pip", "install", "torchruntime"]) |
| 41 | + run_in_conda( |
| 42 | + env_dir, ["python", "-m", "torchruntime", "install", "torch", "torchvision", "--platform", torch_platform] |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def delete_environment(env_dir): |
| 47 | + shutil.rmtree(env_dir) |
| 48 | + |
| 49 | + |
| 50 | +def get_env(env_dir): |
| 51 | + if not os.path.exists(env_dir): |
| 52 | + raise RuntimeError("The system folder is missing!") |
| 53 | + |
| 54 | + python_exe = f"bin/python" if OS_NAME != "Windows" else f"python.exe" |
| 55 | + |
| 56 | + python_version = subprocess.getoutput(f"{env_dir}/{python_exe} --version") |
| 57 | + python_version = python_version.strip().split(" ")[1] |
| 58 | + python_version = ".".join(python_version.split(".")[:2]) |
| 59 | + |
| 60 | + env_entries = { |
| 61 | + "PATH": [ |
| 62 | + f"{env_dir}", |
| 63 | + f"{env_dir}/bin", |
| 64 | + f"{env_dir}/Library/bin", |
| 65 | + f"{env_dir}/Scripts", |
| 66 | + f"{env_dir}/usr/bin", |
| 67 | + ], |
| 68 | + "PYTHONPATH": [ |
| 69 | + f"{env_dir}", |
| 70 | + f"{env_dir}/lib/site-packages", |
| 71 | + f"{env_dir}/lib/python{python_version}/site-packages", |
| 72 | + ], |
| 73 | + "PYTHONHOME": [], |
| 74 | + } |
| 75 | + |
| 76 | + if OS_NAME == "Windows": |
| 77 | + env_entries["PATH"].append("C:/Windows/System32") |
| 78 | + env_entries["PATH"].append("C:/Windows/System32/wbem") |
| 79 | + env_entries["PYTHONNOUSERSITE"] = ["1"] |
| 80 | + env_entries["PYTHON"] = [f"{env_dir}/python"] |
| 81 | + env_entries["GIT"] = [f"{env_dir}/Library/bin/git"] |
| 82 | + else: |
| 83 | + env_entries["PATH"].append("/bin") |
| 84 | + env_entries["PATH"].append("/usr/bin") |
| 85 | + env_entries["PATH"].append("/usr/sbin") |
| 86 | + env_entries["PYTHONNOUSERSITE"] = ["y"] |
| 87 | + env_entries["PYTHON"] = [f"{env_dir}/bin/python"] |
| 88 | + env_entries["GIT"] = [f"{env_dir}/bin/git"] |
| 89 | + |
| 90 | + env = dict(os.environ) |
| 91 | + for key, paths in env_entries.items(): |
| 92 | + paths = [p.replace("/", os.path.sep) for p in paths] |
| 93 | + paths = os.pathsep.join(paths) |
| 94 | + |
| 95 | + env[key] = paths |
| 96 | + |
| 97 | + return env |
| 98 | + |
| 99 | + |
| 100 | +def read_output(pipe, prefix=""): |
| 101 | + while True: |
| 102 | + output = pipe.readline() |
| 103 | + if output: |
| 104 | + print(f"{prefix}{output.decode('utf-8')}", end="") |
| 105 | + else: |
| 106 | + break # Pipe is closed, subprocess has likely exited |
| 107 | + |
| 108 | + |
| 109 | +def run(cmds: list, cwd=None, env=None, stream_output=True, wait=True, output_prefix=""): |
| 110 | + p = subprocess.Popen(cmds, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 111 | + if stream_output: |
| 112 | + output_thread = threading.Thread(target=read_output, args=(p.stdout, output_prefix)) |
| 113 | + output_thread.start() |
| 114 | + |
| 115 | + if wait: |
| 116 | + p.wait() |
| 117 | + |
| 118 | + return p |
| 119 | + |
| 120 | + |
| 121 | +def run_in_conda(env_dir, cmds: list, *args, **kwargs): |
| 122 | + env = get_env(env_dir) |
| 123 | + |
| 124 | + cmds = [CONDA, "run", "--no-capture-output", "--prefix", env_dir] + cmds |
| 125 | + return run(cmds, env=env, *args, **kwargs) |
0 commit comments