Skip to content
Snippets Groups Projects
Commit a35f27a5 authored by Karim Ahmed's avatar Karim Ahmed
Browse files

Add logging, fix old run number length check and move to cycle year instead of 1 cycle

parent 0e959ab5
No related branches found
No related tags found
1 merge request!884[Webservice] Use leading zeros in usr/Reports folders
import argparse import argparse
import logging
from pathlib import Path from pathlib import Path
# Update runs into 4 digits integers e.g. r2 to r0002. # 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): def rename_report_folders_for_instrument(directory_path, cycle_year, really):
for folder_name in directory_path.glob("*/usr/Reports/*"): 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 ( if (
folder_name.name.startswith("r") and folder_name.name.startswith("r") and
folder_name.name[1:].isdigit() 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_folder_name = "r" + folder_name.name[1:].zfill(4)
new_path = folder_name.parent.joinpath(new_folder_name) new_path = folder_name.parent.joinpath(new_folder_name)
if really: if really:
folder_name.rename(new_path) 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: else:
if new_path.exists(): if new_path.exists():
# For Testing # For Testing
print(f"{new_folder_name} exists!.") logging.warning(f"{new_folder_name} exists!.")
else: else:
print( logging.info(
f"Will rename {folder_name} to {new_folder_name}. " f"Will rename {folder_name} to {new_folder_name}. "
"Use --really to go through with renaming.") "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( rename_report_folders_for_instrument(
description="Instrument to change the report folders for.") Path(path.format(instrument=instrument)), cycle_year, args.really)
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( if __name__ == "__main__":
Path(path.format(instrument=instrument, cycle=cycle)), args.really) main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment