Upload assemble.py
Browse files- assemble.py +94 -0
assemble.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import glob
|
| 4 |
+
import argparse
|
| 5 |
+
|
| 6 |
+
def assemble_file(folder_path, output_path, cleanup=True):
|
| 7 |
+
"""
|
| 8 |
+
Reassemble all .part files found inside a given folder into a single output file.
|
| 9 |
+
Automatically infers the base filename from the first .part file.
|
| 10 |
+
By default, deletes the parts after successful reconstruction.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
folder_path (str): Path to the folder containing .part files.
|
| 14 |
+
output_path (str): Path to the folder where the final file will be saved.
|
| 15 |
+
cleanup (bool): If True, deletes part files after successful assembly.
|
| 16 |
+
"""
|
| 17 |
+
if not os.path.isdir(folder_path):
|
| 18 |
+
raise FileNotFoundError(f"β Folder not found: {folder_path}")
|
| 19 |
+
|
| 20 |
+
if not os.path.isdir(output_path):
|
| 21 |
+
print(f"π Output folder '{output_path}' not found β creating it.")
|
| 22 |
+
os.makedirs(output_path, exist_ok=True)
|
| 23 |
+
|
| 24 |
+
parts = sorted(glob.glob(os.path.join(folder_path, "*.part*")))
|
| 25 |
+
if not parts:
|
| 26 |
+
raise FileNotFoundError(f"β No .part files found in folder: {folder_path}")
|
| 27 |
+
|
| 28 |
+
# Infer base filename (everything before .partX)
|
| 29 |
+
first_part = os.path.basename(parts[0])
|
| 30 |
+
base_name = first_part.split(".part")[0]
|
| 31 |
+
final_path = os.path.join(output_path, base_name)
|
| 32 |
+
|
| 33 |
+
print(f"π Found {len(parts)} parts in '{folder_path}'")
|
| 34 |
+
print(f"π§© Assembling into '{final_path}' ...")
|
| 35 |
+
|
| 36 |
+
# Merge all parts
|
| 37 |
+
with open(final_path, 'wb') as out_file:
|
| 38 |
+
for part in parts:
|
| 39 |
+
with open(part, 'rb') as p:
|
| 40 |
+
out_file.write(p.read())
|
| 41 |
+
print(f" β Added {os.path.basename(part)}")
|
| 42 |
+
|
| 43 |
+
print(f"β
Reassembly complete! Saved as: {final_path}")
|
| 44 |
+
|
| 45 |
+
# Optional cleanup
|
| 46 |
+
if cleanup:
|
| 47 |
+
print("π§Ή Cleaning up part files...")
|
| 48 |
+
for part in parts:
|
| 49 |
+
try:
|
| 50 |
+
os.remove(part)
|
| 51 |
+
print(f" ποΈ Deleted {os.path.basename(part)}")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f" β οΈ Could not delete {part}: {e}")
|
| 54 |
+
print("β
Cleanup complete!")
|
| 55 |
+
|
| 56 |
+
return final_path
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def main():
|
| 60 |
+
parser = argparse.ArgumentParser(
|
| 61 |
+
description="Reassemble .part files into a single .h5 file (cleanup ON by default)."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
parser.add_argument(
|
| 65 |
+
"--folder_path",
|
| 66 |
+
type=str,
|
| 67 |
+
help="Path to the folder containing .part files."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
parser.add_argument(
|
| 71 |
+
"--output_path",
|
| 72 |
+
type=str,
|
| 73 |
+
help="Path to the folder where the merged .h5 file will be saved."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
parser.add_argument(
|
| 77 |
+
"--no-cleanup",
|
| 78 |
+
action="store_false",
|
| 79 |
+
dest="cleanup",
|
| 80 |
+
help="Disable deleting .part files after assembly (cleanup is ON by default)."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
args = parser.parse_args()
|
| 84 |
+
|
| 85 |
+
assemble_file(
|
| 86 |
+
folder_path=args.folder_path,
|
| 87 |
+
output_path=args.output_path,
|
| 88 |
+
cleanup=args.cleanup
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
main()
|
| 94 |
+
|