From 6221abd2a7e70b43ae0a60646336440ac568ec4f Mon Sep 17 00:00:00 2001 From: Dimitris Papagiannis Date: Mon, 22 Jul 2024 21:01:45 +0200 Subject: [PATCH] Fix audience for api client --- runregistry/__init__.py | 2 +- update_specific_runs.py | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 update_specific_runs.py diff --git a/runregistry/__init__.py b/runregistry/__init__.py index b946f54..4493b86 100644 --- a/runregistry/__init__.py +++ b/runregistry/__init__.py @@ -2,4 +2,4 @@ # To update: # pip install wheel && pip install twine && python setup.py bdist_wheel && twine upload --skip-existing dist/* -__version__ = "1.2.0" +__version__ = "1.2.1" diff --git a/update_specific_runs.py b/update_specific_runs.py new file mode 100644 index 0000000..13051e3 --- /dev/null +++ b/update_specific_runs.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 + +# Helper program for forcing an update of the attributes of a list of runs in +# Run Registry. +# By default, the changes are made to the development instance of RR, pass "--production" to do it in production. +# Requires an access token with elevated permissions. + +import os +import sys +import runregistry +import argparse +import json + + +def main(): + parser = argparse.ArgumentParser( + prog=os.path.basename(__file__), + description="Hi, I am a program that triggers an update of the attributes for a list of runs in RR. I then revert their state if they were SIGNOFF, and their run class.", + epilog="..and that's it", + ) + + parser.add_argument("run_numbers", type=int, nargs="+") + parser.add_argument( + "--target", + help="Update the production RR instance", + choices=("production", "development", "local"), + default="development", + ) + args = parser.parse_args() + run_numbers = args.run_numbers + runregistry.setup(args.target) + + # Keep track of runs that were not OPEN, so that we can + # move them back to whatever state they were. + runs_to_revert = {} + + runs_info = runregistry.get_runs( + filter={"run_number": {"and": [{">": run_numbers}]}} + ) + for run_info in runs_info: + try: + # Move run to OPEN so that it's editable + if "state" in run_info and run_info["state"] != "OPEN": + # Remember which ones were not OPEN, so that we can revert them + runs_to_revert[run_info["run_number"]] = { + "prev_state": run_info["state"], + } + print( + f"Moving run {run_info['run_number']} from {run_info['state']} to OPEN" + ) + answer = runregistry.move_runs( + run=run_info["run_number"], from_=run_info["state"], to_="OPEN" + ) + if answer.status_code == 401: + print( + f"Permissions error: {json.loads(answer.content.decode('utf-8'))['message']}" + ) + sys.exit(1) + + # Keep track of their previous run class + if run_info["run_number"] not in runs_to_revert: + runs_to_revert[run_info["run_number"]] = {} + runs_to_revert[run_info["run_number"]]["prev_class"] = run_info["class"] + + # Maybe this could be run with the whole list + print(f"Requesting reset of run {run_info['run_number']} attributes.") + answer = runregistry.reset_RR_attributes_and_refresh_runs( + runs=[run_info["run_number"]] + ) + + except Exception as e: + print(f"I ate something rotten: {repr(e)}") + continue + + # Revert runs that we opened + for run_number, run_info in runs_to_revert.items(): + if "prev_class" in run_info: + print(f"Reverting run {run_number} to {run_info['prev_class']}") + answer = runregistry.change_run_class( + run_numbers=run_number, new_class=run_info["prev_class"] + ) + if answer[0].status_code != 200: + print(answer[0].text) + if "prev_state" in run_info: + print(f"Reverting run {run_number} from OPEN to {run_info['prev_state']}") + answer = runregistry.move_runs( + run=run_number, + from_="OPEN", + to_=run_info["prev_state"], + ) + if answer and answer.status_code != 200: + print(answer.text) + + print("Done.") + + +if __name__ == "__main__": + main()