Download scripts/extract_archive.py from husnainbinmunawar/ShiftBudget-Research-Lab: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/husnainbinmunawar/ShiftBudget-Research-Lab/resolve/main/scripts/extract_archive.py
- Command line
-
hf download hf://spaces/husnainbinmunawar/ShiftBudget-Research-Lab/scripts/extract_archive.py
-
curl -L -o extract_archive.py https://huggingface.co/spaces/husnainbinmunawar/ShiftBudget-Research-Lab/resolve/main/scripts/extract_archive.py
1.14 kB
| from __future__ import annotations | |
| import argparse | |
| import tarfile | |
| from pathlib import Path | |
| def safe_extract(archive: Path, destination: Path) -> None: | |
| destination = destination.resolve() | |
| destination.mkdir(parents=True, exist_ok=True) | |
| with tarfile.open(archive, "r:*") as handle: | |
| for member in handle.getmembers(): | |
| if member.issym() or member.islnk(): | |
| raise ValueError(f"archive links are not accepted: {member.name}") | |
| target = (destination / member.name).resolve() | |
| if destination not in target.parents and target != destination: | |
| raise ValueError(f"archive member escapes destination: {member.name}") | |
| handle.extractall(destination) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Safely extract the official VisA archive") | |
| parser.add_argument("archive", type=Path) | |
| parser.add_argument("--destination", type=Path, required=True) | |
| args = parser.parse_args() | |
| safe_extract(args.archive, args.destination) | |
| print(f"extracted {args.archive} -> {args.destination}") | |
| if __name__ == "__main__": | |
| main() | |