File size: 3,065 Bytes
6e33b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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))