-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcca733
commit 6221abd
Showing
2 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |