From f8925c3f0c853e95c901f788b38a4c022d6c61e6 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Wed, 11 Oct 2023 14:06:27 +0200 Subject: [PATCH 1/4] edit --- dsp_permissions_scripts/utils/doap_set.py | 20 +++++++++++++------- dsp_permissions_scripts/utils/oap.py | 23 +++++++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/dsp_permissions_scripts/utils/doap_set.py b/dsp_permissions_scripts/utils/doap_set.py index c946cc13..779d0e4c 100644 --- a/dsp_permissions_scripts/utils/doap_set.py +++ b/dsp_permissions_scripts/utils/doap_set.py @@ -1,3 +1,4 @@ +import warnings from typing import Literal from urllib.parse import quote_plus @@ -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: # pylint: disable=bare-except + 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}") + print(f"{get_timestamp()}: All DOAPs have been updated.") diff --git a/dsp_permissions_scripts/utils/oap.py b/dsp_permissions_scripts/utils/oap.py index 070d6c9a..90527db6 100644 --- a/dsp_permissions_scripts/utils/oap.py +++ b/dsp_permissions_scripts/utils/oap.py @@ -1,4 +1,5 @@ import json +import warnings from typing import Any from urllib.parse import quote_plus @@ -187,15 +188,25 @@ def apply_updated_oaps_on_server( """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 *******") + num_of_errors = 0 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) print(f"{get_timestamp()}: {msg}") - _update_permissions_for_resource_and_values( - resource_iri=resource_oap.object_iri, - scope=resource_oap.scope, - host=host, - token=token, - ) + try: + _update_permissions_for_resource_and_values( + resource_iri=resource_oap.object_iri, + scope=resource_oap.scope, + host=host, + token=token, + ) + except: # pylint: disable=bare-except + 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}") + num_of_errors += 1 logger.info(f"Updated permissions of resource {resource_oap.object_iri} and its values.") + + if num_of_errors: + logger.error(f"ERROR: {num_of_errors} resources could not be updated.") + warnings.warn(f"ERROR: {num_of_errors} resources could not be updated.") From ac96f9aee284b5f58366f61265b8a75b3c655cca Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 12 Oct 2023 08:39:26 +0200 Subject: [PATCH 2/4] except Exception --- dsp_permissions_scripts/utils/doap_set.py | 2 +- dsp_permissions_scripts/utils/oap.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dsp_permissions_scripts/utils/doap_set.py b/dsp_permissions_scripts/utils/doap_set.py index 779d0e4c..26297ddf 100644 --- a/dsp_permissions_scripts/utils/doap_set.py +++ b/dsp_permissions_scripts/utils/doap_set.py @@ -74,7 +74,7 @@ def apply_updated_doaps_on_server( token=token, ) _log_and_print_doap_update(doap=new_doap, state="after") - except: # pylint: disable=bare-except + 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}") diff --git a/dsp_permissions_scripts/utils/oap.py b/dsp_permissions_scripts/utils/oap.py index 90527db6..38045006 100644 --- a/dsp_permissions_scripts/utils/oap.py +++ b/dsp_permissions_scripts/utils/oap.py @@ -201,7 +201,7 @@ def apply_updated_oaps_on_server( host=host, token=token, ) - except: # pylint: disable=bare-except + 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}") num_of_errors += 1 From 1e0c4d4d68eb843627236ff30753f80af4386dd9 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 12 Oct 2023 08:49:50 +0200 Subject: [PATCH 3/4] write failed resources to file --- dsp_permissions_scripts/template.py | 1 + dsp_permissions_scripts/utils/oap.py | 29 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/dsp_permissions_scripts/template.py b/dsp_permissions_scripts/template.py index 37f6f055..9b6066ff 100644 --- a/dsp_permissions_scripts/template.py +++ b/dsp_permissions_scripts/template.py @@ -56,6 +56,7 @@ def update_oaps( resource_oaps=resource_oaps_updated, host=host, token=token, + shortcode=shortcode, ) diff --git a/dsp_permissions_scripts/utils/oap.py b/dsp_permissions_scripts/utils/oap.py index 38045006..3ae28c5d 100644 --- a/dsp_permissions_scripts/utils/oap.py +++ b/dsp_permissions_scripts/utils/oap.py @@ -180,15 +180,27 @@ 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 *******") - num_of_errors = 0 + 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("=====") @@ -204,9 +216,16 @@ def apply_updated_oaps_on_server( 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}") - num_of_errors += 1 + failed_res_iris.append(resource_oap.object_iri) logger.info(f"Updated permissions of resource {resource_oap.object_iri} and its values.") - if num_of_errors: - logger.error(f"ERROR: {num_of_errors} resources could not be updated.") - warnings.warn(f"ERROR: {num_of_errors} resources could not be updated.") + if failed_res_iris: + filename = "FAILED_RESOURCES.txt" + _write_failed_res_iris_to_file( + failed_res_iris=failed_res_iris, + shortcode=shortcode, + host=host, + filename=filename, + ) + 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}.") From ca196a58f79d01f1d32659ecacaf251a8e358119 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 12 Oct 2023 08:52:10 +0200 Subject: [PATCH 4/4] edit --- dsp_permissions_scripts/utils/oap.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dsp_permissions_scripts/utils/oap.py b/dsp_permissions_scripts/utils/oap.py index 3ae28c5d..fe4a6822 100644 --- a/dsp_permissions_scripts/utils/oap.py +++ b/dsp_permissions_scripts/utils/oap.py @@ -203,8 +203,7 @@ def apply_updated_oaps_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}") try: _update_permissions_for_resource_and_values(