TSRDA / MTLCC /tools /patch_fetch.py
Dhruv1000's picture
Add MTLCC: code, tools, results, report, checkpoints (Option 1 complete)
6e33b3b verified
Raw History Blame Contribute Delete
3.07 kB
s=open('fetch_mtlcc.py').read()
s=s.replace('''sel = [e for e in ents if e[0].startswith(PREFIX) and not e[0].endswith("/")]
is17 = lambda n: "/data17/" in n
sel = [e for e in sel if (is17(e[0]) if phase == "b" else not is17(e[0]))]
sel.sort(key=lambda e: e[4])
''','''is17 = lambda n: "/data17/" in n
allp = sorted([e for e in ents if e[0].startswith(PREFIX)], key=lambda e: e[4])
want = lambda e: (not e[0].endswith("/")) and (is17(e[0]) if phase == "b" else not is17(e[0]))
sel = [e for e in allp if want(e)]
''')
s=s.replace('''todo = [e for e in sel if not (os.path.exists(dest(e[0])) and os.path.getsize(dest(e[0])) == e[3])]
print(f"phase {phase}: {len(sel)} members, {len(todo)} to fetch, "
f"{sum(e[2] for e in todo)/1e9:.2f} GB compressed", flush=True)''','''todo_set = set(e[0] for e in sel if not (os.path.exists(dest(e[0])) and os.path.getsize(dest(e[0])) == e[3]))
todo = [e for e in allp if e[0].endswith("/") or e[0] in todo_set]
print(f"phase {phase}: {len(sel)} members, {len(todo_set)} to fetch, "
f"{sum(e[2] for e in todo if e[0] in todo_set)/1e9:.2f} GB compressed", flush=True)''')
old=s[s.index('def worker(chunk):'):s.index('# split by')]
new='''def worker(chunk):
i = 0
while i < len(chunk) and not stop.is_set():
first = chunk[i]
try:
req = urllib.request.Request(URL, headers={"Range": f"bytes={first[4]}-{chunk[-1][4] + 4096 + chunk[-1][2]}"})
with urllib.request.urlopen(req, timeout=120) as r:
pos = first[4]
while i < len(chunk) and not stop.is_set():
name, cm, cs, us, lo = chunk[i]
if lo != pos: break # gap -> reconnect at lo
hdr = r.read(30)
sig, = struct.unpack("<I", hdr[:4])
assert sig == 0x04034b50, "bad local header"
fnl, exl = struct.unpack("<HH", hdr[26:30])
r.read(fnl + exl)
raw = b""
while len(raw) < cs:
b = r.read(min(1 << 20, cs - len(raw)))
if not b: raise IOError("short read")
raw += b
pos = lo + 30 + fnl + exl + cs
if not name.endswith("/"):
data = raw if cm == 0 else zlib.decompress(raw, -15)
assert len(data) == us, f"size mismatch {name}"
if disk_pct() > LIMIT_PCT:
print("DISK LIMIT REACHED - aborting", flush=True); stop.set(); return
p = dest(name); os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p + ".part", "wb") as f: f.write(data)
os.replace(p + ".part", p)
with lock: done_bytes[0] += cs
i += 1
except Exception as ex:
print(f"retry seg after {type(ex).__name__}: {ex}", flush=True); time.sleep(5)
'''
open('fetch_mtlcc.py','w').write(s.replace(old,new))