File size: 2,899 Bytes
58e0a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import os
import glob
import argparse

def assemble_file(folder_path, output_path, cleanup=True):
    """
    Reassemble all .part files found inside a given folder into a single output file.
    Automatically infers the base filename from the first .part file.
    By default, deletes the parts after successful reconstruction.

    Args:
        folder_path (str): Path to the folder containing .part files.
        output_path (str): Path to the folder where the final file will be saved.
        cleanup (bool): If True, deletes part files after successful assembly.
    """
    if not os.path.isdir(folder_path):
        raise FileNotFoundError(f"❌ Folder not found: {folder_path}")

    if not os.path.isdir(output_path):
        print(f"📁 Output folder '{output_path}' not found — creating it.")
        os.makedirs(output_path, exist_ok=True)

    parts = sorted(glob.glob(os.path.join(folder_path, "*.part*")))
    if not parts:
        raise FileNotFoundError(f"❌ No .part files found in folder: {folder_path}")

    # Infer base filename (everything before .partX)
    first_part = os.path.basename(parts[0])
    base_name = first_part.split(".part")[0]
    final_path = os.path.join(output_path, base_name)

    print(f"🔍 Found {len(parts)} parts in '{folder_path}'")
    print(f"🧩 Assembling into '{final_path}' ...")

    # Merge all parts
    with open(final_path, 'wb') as out_file:
        for part in parts:
            with open(part, 'rb') as p:
                out_file.write(p.read())
            print(f"   ➕ Added {os.path.basename(part)}")

    print(f"✅ Reassembly complete! Saved as: {final_path}")

    # Optional cleanup
    if cleanup:
        print("🧹 Cleaning up part files...")
        for part in parts:
            try:
                os.remove(part)
                print(f"   🗑️ Deleted {os.path.basename(part)}")
            except Exception as e:
                print(f"   ⚠️ Could not delete {part}: {e}")
        print("✅ Cleanup complete!")

    return final_path


def main():
    parser = argparse.ArgumentParser(
        description="Reassemble .part files into a single .h5 file (cleanup ON by default)."
    )

    parser.add_argument(
        "--folder_path",
        type=str,
        help="Path to the folder containing .part files."
    )

    parser.add_argument(
        "--output_path",
        type=str,
        help="Path to the folder where the merged .h5 file will be saved."
    )

    parser.add_argument(
        "--no-cleanup",
        action="store_false",
        dest="cleanup",
        help="Disable deleting .part files after assembly (cleanup is ON by default)."
    )

    args = parser.parse_args()

    assemble_file(
        folder_path=args.folder_path,
        output_path=args.output_path,
        cleanup=args.cleanup
    )


if __name__ == "__main__":
    main()