-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
73 lines (56 loc) · 2 KB
/
tasks.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
"""Maintenance tasks for the project."""
import json
import os
import sys
import tempfile
import tomllib
import invoke
try:
from jupyter_client import kernelspec
except ImportError:
raise RuntimeError('Install the "ipykernel" package first') from None
def _ensure_venv():
# Source: https://stackoverflow.com/questions/1871549/how-to-determine-if-python-is-running-inside-a-virtualenv # pylint:disable=C0301
if sys.prefix == sys.base_prefix:
raise RuntimeError("Run this command in an activated `virtualenv`")
def _get_pyproject_name():
with open("pyproject.toml", "rb") as fp:
data = tomllib.load(fp)
try:
project_name = data["tool"]["poetry"]["name"]
except KeyError:
raise RuntimeError('"pyproject.toml" seems to be malformed') from None
return project_name
@invoke.task
def install_kernel(_c):
"""Install the activated `virtualenv` as a `jupyter kernel`.
This helper task
"""
_ensure_venv()
project_name = _get_pyproject_name()
with tempfile.TemporaryDirectory() as tmpdir:
spec = {
"argv": [
sys.prefix + "/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}",
],
"display_name": project_name,
"env": {"PATH": os.environ["PATH"]},
"interrupt_mode": "signal",
"language": "python",
"metadata": {"debugger": True},
}
with open(os.path.join(tmpdir, "kernel.json"), "w", encoding="utf-8") as fp:
json.dump(spec, fp, indent=4)
manager = kernelspec.KernelSpecManager()
manager.install_kernel_spec(tmpdir, kernel_name=project_name, user=True)
@invoke.task
def remove_kernel(_c):
"""Remove the `jupyter kernel` corresponding to the activated `virtualenv`."""
_ensure_venv()
project_name = _get_pyproject_name()
manager = kernelspec.KernelSpecManager()
manager.remove_kernel_spec(project_name)