Skip to content

Commit

Permalink
Fix audience for api client
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingface0 committed Jul 22, 2024
1 parent dcca733 commit 6221abd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runregistry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
98 changes: 98 additions & 0 deletions update_specific_runs.py
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()

0 comments on commit 6221abd

Please sign in to comment.