OSMGraphCLIP-MS / unpack_shards.py
d-michail's picture
Initial version
0e2b470
Raw
History Blame Contribute Delete
1.72 kB
#!/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()