|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}' ...") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|