From a35f27a51281cb70e8ba2c5a1d8c58c1cb0b96ea Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Wed, 17 Jan 2024 14:16:21 +0100
Subject: [PATCH] Add logging, fix old run number length check and move to
 cycle year instead of 1 cycle

---
 .../useful_scripts/rename_report_folders.py   | 88 +++++++++++--------
 1 file changed, 53 insertions(+), 35 deletions(-)

diff --git a/webservice/useful_scripts/rename_report_folders.py b/webservice/useful_scripts/rename_report_folders.py
index 008f2f692..ba7240cdf 100644
--- a/webservice/useful_scripts/rename_report_folders.py
+++ b/webservice/useful_scripts/rename_report_folders.py
@@ -1,58 +1,76 @@
 import argparse
+import logging
 from pathlib import Path
 
 # Update runs into 4 digits integers e.g. r2 to r0002.
 
+# Configure logging
+logging.basicConfig(
+    level=logging.INFO,
+    format="%(asctime)s - %(levelname)s - %(message)s",
+    handlers=[
+        logging.StreamHandler(),
+        logging.FileHandler('rename_reports.log')]
+)
+
 
-def rename_report_folders_for_instrument(directory_path, really):
-    for folder_name in directory_path.glob("*/usr/Reports/*"):
+def rename_report_folders_for_instrument(directory_path, cycle_year, really):
+    logging.info(f"Starting the script for {directory_path} and {cycle_year}.")
+    for folder_name in directory_path.glob(f"{cycle_year}/**/*/usr/Reports/*"):
         if (
             folder_name.name.startswith("r") and
             folder_name.name[1:].isdigit() and
-            len(folder_name.name[1:]) == 1
+            len(folder_name.name[1:]) < 4
         ):
             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}")
+                logging.info(f"Renamed {folder_name} to {new_folder_name}")
             else:
                 if new_path.exists():
                     # For Testing
-                    print(f"{new_folder_name} exists!.")
+                    logging.warning(f"{new_folder_name} exists!.")
                 else:
-                    print(
+                    logging.info(
                         f"Will rename {folder_name} to {new_folder_name}. "
                         "Use --really to go through with renaming.")
+    logging.info("Renaming done .")
+    logging.info(".")
+    logging.info(".")
+    logging.info(".")
 
+def main():
+    parser = argparse.ArgumentParser(
+        description="Instrument to change the report folders for.")
+    parser.add_argument(
+        "--path",
+        type=str,
+        help="Main path for Instruments",
+        default="/gpfs/exfel/exp/{instrument}"
+    )
+    parser.add_argument(
+        "--instrument",
+        type=str,
+        help="instrument name",
+        required=True,
+    )
+    parser.add_argument(
+        "--cycle-year",
+        type=str,
+        help="Cycle year number",
+        required=True,
+    )
+    parser.add_argument(
+        "--really", action="store_true",
+        help="Actually make changes (otherwise dry-run)")
+    args = parser.parse_args()
+    path = args.path
+    instrument = args.instrument
+    cycle_year = args.cycle_year
 
-parser = argparse.ArgumentParser(
-    description="Instrument to change the report folders for.")
-parser.add_argument(
-    "--path",
-    type=str,
-    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",
-    help="Actually make changes (otherwise dry-run)")
-args = parser.parse_args()
-path = args.path
-instrument = args.instrument
-cycle = args.cycle
+    rename_report_folders_for_instrument(
+        Path(path.format(instrument=instrument)), cycle_year, args.really)
 
-rename_report_folders_for_instrument(
-    Path(path.format(instrument=instrument, cycle=cycle)), args.really)
+if __name__ == "__main__":
+    main()
\ No newline at end of file
-- 
GitLab