forked from jupyterlite/xeus-python-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_build_addon.py
340 lines (277 loc) · 10.9 KB
/
env_build_addon.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""a JupyterLite addon for creating the env for xeus-python"""
import json
import os
from pathlib import Path
import requests
import shutil
from subprocess import check_call, run, DEVNULL
from tempfile import TemporaryDirectory
from urllib.parse import urlparse
import yaml
from traitlets import List, Unicode
from empack.file_packager import pack_environment
from empack.file_patterns import PkgFileFilter, pkg_file_filter_from_yaml
from jupyterlite.constants import (
SHARE_LABEXTENSIONS,
LAB_EXTENSIONS,
JUPYTERLITE_JSON,
UTF8,
FEDERATED_EXTENSIONS,
)
from jupyterlite.addons.federated_extensions import (
FederatedExtensionAddon,
ENV_EXTENSIONS,
)
JUPYTERLITE_XEUS_PYTHON = "@jupyterlite/xeus-python-kernel"
# TODO Make this configurable
PYTHON_VERSION = "3.10"
CHANNELS = [
"https://repo.mamba.pm/emscripten-forge",
"https://repo.mamba.pm/conda-forge",
]
PLATFORM = "emscripten-32"
SILENT = dict(stdout=DEVNULL, stderr=DEVNULL)
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")
class PackagesList(List):
def from_string(self, s):
return s.split(",")
class XeusPythonEnv(FederatedExtensionAddon):
__all__ = ["post_build"]
xeus_python_version = Unicode().tag(
config=True, description="The xeus-python version to use"
)
empack_config = Unicode(
None,
config=True,
allow_none=True,
description="The path or URL to the empack config file",
)
packages = PackagesList([]).tag(
config=True,
description="A comma-separated list of packages to install in the xeus-python env",
)
environment_file = Unicode(
"environment.yml",
config=True,
description="The path to the environment file. Defaults to \"environment.yml\"",
)
@property
def prefix_path(self):
"""The environment prefix."""
return Path(self.root_prefix) / "envs" / self.env_name
def __init__(self, *args, **kwargs):
super(XeusPythonEnv, self).__init__(*args, **kwargs)
self.cwd = TemporaryDirectory()
self.root_prefix = "/tmp/xeus-python-kernel"
self.env_name = "xeus-python-kernel"
self.channels = CHANNELS
self.specs = [
f"python={PYTHON_VERSION}",
"xeus-python"
if not self.xeus_python_version
else f"xeus-python={self.xeus_python_version}",
*self.packages,
]
# Cleanup tmp dir in case it's not empty
shutil.rmtree(Path(self.root_prefix) / "envs", ignore_errors=True)
Path(self.root_prefix).mkdir(parents=True, exist_ok=True)
self.orig_config = os.environ.get("CONDARC")
def post_build(self, manager):
"""yield a doit task to create the emscripten-32 env and grab anything we need from it"""
# Install the jupyterlite-xeus-python ourselves
for pkg_json in self.env_extensions(ENV_EXTENSIONS):
pkg_data = json.loads(pkg_json.read_text(**UTF8))
if pkg_data.get("name") == JUPYTERLITE_XEUS_PYTHON:
yield from self.safe_copy_extension(pkg_json)
bail_early = True
if self.packages or self.xeus_python_version:
bail_early = False
# Process environment.yml file
if (Path(self.manager.lite_dir) / self.environment_file).exists():
bail_early = False
with open(Path(self.manager.lite_dir) / self.environment_file) as f:
env_data = yaml.safe_load(f)
if env_data.get("name") is not None:
self.env_name = env_data["name"]
if env_data.get("channels") is not None:
channels = env_data["channels"]
for channel in channels:
if channel not in self.channels:
self.channels.append(channel)
if env_data.get("dependencies") is not None:
dependencies = env_data["dependencies"]
for dependency in dependencies:
if isinstance(dependency, str) and dependency not in self.specs:
self.specs.append(dependency)
elif isinstance(dependency, dict) and dependency.get("pip") is not None:
raise RuntimeError(
"""Cannot install pip dependencies in the xeus-python Emscripten environment (yet?).
"""
)
# Bail early if there is nothing to do
if bail_early:
return []
# Create emscripten env with the given packages
self.create_env()
pack_kwargs = {}
# Download env filter config
if self.empack_config is not None:
empack_config_is_url = urlparse(self.empack_config).scheme in ("http", "https")
if empack_config_is_url:
empack_config_content = requests.get(self.empack_config).content
pack_kwargs["pkg_file_filter"] = PkgFileFilter.parse_obj(
yaml.safe_load(empack_config_content)
)
else:
pack_kwargs["pkg_file_filter"] = pkg_file_filter_from_yaml(self.empack_config)
# Pack the environment
pack_environment(
env_prefix=self.prefix_path,
outname=Path(self.cwd.name) / "python_data",
export_name="globalThis.Module",
**pack_kwargs,
)
# Find the federated extensions in the emscripten-env and install them
root = self.prefix_path / SHARE_LABEXTENSIONS
# Copy federated extensions found in the emscripten-env
for pkg_json in self.env_extensions(root):
yield from self.safe_copy_extension(pkg_json)
# TODO Currently we're shamelessly overwriting the
# python_data.{js,data} into the jupyterlite-xeus-python labextension.
# We should really find a nicer way.
# (make jupyterlite-xeus-python extension somewhat configurable?)
dest = self.output_extensions / "@jupyterlite" / "xeus-python-kernel" / "static"
for file in ["python_data.js", "python_data.data"]:
yield dict(
name=f"xeus:copy:{file}",
actions=[(self.copy_one, [Path(self.cwd.name) / file, dest / file])],
)
for file in ["xpython_wasm.js", "xpython_wasm.wasm"]:
yield dict(
name=f"xeus:copy:{file}",
actions=[
(
self.copy_one,
[
self.prefix_path / "bin" / file,
dest / file,
],
)
],
)
jupyterlite_json = manager.output_dir / JUPYTERLITE_JSON
lab_extensions_root = manager.output_dir / LAB_EXTENSIONS
lab_extensions = self.env_extensions(lab_extensions_root)
yield dict(
name="patch:xeus",
doc=f"ensure {JUPYTERLITE_JSON} includes the federated_extensions",
file_dep=[*lab_extensions, jupyterlite_json],
actions=[(self.patch_jupyterlite_json, [jupyterlite_json])],
)
def create_env(self):
"""Create the xeus-python emscripten-32 env with either mamba, micromamba or conda."""
if MAMBA_PYTHON_AVAILABLE:
mamba_create(
env_name=self.env_name,
base_prefix=self.root_prefix,
specs=self.specs,
channels=self.channels,
target_platform=PLATFORM,
)
return
channels = []
for channel in self.channels:
channels.extend(["-c", channel])
if MAMBA_COMMAND:
# Mamba needs the directory to exist already
self.prefix_path.mkdir(parents=True, exist_ok=True)
return self._create_env_with_config(MAMBA_COMMAND, channels)
if MICROMAMBA_COMMAND:
run(
[
MICROMAMBA_COMMAND,
"create",
"--yes",
"--root-prefix",
self.root_prefix,
"--name",
self.env_name,
f"--platform={PLATFORM}",
*channels,
*self.specs,
],
cwd=self.cwd.name,
check=True,
)
return
if CONDA_COMMAND:
return self._create_env_with_config(CONDA_COMMAND, channels)
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(self, conda, channels):
run(
[conda, "create", "--yes", "--prefix", self.prefix_path, *channels],
cwd=self.cwd.name,
check=True,
)
self._create_config()
run(
[
conda,
"install",
"--yes",
"--prefix",
self.prefix_path,
*channels,
*self.specs,
],
cwd=self.cwd.name,
check=True,
)
def _create_config(self):
with open(self.prefix_path / ".condarc", "w") as fobj:
fobj.write(f"subdir: {PLATFORM}")
os.environ["CONDARC"] = str(self.prefix_path / ".condarc")
def safe_copy_extension(self, pkg_json):
"""Copy a labextension, and overwrite it
if it's already in the output
"""
pkg_path = pkg_json.parent
stem = json.loads(pkg_json.read_text(**UTF8))["name"]
dest = self.output_extensions / stem
file_dep = [
p
for p in pkg_path.rglob("*")
if not (p.is_dir() or self.is_ignored_sourcemap(p.name))
]
yield dict(
name=f"xeus:copy:ext:{stem}",
file_dep=file_dep,
actions=[(self.copy_one, [pkg_path, dest])],
)
def dedupe_federated_extensions(self, config):
if FEDERATED_EXTENSIONS not in config:
return
named = {}
# Making sure to dedupe extensions by keeping the most recent ones
for ext in config[FEDERATED_EXTENSIONS]:
if os.path.exists(self.output_extensions / ext["name"] / ext["load"]):
named[ext["name"]] = ext
config[FEDERATED_EXTENSIONS] = sorted(named.values(), key=lambda x: x["name"])
def __del__(self):
# Cleanup
shutil.rmtree(Path(self.root_prefix) / "envs", ignore_errors=True)
if self.orig_config is not None:
os.environ["CONDARC"] = self.orig_config
elif "CONDARC" in os.environ:
del os.environ["CONDARC"]