forked from robots-from-jupyter/robotkernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.py
315 lines (286 loc) · 8.91 KB
/
dodo.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
"""dodo.py"""
from pathlib import Path
from re import sub
import shutil
from hashlib import sha256
import sys
import os
import subprocess
import json
import doit.tools
PY = Path(sys.executable)
DOIT_CONFIG = dict(verbosity=2)
DODO = Path(__file__)
HERE = DODO.parent
CONF = HERE / "conf.py"
DIST = HERE / "dist"
DOCS = HERE / "docs"
LITE = HERE / "lite"
LICENSE = HERE / "LICENSE"
EXT = LITE / "jupyterlite-robotkernel"
EXT_LICENSE = EXT / LICENSE.name
EXT_SRC_PKG = EXT / "package.json"
EXT_SRC_PKG_DATA = json.load(EXT_SRC_PKG.open())
EXT_DIST_PKG = EXT / "py_src/jupyterlite_robotkernel/labextension/package.json"
EXT_DIST = EXT / "dist"
EXT_WHL_NAME = f"""jupyterlite_robotkernel-{EXT_SRC_PKG_DATA["version"]}-py3-none-any.whl"""
EXT_WHL = EXT_DIST / EXT_WHL_NAME
EXT_ICON = EXT / "style/robotkernel.png"
SHA256SUMS = DIST / "SHA256SUMS"
KERNEL_DATA = HERE / "src/robotkernel/resources/kernel"
KERNEL_ICON = KERNEL_DATA / "logo-64x64.png"
WHL_PY = [
p
for p in [
HERE / "setup.py",
*Path("src").rglob("*.py"),
]
if p.name != "_version.py"
]
WHL_MD = [HERE / "README.rst"]
WHL_DEPS = [
HERE / "setup.cfg",
*KERNEL_DATA.rglob("*"),
*WHL_MD,
*WHL_PY,
]
ALL_PY = [DODO, *WHL_PY]
ALL_MD = [*WHL_MD]
ALL_JSON = [
*LITE.glob("*.json"),
*LITE.glob("*/*.json"),
*[p for p in WHL_DEPS if p.suffix == "json"],
]
SOURCE_DATE_EPOCH = (
subprocess.check_output(["git", "log", "-1", "--format=%ct"])
.decode("utf-8")
.strip()
)
os.environ.update(SOURCE_DATE_EPOCH=SOURCE_DATE_EPOCH)
def task_dist():
"""build distributions"""
def hashfile():
lines = []
for p in sorted([*DIST.glob("robotkernel*.whl"), *DIST.glob("robotkernel*.tar.gz")]):
if p == SHA256SUMS:
continue
lines += [" ".join([sha256(p.read_bytes()).hexdigest(), p.name])]
output = "\n".join(lines)
print(output)
SHA256SUMS.write_text(output)
yield dict(
name="robotkernel",
doc="build robotkernel distributions",
file_dep=WHL_DEPS,
actions=[
lambda: [shutil.rmtree(DIST) if DIST.is_dir() else None, None][-1],
[PY, "setup.py", "sdist"],
[PY, "-m", "pip", "wheel", "-w", DIST, "--no-deps", "."],
hashfile,
],
targets=[SHA256SUMS],
)
@doit.create_after("dist")
def task_labext():
pkg = EXT / "package.json"
lock = EXT / "yarn.lock"
integrity = EXT / "node_modules" / ".yarn-integrity"
packages = EXT / "packages"
packages_pkg = [
packages / "robolite-kernel" / "package.json",
packages / "robolite-kernel-extension" / "package.json",
]
packages_lock = [
packages / "robolite-kernel" / "yarn.lock",
packages / "robolite-kernel-extension" / "yarn.lock",
]
packages_integrity= [
packages / "robolite-kernel" / "node_modules" / ".yarn-integrity",
packages / "robolite-kernel-extension" / "node_modules" / ".yarn-integrity",
]
packages_ts_src = [
*(packages / "robolite-kernel" / "src").rglob("*.ts"),
*(packages / "robolite-kernel-extension" / "src").rglob("*.ts"),
]
packages_ts_buildinfo = [
*(packages / "robolite-kernel" / "src").rglob("tsconfig.buildinfo"),
*(packages / "robolite-kernel-extension" / "src").rglob("tsconfig.buildinfo"),
]
lerna = EXT / "node_modules" / ".bin" / "lerna"
def _do(*args, **kwargs):
cwd = str(kwargs.pop("cwd", EXT))
return doit.tools.CmdAction([*args], **kwargs, cwd=cwd, shell=False)
def _copy_static():
copy_one(KERNEL_ICON, EXT_ICON)
copy_one(LICENSE, EXT_LICENSE)
yield dict(
name="lerna install",
file_dep=[pkg] + ([lock] if lock.exists() else []),
targets=[integrity],
actions=[_do("yarn", "--prefer-offline", "--ignore-optional")],
)
yield dict(
name="lerna bootstrap",
file_dep=[*packages_pkg] + ([lock for lock in packages_lock if lock.exists()]),
targets=[*packages_integrity],
actions=[_do(lerna, "bootstrap")],
)
yield dict(
name="copy:static",
file_dep=[*DIST.glob("*.any.whl"), KERNEL_ICON, LICENSE],
targets=[EXT_ICON, EXT_LICENSE],
actions=[_copy_static],
)
yield dict(
name="build:ts",
file_dep=[*packages_ts_src, *packages_pkg, *packages_integrity],
targets=[*packages_ts_buildinfo],
actions=[_do(lerna, "run", "build:lib")],
)
yield dict(
name="build:ext",
file_dep=[*packages_ts_buildinfo, *packages_integrity, *packages_pkg],
targets=[EXT_DIST_PKG],
actions=[_do(lerna, "run", "build:labextension")],
)
yield dict(
name="wheel:ext",
file_dep=[EXT_DIST_PKG, EXT_LICENSE],
actions=[_do(PY, "-m", "pip", "wheel", "--no-deps", "-w", EXT_DIST, ".")],
targets=[EXT_WHL],
)
@doit.create_after("labext")
def task_lite():
"""build jupyterlite site and pre-requisites"""
wheel = sorted(DIST.glob("robotkernel*.whl"))[-1]
def _clean_wheels():
# Remove wheels that conflict with pyolite shims
for path in (LITE / "pypi").glob("ipykernel-*"):
os.unlink(path)
for path in (LITE / "pypi").glob("widgetsnbextension-*"):
os.unlink(path)
# Remove binary wheels
for path in set((LITE / "pypi").glob("*")) - (set((LITE / "pypi").glob("*-none-any.whl"))):
os.unlink(path)
# Remove addon fetched
for path in json.loads((EXT / "py_src" / "jupyterlite_robotkernel" / "addons" / "wheels.json").read_text()):
for path_ in (LITE / "pypi").glob(path.rsplit("/")[-1]):
os.unlink(path_)
yield dict(
name="wheels",
file_dep=[SHA256SUMS, wheel],
actions=[
(doit.tools.create_folder, [LITE / "pypi"]),
doit.tools.CmdAction(
[PY, "-m", "pip", "wheel", "--prefer-binary", "--no-deps", wheel],
cwd=str(LITE / "pypi"),
shell=False,
),
# Not sure, why these were not discovered from conda environment
doit.tools.CmdAction(
[PY, "-m", "pip", "wheel", "--no-deps", "--prefer-binary", "jupyterlab-drawio"],
cwd=str(LITE),
shell=False,
),
_clean_wheels,
],
)
def _build():
subprocess.check_call(
[
PY,
"-m",
"jupyter",
"lite",
"build",
"--debug",
"--contents",
str(LITE / "contents"),
"--LiteBuildConfig.federated_extensions",
f"{list(LITE.glob('jupyterlab_drawio*'))[-1]}",
"--LiteBuildConfig.federated_extensions",
EXT_WHL,
],
cwd=str(LITE)
)
subprocess.check_call(
[
"mkdir",
"-p",
"_",
],
cwd=str(LITE)
)
subprocess.check_call(
[
"mv",
"_output",
"_/_",
],
cwd=str(LITE)
)
yield dict(
name="build",
file_dep=[
SHA256SUMS,
wheel,
EXT_WHL,
*LITE.glob("*.json"),
*(LITE / "retro").glob("*.json"),
*DOCS.rglob("*.ipynb"),
],
actions=[_build],
targets=[LITE / "_/_/jupyter-lite.json"],
)
@doit.create_after("lite")
def task_docs():
def post():
CONF.write_text(
"\n".join(
[
"import subprocess",
"""subprocess.check_call(["doit", "lite"])""",
sub(
r'external_toc_path = "\S+_toc.yml"',
r'external_toc_path = "_toc.yml"',
CONF.read_text(),
),
]
)
)
yield dict(
name="sphinx-config",
file_dep=["_toc.yml", "_config.yml"],
actions=[
"jb config sphinx .",
post,
"black conf.py",
],
targets=[CONF],
)
yield dict(
name="sphinx-build",
file_dep=[
CONF,
*DOCS.rglob("*.ipynb"),
*(HERE / "_templates").glob("*.html"),
LITE / "_/_/jupyter-lite.json",
],
actions=["sphinx-build . _build/html %(pos)s"],
pos_arg="pos",
targets=[HERE / "_build/html/.buildinfo"],
)
def copy_one(src, dest):
if not src.exists():
return False
if not dest.parent.exists():
dest.parent.mkdir(parents=True)
if dest.exists():
if dest.is_dir():
shutil.rmtree(dest)
else:
dest.unlink()
if src.is_dir():
shutil.copytree(src, dest)
else:
shutil.copy2(src, dest)