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(" 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))