diff --git a/copernicusmarine/download_functions/download_original_files.py b/copernicusmarine/download_functions/download_original_files.py index b6d7e29c..1720cc6b 100644 --- a/copernicusmarine/download_functions/download_original_files.py +++ b/copernicusmarine/download_functions/download_original_files.py @@ -536,7 +536,7 @@ def _original_files_file_download( endpoint_url: str, bucket: str, file_in: str, file_out: pathlib.Path ) -> pathlib.Path: """ - Download ONE file and return a string of the result + Download ONE file and return the path of the result """ s3_client, s3_resource = get_configured_boto3_session( endpoint_url, @@ -554,9 +554,15 @@ def _original_files_file_download( file_out, ) - os.utime( - file_out, (last_modified_date_epoch, last_modified_date_epoch) - ) + try: + os.utime( + file_out, (last_modified_date_epoch, last_modified_date_epoch) + ) + except PermissionError: + logger.warning( + f"Permission to modify the last modified date " + f"of the file {file_out} is denied." + ) return file_out diff --git a/tests/test_command_line_interface.py b/tests/test_command_line_interface.py index 101735d4..deaee3b9 100644 --- a/tests/test_command_line_interface.py +++ b/tests/test_command_line_interface.py @@ -1925,8 +1925,6 @@ def test_last_modified_date_is_set_with_s3(self, tmp_path): "--force-download", "--filter", "*2022053112000*", - "--force-service", - "original-files", "--output-directory", f"{tmp_path}", "--no-directories", diff --git a/tests/test_python_interface.py b/tests/test_python_interface.py index d4cb3bbc..9b248060 100644 --- a/tests/test_python_interface.py +++ b/tests/test_python_interface.py @@ -2,6 +2,7 @@ import os from datetime import datetime, timedelta from pathlib import Path +from unittest import mock import xarray @@ -39,6 +40,28 @@ def test_get_function(self, tmp_path): assert get_result is not None assert all(map(lambda x: x.exists(), get_result)) + @mock.patch("os.utime", side_effect=PermissionError) + def test_permission_denied_for_modification_date( + self, mock_utime, tmp_path, caplog + ): + get( + dataset_id="METOFFICE-GLO-SST-L4-REP-OBS-SST", + force_download=True, + filter="*2022053112000*", + output_directory=f"{tmp_path}", + no_directories=True, + ) + assert "Permission to modify the last modified date" in caplog.text + assert "is denied" in caplog.text + output_file = Path( + tmp_path, + "20220531120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB_REP-v02.0-fv02.0.nc", + ) + five_minutes_ago = datetime.now() - timedelta(minutes=5) + assert datetime.fromtimestamp(os.path.getmtime(output_file)) > ( + five_minutes_ago + ) + def test_subset_function(self, tmp_path): subset_result = subset( username=os.getenv("COPERNICUSMARINE_SERVICE_USERNAME"),