Skip to content

Commit

Permalink
fix: if not permission modifie last updated date on file continue (#98)
Browse files Browse the repository at this point in the history
If file modification date cannot be changed, save the file anyway but with the downloaded date instead
  • Loading branch information
renaudjester authored and uriii3 committed Jul 31, 2024
1 parent 36291b6 commit 183cfc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
14 changes: 10 additions & 4 deletions copernicusmarine/download_functions/download_original_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
2 changes: 0 additions & 2 deletions tests/test_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from datetime import datetime, timedelta
from pathlib import Path
from unittest import mock

import xarray

Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 183cfc8

Please sign in to comment.