openart-tools / scripts /download_tool.py
dongdongunique's picture
Add files using upload-large-folder tool
ceb9c75 verified
Raw History Blame Contribute Delete
1.46 kB
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import zipfile
from pathlib import Path
import pyarrow.parquet as pq
def main() -> int:
parser = argparse.ArgumentParser(description="Extract one OpenART tool from a local dataset clone.")
parser.add_argument("--dataset-dir", required=True)
parser.add_argument("--tool-id", required=True)
parser.add_argument("--output-dir", required=True)
args = parser.parse_args()
dataset = Path(args.dataset_dir)
rows = pq.read_table(dataset / "data" / "tools" / "metadata-00000.parquet").to_pylist()
match = next((row for row in rows if row["tool_id"] == args.tool_id), None)
if match is None:
raise SystemExit(f"tool not found: {args.tool_id}")
archive_path = dataset / match["bundle_path"]
prefix = match["bundle_member_prefix"].rstrip("/") + "/"
out = Path(args.output_dir)
out.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(archive_path) as archive:
for member in archive.namelist():
if not member.startswith(prefix) or member.endswith("/"):
continue
target = out / member[len(prefix):]
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(archive.read(member))
print(json.dumps({"output_dir": str(out), "tool_id": args.tool_id}, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())