diff --git a/webservice/useful_scripts/rename_report_folders.py b/webservice/useful_scripts/rename_report_folders.py index f77b0d78d3484ce8786e35bb4f420eb2807e566e..008f2f6929cb0e8f8fbe43c85d82c9aff3e4bfe4 100644 --- a/webservice/useful_scripts/rename_report_folders.py +++ b/webservice/useful_scripts/rename_report_folders.py @@ -1,38 +1,58 @@ import argparse -import os from pathlib import Path # Update runs into 4 digits integers e.g. r2 to r0002. def rename_report_folders_for_instrument(directory_path, really): - for folder_name in directory_path.glob("*/*/usr/Reports/*"): - if folder_name.name.startswith('r') and folder_name.name[1:].isdigit(): - new_folder_name = 'r' + folder_name.name[1:].zfill(4) + for folder_name in directory_path.glob("*/usr/Reports/*"): + if ( + folder_name.name.startswith("r") and + folder_name.name[1:].isdigit() and + len(folder_name.name[1:]) == 1 + ): + new_folder_name = "r" + folder_name.name[1:].zfill(4) new_path = folder_name.parent.joinpath(new_folder_name) if really: folder_name.rename(new_path) - print(f'Renamed {folder_name} to {new_folder_name}') + print(f"Renamed {folder_name} to {new_folder_name}") else: - print(f'Will rename {folder_name} to {new_folder_name}') + if new_path.exists(): + # For Testing + print(f"{new_folder_name} exists!.") + else: + print( + f"Will rename {folder_name} to {new_folder_name}. " + "Use --really to go through with renaming.") parser = argparse.ArgumentParser( - description='Instrument to change the report folders for.') + description="Instrument to change the report folders for.") parser.add_argument( - '--instrument', + "--path", type=str, - help='instrument name', + help="Main path for Instruments", + default="/gpfs/exfel/exp/{instrument}/{cycle}" +) +parser.add_argument( + "--instrument", + type=str, + help="instrument name", + required=True, +) +parser.add_argument( + "--cycle", + type=str, + help="cycle name", required=True, ) parser.add_argument( - '--really', action='store_true', + "--really", action="store_true", help="Actually make changes (otherwise dry-run)") args = parser.parse_args() +path = args.path instrument = args.instrument +cycle = args.cycle - -path = "/gpfs/exfel/exp/{instrument}" - -rename_report_folders_for_instrument(Path(path.format(instrument=instrument)), args.really) - +rename_report_folders_for_instrument( + Path(path.format(instrument=instrument, cycle=cycle)), args.really)