File size: 10,017 Bytes
103d9b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffc3595
103d9b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffc3595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103d9b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
"""Download selected public PhysInOne scene archives from dataset shards."""

from __future__ import annotations

import argparse
import json
import re
import sys
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path, PurePosixPath
from typing import Optional, Sequence
from urllib.parse import quote, urlparse

from download_data import DownloadProgress, DownloadResult, download_file, utc_now


SCRIPT_ROOT = Path(__file__).resolve().parent
DEFAULT_SELECTION = Path("selected_cases.json")
DEFAULT_REPO_MAP = SCRIPT_ROOT / "download_lists" / "repo_map.json"


def load_json(path: Path) -> object:
    if not path.is_file():
        raise FileNotFoundError(f"file not found: {path}")
    return json.loads(path.read_text(encoding="utf-8"))


def normalize_repository(value: str) -> str:
    value = value.strip().rstrip("/")
    if value.startswith(("http://", "https://")):
        parsed = urlparse(value)
        parts = [part for part in parsed.path.split("/") if part]
        if len(parts) >= 3 and parts[0] == "datasets":
            value = f"{parts[1]}/{parts[2]}"
        else:
            raise ValueError(f"cannot parse dataset repository URL: {value}")
    if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", value):
        raise ValueError(f"invalid dataset repository name: {value}")
    return value


def load_repo_map(path: Path) -> dict[str, str]:
    raw = load_json(path)
    if not isinstance(raw, dict):
        raise ValueError("repository map must be a JSON object")
    return {str(key): normalize_repository(str(value)) for key, value in raw.items()}


def safe_component(value: object, field: str) -> str:
    text = str(value)
    if not text or text in {".", ".."} or "/" in text or "\\" in text:
        raise ValueError(f"unsafe {field}: {text!r}")
    if any(character.isspace() for character in text):
        raise ValueError(f"{field} contains whitespace: {text!r}")
    return text


def encoded_remote_path(case: dict) -> str:
    raw = str(case.get("hf_zip_path", ""))
    path = PurePosixPath(raw)
    if path.is_absolute() or not path.parts:
        raise ValueError(f"unsafe dataset path: {raw!r}")
    if any(part in {"", ".", ".."} for part in path.parts) or "\\" in raw:
        raise ValueError(f"unsafe dataset path: {raw!r}")
    return quote(raw, safe="/._-~")


def output_path(output_dir: Path, case: dict, keep_structure: bool) -> Path:
    remote = PurePosixPath(str(case["hf_zip_path"]))
    filename = safe_component(remote.name, "archive name")
    if not keep_structure:
        return output_dir / filename
    return output_dir.joinpath(
        safe_component(case["part_id"], "part ID"),
        safe_component(case["split"], "split"),
        safe_component(case["activity_type"], "activity type"),
        filename,
    )


class Logs:
    def __init__(self, root: Path) -> None:
        self.root = root
        self.root.mkdir(parents=True, exist_ok=True)
        self.success = root / "downloads.tsv"
        self.failures = root / "failures.tsv"
        self._lock = threading.Lock()
        self.success.write_text(
            "case_id\trepository\tremote_path\tlocal_path\tstatus\tbytes_written\ttimestamp\n",
            encoding="utf-8",
        )
        self.failures.write_text(
            "case_id\trepository\tremote_path\terror\ttimestamp\n",
            encoding="utf-8",
        )

    def record(self, case_id: str, repository: str, result: DownloadResult) -> None:
        now = utc_now()
        with self._lock:
            if result.error:
                with self.failures.open("a", encoding="utf-8") as handle:
                    handle.write(
                        f"{case_id}\t{repository}\t{result.remote_path}\t"
                        f"{result.error}\t{now}\n"
                    )
            else:
                with self.success.open("a", encoding="utf-8") as handle:
                    handle.write(
                        f"{case_id}\t{repository}\t{result.remote_path}\t"
                        f"{result.local_path}\t{result.status}\t"
                        f"{result.bytes_written}\t{now}\n"
                    )


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Download selected public PhysInOne scene archives."
    )
    parser.add_argument(
        "--selection",
        type=Path,
        default=DEFAULT_SELECTION,
        help="Selection JSON produced by filter_cases.py (default: selected_cases.json).",
    )
    parser.add_argument(
        "--repo-map",
        "--repo_map",
        dest="repo_map",
        type=Path,
        default=DEFAULT_REPO_MAP,
        help="Dataset shard map supplied with this repository.",
    )
    parser.add_argument(
        "--output-dir",
        "--output_dir",
        dest="output_dir",
        type=Path,
        default=Path("PhysInOne_data/full_dataset"),
        help="Whitespace-free local output directory.",
    )
    parser.add_argument(
        "--revision", default="main", help="Dataset revision (default: main)."
    )
    parser.add_argument(
        "--workers", type=int, default=4, help="Concurrent downloads (default: 4)."
    )
    parser.add_argument(
        "--retries", type=int, default=3, help="Retries after each failure (default: 3)."
    )
    parser.add_argument(
        "--timeout", type=float, default=60.0, help="Request timeout in seconds."
    )
    parser.add_argument(
        "--keep-shard-structure",
        "--keep_shard_structure",
        dest="keep_shard_structure",
        action="store_true",
        help="Preserve part/split/activity directories instead of a flat output.",
    )
    parser.add_argument(
        "--no-resume",
        action="store_true",
        help="Restart incomplete .part files instead of resuming them.",
    )
    parser.add_argument(
        "--overwrite", action="store_true", help="Replace completed local files."
    )
    parser.add_argument(
        "--dry-run",
        "--dry_run",
        dest="dry_run",
        action="store_true",
        help="Print the planned transfers without downloading.",
    )
    return parser


