---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"Make Appendix III cadence/update metadata agree with its evidence rows.\"\"\" from __future__ import annotations import argparse import json import os import shutil import tempfile from pathlib."
resource: "https://thesis.copecheck.com"
tags: ["copecheck", "thesis", "source-file", "py"]
timestamp: "2026-08-24T14:09:41Z"
site: "thesis"
source_path: "copecheck-thesis/migrate_register_metadata.py"
---

# !/usr/bin/env python3

Source path: `copecheck-thesis/migrate_register_metadata.py`  
Index snapshot generated: `2026-11-24T13:27:52Z`  
File modified: `2026-08-24T14:09:41Z`

```py
#!/usr/bin/env python3
"""Make Appendix III cadence/update metadata agree with its evidence rows."""
from __future__ import annotations

import argparse
import json
import os
import shutil
import tempfile
from pathlib import Path


CADENCE = "Curated; updated in reviewed batches, not a continuous feed."
SUMMARY = (
    "Appendix III is a curated evidence register. The baseline document remains "
    "fixed at v1.1.2; reviewed additions can be appended without rebuilding it."
)


def item_date(item: dict) -> str:
    return str(
        item.get("observed_at")
        or item.get("source_date")
        or item.get("published")
        or item.get("updated_at")
        or ""
    )


def migrate(path: Path, apply: bool = False, backup: Path | None = None) -> dict:
    original_text = path.read_text(encoding="utf-8")
    data = json.loads(original_text)
    items = [item for item in data.get("items", []) if isinstance(item, dict)]
    newest_source_date = max((item_date(item) for item in items), default="")
    previous = {
        "status": data.get("status"),
        "updated_at": data.get("updated_at"),
        "summary": data.get("summary"),
        "cadence": data.get("cadence"),
    }
    if newest_source_date and newest_source_date[:10] > str(data.get("updated_at") or "")[:10]:
        data["updated_at"] = newest_source_date[:10] + "T00:00:00Z"
    data["status"] = "curated_register_active"
    data["summary"] = SUMMARY
    data["cadence"] = CADENCE
    after = {key: data.get(key) for key in previous}
    changed = previous != after
    report = {
        "mode": "apply" if apply else "dry-run",
        "items": len(items),
        "newest_source_date": newest_source_date,
        "previous": previous,
        "after": after,
        "changed": changed,
        "backup": None,
    }
    if not apply or not changed:
        return report
    if backup is None:
        backup = path.with_name(path.name + ".pre-cadence-fix.bak")
    if backup.exists():
        raise FileExistsError(backup)
    shutil.copy2(path, backup)
    old_stat = path.stat()
    with tempfile.NamedTemporaryFile("w", encoding="utf-8", dir=path.parent, delete=False) as handle:
        json.dump(data, handle, ensure_ascii=False, indent=2)
        handle.write("\n")
        temp_path = Path(handle.name)
    os.chmod(temp_path, old_stat.st_mode & 0o777)
    try:
        os.chown(temp_path, old_stat.st_uid, old_stat.st_gid)
    except PermissionError:
        pass
    os.replace(temp_path, path)
    report["backup"] = str(backup)
    return report


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("register", type=Path)
    parser.add_argument("--apply", action="store_true")
    parser.add_argument("--backup", type=Path)
    arguments = parser.parse_args()
    print(json.dumps(migrate(arguments.register, arguments.apply, arguments.backup), indent=2))
```
