sahbazhsdt commited on
Commit
58e0a5a
Β·
verified Β·
1 Parent(s): d462184

Upload assemble.py

Browse files
Files changed (1) hide show
  1. 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
+