-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_and_remove_invalid_bbb_files.py
More file actions
179 lines (149 loc) · 6.21 KB
/
Copy pathscan_and_remove_invalid_bbb_files.py
File metadata and controls
179 lines (149 loc) · 6.21 KB
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
#!/usr/bin/env python3
"""
Scan .bbb files for one or more dates and remove files that cannot be read.
Example:
python scan_and_remove_invalid_bbb_files.py --dates 20160819 20160820
python scan_and_remove_invalid_bbb_files.py --dates 2016-08-19 --dry-run
python scan_and_remove_invalid_bbb_files.py --dates 20160819 --deep-check-bbb
python scan_and_remove_invalid_bbb_files.py --dates 20160819 --deep-check-bbb --num-workers 16
"""
import argparse
import os
import shlex
import subprocess
import sys
from multiprocessing import Pool
from bb_hpc import settings
from bb_hpc.src.fileinfo import is_bbb_file_valid_basicmatch, is_bbb_file_valid_deep
def _pick_pipeline_root(prefer: str | None = None) -> str:
pr_local = getattr(settings, "pipeline_root_local", "")
pr_hpc = getattr(settings, "pipeline_root_hpc", "")
def _exists(p: str) -> bool:
return bool(p) and os.path.exists(p)
if prefer == "local" and _exists(pr_local):
return pr_local
if prefer == "hpc" and _exists(pr_hpc):
return pr_hpc
if _exists(pr_local):
return pr_local
if _exists(pr_hpc):
return pr_hpc
return pr_local or pr_hpc
def _normalize_date(d: str) -> str:
s = d.strip().replace("/", "").replace("-", "")
if len(s) != 8 or not s.isdigit():
raise ValueError(f"Invalid date {d!r}; expected YYYYMMDD or YYYY-MM-DD.")
return s
def _find_bbb_files(day_dir: str) -> list[str]:
try:
cmd = f"find {shlex.quote(day_dir)} -type f -name '*.bbb' -print"
out = subprocess.check_output(["bash", "-lc", cmd], stderr=subprocess.DEVNULL)
return [line.decode("utf-8", "replace").strip() for line in out.splitlines() if line.strip()]
except Exception:
bbb_files: list[str] = []
for root, _dirs, files in os.walk(day_dir):
for name in files:
if name.endswith(".bbb"):
bbb_files.append(os.path.join(root, name))
return bbb_files
def _validate_file(args: tuple[str, bool]) -> tuple[str, bool]:
"""Worker function for parallel validation. Returns (path, is_valid)."""
path, deep_check = args
if deep_check:
ok = is_bbb_file_valid_deep(path)
else:
ok = is_bbb_file_valid_basicmatch(path, check_read_file=True)
return path, ok
def _scan_day(
day_dir: str,
dry_run: bool,
verbose: bool,
deep_check_bbb: bool,
num_workers: int = 1,
) -> tuple[int, int, int, list[str]]:
files = _find_bbb_files(day_dir)
invalid: list[str] = []
if num_workers > 1 and len(files) > 1:
# Parallel validation
work_items = [(path, deep_check_bbb) for path in files]
with Pool(processes=num_workers) as pool:
for path, ok in pool.imap_unordered(_validate_file, work_items, chunksize=10):
if not ok:
invalid.append(path)
if verbose:
print(f"[invalid] {path}")
else:
# Sequential validation
for path in files:
if deep_check_bbb:
ok = is_bbb_file_valid_deep(path)
else:
ok = is_bbb_file_valid_basicmatch(path, check_read_file=True)
if not ok:
invalid.append(path)
if verbose:
print(f"[invalid] {path}")
removed = 0
if not dry_run:
for path in invalid:
try:
os.remove(path)
removed += 1
except Exception as e:
print(f"[warn] failed to remove {path}: {e}")
return len(files), len(invalid), removed, invalid
def parse_args():
p = argparse.ArgumentParser(description="Scan .bbb files for dates and remove unreadable ones.")
p.add_argument("--dates", nargs="+", required=True, help="Dates to scan (YYYYMMDD or YYYY-MM-DD).")
p.add_argument("--paths", choices=["auto", "local", "hpc"], default="auto",
help="Which settings paths to use (auto prefers *_local if they exist).")
p.add_argument("--pipeline-root", default="", help="Override pipeline_root from settings.")
p.add_argument("--dry-run", action="store_true", help="Scan only; do not delete files.")
p.add_argument("--verbose", action="store_true", help="Print each invalid path.")
p.add_argument("--deep-check-bbb", action="store_true",
help="Read through all frames for each .bbb (slower, catches premature EOF).")
p.add_argument("--num-workers", type=int, default=8,
help="Number of parallel workers for validation (default: 8, use 1 for sequential).")
return p.parse_args()
def main():
args = parse_args()
pipeline_root = args.pipeline_root or _pick_pipeline_root(None if args.paths == "auto" else args.paths)
if not pipeline_root or not os.path.exists(pipeline_root):
print("ERROR: pipeline_root not set or does not exist.", file=sys.stderr)
sys.exit(2)
dates = []
for d in args.dates:
try:
dates.append(_normalize_date(d))
except ValueError as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(2)
total_files = 0
total_invalid = 0
total_removed = 0
for d in dates:
day_dir = os.path.join(pipeline_root, d[:4], d[4:6], d[6:8])
if not os.path.exists(day_dir):
print(f"[skip] missing day directory: {day_dir}")
continue
print(f"[scan] {day_dir}")
n_files, n_invalid, n_removed, invalid_paths = _scan_day(
day_dir, args.dry_run, args.verbose, args.deep_check_bbb, args.num_workers
)
total_files += n_files
total_invalid += n_invalid
total_removed += n_removed
if args.dry_run:
print(f"[day] files={n_files} invalid={n_invalid} (dry-run)")
if invalid_paths:
print("[invalid paths]")
for p in invalid_paths:
print(p)
else:
print(f"[day] files={n_files} invalid={n_invalid} removed={n_removed}")
if args.dry_run:
print(f"[total] files={total_files} invalid={total_invalid} (dry-run)")
else:
print(f"[total] files={total_files} invalid={total_invalid} removed={total_removed}")
if __name__ == "__main__":
main()