def main(argv: Optional[Sequence[str]] = None) -> int:
    args = build_parser().parse_args(argv)
    if args.workers < 1:
        raise SystemExit("--workers must be at least 1")
    if args.retries < 0:
        raise SystemExit("--retries cannot be negative")
    if args.timeout <= 0:
        raise SystemExit("--timeout must be positive")
    if any(character.isspace() for character in str(args.output_dir)):
        raise SystemExit("--output-dir must not contain whitespace")

    selection = load_json(args.selection)
    if not isinstance(selection, dict) or not isinstance(selection.get("cases"), list):
        raise ValueError("selection JSON must contain a list named 'cases'")
    cases = selection["cases"]
    repository_map = load_repo_map(args.repo_map)
    missing = sorted(
        {
            str(case.get("part_id", ""))
            for case in cases
            if str(case.get("part_id", "")) not in repository_map
        }
    )
    if missing:
        raise KeyError("part IDs missing from repository map: " + ", ".join(missing))

    output_dir = args.output_dir.expanduser().resolve()
    planned = []
    for case in cases:
        part_id = str(case["part_id"])
        planned.append(
            (
                str(case.get("case_id", "")),
                repository_map[part_id],
                encoded_remote_path(case),
                output_path(output_dir, case, args.keep_shard_structure),
            )
        )

    print(f"cases: {len(planned)}")
    if args.dry_run:
        for case_id, repository, remote_path, destination in planned[:20]:
            print(f"{case_id}: {repository}/{remote_path} -> {destination}")
        if len(planned) > 20:
            print(f"... and {len(planned) - 20} more")
        return 0

    output_dir.mkdir(parents=True, exist_ok=True)
    run_id = datetime.now().strftime("%Y%m%d_%H%M%S")
    logs = Logs(output_dir / "_download_logs" / run_id)
    results = []
    progress = DownloadProgress(len(planned), "Downloading")
    try:
        with ThreadPoolExecutor(max_workers=args.workers) as pool:
            futures = {
                pool.submit(
                    download_file,
                    "full_dataset",
                    remote_path,
                    destination,
                    args.revision,
                    args.timeout,
                    args.retries,
                    not args.no_resume,
                    args.overwrite,
                    False,
                    repository,
                    progress,
                    index,
                ): (index, case_id, repository)
                for index, (case_id, repository, remote_path, destination)
                in enumerate(planned)
            }
            for future in as_completed(futures):
                key, case_id, repository = futures[future]
                result = future.result()
                results.append(result)
                logs.record(case_id, repository, result)
                progress.complete(
                    key, result.status, result.local_path, failed=bool(result.error)
                )
    finally:
        progress.finish()

    failures = sum(1 for result in results if result.error)
    summary = {
        "selected_cases": len(planned),
        "successful_downloads": len(results) - failures,
        "download_failures": failures,
        "output_dir": str(output_dir),
        "log_dir": str(logs.root),
        "finished_at": utc_now(),
    }
    (logs.root / "summary.json").write_text(
        json.dumps(summary, indent=2, sort_keys=True) + "\n", encoding="utf-8"
    )
    print(json.dumps(summary, indent=2, sort_keys=True))
    return 1 if failures else 0


if __name__ == "__main__":
    raise SystemExit(main())