CoolFace
Apppublic

rerun/InstantMesh

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
4likes
template_update.py192 linesDownload Raw Back to scripts
1#!/usr/bin/env python32# Copied from https://github.com/rerun-io/rerun_template3 4"""5The script has two purposes.6 7After using `rerun_template` as a template, run this to clean out things you don't need.8Use `scripts/template_update.py init --languages cpp,rust,python` for this.9 10Update an existing repository with the latest changes from the template.11Use `scripts/template_update.py update --languages cpp,rust,python` for this.12 13In either case, make sure the list of languages matches the languages you want to support.14You can also use `--dry-run` to see what would happen without actually changing anything.15"""16 17from __future__ import annotations18 19import argparse20import os21import shutil22import tempfile23 24from git import Repo  # pip install GitPython25 26OWNER = "rerun-io"27 28# Don't overwrite these when updating existing repository from the template29DO_NOT_OVERWRITE = {30    "Cargo.lock",31    "CHANGELOG.md",32    "main.py",33    "pixi.lock",34    "README.md",35    "requirements.txt",36}37 38# Files required by C++, but not by _both_ Python and Rust39CPP_FILES = {40    ".clang-format",41    ".github/workflows/cpp.yml",42    "CMakeLists.txt",43    "pixi.lock",  # Pixi is only C++ & Python - For Rust we only use cargo44    "pixi.toml",  # Pixi is only C++ & Python - For Rust we only use cargo45    "src/",46    "src/main.cpp",47}48 49# Files required by Python, but not by _both_ C++ and Rust50PYTHON_FILES = {51    ".github/workflows/python.yml",52    ".mypy.ini",53    "main.py",54    "pixi.lock",  # Pixi is only C++ & Python - For Rust we only use cargo55    "pixi.toml",  # Pixi is only C++ & Python - For Rust we only use cargo56    "pyproject.toml",57    "requirements.txt",58}59 60# Files required by Rust, but not by _both_ C++ and Python61RUST_FILES = {62    ".github/workflows/rust.yml",63    "bacon.toml",64    "Cargo.lock",65    "Cargo.toml",66    "CHANGELOG.md",  # We only keep a changelog for Rust crates at the moment67    "clippy.toml",68    "Cranky.toml",69    "deny.toml",70    "rust-toolchain",71    "scripts/clippy_wasm/",72    "scripts/clippy_wasm/clippy.toml",73    "scripts/generate_changelog.py",  # We only keep a changelog for Rust crates at the moment74    "src/",75    "src/lib.rs",76    "src/main.rs",77}78 79# Files we used to have, but have been removed in never version of rerun_template80DEAD_FILES = ["bacon.toml", "Cranky.toml"]81 82 83def parse_languages(lang_str: str) -> set[str]:84    languages = lang_str.split(",") if lang_str else []85    for lang in languages:86        assert lang in ["cpp", "python", "rust"], f"Unsupported language: {lang}"87    return set(languages)88 89 90def calc_deny_set(languages: set[str]) -> set[str]:91    """The set of files to delete/ignore."""92    files_to_delete = CPP_FILES | PYTHON_FILES | RUST_FILES93    if "cpp" in languages:94        files_to_delete -= CPP_FILES95    if "python" in languages:96        files_to_delete -= PYTHON_FILES97    if "rust" in languages:98        files_to_delete -= RUST_FILES99    return files_to_delete100 101 102def init(languages: set[str], dry_run: bool) -> None:103    print("Removing all language-specific files not needed for languages {languages}.")104    files_to_delete = calc_deny_set(languages)105    delete_files_and_folder(files_to_delete, dry_run)106 107 108def remove_file(filepath: str):109    try:110        os.remove(filepath)111    except FileNotFoundError:112        pass113 114 115def delete_files_and_folder(paths: set[str], dry_run: bool) -> None:116    repo_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))117    for path in paths:118        full_path = os.path.join(repo_path, path)119        if os.path.exists(full_path):120            if os.path.isfile(full_path):121                print(f"Removing file {full_path}…")122                if not dry_run:123                    remove_file(full_path)124            elif os.path.isdir(full_path):125                print(f"Removing folder {full_path}…")126                if not dry_run:127                    shutil.rmtree(full_path)128 129 130def update(languages: set[str], dry_run: bool) -> None:131    for file in DEAD_FILES:132        print(f"Removing dead file {file}…")133        if not dry_run:134            remove_file(file)135 136    files_to_ignore = calc_deny_set(languages) | DO_NOT_OVERWRITE137    repo_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))138 139    with tempfile.TemporaryDirectory() as temp_dir:140        Repo.clone_from("https://github.com/rerun-io/rerun_template.git", temp_dir)141        for root, dirs, files in os.walk(temp_dir):142            for file in files:143                src_path = os.path.join(root, file)144                rel_path = os.path.relpath(src_path, temp_dir)145 146                if rel_path.startswith(".git/"):147                    continue148                if rel_path.startswith("src/"):149                    continue150                if rel_path in files_to_ignore:151                    continue152 153                dest_path = os.path.join(repo_path, rel_path)154 155                print(f"Updating {rel_path}…")156                if not dry_run:157                    os.makedirs(os.path.dirname(dest_path), exist_ok=True)158                    shutil.copy2(src_path, dest_path)159 160 161def main() -> None:162    parser = argparse.ArgumentParser(description="Handle the Rerun template.")163    subparsers = parser.add_subparsers(dest="command")164 165    init_parser = subparsers.add_parser("init", help="Initialize a new checkout of the template.")166    init_parser.add_argument(167        "--languages", default="", nargs="?", const="", help="The languages to support (e.g. `cpp,python,rust`)."168    )169    init_parser.add_argument("--dry-run", action="store_true", help="Don't actually delete any files.")170 171    update_parser = subparsers.add_parser(172        "update", help="Update all existing Rerun repositories with the latest changes from the template"173    )174    update_parser.add_argument(175        "--languages", default="", nargs="?", const="", help="The languages to support (e.g. `cpp,python,rust`)."176    )177    update_parser.add_argument("--dry-run", action="store_true", help="Don't actually delete any files.")178 179    args = parser.parse_args()180 181    if args.command == "init":182        init(parse_languages(args.languages), args.dry_run)183    elif args.command == "update":184        update(parse_languages(args.languages), args.dry_run)185    else:186        parser.print_help()187        exit(1)188 189 190if __name__ == "__main__":191    main()192