Repository navigation
Expand file tree
/
Copy pathpromote
More file actions
executable file
·1641 lines (1495 loc) · 71.9 KB
/
Copy pathpromote
File metadata and controls
executable file
·1641 lines (1495 loc) · 71.9 KB
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.9"
# dependencies = ["pyyaml"]
# ///
"""tools/promote — promote lectures from ``mirror/`` into the ``lectures/`` pool.
Promotion copies a lecture from ``mirror/<series>/lectures/`` into the flat
pool at ``lectures/``, together with its ASSET CLOSURE — the files the lecture
actually references (figures, includes, ``:load:`` scripts, downloads), found
by parsing the MyST source. Nothing is copied wholesale: only referenced
assets enter the pool, so orphans are excluded by construction.
Dedup happens at promotion time: the same-named copy is hashed across every
non-excluded series in ``sync/manifest.yml``; the pool takes the copy from the
CANONICAL series (the highest-priority series, in manifest order, whose copy is
identical to the requested source) and records every identical series as a
source. "Identical" means byte-identical, or identical once each copy's asset
references are normalised to the pool paths promotion would give them.
A same-named copy that DIFFERS is treated as a different lecture under the same
name, and promotion refuses it: the error names the lecture and every series
involved. A copy that is merely STALE is not different: a series recorded as a
source at the entry's last promotion whose copy is still at a version recorded
then (its own, or the canonical copy's), while the canonical copy moved on,
stays a source (the drift-check's ``stale-copy``, which is info). Only a copy
edited to something else is refused, and a canonical-map entry that names a
stale copy, since following it would take the pool back to the lagging text.
Which copy the pool keeps never falls out of run order. The one exception is
the committed canonical map, ``sync/canonical.yml`` (human intent), which
names the winning series per slug; the losing copies are then recorded under
``divergent:`` and the entry is marked ``interim: true`` until a rename or
merge upstream retires the map entry.
Series classes (``class:`` on a manifest stanza). The default class is
canonical-eligible. A ``consumer`` series (one that republishes other series'
lectures) is never canonical and contributes no lecture to the pool; its
same-named copies, equal or not, are recorded under ``superseded:`` and are
NOT watched by the drift-check. No series is excluded by default: ``jax``
takes part like any other, so until its renames land its colliding names fail
by the rule above. ``--exclude`` drops series for one run.
Asset references are normalised into the pool ``_static`` conventions
(``_static/<slug>/`` for per-lecture assets, ``_static/_shared/`` for assets
used by two or more lectures) via an ORDERED LIST OF LITERAL STRING
REPLACEMENTS, each recorded in ``sync/ledger.yml``. Applying exactly those
replacements to the canonical mirror copy reproduces the pool copy
byte-for-byte — the invariant the drift-check relies on. No unrecorded
transformation of lecture text is permitted.
Two files record the outcome:
* ``sync/ledger.yml`` — MACHINE STATE: provenance, pins, assets, and rewrites
per pool lecture (written by this script; do not hand-edit).
* ``lectures/`` — the pool itself: ``<slug>.md`` files plus ``_static/``.
Usage
-----
tools/promote SLUG [SLUG...] # promote named lectures (source resolved
# per-lecture, highest-priority series wins)
tools/promote --toc dp # promote every lecture in dp's _toc.yml
tools/promote --refresh # re-promote entries whose canonical pin moved
Flags
-----
--toc SERIES take the slug list from mirror/SERIES/lectures/_toc.yml
(product pages — intro, status, troubleshooting,
zreferences — are skipped: they become products/<key>/
pages in M3, not pool content). For a consumer series
each lecture comes from its canonical home instead.
--refresh re-run the promotion pipeline for every ledger entry
whose canonical series pin in sync/state.yml differs
from its recorded promoted_at pin; unchanged pins are a
strict no-op. An entry whose canonical series this run
cannot use (excluded, or a consumer) is skipped while
its pin is unchanged and FAILS once the pin has moved.
CI runs it with --exclude $PROMOTE_EXCLUDE (see
.github/workflows/drift-check.yml).
--exclude SERIES [SERIES...]
series excluded from source/dedup consideration for
this run (none is excluded by default).
--dry-run print the full plan (sources, closures, rewrites,
collisions, skips) without writing anything.
-h/--help show help and exit.
This script NEVER commits anything and NEVER touches ``mirror/`` — it only
reads the mirror and writes the pool plus ``sync/ledger.yml``.
"""
from __future__ import annotations
import argparse
import hashlib
import os
import posixpath
import re
import shutil
import sys
from pathlib import Path
try:
import yaml
except ImportError: # pragma: no cover - dependency is declared in PEP-723 block
sys.stderr.write(
"error: PyYAML is required. Run this script via `uv run` (the shebang\n"
" `#!/usr/bin/env -S uv run --script` handles the dependency), or\n"
" install it with `pip install pyyaml`.\n"
)
sys.exit(1)
# --------------------------------------------------------------------------- #
# Paths (resolved relative to the repo root, derived from this script's own #
# location, so the script works from any current working directory). #
# --------------------------------------------------------------------------- #
SCRIPT_PATH = Path(__file__).resolve()
REPO_ROOT = SCRIPT_PATH.parent.parent
SYNC_DIR = REPO_ROOT / "sync"
MANIFEST_PATH = SYNC_DIR / "manifest.yml"
STATE_PATH = SYNC_DIR / "state.yml"
LEDGER_PATH = SYNC_DIR / "ledger.yml"
CANONICAL_MAP_PATH = SYNC_DIR / "canonical.yml"
MIRROR_DIR = REPO_ROOT / "mirror"
POOL_DIR = REPO_ROOT / "lectures"
# Series classes (``class:`` on a manifest stanza). The default is canonical-
# eligible; a consumer republishes other series' lectures, so it is never
# canonical, contributes no lecture, and its copies are recorded as superseded.
SOURCE_CLASS = "source"
CONSUMER_CLASS = "consumer"
SERIES_CLASSES = (SOURCE_CLASS, CONSUMER_CLASS)
# Per-series boilerplate: product pages, not pool content (M3 owns them).
PRODUCT_PAGES = ("intro", "status", "troubleshooting", "zreferences")
# The merged bibliography is owned by the separate bib-merge tool.
BIB_BASENAME = "quant-econ.bib"
# Branding assets are only referenced from _config.yml, never from lecture
# text, so they must never appear in a closure — asserted per reference.
BRANDING_PREFIXES = ("qe-logo", "lectures-favicon", "qe-menubar", "qemb-logo")
# Byte-identical header on every write, so regenerating the ledger with the
# same inputs is a zero-diff no-op.
LEDGER_HEADER = (
"# generated by tools/promote — do not hand-edit\n"
"# Provenance ledger: one entry per pool lecture (keyed lectures/<slug>.md)\n"
"# plus a top-level `assets:` map giving the content digest and source\n"
"# provenance for every pool asset. Asset/rewrite paths are relative to the\n"
"# pool root (lectures/); source `path` values are relative to\n"
"# mirror/<series>/lectures/.\n"
"# Every lecture `sources`/`divergent`/`superseded` item records the sha256\n"
"# `digest` of that series' copy AS OF PROMOTION — the drift-check's\n"
"# baseline for telling fresh upstream movement from already-recorded\n"
"# divergence. `superseded` items are consumer-series copies: provenance\n"
"# only, never watched. A `sources` item whose digest differs from the\n"
"# canonical one is either a STALE copy (still at an earlier promoted\n"
"# version while the canonical copy moved on) or a copy equal to the\n"
"# canonical one once asset paths are normalised.\n"
"# `rewrites` is an ordered list of literal replacements: applying each\n"
"# {from -> to} in turn to the canonical mirror copy reproduces the pool\n"
"# copy byte-for-byte. Each pair is ONE non-rescanning pass with Python\n"
"# str.replace semantics: every occurrence present when its turn comes is\n"
"# replaced, but a pair's own output is never re-matched — a `from`\n"
"# embedded in its own `to` (e.g. an _admonition include) is applied\n"
"# exactly once, never to a fixpoint. The drift-check applies exactly\n"
"# this transform before comparing.\n"
)
class PromoteError(Exception):
"""A recoverable, per-lecture error. Reported, then the run continues."""
# --------------------------------------------------------------------------- #
# Reference extraction (the asset-closure parser) #
# --------------------------------------------------------------------------- #
# Directive argument on the opening fence line, backtick- or colon-fenced:
# ```{figure} /_static/... :::{image} _static/...
DIRECTIVE_RE = re.compile(
r"^(?:`{3,}|:{3,})\{(figure|image|include|literalinclude|bibliography)\}\s+(\S+)\s*$"
)
# code-cell blocks delimit where code-string/:load: scanning applies.
CODE_CELL_OPEN_RE = re.compile(r"^(`{3,})\{code-cell\}")
FENCE_ONLY_RE = re.compile(r"^`{3,}\s*$")
# :load: is a code-cell directive option loading a script by path. It has a
# second spelling: a YAML options block (--- fenced, first thing in the cell)
# with a plain `load:` key — the estate uses BOTH forms, so both are parsed.
LOAD_RE = re.compile(r"^\s*:load:\s+(\S+)\s*$")
YAML_LOAD_RE = re.compile(r"^\s*load:\s+[\"']?([^\"'\s]+)[\"']?\s*$")
# Markdown images / links (images guarded out of the link pattern).
MD_IMAGE_RE = re.compile(r"!\[[^\]]*\]\(\s*<?([^)<>\s]+)>?\s*(?:[\"'][^)\"']*[\"']\s*)?\)")
MD_LINK_RE = re.compile(r"(?<!!)\[[^\]]*\]\(\s*<?([^)<>\s]+)>?\s*(?:[\"'][^)\"']*[\"']\s*)?\)")
# {download} role: {download}`path` or {download}`text <path>`.
DOWNLOAD_RE = re.compile(r"\{download\}`(?:[^`]*?<([^`>]+)>|([^`<>]+))`")
# Raw HTML anchors — the estate uses UNQUOTED attributes (<a href=_static/...
# download>), which pure MyST tooling misses; all three quoting forms handled.
HREF_RE = re.compile(r"<a\s[^>]*?href=(?:\"([^\"]+)\"|'([^']+)'|([^\"'\s>]+))", re.IGNORECASE)
# Hardcoded published asset URLs (the Colab-compatibility pattern).
QE_URL_RE = re.compile(
r"https://[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.quantecon\.org(/_static/[^\s\"'<>()\[\]`]+)"
)
# Quoted string literals inside code cells that look like repo asset paths.
CODE_STR_RE = re.compile(
r"[\"']([^\"'\n]*(?:_static/|datasets/|lecture_specific)[^\"'\n]*)[\"']"
)
SCHEME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:")
def extract_refs(text):
"""Scan MyST source for candidate file references.
Returns a list of ``(kind, written)`` pairs where ``written`` is the path
(or URL) exactly as it appears in the source — the literal a rewrite's
``from`` field must match. Prose and code-cell contexts are scanned with
different patterns so quoted strings in code are not confused with
directives and vice versa.
"""
refs = []
in_code = 0 # opening-fence length while inside a {code-cell} block
cell_first_line = False # the line straight after a {code-cell} fence
in_options = False # inside the cell's ---/--- YAML options block
for line in text.split("\n"):
if in_code:
stripped = line.strip()
if FENCE_ONLY_RE.match(stripped) and len(stripped) >= in_code:
in_code = 0
in_options = False
continue
if cell_first_line:
cell_first_line = False
if stripped == "---":
in_options = True
continue
if in_options:
if stripped == "---":
in_options = False
continue
m = YAML_LOAD_RE.match(line)
if m:
refs.append(("load", m.group(1)))
continue
m = LOAD_RE.match(line)
if m:
refs.append(("load", m.group(1)))
for m in QE_URL_RE.finditer(line):
refs.append(("url", m.group(0)))
for m in CODE_STR_RE.finditer(line):
if "://" not in m.group(1):
refs.append(("code-str", m.group(1)))
continue
m = CODE_CELL_OPEN_RE.match(line)
if m:
in_code = len(m.group(1))
cell_first_line = True
continue
m = DIRECTIVE_RE.match(line.strip())
if m:
refs.append((m.group(1), m.group(2)))
for m in MD_IMAGE_RE.finditer(line):
refs.append(("image-md", m.group(1)))
for m in MD_LINK_RE.finditer(line):
refs.append(("link", m.group(1)))
for m in DOWNLOAD_RE.finditer(line):
refs.append(("download", m.group(1) or m.group(2)))
for m in HREF_RE.finditer(line):
refs.append(("href", m.group(1) or m.group(2) or m.group(3)))
for m in QE_URL_RE.finditer(line):
refs.append(("url", m.group(0)))
return refs
# --------------------------------------------------------------------------- #
# Small helpers #
# --------------------------------------------------------------------------- #
def sha256_file(path: Path) -> str:
"""Content digest of a file (streamed, so large binaries are fine)."""
h = hashlib.sha256()
with open(path, "rb") as fh:
for chunk in iter(lambda: fh.read(1 << 16), b""):
h.update(chunk)
return h.hexdigest()
def tmp_sibling(path: Path) -> Path:
"""A per-process temp name beside ``path`` — the pid suffix keeps two
concurrent runs from clobbering each other's in-flight temp file."""
return path.with_name(f"{path.name}.promote-tmp.{os.getpid()}")
def atomic_write(path: Path, data: bytes):
"""Write via a sibling temp file (fsynced) + ``os.replace`` so a crash
mid-write can never leave a truncated file — pool files and the ledger
must always be either the old bytes or the new bytes, never a partial
write. The temp file is removed even when the write itself fails."""
path.parent.mkdir(parents=True, exist_ok=True)
tmp = tmp_sibling(path)
try:
with open(tmp, "wb") as fh:
fh.write(data)
fh.flush()
os.fsync(fh.fileno())
os.replace(tmp, path)
finally:
tmp.unlink(missing_ok=True)
def read_text_exact(path: Path) -> str:
"""Read UTF-8 text WITHOUT universal-newline translation.
``Path.read_text`` silently normalises ``\\r\\n``/``\\r`` to ``\\n``, which
would be an UNRECORDED transformation of lecture text — a CRLF upstream
file would break the byte-for-byte rewrite-replay invariant. Reading raw
bytes keeps the mirror's exact line endings in the pool copy."""
return path.read_bytes().decode("utf-8")
def mirror_lectures(series: str) -> Path:
"""The lectures root of a series' mirror tree."""
return MIRROR_DIR / series / "lectures"
def to_src_rel(written: str) -> str:
"""Normalise a path-as-written to a mirror-lectures-relative POSIX path.
Root-relative forms (``/_static/...``) and file-relative forms both occur
upstream; the pool is flat, so a lecture's directory IS the lectures root
and both resolve against the same base.
"""
p = written[1:] if written.startswith("/") else written
p = posixpath.normpath(p)
if p.startswith("../") or p in ("..", ".") or p.startswith("/"):
raise PromoteError(f"reference escapes the lectures tree: {written!r}")
return p
def slug_of(name: str) -> str:
"""Accept both ``short_path`` and ``short_path.md`` as a slug."""
return name[:-3] if name.endswith(".md") else name
# --------------------------------------------------------------------------- #
# Manifest / state / ledger I/O #
# --------------------------------------------------------------------------- #
class UniqueKeyLoader(yaml.SafeLoader):
"""A SafeLoader that refuses a mapping with a repeated key, which plain
YAML loading silently resolves to the last value."""
def _construct_unique_mapping(loader, node, deep=False):
loader.flatten_mapping(node) # resolve merge keys first, as safe_load does
seen = set()
for key_node, _ in node.value:
key = loader.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError:
raise yaml.constructor.ConstructorError(
None, None, "found an unhashable key", key_node.start_mark
)
if key in seen:
raise yaml.constructor.ConstructorError(
None, None, f"repeated key {key!r}", key_node.start_mark
)
seen.add(key)
return loader.construct_mapping(node, deep=deep)
UniqueKeyLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _construct_unique_mapping
)
def load_yaml(path: Path, loader=yaml.SafeLoader):
"""Load a YAML mapping, tolerating an empty/None document (-> {})."""
if not path.exists():
return None
try:
data = yaml.load(path.read_text(encoding="utf-8"), Loader=loader) or {}
except yaml.YAMLError as exc:
raise SystemExit(f"error: could not parse {path}:\n{exc}")
if not isinstance(data, dict):
raise SystemExit(f"error: expected a mapping at the top of {path}.")
return data
def load_manifest():
"""Return ``(order, consumers)``: the series keys in manifest order (=
dedup priority order) and the set of series declared ``class: consumer``.
A stanza without ``class:`` is canonical-eligible. An unknown class is an
error, never a silent default — it decides what may become canonical.
"""
data = load_yaml(MANIFEST_PATH)
if data is None:
raise SystemExit(
f"error: manifest not found at {MANIFEST_PATH}.\n"
" Restore sync/manifest.yml before running tools/promote."
)
series_block = data.get("series") or {}
if not isinstance(series_block, dict) or not series_block:
raise SystemExit(f"error: {MANIFEST_PATH} has no `series:` mapping.")
consumers = set()
for key, cfg in series_block.items():
cls = (cfg or {}).get("class", SOURCE_CLASS)
if cls not in SERIES_CLASSES:
raise SystemExit(
f"error: series '{key}' in {MANIFEST_PATH} has unknown "
f"class {cls!r} (valid: {', '.join(SERIES_CLASSES)})."
)
if cls == CONSUMER_CLASS:
consumers.add(key)
return list(series_block.keys()), consumers
def load_canonical_map(order, consumers):
"""Return the committed canonical map: ``slug -> series``.
``sync/canonical.yml`` is HUMAN INTENT — the one way a same-name collision
is settled. It is validated up front: a repeated key, two keys naming one
lecture (``slug`` and ``slug.md``), and an unknown or consumer series are
errors before anything is planned. A slug that no series holds cannot be
checked here (it needs the mirror); ``map_slugs_held_nowhere`` reports
it. A missing or empty file is an empty map.
"""
data = load_yaml(CANONICAL_MAP_PATH, loader=UniqueKeyLoader)
if not data:
return {}
bad, named = [], {}
for slug, series in data.items():
if not isinstance(slug, str) or not isinstance(series, str):
bad.append(f"{slug!r}: {series!r} (expected `<slug>: <series>`)")
continue
if slug_of(slug) in named:
bad.append(f"{slug}: names the same lecture as '{named[slug_of(slug)]}'")
named[slug_of(slug)] = slug
if series not in order:
bad.append(f"{slug}: unknown series '{series}'")
elif series in consumers:
bad.append(f"{slug}: '{series}' is a consumer series and is never canonical")
if bad:
raise SystemExit(
f"error: invalid entries in {CANONICAL_MAP_PATH}:\n "
+ "\n ".join(bad)
)
return {slug_of(slug): series for slug, series in data.items()}
def map_slugs_held_nowhere(canonical_map, order, consumers):
"""Map slugs whose lecture no reconstructed canonical-eligible mirror
holds: a typo, or a lecture renamed or retired upstream. Names are matched
exactly, so a wrong-case key is caught on a case-insensitive file system
too. Reported, not refused — one dead line must not stop every
promotion."""
held = set()
for s in order:
root = mirror_lectures(s)
if s in consumers or not root.is_dir():
continue
held.update(p.relative_to(root).as_posix()[:-3] for p in root.rglob("*.md"))
return sorted(slug for slug in canonical_map if slug not in held)
def load_state():
"""Return the pinned-SHA state mapping written by tools/sync.
SHAs are stripped here, once, so every consumer (pin validation, refresh
comparison, promoted_at recording) sees the same normalised value — a
padded sha in state.yml must not make --refresh spin forever. A mangled
non-mapping entry (bad hand-edit, truncated write) is normalised to an
EMPTY mapping so it reads as a missing pin — a clean, actionable error
downstream instead of an AttributeError.
"""
data = load_yaml(STATE_PATH)
if not data:
raise SystemExit(
f"error: no pinned SHAs found — {STATE_PATH} is missing or empty.\n"
" Run `tools/sync --update` first; promotion records pins."
)
normalised = {}
for key, meta in data.items():
if not isinstance(meta, dict):
meta = {}
elif isinstance(meta.get("sha"), str):
meta["sha"] = meta["sha"].strip()
normalised[key] = meta
return normalised
def load_ledger():
"""Return ``(lecture_entries, assets_map)`` from sync/ledger.yml.
The ledger is bot-written, so malformed entries mean corruption or a bad
hand-edit — error out rather than silently dropping data on the next write.
"""
data = load_yaml(LEDGER_PATH) or {}
assets = data.pop("assets", {}) or {}
bad = [str(k) for k, v in data.items() if not isinstance(v, dict)]
if not isinstance(assets, dict):
bad.append("assets")
if bad:
raise SystemExit(
f"error: malformed entries in {LEDGER_PATH}: {', '.join(sorted(bad))}\n"
" The ledger is bot-written — restore it from git before promoting."
)
return data, assets
def ledger_asset_digest(pool_path: str, meta: dict):
"""The recorded content digest of a ledger asset (falling back to hashing
the pool file itself for ledgers written before digests were recorded).
The digest is part of an asset's IDENTITY: two upstream files that merely
share a relative path across series are different assets, and conflating
them would deadlock legitimate promotions on phantom collisions."""
digest = meta.get("digest")
if not digest:
f = POOL_DIR / pool_path
if f.is_file():
digest = sha256_file(f)
return digest
def dump_ledger(lectures: dict, assets: dict) -> str:
"""Serialise the ledger deterministically (sorted keys, fixed field order)."""
doc = {}
for key in sorted(lectures):
doc[key] = lectures[key]
if assets:
doc["assets"] = {k: assets[k] for k in sorted(assets)}
body = yaml.safe_dump(
doc, sort_keys=False, default_flow_style=False, allow_unicode=True, width=4096
)
return LEDGER_HEADER + body
def ordered_entry(canonical, interim, sources, divergent, superseded, promoted_at, assets, rewrites):
"""A ledger lecture entry with a stable key order.
``interim`` is present only while ``divergent`` is non-empty — it marks
a canonical choice the canonical map made over differing copies, pending
the rename or merge upstream that retires the map entry. ``superseded``
is present only when a consumer series holds a copy.
"""
entry = {"canonical": canonical}
if interim:
entry["interim"] = True
entry["sources"] = sources
if divergent:
entry["divergent"] = divergent
if superseded:
entry["superseded"] = superseded
entry["promoted_at"] = promoted_at
entry["assets"] = assets
entry["rewrites"] = rewrites
return entry
# --------------------------------------------------------------------------- #
# The per-lecture plan #
# --------------------------------------------------------------------------- #
class Plan:
"""Everything decided about one lecture before anything is written."""
def __init__(self, slug, requested_series):
self.slug = slug
self.requested = requested_series # None => resolve by priority
self.canonical = None
self.sources = [] # series with identical or stale copies (manifest order)
self.divergent = [] # differing copies the canonical map overruled
self.superseded = [] # consumer series holding a copy (manifest order)
self.stale = [] # sources unchanged since promotion while the canonical moved
self.digests = {} # series -> sha256 of its copy at promotion time
self.refs = [] # (kind, literal, src_rel, digest) — deduped, source order
self.warnings = []
self.error = None
self.rewrites = [] # ordered [(from, to)]
self.pool_text = None
self.assets = [] # pool-relative destinations (sorted)
def normalised_text(series: str, slug: str) -> str:
"""A copy's text with each local asset reference replaced by the pool
path promotion would give it — the form two copies are COMPARED in.
Series spell the same reference differently (``/_static/...`` against
``_static/...``, a ``lecture_specific/<slug>/`` dir against a new-style
``_static/<slug>/`` one); promotion rewrites both to one pool path, so
copies that differ only there are one lecture. Comparison only: the pool
text is always built by ``build_rewrites`` from the canonical copy. A
reference that resolves to no file stays as written.
"""
root = mirror_lectures(series)
text = read_text_exact(root / f"{slug}.md")
pairs = {}
for kind, written in extract_refs(text):
if kind == "url":
literal, src_rel = written, written[written.find("/_static/") + 1 :]
else:
literal = written.split("#", 1)[0]
if not literal or SCHEME_RE.match(literal):
continue
try:
src_rel = to_src_rel(literal)
except PromoteError:
continue
if kind == "link" and src_rel.endswith(".md"):
continue
if (root / src_rel).is_file():
pairs[literal] = plan_destination(src_rel, [slug])
for frm, to in sorted(pairs.items(), key=lambda kv: (-len(kv[0]), kv[0])):
text = text.replace(frm, to)
return text
def resolve_sources(plan: Plan, considered, consumers, canonical_map, recorded=None, order=()):
"""Fill in canonical/sources/divergent/superseded by hashing across series.
A same-named file is presumed to be ONE lecture. The anchor copy (the
canonical map's choice, else the requested copy, else the highest-priority
one) defines the identity set: every canonical-eligible copy that is
byte-identical to it, or identical after ``normalised_text``. The canonical
series is the highest-priority member of that set (manifest order).
A copy outside the set is a different lecture under the same name, and an
error naming the lecture and every series involved: which copy survives
must never fall out of run order. Only the committed canonical map settles
it — the map's copy anchors the set, and the differing copies land in
``divergent`` (the entry is ``interim``). The map picks a COPY, never a
priority: among identical copies the highest-priority series is still
canonical, so deleting an entry that settles nothing changes nothing.
Consumer copies, equal or not, are recorded under ``superseded`` and
never anchor anything.
``recorded`` is the slug's ledger entry, if any. A copy is STALE, not a
different lecture, when it is recorded under ``sources``, is not the
recorded canonical copy's lecture as that copy now stands, and is still
at a version the pool already took: its own recorded digest (it has not
moved) or the canonical copy's (it caught up, then the canonical copy
moved on).
Staleness is judged against the recorded canonical copy whatever anchors
the run: a stale requested copy (``--toc`` of a series that lags) gives
way to the canonical copy, and a canonical-map entry naming a stale copy
is refused, since it no longer settles a collision and following it
would keep the lagging text. A stale copy stays a source
(``plan.stale``) — the drift-check's ``stale-copy`` — and only a copy
edited to something else is a collision.
"""
rel = f"{plan.slug}.md"
digests = {}
for series in considered:
f = mirror_lectures(series) / rel
if f.is_file():
digests[series] = sha256_file(f)
eligible = [s for s in considered if s in digests and s not in consumers]
if not eligible:
held = [s for s in considered if s in digests]
if held:
raise PromoteError(
f"{rel} is held only by consumer series ({', '.join(held)}) — "
"a consumer contributes no lecture to the pool"
)
if recorded:
raise PromoteError(
f"{rel} is in the pool, but no series this run considers holds "
"it any more (removed or renamed upstream?) — decide whether "
"the lecture was retired, renamed or moved; the drift-check "
"reports it as canonical-removed"
)
raise PromoteError(
f"{rel} not found in any non-excluded series "
f"(searched: {', '.join(considered)})"
)
requested = plan.requested if plan.requested not in consumers else None
if requested is not None and requested not in digests:
if recorded and recorded.get("canonical") == requested:
raise PromoteError(
f"the canonical copy of {rel} is gone from {requested} upstream "
"(removed or renamed) — decide whether the lecture was "
"retired, renamed or moved; the drift-check reports it as "
"canonical-removed"
)
raise PromoteError(f"{rel} not found in mirror/{requested}/lectures/")
mapped = canonical_map.get(plan.slug)
if mapped is not None and mapped not in considered:
plan.warnings.append(
f"{CANONICAL_MAP_PATH.name} names '{mapped}' for {plan.slug}, but "
f"'{mapped}' is excluded from this run — the map entry is ignored"
)
mapped = None
if mapped is not None and mapped not in digests:
raise PromoteError(
f"{CANONICAL_MAP_PATH.name} names '{mapped}' for {plan.slug}, but "
f"{rel} is not in mirror/{mapped}/lectures/"
)
base = mapped or requested or eligible[0]
normalised = {} # series -> normalised text, computed only when needed
def split(anchor):
"""``(same, differ)``: the eligible copies that are, and are not, the
anchor's lecture (byte-identical, or identical once normalised)."""
same, differ = [], []
for series in eligible:
if digests[series] == digests[anchor]:
same.append(series)
continue
for s in (anchor, series):
if s not in normalised:
normalised[s] = normalised_text(s, plan.slug)
(same if normalised[series] == normalised[anchor] else differ).append(series)
return same, differ
# Staleness is judged against the recorded canonical copy, whatever
# anchors this run, so a refresh and --toc of a lagging series agree.
recorded_canonical, stale_copies = None, []
if recorded and recorded.get("canonical") in eligible:
recorded_canonical = recorded.get("canonical")
was = {s.get("series"): s.get("digest") for s in recorded.get("sources") or []}
_, differ_c = split(recorded_canonical)
stale_copies = [
s for s in differ_c
if s in was and digests[s] in (was[s], was.get(recorded_canonical))
]
anchor = base
if base in stale_copies:
if base == mapped:
if digests[recorded_canonical] != was.get(recorded_canonical):
why = (
f"'{mapped}' holds an earlier promoted version, and the "
f"recorded canonical copy in '{recorded_canonical}' has "
"moved on"
)
else:
why = (
f"'{mapped}' no longer matches the recorded canonical copy "
f"in '{recorded_canonical}' (it is an earlier version, or "
"a file it references changed upstream)"
)
raise PromoteError(
f"{CANONICAL_MAP_PATH.name} names '{mapped}' for {plan.slug}, "
f"but {why}: point the entry at '{recorded_canonical}', remove "
f"it if no other copy differs, or bring {mapped}'s copy up to "
"date upstream"
)
anchor = recorded_canonical
same, differ = split(anchor)
stale = []
if recorded_canonical is not None and recorded_canonical in same:
stale = [s for s in differ if s in stale_copies]
differ = [s for s in differ if s not in stale]
if differ and mapped is None:
detail = "same: " + ", ".join(f"{s}:{digests[s][:12]}" for s in same)
detail += "; different: " + ", ".join(f"{s}:{digests[s][:12]}" for s in differ)
if stale:
detail += "; stale (at an earlier promoted version): " + ", ".join(stale)
raise PromoteError(
f"{rel} differs across series ({detail}) — a same-named file must "
"be one lecture: reconcile or rename it upstream, or name the "
f"canonical series in sync/{CANONICAL_MAP_PATH.name}"
)
if mapped is not None and not differ:
# ``order`` is every manifest series. A canonical-eligible series this
# run excludes may hold the copy the entry settles, so its copy is
# compared too; without its mirror the run cannot tell.
differing, unchecked = [], []
for s in order:
if s in considered or s in consumers:
continue
if not mirror_lectures(s).is_dir():
unchecked.append(s)
continue
f = mirror_lectures(s) / rel
if not f.is_file() or sha256_file(f) == digests[anchor]:
continue
for x in (anchor, s):
if x not in normalised:
normalised[x] = normalised_text(x, plan.slug)
if normalised[s] != normalised[anchor]:
differing.append(s)
head = (
f"{CANONICAL_MAP_PATH.name} names '{mapped}' for {plan.slug}; "
"every copy this run considers is the same lecture"
)
if differing:
plan.warnings.append(
f"{head}, but {', '.join(differing)} (excluded from this run) "
+ ("holds a different copy" if len(differing) == 1 else "hold different copies")
+ ", so keep the entry while it settles that collision"
)
elif unchecked:
plan.warnings.append(
f"{head}, but {', '.join(unchecked)} (excluded, with no mirror "
f"on disk) could not be checked: run `tools/sync "
f"{' '.join(unchecked)}` and a run that considers "
+ ("it" if len(unchecked) == 1 else "them")
+ " before removing the entry"
)
else:
note = (
f"{CANONICAL_MAP_PATH.name} names '{mapped}' for {plan.slug}, but "
"every copy is the same lecture — the map entry settles nothing "
"and can be removed"
)
if mapped != same[0]:
note += (
f"; remove it now, since once {same[0]}'s copy moves on, "
f"an entry naming {mapped}'s lagging copy is refused"
)
plan.warnings.append(note)
plan.sources = [s for s in eligible if s in same or s in stale]
plan.stale = stale
plan.divergent = differ
plan.superseded = [s for s in considered if s in digests and s in consumers]
plan.canonical = same[0]
plan.digests = digests
return read_text_exact(mirror_lectures(plan.canonical) / rel)
def check_include_is_leaf(root: Path, src_rel: str):
"""Guard the depth-1 closure assumption for ``{include}`` targets.
Included files are copied into the pool VERBATIM, so any local file
reference inside one would be neither closed over nor rewritten — a
silently broken pool lecture. Transitive closures are deliberately not
supported; refuse instead, so the include must be flattened upstream.
(Today the only include in the estate, ``_admonition/gpu.md``, is
reference-free — this asserts that stays true.)
"""
text = read_text_exact(root / src_rel)
base_dir = posixpath.dirname(src_rel)
for kind, written in extract_refs(text):
candidate = written.split("#", 1)[0]
if not candidate or SCHEME_RE.match(candidate):
continue
try:
rel = to_src_rel(candidate)
except PromoteError:
continue
if kind == "link" and rel.endswith(".md"):
continue # cross-lecture links resolve in the flat pool as-is
rel_to_include = posixpath.normpath(posixpath.join(base_dir, rel))
if (root / rel).is_file() or (root / rel_to_include).is_file():
raise PromoteError(
f"include target {src_rel} contains its own file reference "
f"{written!r} — transitive asset closures are not supported; "
"flatten the include upstream first"
)
def collect_refs(plan: Plan, text: str):
"""Resolve extracted references into the lecture's asset closure.
Every kept item is ``(kind, literal, src_rel, digest)`` where ``literal``
is the exact source substring a rewrite must replace, ``src_rel`` is the
asset's path relative to the canonical mirror's lectures root, and
``digest`` is the referenced file's content hash — part of the asset's
identity, so same-named but byte-different files across series are never
conflated.
"""
root = mirror_lectures(plan.canonical)
seen = set()
for kind, written in extract_refs(text):
if kind == "url":
# Rule 7: hardcoded published URL — localise only when the target
# exists in the source series' mirror _static, else warn.
idx = written.find("/_static/")
src_rel = written[idx + 1 :]
if (root / src_rel).is_file():
key = (written, src_rel)
if key not in seen:
seen.add(key)
plan.refs.append((kind, written, src_rel, sha256_file(root / src_rel)))
else:
plan.warnings.append(
f"hardcoded URL target not in mirror/{plan.canonical} "
f"_static — left untouched: {written}"
)
continue
literal = written.split("#", 1)[0]
if not literal or SCHEME_RE.match(literal) or literal.startswith("#"):
continue
try:
src_rel = to_src_rel(literal)
except PromoteError:
if kind in ("link", "code-str"):
continue # heuristic kinds: not a repo file reference
raise
base = posixpath.basename(src_rel)
if base == BIB_BASENAME:
plan.warnings.append(
f"skipping {literal} — quant-econ.bib is owned by the "
"bib-merge tool, not promotion"
)
continue
if base.startswith(BRANDING_PREFIXES):
raise PromoteError(
f"branding asset {literal!r} referenced from lecture text — "
"branding is a product/_base concern and must never enter a "
"closure (assertion per _static conventions rule 6)"
)
exists = (root / src_rel).is_file()
if kind == "link":
# Local-file markdown links only: not .md (cross-lecture links
# stay as-is) and only when the target is a real repo file.
if src_rel.endswith(".md") or not exists:
continue
elif kind == "code-str":
if not exists:
plan.warnings.append(
f"code string looks like an asset path but resolves to no "
f"file in mirror/{plan.canonical} — ignored: {literal!r}"
)
continue
elif not exists:
raise PromoteError(
f"{kind} reference {literal!r} does not resolve to a file "
f"under mirror/{plan.canonical}/lectures/ — closure incomplete"
)
if kind == "include":
check_include_is_leaf(root, src_rel)
key = (literal, src_rel)
if key not in seen:
seen.add(key)
plan.refs.append((kind, literal, src_rel, sha256_file(root / src_rel)))
# --------------------------------------------------------------------------- #
# Destination mapping (the pool _static conventions) #
# --------------------------------------------------------------------------- #
def plan_destination(src_rel: str, consumers) -> str:
"""Map a mirror asset path to its pool destination.
Conventions (PLAN.md, M2): ``_static/<slug>/`` for single-consumer assets
owned by that lecture; ``_static/_shared/`` for assets referenced by two
or more pool lectures, for ``lecture_specific/<dir>/`` family dirs whose
name is not the consuming slug (cross-lecture by construction), and for
``_admonition/`` includes (multi-lecture by construction).
"""
if src_rel.startswith("_admonition/"):
return "_static/_shared/" + src_rel
shared = len(consumers) >= 2
if src_rel.startswith("_static/lecture_specific/"):
rest = src_rel[len("_static/lecture_specific/") :]
dirname, _, tail = rest.partition("/")
if tail:
if shared or any(slug != dirname for slug in consumers):
return f"_static/_shared/{dirname}/{tail}"
return f"_static/{dirname}/{tail}"
if src_rel.startswith("_static/"):
rest = src_rel[len("_static/") :]
if shared:
return f"_static/_shared/{rest}"
(only,) = tuple(consumers)
if rest.startswith(only + "/"):
return src_rel # already in new-style _static/<slug>/ form
return f"_static/{only}/{rest}"
# Outside _static entirely (e.g. datasets/): keep the relative shape.
if shared:
return f"_static/_shared/{src_rel}"
(only,) = tuple(consumers)
return f"_static/{only}/{src_rel}"
def check_admonition_identity(src_rel: str, eligible):
"""Rule 4 guard: an ``_admonition/`` include merges into ``_shared`` only
if every non-excluded, canonical-eligible series that carries the file
agrees byte-for-byte (a consumer's copy is never a source)."""
digests = {}
for series in eligible:
f = mirror_lectures(series) / src_rel
if f.is_file():
digests[series] = sha256_file(f)
if len(set(digests.values())) > 1:
detail = ", ".join(f"{s}:{d[:12]}" for s, d in digests.items())
raise PromoteError(
f"{src_rel} differs across series ({detail}) — cannot merge into "
"_static/_shared/; reconcile upstream first"
)
def build_rewrites(plan: Plan, text: str, dest_map):
"""Derive the ordered literal replacements and the transformed pool text.
Replacements are applied longest-``from`` first so a root-relative form
(``/_static/...``) can never be corrupted by its embedded file-relative
substring, then each is verified to (a) still occur when its turn comes
and (b) not be re-matchable inside an earlier replacement's output. The
recorded order IS the application order, and each pair is a SINGLE
non-rescanning ``str.replace`` pass — a ``from`` embedded in its own
``to`` (the common ``_admonition/gpu.md`` include rewrite) is therefore
legal and correct, but any replayer MUST NOT apply the list iteratively
to a fixpoint or via rescanning substitution (see LEDGER_HEADER).
"""
pairs = {}