Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: handle errors during permission updating (prevent from crashing) #37

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def update_oaps(
resource_oaps=resource_oaps_updated,
host=host,
token=token,
shortcode=shortcode,
)


Expand Down
20 changes: 13 additions & 7 deletions dsp_permissions_scripts/utils/doap_set.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Literal
from urllib.parse import quote_plus

Expand Down Expand Up @@ -65,11 +66,16 @@ def apply_updated_doaps_on_server(
print(f"\n{heading}\n{'=' * len(heading)}\n")
for d in doaps:
_log_and_print_doap_update(doap=d, state="before")
new_doap = _update_doap_scope(
doap_iri=d.doap_iri,
scope=d.scope,
host=host,
token=token,
)
_log_and_print_doap_update(doap=new_doap, state="after")
try:
new_doap = _update_doap_scope(
doap_iri=d.doap_iri,
scope=d.scope,
host=host,
token=token,
)
_log_and_print_doap_update(doap=new_doap, state="after")
except Exception: # pylint: disable=broad-exception-caught
logger.error(f"ERROR while updating permissions of resource {d.doap_iri}", exc_info=True)
warnings.warn(f"ERROR while updating permissions of resource {d.doap_iri}")
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved

print(f"{get_timestamp()}: All DOAPs have been updated.")
43 changes: 36 additions & 7 deletions dsp_permissions_scripts/utils/oap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import warnings
from typing import Any
from urllib.parse import quote_plus

Expand Down Expand Up @@ -179,23 +180,51 @@ def _update_permissions_for_resource_and_values(
)


def _write_failed_res_iris_to_file(
failed_res_iris: list[str],
shortcode: str,
host: str,
filename: str,
) -> None:
with open(filename, "w", encoding="utf-8") as f:
f.write(f"Failed to update the OAPs of the following resources in project {shortcode} on host {host}:\n")
f.write("\n".join(failed_res_iris))


def apply_updated_oaps_on_server(
resource_oaps: list[Oap],
host: str,
token: str,
shortcode: str,
) -> None:
"""Applies object access permissions on a DSP server."""
logger.info("******* Applying updated object access permissions on server *******")
print(f"{get_timestamp()}: ******* Applying updated object access permissions on server *******")
failed_res_iris: list[str] = []
for index, resource_oap in enumerate(resource_oaps):
msg = f"Updating permissions of resource {index + 1}/{len(resource_oaps)}: {resource_oap.object_iri}..."
logger.info("=====")
logger.info(msg)
logger.info(f"=====\n{msg}")
print(f"{get_timestamp()}: {msg}")
_update_permissions_for_resource_and_values(
resource_iri=resource_oap.object_iri,
scope=resource_oap.scope,
try:
_update_permissions_for_resource_and_values(
resource_iri=resource_oap.object_iri,
scope=resource_oap.scope,
host=host,
token=token,
)
except Exception: # pylint: disable=broad-exception-caught
logger.error(f"ERROR while updating permissions of resource {resource_oap.object_iri}", exc_info=True)
warnings.warn(f"ERROR while updating permissions of resource {resource_oap.object_iri}")
failed_res_iris.append(resource_oap.object_iri)
logger.info(f"Updated permissions of resource {resource_oap.object_iri} and its values.")

if failed_res_iris:
filename = "FAILED_RESOURCES.txt"
_write_failed_res_iris_to_file(
failed_res_iris=failed_res_iris,
shortcode=shortcode,
host=host,
token=token,
filename=filename,
)
logger.info(f"Updated permissions of resource {resource_oap.object_iri} and its values.")
logger.error(f"ERROR: {len(failed_res_iris)} resources could not be updated. They were written to {filename}.")
warnings.warn(f"ERROR: {len(failed_res_iris)} resources could not be updated. They were written to {filename}.")