Download usd_models/scripts/usd_cleaner/usd_models_delete.py from SberRoboticsCenter/GreenChallengeAssets: direct link, hf CLI and curl.
- Browser
- Download file 7.01 kB
-
https://huggingface.co/datasets/SberRoboticsCenter/GreenChallengeAssets/resolve/main/usd_models/scripts/usd_cleaner/usd_models_delete.py
- Command line
-
hf download hf://datasets/SberRoboticsCenter/GreenChallengeAssets/usd_models/scripts/usd_cleaner/usd_models_delete.py
-
curl -L -o usd_models_delete.py https://huggingface.co/datasets/SberRoboticsCenter/GreenChallengeAssets/resolve/main/usd_models/scripts/usd_cleaner/usd_models_delete.py
7.01 kB
| #!/usr/bin/env python3 | |
| """Delete the USD files listed by ``usd_models_cleaner.py --unused``. | |
| Takes the JSON list written by ``usd_models_cleaner.py --out`` and removes each | |
| file from ``assets/usd_models``. Afterwards any directory left holding no USD | |
| file at all is removed whole (its leftover textures/materials went with the | |
| model that referenced them). | |
| Destructive. Preview with --dry-run first; the real run asks for confirmation | |
| unless --yes is given. | |
| Usage: | |
| python scripts/usd_cleaner/usd_models_cleaner.py --unused --out unused_usds.json | |
| python scripts/usd_cleaner/usd_models_delete.py unused_usds.json --dry-run # found in scripts/ | |
| python scripts/usd_cleaner/usd_models_delete.py unused_usds.json | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import shutil | |
| from pathlib import Path | |
| SCRIPT_DIR = Path(__file__).resolve().parent | |
| REPO_ROOT = SCRIPT_DIR.parent.parent | |
| USD_SUFFIXES = {".usd", ".usda", ".usdc", ".usdz"} | |
| # Never sweep VCS/metadata dirs: they hold no USD, so the "USD-less" rule would | |
| # otherwise eat assets/usd_models/.git wholesale. | |
| PROTECTED = {".git", ".svn", ".hg", ".datasets", "__pycache__"} | |
| def human(nbytes: int) -> str: | |
| size = float(nbytes) | |
| for unit in ("B", "K", "M", "G", "T"): | |
| if size < 1024 or unit == "T": | |
| return f"{size:.1f}{unit}" | |
| size /= 1024 | |
| def resolve(value: str) -> Path: | |
| path = Path(value) | |
| return path if path.is_absolute() else REPO_ROOT / path | |
| def is_protected(path: Path, root: Path) -> bool: | |
| """True if ``path`` is, or sits inside, a protected directory.""" | |
| return any( | |
| part in PROTECTED or part.startswith(".") | |
| for part in path.relative_to(root).parts | |
| ) | |
| def usd_census(directory: Path, doomed: set): | |
| """(had a USD beneath, keeps one after ``doomed`` is deleted).""" | |
| had = keeps = False | |
| for f in directory.rglob("*"): | |
| if f.is_file() and f.suffix.lower() in USD_SUFFIXES: | |
| had = True | |
| if f not in doomed: | |
| return True, True | |
| return had, keeps | |
| def main() -> None: | |
| parser = argparse.ArgumentParser( | |
| description="Delete unreferenced USD files and the folders left without any USD" | |
| ) | |
| parser.add_argument( | |
| "usd_delete_list", | |
| help="JSON list of paths to delete (from usd_models_cleaner.py --out)", | |
| ) | |
| parser.add_argument( | |
| "--models_dir", | |
| default="assets/usd_models", | |
| help="Deletion is confined to this directory", | |
| ) | |
| parser.add_argument( | |
| "--dry-run", | |
| action="store_true", | |
| help="Report what would be removed without touching anything", | |
| ) | |
| parser.add_argument( | |
| "--yes", action="store_true", help="Skip the confirmation prompt" | |
| ) | |
| parser.add_argument( | |
| "--keep_empty_dirs", | |
| action="store_true", | |
| help="Delete only the files; leave USD-less directories in place", | |
| ) | |
| args = parser.parse_args() | |
| models_dir = resolve(args.models_dir).resolve() | |
| if not models_dir.is_dir(): | |
| raise SystemExit(f"Models directory not found: {models_dir}") | |
| # Accept the list as given, or by bare name from scripts/ or the repo root. | |
| for candidate in ( | |
| Path(args.usd_delete_list), | |
| SCRIPT_DIR / args.usd_delete_list, | |
| REPO_ROOT / args.usd_delete_list, | |
| ): | |
| if candidate.is_file(): | |
| list_path = candidate.resolve() | |
| break | |
| else: | |
| raise SystemExit(f"Delete list not found: {args.usd_delete_list}") | |
| entries = json.loads(list_path.read_text()) | |
| if not isinstance(entries, list): | |
| raise SystemExit(f"{list_path} must contain a JSON list of paths") | |
| targets, missing, outside = [], [], [] | |
| for entry in entries: | |
| path = resolve(entry).resolve() | |
| # Containment check: a stray "../" in the list must never escape the tree. | |
| if not path.is_relative_to(models_dir): | |
| outside.append(entry) | |
| elif not path.is_file(): | |
| missing.append(entry) | |
| else: | |
| targets.append(path) | |
| if outside: | |
| raise SystemExit( | |
| f"Refusing to run: {len(outside)} path(s) fall outside {models_dir}, " | |
| f"e.g. {outside[0]}" | |
| ) | |
| doomed = set(targets) | |
| # Plan the directory sweep before deleting anything, so the dry-run and the | |
| # real run agree. Keep only the top-most doomed dirs: rmtree takes the rest. | |
| doomed_dirs = [] | |
| if not args.keep_empty_dirs: | |
| # A dir is swept only if it HELD a USD and now holds none. A dir that never | |
| # held one (textures/, materials/) belongs to its parent model — deleting it | |
| # standalone would strip a surviving model of its assets. | |
| candidates = [] | |
| for d in models_dir.rglob("*"): | |
| if not d.is_dir() or is_protected(d, models_dir): | |
| continue | |
| had, keeps = usd_census(d, doomed) | |
| if had and not keeps: | |
| candidates.append(d) | |
| candidates.sort(key=lambda d: len(d.parts)) | |
| for directory in candidates: | |
| if not any(directory.is_relative_to(kept) for kept in doomed_dirs): | |
| doomed_dirs.append(directory) | |
| # Every file that actually disappears, counted exactly once. | |
| going = set(doomed) | |
| for directory in doomed_dirs: | |
| going.update(f for f in directory.rglob("*") if f.is_file()) | |
| freed = sum(f.stat().st_size for f in going if f.exists()) | |
| print(f"[delete] list : {list_path}") | |
| print(f"[delete] confined : {models_dir}") | |
| print(f"[delete] to delete : {len(targets)} listed USD files") | |
| if doomed_dirs: | |
| print( | |
| f"[delete] plus : {len(doomed_dirs)} USD-less directories " | |
| f"({len(going) - len(doomed)} further files)" | |
| ) | |
| if missing: | |
| print(f"[delete] skipping : {len(missing)} already absent") | |
| print(f"[delete] frees : {human(freed)}") | |
| if not targets: | |
| print("[delete] nothing to do") | |
| return | |
| if args.dry_run: | |
| for path in sorted(going): | |
| print(f" would delete {path.relative_to(REPO_ROOT)}") | |
| for directory in sorted(doomed_dirs): | |
| print(f" would remove dir {directory.relative_to(REPO_ROOT)}") | |
| print(f"\n[delete] dry run — nothing was touched, would free {human(freed)}") | |
| return | |
| if not args.yes: | |
| answer = input(f"Delete {len(going)} files under {models_dir}? [y/N] ") | |
| if answer.strip().lower() not in ("y", "yes"): | |
| raise SystemExit("[delete] aborted") | |
| removed = 0 | |
| for path in targets: | |
| if path.exists(): | |
| path.unlink() | |
| removed += 1 | |
| for directory in doomed_dirs: | |
| if directory.exists(): | |
| shutil.rmtree(directory) | |
| print( | |
| f"\n[delete] deleted {removed} listed files, " | |
| f"removed {len(doomed_dirs)} USD-less directories" | |
| ) | |
| print(f"[delete] freed {human(freed)}") | |
| if __name__ == "__main__": | |
| main() | |