-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovisioner.py
49 lines (38 loc) · 1.94 KB
/
provisioner.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
from logging import Logger
from pathlib import Path
from typing import Any, Optional, cast
from jupyter_client.kernelspec import KernelSpec
from jupyter_client.provisioning.local_provisioner import LocalProvisioner
from .pixi import ensure_readiness
class PixiKernelProvisioner(LocalProvisioner): # type: ignore
async def pre_launch(self, **kwargs: Any) -> dict[str, Any]:
"""Perform any steps in preparation for kernel process launch.
This includes ensuring Pixi is installed and that a Pixi project is available.
"""
logger = cast(Logger, self.log)
kernel_spec = cast(KernelSpec, self.kernel_spec)
kernel_metadata: Optional[dict[str, str]] = kernel_spec.metadata.get("pixi-kernel")
if kernel_metadata is None:
raise ValueError(
f"Kernel {kernel_spec.display_name} uses the PixiKernelProvisioner but it"
"does not have Pixi Kernel metadata."
)
required_package = kernel_metadata.get("required-package")
if required_package is None:
raise ValueError("Pixi Kernel metadata is missing the 'required-package' key")
cwd = Path(kwargs.get("cwd", Path.cwd()))
logger.info(f"The current working directory is {cwd}")
environment = ensure_readiness(
cwd=cwd,
required_package=required_package,
kernel_name=kernel_spec.display_name,
)
# R kernel needs special treatment
# https://github.com/renan-r-santos/pixi-kernel/issues/15
if required_package == "r-irkernel":
r_libs_path = str(Path(environment.prefix) / "lib" / "R" / "library")
kernel_spec.env["R_LIBS"] = r_libs_path
kernel_spec.env["R_LIBS_SITE"] = r_libs_path
kernel_spec.env["R_LIBS_USER"] = r_libs_path
logger.info(f"Launching {kernel_spec.display_name}: {kernel_spec.to_dict()}")
return await super().pre_launch(**kwargs)