tancilon commited on
Commit
8bfc2cd
·
1 Parent(s): c008041

add dataset unpack script

Browse files
Files changed (1) hide show
  1. scripts/unpack_dataset.py +66 -0
scripts/unpack_dataset.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Extract all tar archives under train/ and val/ directories.
4
+ """
5
+ from __future__ import annotations
6
+
7
+ import argparse
8
+ import tarfile
9
+ from pathlib import Path
10
+
11
+
12
+ def iter_archives(root: Path) -> list[Path]:
13
+ patterns = ("*.tar", "*.tar.gz", "*.tgz")
14
+ archives: list[Path] = []
15
+ for pattern in patterns:
16
+ archives.extend(root.rglob(pattern))
17
+ return sorted(set(archives))
18
+
19
+
20
+ def extract_archive(archive: Path, dest: Path, *, dry_run: bool) -> None:
21
+ if dry_run:
22
+ print(f"[dry-run] {archive} -> {dest}")
23
+ return
24
+ with tarfile.open(archive, "r:*") as tar:
25
+ tar.extractall(path=dest)
26
+ print(f"extracted {archive} -> {dest}")
27
+
28
+
29
+ def main() -> int:
30
+ parser = argparse.ArgumentParser(
31
+ description="Extract tar archives under train/ and val/ directories."
32
+ )
33
+ parser.add_argument(
34
+ "--root",
35
+ type=Path,
36
+ default=Path("."),
37
+ help="Project root containing train/ and val/ (default: current directory).",
38
+ )
39
+ parser.add_argument(
40
+ "--dry-run",
41
+ action="store_true",
42
+ help="List archives without extracting.",
43
+ )
44
+ args = parser.parse_args()
45
+
46
+ root = args.root.resolve()
47
+ targets = [root / "train", root / "val"]
48
+ archives: list[Path] = []
49
+ for target in targets:
50
+ if target.is_dir():
51
+ archives.extend(iter_archives(target))
52
+ else:
53
+ print(f"skip missing directory: {target}")
54
+
55
+ if not archives:
56
+ print("no archives found")
57
+ return 0
58
+
59
+ for archive in archives:
60
+ extract_archive(archive, archive.parent, dry_run=args.dry_run)
61
+
62
+ return 0
63
+
64
+
65
+ if __name__ == "__main__":
66
+ raise SystemExit(main())