-
Notifications
You must be signed in to change notification settings - Fork 12
/
create_conda_env.py
164 lines (136 loc) · 4.58 KB
/
create_conda_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import shutil
import sys
from pathlib import Path
from subprocess import run as subprocess_run
import os
from ._pip import _install_pip_dependencies
try:
from mamba.api import create as mamba_create
MAMBA_PYTHON_AVAILABLE = True
except ImportError:
MAMBA_PYTHON_AVAILABLE = False
MAMBA_COMMAND = shutil.which("mamba")
MICROMAMBA_COMMAND = shutil.which("micromamba")
CONDA_COMMAND = shutil.which("conda")
PLATFORM = "emscripten-wasm32"
def _extract_specs(env_location, env_data):
specs = []
pip_dependencies = []
# iterate dependencies
for dependency in env_data.get("dependencies", []):
if isinstance(dependency, str):
specs.append(dependency)
elif isinstance(dependency, dict) and "pip" in dependency:
for pip_dependency in dependency["pip"]:
# If it's a local Python package, make its path relative to the environment file
if (env_location / pip_dependency).is_dir():
pip_dependencies.append((env_location / pip_dependency).resolve())
else:
pip_dependencies.append(pip_dependency)
return specs, pip_dependencies
def create_conda_env_from_env_file(root_prefix, env_file_content, env_file_location):
# get the name of the environment
env_name = env_file_content.get("name", "xeus-env")
# get the channels
channels = env_file_content.get(
"channels", ["https://repo.mamba.pm/emscripten-forge", "conda-forge"]
)
# get the specs
specs, pip_dependencies = _extract_specs(env_file_location, env_file_content)
# Force emscripten version
specs.append("emscripten-abi=3.1.45")
create_conda_env_from_specs(
env_name=env_name,
root_prefix=root_prefix,
specs=specs,
channels=channels,
pip_dependencies=pip_dependencies,
)
def create_conda_env_from_specs(
env_name,
root_prefix,
specs,
channels,
pip_dependencies=None,
):
_create_conda_env_from_specs_impl(
env_name=env_name,
root_prefix=root_prefix,
specs=specs,
channels=channels,
)
if pip_dependencies:
_install_pip_dependencies(
prefix_path=Path(root_prefix) / "envs" / env_name,
dependencies=pip_dependencies,
)
def _create_conda_env_from_specs_impl(env_name, root_prefix, specs, channels):
"""Create the emscripten environment with the given specs."""
prefix_path = Path(root_prefix) / "envs" / env_name
# Cleanup tmp dir in case it's not empty
shutil.rmtree(Path(root_prefix) / "envs", ignore_errors=True)
Path(root_prefix).mkdir(parents=True, exist_ok=True)
if MAMBA_PYTHON_AVAILABLE:
mamba_create(
env_name=env_name,
base_prefix=root_prefix,
specs=specs,
channels=channels,
target_platform=PLATFORM,
)
return
channels_args = []
for channel in channels:
channels_args.extend(["-c", channel])
if MICROMAMBA_COMMAND:
subprocess_run(
[
MICROMAMBA_COMMAND,
"create",
"--yes",
"--no-pyc",
"--root-prefix",
root_prefix,
"--name",
env_name,
f"--platform={PLATFORM}",
*channels_args,
*specs,
],
check=True,
)
return
if MAMBA_COMMAND:
# Mamba needs the directory to exist already
prefix_path.mkdir(parents=True, exist_ok=True)
return _create_env_with_config(MAMBA_COMMAND, prefix_path, specs, channels_args)
if CONDA_COMMAND:
return _create_env_with_config(CONDA_COMMAND, prefix_path, specs, channels_args)
raise RuntimeError(
"""Failed to create the virtual environment for xeus-python,
please make sure at least mamba, micromamba or conda is installed.
"""
)
def _create_env_with_config(conda, prefix_path, specs, channels_args):
subprocess_run(
[conda, "create", "--yes", "--prefix", prefix_path, *channels_args],
check=True,
)
_create_config(prefix_path)
subprocess_run(
[
conda,
"install",
"--yes",
"--prefix",
prefix_path,
*channels_args,
*specs,
],
check=True,
env=os.environ.copy(),
)
def _create_config(prefix_path):
with open(prefix_path / ".condarc", "w") as fobj:
fobj.write(f"subdir: {PLATFORM}")
os.environ["CONDARC"] = str(prefix_path / ".condarc")