forked from jupyterlite/jupyterlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.py
938 lines (822 loc) · 28 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
import json
import os
import re
import shutil
import subprocess
import sys
from collections import defaultdict
from hashlib import sha256
from pathlib import Path
import doit
import jsonschema
def task_env():
"""keep environments in sync"""
yield dict(
name="binder",
file_dep=[P.DOCS_ENV],
targets=[P.BINDER_ENV],
actions=[(U.sync_env, [P.DOCS_ENV, P.BINDER_ENV, C.DOCS_ENV_MARKER])],
)
def task_setup():
"""perform initial non-python setup"""
args = ["yarn", "--prefer-offline", "--ignore-optional"]
if C.CI:
# .yarn-integrity will only exist on a full cache hit vs yarn.lock, saves 1min+
if B.YARN_INTEGRITY.exists():
return
args += ["--frozen-lockfile"]
yield dict(
name="js",
doc="install node packages",
file_dep=[
P.YARN_LOCK,
*P.PACKAGE_JSONS,
P.ROOT_PACKAGE_JSON,
P.APP_PACKAGE_JSON,
*P.APP_JSONS,
],
actions=[U.do(*args)],
targets=[B.YARN_INTEGRITY],
)
def task_lint():
"""format and ensure style of code, docs, etc."""
if C.RTD:
return
yield U.ok(
B.OK_PRETTIER,
name="prettier",
doc="format .ts, .md, .json, etc. files with prettier",
file_dep=[*L.ALL_PRETTIER, B.YARN_INTEGRITY],
actions=[U.do("yarn", "prettier:check" if C.CI else "prettier")],
)
yield U.ok(
B.OK_ESLINT,
name="eslint",
doc="format and verify .ts, .js files with eslint",
file_dep=[B.OK_PRETTIER, *L.ALL_ESLINT],
actions=[U.do("yarn", "eslint:check" if C.CI else "eslint")],
)
yield U.ok(
B.OK_BLACK,
name="black",
doc="format python files with black",
file_dep=L.ALL_BLACK,
actions=[
U.do("isort", *L.ALL_BLACK),
U.do("black", *(["--check"] if C.CI else []), *L.ALL_BLACK),
],
)
yield U.ok(
B.OK_PYFLAKES,
name="pyflakes",
doc="ensure python code style with pyflakes",
file_dep=[*L.ALL_BLACK, B.OK_BLACK],
actions=[U.do("pyflakes", *L.ALL_BLACK)],
)
def task_build():
"""build code and intermediate packages"""
yield dict(
name="favicon",
doc="rebuild favicons from svg source, requires imagemagick",
file_dep=[P.DOCS_ICON],
targets=[P.LAB_FAVICON],
actions=[["echo", "... `convert` not found, install imagemagick"]]
if not shutil.which("convert")
else [
lambda: [
subprocess.call(
[
"convert",
"-verbose",
"-density",
"256x256",
"-background",
"transparent",
P.DOCS_ICON,
"-define",
"icon:auto-resize",
"-colors",
"256",
P.LAB_FAVICON,
]
),
None,
][-1]
],
)
yield dict(
name="ui-components",
doc="copy the icon and wordmark to the ui-components package",
file_dep=[P.DOCS_ICON, P.DOCS_WORDMARK, B.YARN_INTEGRITY],
targets=[P.LITE_ICON, P.LITE_WORDMARK],
actions=[
U.do(
"yarn",
"svgo",
"--pretty",
"--indent=2",
P.DOCS_ICON,
P.DOCS_WORDMARK,
"-o",
P.LITE_ICON,
P.LITE_WORDMARK,
),
],
)
yield dict(
name="js:lib",
doc="build .ts files into .js files",
file_dep=[
*L.ALL_ESLINT,
P.ROOT_PACKAGE_JSON,
*P.PACKAGE_JSONS,
B.YARN_INTEGRITY,
],
actions=[
U.do("yarn", "build:lib"),
],
targets=[B.META_BUILDINFO],
)
wheels = []
for py_pkg, version in P.PYOLITE_PACKAGES.items():
name = py_pkg.name
wheel = py_pkg / f"dist/{name}-{version}-{C.NOARCH_WHL}"
wheels += [wheel]
yield dict(
name=f"js:py:{name}",
doc=f"build the {name} python package for the brower with flit",
file_dep=[*py_pkg.rglob("*.py"), py_pkg / "pyproject.toml"],
actions=[U.do("flit", "build", cwd=py_pkg)],
# TODO: get version
targets=[wheel],
)
app_deps = [B.META_BUILDINFO, P.WEBPACK_CONFIG, P.LITE_ICON, P.LITE_WORDMARK]
all_app_targets = []
for app_json in P.APP_JSONS:
app = app_json.parent
app_data = json.loads(app_json.read_text(**C.ENC))
app_build = app / "build"
app_targets = [
app_build / "bundle.js",
app_build / "index.js",
app_build / "style.js",
]
app_targets += [app_build / w.name for w in wheels]
all_app_targets += app_targets
yield dict(
name=f"js:app:{app.name}",
doc=f"build JupyterLite {app.name.title()} with webpack",
file_dep=[
*app_deps,
*wheels,
app / "index.template.js",
app_json,
],
actions=[
U.do("yarn", "lerna", "run", "build:prod", "--scope", app_data["name"])
],
targets=[*app_targets],
)
yield dict(
name="js:pack",
doc="build the JupyterLite distribution",
file_dep=[
*all_app_targets,
P.APP_SCHEMA,
*P.APP.glob("*/*/index.html"),
*P.APP.glob("*/build/schemas/**/.json"),
*P.APP.glob("*.js"),
*P.APP.glob("*.json"),
B.META_BUILDINFO,
P.APP / "index.html",
P.APP_NPM_IGNORE,
],
actions=[
(doit.tools.create_folder, [B.DIST]),
U.do("npm", "pack", "../app", cwd=B.DIST),
],
targets=[B.APP_PACK],
)
for py_name, setup_py in P.PY_SETUP_PY.items():
py_pkg = setup_py.parent
wheel = (
py_pkg
/ f"""dist/{py_name.replace("-", "_")}-{D.PY_VERSION}-{C.NOARCH_WHL}"""
)
sdist = py_pkg / f"""dist/{py_name.replace("_", "-")}-{D.PY_VERSION}.tar.gz"""
args = ["python", "setup.py", "sdist", "bdist_wheel"]
file_dep = [
*P.PY_SETUP_DEPS[py_name](),
*py_pkg.rglob("*.py"),
setup_py,
]
pyproj_toml = py_pkg / "pyproject.toml"
targets = [wheel, sdist]
# we might tweak the args
if pyproj_toml.exists() and "flit" in pyproj_toml.read_text(encoding="utf-8"):
args = ["flit", "build"]
file_dep += [pyproj_toml]
# make "the" action
actions = [U.do(*args, cwd=py_pkg)]
# may do some setup steps: TODO: refactor into separate task
if py_name == C.NAME:
dest = py_pkg / "src" / py_name / B.APP_PACK.name
actions = [
lambda: [dest.exists() and dest.unlink(), None][-1],
lambda: [shutil.copy2(B.APP_PACK, dest), None][-1],
*actions,
]
file_dep += [B.APP_PACK, pyproj_toml]
targets += [dest]
yield dict(
name=f"py:{py_name}",
doc=f"build the {py_name} python package",
file_dep=file_dep,
actions=actions,
targets=targets,
)
@doit.create_after("build")
def task_dist():
"""fix up the state of the distribution directory"""
dests = []
for dist in B.DISTRIBUTIONS:
dest = B.DIST / dist.name
dests += [dest]
yield dict(
name=f"copy:{dist.name}",
actions=[(U.copy_one, [dist, dest])],
file_dep=[dist],
targets=[dest],
)
yield dict(
name="hash",
file_dep=dests,
actions=[(U.hashfile, [B.DIST])],
targets=[B.DIST / "SHA256SUMS"],
)
def task_dev():
"""setup up local packages for interactive development"""
py_pkg = P.PY_SETUP_PY[C.NAME].parent
# TODO: probably name this file
dest = py_pkg / "src" / C.NAME / B.APP_PACK.name
yield dict(
name=f"py:{C.NAME}",
actions=[U.do("flit", "install", "--pth-file", cwd=py_pkg)],
file_dep=[dest],
)
def task_docs():
"""build documentation"""
yield dict(
name="typedoc:ensure",
file_dep=[*P.PACKAGE_JSONS],
actions=[U.typedoc_conf],
targets=[P.TYPEDOC_JSON, P.TSCONFIG_TYPEDOC],
)
yield dict(
name="typedoc:build",
doc="build the TS API documentation with typedoc",
file_dep=[B.META_BUILDINFO, *P.TYPEDOC_CONF],
actions=[U.do("yarn", "docs")],
targets=[B.DOCS_RAW_TYPEDOC_README],
)
yield dict(
name="typedoc:mystify",
doc="transform raw typedoc into myst markdown",
file_dep=[B.DOCS_RAW_TYPEDOC_README],
targets=[B.DOCS_TS_MYST_INDEX, *B.DOCS_TS_MODULES],
actions=[U.mystify, U.do("yarn", "prettier")],
)
yield dict(
name="app:build",
doc="use the jupyterlite CLI to (pre-)build the docs app",
task_dep=[f"dev:py:{C.NAME}"],
actions=[(U.docs_app, [])],
file_dep=[
B.APP_PACK,
*P.ALL_EXAMPLES,
# NOTE: these won't always trigger a rebuild because of the inner dodo
*P.PY_SETUP_PY[C.NAME].rglob("*.py"),
],
targets=[B.DOCS_APP_SHA256SUMS],
)
yield dict(
name="app:pack",
doc="build the as-deployed app archive",
file_dep=[B.DOCS_APP_SHA256SUMS],
actions=[(U.docs_app, ["archive"])],
targets=[B.DOCS_APP_ARCHIVE],
)
yield dict(
name="sphinx",
doc="build the documentation site with sphinx",
file_dep=[
*P.DOCS_MD,
*P.DOCS_PY,
*P.DOCS_IPYNB,
B.DOCS_APP_ARCHIVE,
B.DOCS_TS_MYST_INDEX,
],
actions=[U.do("sphinx-build", *C.SPHINX_ARGS, "-b", "html", P.DOCS, B.DOCS)],
targets=[B.DOCS_BUILDINFO, B.DOCS_STATIC_APP],
)
def task_schema():
"""update, validate the schema and instance documents"""
yield dict(
name="self", file_dep=[P.APP_SCHEMA], actions=[(U.validate, [P.APP_SCHEMA])]
)
for config in D.APP_CONFIGS:
yield dict(
name=f"validate:{config.relative_to(P.ROOT)}",
file_dep=[P.APP_SCHEMA, config],
actions=[(U.validate, (P.APP_SCHEMA, config))],
)
@doit.create_after("docs")
def task_check():
"""perform checks of built artifacts"""
yield dict(
name="docs:links",
doc="check for broken (internal) links",
file_dep=[*B.DOCS.rglob("*.html")],
actions=[
U.do(
"pytest-check-links",
B.DOCS,
"-p",
"no:warnings",
"--links-ext=html",
"--check-anchors",
"--check-links-ignore",
"^https?://",
)
],
)
yield dict(
name="app",
doc="use the jupyterlite CLI to check the docs app",
task_dep=[f"dev:py:{C.NAME}"],
actions=[(U.docs_app, ["check"])],
file_dep=[
B.DOCS_APP_SHA256SUMS,
# NOTE: these won't always trigger a rebuild because of the inner dodo
*P.PY_SETUP_PY[C.NAME].rglob("*.py"),
],
)
def task_watch():
"""watch sources and rebuild on change"""
yield dict(
name="js",
doc="watch .ts, .js, and .css sources and rebuild packages and apps",
uptodate=[lambda: False],
file_dep=[B.YARN_INTEGRITY],
actions=[U.do("yarn", "watch")],
)
if shutil.which("sphinx-autobuild"):
yield dict(
name="docs",
doc="watch .md sources and rebuild the documentation",
uptodate=[lambda: False],
file_dep=[*P.DOCS_MD, *P.DOCS_PY, B.APP_PACK],
actions=[
U.do(
"sphinx-autobuild",
*(C.SPHINX_ARGS or ["-a", "-j8"]),
P.DOCS,
B.DOCS,
)
],
)
def task_test():
"""test jupyterlite"""
yield U.ok(
B.OK_JEST,
name="js",
doc="run the .js, .ts unit tests with jest",
file_dep=[B.YARN_INTEGRITY, B.META_BUILDINFO],
actions=[U.do("yarn", "build:test"), U.do("yarn", "test")],
)
pytest_args = [
"pytest",
"--ff",
"--script-launch-mode=subprocess",
"-n=4",
"-vv",
f"--cov-fail-under={C.COV_THRESHOLD}",
"--cov-report=term-missing:skip-covered",
"--no-cov-on-fail",
"--durations=5",
]
for py_name, setup_py in P.PY_SETUP_PY.items():
if py_name != C.NAME:
# TODO: we'll get there
continue
py_mod = py_name.replace("-", "_")
cov_path = B.BUILD / f"htmlcov/{py_name}"
cov_index = cov_path / "index.html"
html_index = B.BUILD / f"pytest/{py_name}/index.html"
yield U.ok(
B.OK_LITE_PYTEST,
name=f"py:{py_name}",
doc=f"run pytest for {py_name}",
task_dep=[f"dev:py:{py_name}"],
file_dep=[
*setup_py.parent.rglob("*.py"),
setup_py.parent / "pyproject.toml",
],
targets=[cov_index, html_index],
actions=[
U.do(
*pytest_args,
*(C.PYTEST_ARGS or []),
"--cov",
py_mod,
"--cov-report",
f"html:{cov_path}",
f"--html={html_index}",
"--self-contained-html",
cwd=setup_py.parent,
)
],
)
class C:
NAME = "jupyterlite"
APPS = ["retro", "lab"]
NOARCH_WHL = "py3-none-any.whl"
ENC = dict(encoding="utf-8")
CI = bool(json.loads(os.environ.get("CI", "0")))
RTD = bool(json.loads(os.environ.get("READTHEDOCS", "False").lower()))
PYTEST_ARGS = json.loads(os.environ.get("PYTEST_ARGS", "[]"))
SPHINX_ARGS = json.loads(os.environ.get("SPHINX_ARGS", "[]"))
DOCS_ENV_MARKER = "### DOCS ENV ###"
NO_TYPEDOC = ["_metapackage"]
LITE_CONFIG_FILES = ["jupyter-lite.json", "jupyter-lite.ipynb"]
COV_THRESHOLD = 88
class P:
DODO = Path(__file__)
ROOT = DODO.parent
PACKAGES = ROOT / "packages"
PACKAGE_JSONS = sorted(PACKAGES.glob("*/package.json"))
UI_COMPONENTS = PACKAGES / "ui-components"
UI_COMPONENTS_ICONS = UI_COMPONENTS / "style" / "icons"
ROOT_PACKAGE_JSON = ROOT / "package.json"
YARN_LOCK = ROOT / "yarn.lock"
ENV_EXTENSIONS = Path(sys.prefix) / "share/jupyter/labextensions"
EXAMPLES = ROOT / "examples"
ALL_EXAMPLES = [p for p in EXAMPLES.rglob("*") if not p.is_dir()]
# set later
PYOLITE_PACKAGES = {}
APP = ROOT / "app"
APP_JUPYTERLITE_JSON = APP / "jupyter-lite.json"
APP_PACKAGE_JSON = APP / "package.json"
APP_SCHEMA = APP / "jupyterlite.schema.v0.json"
APP_HTMLS = [APP / "index.html", *APP.glob("*/index.html")]
WEBPACK_CONFIG = APP / "webpack.config.js"
APP_JSONS = sorted(APP.glob("*/package.json"))
APP_NPM_IGNORE = APP / ".npmignore"
LAB_FAVICON = APP / "lab/favicon.ico"
LITE_ICON = UI_COMPONENTS_ICONS / "liteIcon.svg"
LITE_WORDMARK = UI_COMPONENTS_ICONS / "liteWordmark.svg"
# "real" py packages have a `setup.py`, even if handled by `.toml` or `.cfg`
PY_SETUP_PY = {p.parent.name: p for p in (ROOT / "py").glob("*/setup.py")}
PY_SETUP_DEPS = {
C.NAME: lambda: [B.APP_PACK],
}
# docs
README = ROOT / "README.md"
CONTRIBUTING = ROOT / "CONTRIBUTING.md"
CHANGELOG = ROOT / "CHANGELOG.md"
DOCS = ROOT / "docs"
DOCS_ICON = DOCS / "_static/icon.svg"
DOCS_WORDMARK = DOCS / "_static/wordmark.svg"
EXAMPLE_OVERRIDES = EXAMPLES / "overrides.json"
TSCONFIG_TYPEDOC = ROOT / "tsconfig.typedoc.json"
TYPEDOC_JSON = ROOT / "typedoc.json"
TYPEDOC_CONF = [TSCONFIG_TYPEDOC, TYPEDOC_JSON]
DOCS_SRC_MD = sorted(
[p for p in DOCS.rglob("*.md") if "docs/api/ts" not in str(p.as_posix())]
)
DOCS_ENV = DOCS / "environment.yml"
DOCS_PY = sorted([p for p in DOCS.rglob("*.py") if "jupyter_execute" not in str(p)])
DOCS_MD = sorted([*DOCS_SRC_MD, README, CONTRIBUTING, CHANGELOG])
DOCS_IPYNB = sorted(DOCS.glob("*.ipynb"))
# demo
BINDER = ROOT / ".binder"
BINDER_ENV = BINDER / "environment.yml"
# CI
CI = ROOT / ".github"
class D:
# data
APP = json.loads(P.APP_PACKAGE_JSON.read_text(**C.ENC))
APP_VERSION = APP["version"]
# derive the PEP-compatible version
PY_VERSION = APP["version"].replace("-alpha.", "a")
PACKAGE_JSONS = {
p.parent.name: json.loads(p.read_text(**C.ENC)) for p in P.PACKAGE_JSONS
}
APP_CONFIGS = [
p
for fname in C.LITE_CONFIG_FILES
for p in [
*P.APP.glob(fname),
*P.APP.glob(f"*/{fname}"),
*P.APP.glob(f"*/*/{fname}"),
]
if p.exists()
]
P.PYOLITE_PACKAGES = {
P.PACKAGES / pkg / pyp: pyp_version
for pkg, pkg_data in D.PACKAGE_JSONS.items()
for pyp, pyp_version in pkg_data.get("pyolite", {}).get("packages", {}).items()
}
def _clean_paths(*paths_or_globs):
final_paths = []
for pg in paths_or_globs:
if pg is None:
continue
elif isinstance(pg, Path):
paths = [pg]
else:
paths = set(pg)
for path in paths:
if "node_modules" in str(path) or ".ipynb_checkpoints" in str(path):
continue
final_paths += [path]
return sorted(set(final_paths))
class L:
# linting
ALL_ESLINT = _clean_paths(
P.PACKAGES.rglob("*/src/**/*.js"),
P.PACKAGES.rglob("*/src/**/*.ts"),
)
ALL_JSON = _clean_paths(
P.PACKAGE_JSONS, P.APP_JSONS, P.ROOT_PACKAGE_JSON, P.ROOT.glob("*.json")
)
ALL_JS = _clean_paths(
(P.ROOT / "scripts").glob("*.js"), P.APP.glob("*/index.template.js")
)
ALL_HTML = [*P.APP_HTMLS]
ALL_MD = [*P.CI.rglob("*.md"), *P.DOCS_MD]
ALL_YAML = _clean_paths(
P.ROOT.glob("*.yml"), P.BINDER.glob("*.yml"), P.CI.rglob("*.yml")
)
ALL_PRETTIER = _clean_paths(
ALL_JSON, ALL_MD, ALL_YAML, ALL_ESLINT, ALL_JS, ALL_HTML
)
ALL_BLACK = _clean_paths(
*P.DOCS_PY,
P.DODO,
*sum([[*p.parent.rglob("*.py")] for p in P.PY_SETUP_PY.values()], []),
*sum([[*p.rglob("*.py")] for p in P.PYOLITE_PACKAGES.keys()], []),
)
class B:
# built
NODE_MODULES = P.ROOT / "node_modules"
YARN_INTEGRITY = NODE_MODULES / ".yarn-integrity"
META_BUILDINFO = P.PACKAGES / "_metapackage/tsconfig.tsbuildinfo"
# built things
BUILD = P.ROOT / "build"
DIST = P.ROOT / "dist"
APP_PACK = DIST / f"""{C.NAME}-app-{D.APP_VERSION}.tgz"""
DOCS_APP = BUILD / "docs-app"
DOCS_APP_SHA256SUMS = DOCS_APP / "SHA256SUMS"
DOCS_APP_ARCHIVE = DOCS_APP / f"""jupyterlite-docs-{D.APP_VERSION}.tgz"""
DOCS = Path(os.environ.get("JLITE_DOCS_OUT", P.DOCS / "_build"))
DOCS_BUILDINFO = DOCS / ".buildinfo"
DOCS_STATIC = DOCS / "_static"
DOCS_STATIC_APP = DOCS_STATIC / DOCS_APP_ARCHIVE.name
# typedoc
DOCS_RAW_TYPEDOC = BUILD / "typedoc"
DOCS_RAW_TYPEDOC_README = DOCS_RAW_TYPEDOC / "README.md"
DOCS_TS = P.DOCS / "api/ts"
DOCS_TS_MYST_INDEX = DOCS_TS / "index.md"
DOCS_TS_MODULES = [
P.ROOT / "docs/api/ts" / f"{p.parent.name}.md"
for p in P.PACKAGE_JSONS
if p.parent.name not in C.NO_TYPEDOC
]
OK = BUILD / "ok"
OK_BLACK = OK / "black"
OK_ESLINT = OK / "eslint"
OK_JEST = OK / "jest"
OK_PRETTIER = OK / "prettier"
OK_PYFLAKES = OK / "pyflakes"
OK_LITE_PYTEST = OK / "jupyterlite.pytest"
DISTRIBUTIONS = [
*P.ROOT.glob("py/*/dist/*.whl"),
*P.ROOT.glob("py/*/dist/*.tar.gz"),
]
DIST_HASH_INPUTS = sorted([*DISTRIBUTIONS, APP_PACK])
class U:
@staticmethod
def do(*args, cwd=P.ROOT, **kwargs):
"""wrap a CmdAction for consistency"""
cmd = args[0]
cmd = Path(
shutil.which(cmd)
or shutil.which(f"{cmd}.exe")
or shutil.which(f"{cmd}.cmd")
or shutil.which(f"{cmd}.bat")
).resolve()
return doit.tools.Interactive([cmd, *args[1:]], shell=False, cwd=str(Path(cwd)))
@staticmethod
def ok(ok, **task):
task.setdefault("targets", []).append(ok)
task["actions"] = [
lambda: [ok.unlink() if ok.exists() else None, None][-1],
*task["actions"],
(doit.tools.create_folder, [B.OK]),
lambda: [ok.touch(), None][-1],
]
return task
@staticmethod
def sync_env(from_env, to_env, marker):
from_chunks = from_env.read_text(**C.ENC).split(marker)
to_chunks = to_env.read_text(**C.ENC).split(marker)
to_env.write_text(
"".join([to_chunks[0], marker, from_chunks[1], marker, to_chunks[2]]),
**C.ENC,
)
@staticmethod
def typedoc_conf():
typedoc = json.loads(P.TYPEDOC_JSON.read_text(**C.ENC))
original_entry_points = sorted(typedoc["entryPoints"])
new_entry_points = sorted(
[
str(next(p.parent.glob("src/index.ts*")).relative_to(P.ROOT).as_posix())
for p in P.PACKAGE_JSONS
if p.parent.name not in C.NO_TYPEDOC
]
)
if json.dumps(original_entry_points) != json.dumps(new_entry_points):
typedoc["entryPoints"] = new_entry_points
P.TYPEDOC_JSON.write_text(
json.dumps(typedoc, indent=2, sort_keys=True), **C.ENC
)
tsconfig = json.loads(P.TSCONFIG_TYPEDOC.read_text(**C.ENC))
original_references = tsconfig["references"]
new_references = [
{"path": f"./packages/{p.parent.name}"}
for p in P.PACKAGE_JSONS
if p.parent.name not in C.NO_TYPEDOC
]
if json.dumps(original_references) != json.dumps(new_references):
tsconfig["references"] = new_references
P.TSCONFIG_TYPEDOC.write_text(
json.dumps(tsconfig, indent=2, sort_keys=True), **C.ENC
)
@staticmethod
def mystify():
"""unwrap monorepo docs into per-module docs"""
mods = defaultdict(lambda: defaultdict(list))
if B.DOCS_TS.exists():
shutil.rmtree(B.DOCS_TS)
def mod_md_name(mod):
return mod.replace("@jupyterlite/", "") + ".md"
for doc in sorted(B.DOCS_RAW_TYPEDOC.rglob("*.md")):
if doc.parent == B.DOCS_RAW_TYPEDOC:
continue
if doc.name == "README.md":
continue
doc_text = doc.read_text(**C.ENC)
doc_lines = doc_text.splitlines()
mod_chunks = doc_lines[0].split(" / ")
src = mod_chunks[1]
if src.startswith("["):
src = re.findall(r"\[(.*)/src\]", src)[0]
else:
src = src.replace("/src", "")
pkg = f"""@jupyterlite/{src.replace("/src", "")}"""
mods[pkg][doc.parent.name] += [
str(doc.relative_to(B.DOCS_RAW_TYPEDOC).as_posix())[:-3]
]
# rewrite doc and write back out
out_doc = B.DOCS_TS / doc.relative_to(B.DOCS_RAW_TYPEDOC)
if not out_doc.parent.exists():
out_doc.parent.mkdir(parents=True)
out_text = "\n".join([*doc_lines[1:], ""]).replace("README.md", "index.md")
out_text = re.sub(
r"## Table of contents(.*?)\n## ",
"\n## ",
out_text,
flags=re.M | re.S,
)
out_text = out_text.replace("/src]", "]")
out_text = re.sub("/src$", "", out_text, flags=re.M)
out_text = re.sub(
r"^Defined in: ([^\n]+)$",
"_Defined in:_ `\\1`",
out_text,
flags=re.M | re.S,
)
out_text = re.sub(
r"^((Implementation of|Overrides|Inherited from):)",
"_\\1_",
out_text,
flags=re.M | re.S,
)
out_doc.write_text(out_text, **C.ENC)
for mod, sections in mods.items():
out_doc = B.DOCS_TS / mod_md_name(mod)
mod_lines = [f"""# `{mod.replace("@jupyterlite/", "")}`\n"""]
for label, contents in sections.items():
mod_lines += [
f"## {label.title()}\n",
"```{toctree}",
":maxdepth: 1",
*contents,
"```\n",
]
out_doc.write_text("\n".join(mod_lines))
B.DOCS_TS_MYST_INDEX.write_text(
"\n".join(
[
"# `@jupyterlite`\n",
"```{toctree}",
":maxdepth: 1",
*[mod_md_name(mod) for mod in sorted(mods)],
"```",
]
),
**C.ENC,
)
@staticmethod
def validate(schema_path, instance_path=None, instance_obj=None, ref=None):
schema = json.loads(schema_path.read_text(**C.ENC))
if ref:
schema["$ref"] = ref
validator = jsonschema.Draft7Validator(schema)
if instance_path is None and instance_obj is None:
# probably just validating itself, carry on
return
if instance_obj:
instance = instance_obj
label = "some JSON"
else:
instance = json.loads(instance_path.read_text(**C.ENC))
# handle special case of loading from ipynb
if instance_path.name.endswith(".ipynb"):
instance = instance["metadata"]["jupyterlite"]
label = instance_path.relative_to(P.ROOT)
errors = [*validator.iter_errors(instance)]
for error in errors:
print(f"""{label}#/{"/".join(error.relative_path)}""")
print("\t!!!", error.message)
print("\ton:", str(error.instance)[:64])
return not errors
@staticmethod
def docs_app(lite_task="build"):
"""before sphinx ensure a custom build of JupyterLite"""
for task in ["status", lite_task]:
args = [
"jupyter",
"lite",
task,
"--debug",
"--files",
".",
"--output-dir",
B.DOCS_APP,
"--app-archive",
B.APP_PACK,
"--output-archive",
B.DOCS_APP_ARCHIVE,
]
subprocess.check_call(list(map(str, args)), cwd=str(P.EXAMPLES))
@staticmethod
def hashfile(path):
shasums = path / "SHA256SUMS"
lines = []
for p in path.glob("*"):
if p.name == "SHA256SUMS":
continue
lines += [" ".join([sha256(p.read_bytes()).hexdigest(), p.name])]
output = "\n".join(lines)
print(output)
shasums.write_text(output)
@staticmethod
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)
# environment overloads
os.environ.update(
NODE_OPTS="--max-old-space-size=4096",
PYTHONIOENCODING=C.ENC["encoding"],
PIP_DISABLE_PIP_VERSION_CHECK="1",
)
# doit configuration
DOIT_CONFIG = {
"backend": "sqlite3",
"verbosity": 2,
"par_type": "thread",
"default_tasks": ["lint", "schema", "build"],
}