|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.12" |
| 3 | +# dependencies = [ |
| 4 | +# "pyyaml>=6.0.2", |
| 5 | +# ] |
| 6 | +# /// |
| 7 | + |
| 8 | +""" |
| 9 | +This script gathers all of the files needed to build the publication. |
| 10 | +These files do not all exist in any single commit in the repository, |
| 11 | +because the publication needs to include versions of the `index.ipynb` notebook |
| 12 | +for each git tag. |
| 13 | +
|
| 14 | +This script executes the following steps: |
| 15 | +- Copies the `index.ipynb` notebook from each tag to a `_freeze/index_v{tag}.ipynb` file. |
| 16 | +- Copies the `_freeze/index` directory from each tag to a `_freeze/index_v{tag}` directory. |
| 17 | +- Updates the `_quarto.yml` file to include a `version-control` item for each tag. |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import shutil |
| 22 | +import subprocess |
| 23 | +from contextlib import contextmanager |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +import yaml |
| 27 | + |
| 28 | + |
| 29 | +def get_versioned_notebook_path(tag: str) -> Path: |
| 30 | + """Get the path to the versioned `index.ipynb` notebook for a given tag.""" |
| 31 | + return Path(f"index_{tag}.ipynb") |
| 32 | + |
| 33 | + |
| 34 | +def get_versioned_freeze_directory_path(tag: str) -> Path: |
| 35 | + """Get the path to the versioned `_freeze/index` directory for a given tag.""" |
| 36 | + return Path(f"_freeze/index_{tag}") |
| 37 | + |
| 38 | + |
| 39 | +@contextmanager |
| 40 | +def git_checkout(ref: str): |
| 41 | + original_ref = ( |
| 42 | + subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]) |
| 43 | + .decode("utf-8") |
| 44 | + .strip() |
| 45 | + ) |
| 46 | + |
| 47 | + try: |
| 48 | + subprocess.run(["git", "checkout", ref], check=True) |
| 49 | + yield |
| 50 | + finally: |
| 51 | + subprocess.run(["git", "checkout", original_ref], check=True) |
| 52 | + |
| 53 | + |
| 54 | +def get_tags() -> list[str]: |
| 55 | + """Get a list of all git tags.""" |
| 56 | + result = subprocess.run(["git", "tag"], capture_output=True, text=True, check=True) |
| 57 | + return result.stdout.splitlines() |
| 58 | + |
| 59 | + |
| 60 | +def copy_notebook(tag: str, dry_run: bool) -> None: |
| 61 | + """Copy the `index.ipynb` notebook for a given tag to a versioned notebook.""" |
| 62 | + src = Path("index.ipynb") |
| 63 | + dst = get_versioned_notebook_path(tag) |
| 64 | + |
| 65 | + if dry_run: |
| 66 | + print(f"Would copy '{src}' to '{dst}'") |
| 67 | + return |
| 68 | + |
| 69 | + shutil.copy2(src, dst) |
| 70 | + |
| 71 | + |
| 72 | +def copy_freeze_directory(tag: str, dry_run: bool) -> None: |
| 73 | + """Copy the `_freeze/index` directory for a given tag to a versioned directory.""" |
| 74 | + src = Path("_freeze/index") |
| 75 | + dst = get_versioned_freeze_directory_path(tag) |
| 76 | + |
| 77 | + if dry_run: |
| 78 | + print(f"Would copy '{src}' to '{dst}'") |
| 79 | + return |
| 80 | + |
| 81 | + shutil.copytree(src, dst, dirs_exist_ok=True) |
| 82 | + |
| 83 | + |
| 84 | +def update_index_notebook_and_freeze_directory(tag: str, dry_run: bool) -> None: |
| 85 | + """ |
| 86 | + Rename the most recent tagged version of the notebook and its freeze directory |
| 87 | + to `index.ipynb` and `_freeze/index`, respectively. |
| 88 | + """ |
| 89 | + notebook_src = get_versioned_notebook_path(tag) |
| 90 | + notebook_dst = Path("index.ipynb") |
| 91 | + |
| 92 | + freeze_src = get_versioned_freeze_directory_path(tag) |
| 93 | + freeze_dst = Path("_freeze/index") |
| 94 | + |
| 95 | + if dry_run: |
| 96 | + print(f"Would move '{notebook_src}' to '{notebook_dst}'") |
| 97 | + print(f"Would move '{freeze_src}' to '{freeze_dst}'") |
| 98 | + return |
| 99 | + |
| 100 | + notebook_dst.unlink() |
| 101 | + notebook_src.replace(notebook_dst) |
| 102 | + |
| 103 | + shutil.rmtree(freeze_dst, ignore_errors=True) |
| 104 | + freeze_src.replace(freeze_dst) |
| 105 | + |
| 106 | + |
| 107 | +def update_quarto_yaml(most_recent_tag: str, previous_tags: list[str], dry_run: bool) -> None: |
| 108 | + """Update the _quarto.yml file to include menu items for each tagged release.""" |
| 109 | + yaml_path = Path("_quarto.yml") |
| 110 | + content = yaml.safe_load(yaml_path.read_text()) |
| 111 | + |
| 112 | + most_recent_version_item = { |
| 113 | + "text": f"{most_recent_tag} (latest)", |
| 114 | + # The link for the most recent version is always `index.ipynb`. |
| 115 | + # (This version of the notebook is renamed to `index.ipynb` elsewhere in this script.) |
| 116 | + "href": "index.ipynb", |
| 117 | + } |
| 118 | + previous_version_items = [ |
| 119 | + {"text": tag, "href": str(get_versioned_notebook_path(tag))} for tag in previous_tags |
| 120 | + ] |
| 121 | + |
| 122 | + if dry_run: |
| 123 | + print(f"Would update '{yaml_path}' with the following menu items:") |
| 124 | + print(f" - {most_recent_version_item}") |
| 125 | + for item in previous_version_items: |
| 126 | + print(f" - {item}") |
| 127 | + return |
| 128 | + |
| 129 | + # Insert the new items. |
| 130 | + for item in content["website"]["navbar"]["left"]: |
| 131 | + if "version-control" in item.get("text", ""): |
| 132 | + item["menu"] = [most_recent_version_item] + previous_version_items |
| 133 | + |
| 134 | + yaml_path.write_text(yaml.dump(content, sort_keys=False, allow_unicode=True)) |
| 135 | + |
| 136 | + |
| 137 | +def main() -> None: |
| 138 | + """ |
| 139 | + Entrypoint for the script. |
| 140 | +
|
| 141 | + CLI args: |
| 142 | + --dry-run: Print the actions that would be taken without actually taking them. |
| 143 | + This is intended to be used when running this script locally during development, |
| 144 | + as a way to preview the changes the script would make to the working directory. |
| 145 | + """ |
| 146 | + parser = argparse.ArgumentParser() |
| 147 | + parser.add_argument("--dry-run", action="store_true") |
| 148 | + args = parser.parse_args() |
| 149 | + |
| 150 | + tags = get_tags() |
| 151 | + if not tags: |
| 152 | + raise ValueError("No tags found") |
| 153 | + |
| 154 | + for tag in tags: |
| 155 | + print(f"Processing tag {tag}") |
| 156 | + with git_checkout(tag): |
| 157 | + copy_notebook(tag, dry_run=args.dry_run) |
| 158 | + copy_freeze_directory(tag, dry_run=args.dry_run) |
| 159 | + |
| 160 | + tags = sorted(tags, reverse=True) |
| 161 | + most_recent_tag, *previous_tags = tags |
| 162 | + |
| 163 | + update_index_notebook_and_freeze_directory(most_recent_tag, dry_run=args.dry_run) |
| 164 | + update_quarto_yaml(most_recent_tag, previous_tags, dry_run=args.dry_run) |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + main() |
0 commit comments