File size: 1,719 Bytes
0e2b470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Reassemble sharded folders produced by make_shards.py.

Finds every "<name>.zip-parts/" directory under the given root(s) and
unzips all shard zips inside it into the parent directory, reconstructing
the original "<name>/" folder. Safe to re-run (existing files are
overwritten with -o).

Usage:
    python3 unpack_shards.py [root ...]

With no arguments, searches the current directory.
"""
import glob
import os
import subprocess
import sys


def find_zip_parts_dirs(root):
    matches = []
    for dirpath, dirnames, _ in os.walk(root):
        for d in list(dirnames):
            if d.endswith(".zip-parts"):
                matches.append(os.path.join(dirpath, d))
                dirnames.remove(d)  # no need to descend into it
    return sorted(matches)


def unpack(zip_parts_dir):
    dest = os.path.dirname(zip_parts_dir)
    shards = sorted(glob.glob(os.path.join(zip_parts_dir, "*.zip")))
    if not shards:
        print(f"  no shards found in {zip_parts_dir}, skipping")
        return
    name = os.path.basename(zip_parts_dir)[: -len(".zip-parts")]
    print(f"=== {zip_parts_dir} -> {os.path.join(dest, name)}/ ({len(shards)} shard(s)) ===")
    for shard in shards:
        print(f"  extracting {os.path.basename(shard)}")
        subprocess.run(["unzip", "-oq", shard, "-d", dest], check=True)
    print("  done")


def main():
    roots = sys.argv[1:] or ["."]
    zip_parts_dirs = []
    for root in roots:
        zip_parts_dirs.extend(find_zip_parts_dirs(root))

    if not zip_parts_dirs:
        print("No *.zip-parts directories found.")
        return

    for zip_parts_dir in zip_parts_dirs:
        unpack(zip_parts_dir)


if __name__ == "__main__":
    main()