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

Use timezone aware timestamps for file modification #1992

Merged
merged 3 commits into from
Sep 25, 2024
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
15 changes: 7 additions & 8 deletions src/MCPClient/lib/clientScripts/store_file_modification_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@
import django

django.setup()
# archivematicaCommon
from custom_handlers import get_script_logger
from django.db import transaction

# dashboard
from django.utils.timezone import get_current_timezone
from main import models

logger = get_script_logger("archivematica.mcp.client.storeFileModificationDates")


def get_modification_date(file_path):
def get_modification_date(file_path, timezone):
mod_time = os.path.getmtime(file_path)
return datetime.datetime.utcfromtimestamp(int(mod_time))
return datetime.datetime.fromtimestamp(int(mod_time), tz=timezone)


def main(transfer_uuid, shared_directory_path):
def main(transfer_uuid, shared_directory_path, timezone):
transfer = models.Transfer.objects.get(uuid=transfer_uuid)

files = models.File.objects.filter(transfer=transfer)
Expand All @@ -57,18 +55,19 @@
file_path = file_path_relative_to_shared_directory.replace(
"%sharedPath%", shared_directory_path, 1
)
transfer_file.modificationtime = get_modification_date(file_path)
transfer_file.modificationtime = get_modification_date(file_path, timezone)
transfer_file.save()
mods_stored += 1

logger.info("Stored modification dates of %d files.", mods_stored)


def call(jobs):
timezone = get_current_timezone()

Check warning on line 66 in src/MCPClient/lib/clientScripts/store_file_modification_dates.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/store_file_modification_dates.py#L66

Added line #L66 was not covered by tests
with transaction.atomic():
for job in jobs:
with job.JobContext(logger=logger):
transfer_uuid = job.args[1]
shared_directory_path = job.args[2]
main(transfer_uuid, shared_directory_path)
main(transfer_uuid, shared_directory_path, timezone)

Check warning on line 72 in src/MCPClient/lib/clientScripts/store_file_modification_dates.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/store_file_modification_dates.py#L72

Added line #L72 was not covered by tests
job.set_status(0)
18 changes: 12 additions & 6 deletions tests/MCPClient/test_store_file_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import store_file_modification_dates
from django.test import TestCase
from django.test import override_settings
from django.utils.timezone import get_current_timezone
from main import models

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -25,6 +27,7 @@ def tearDown(self):
)
shutil.rmtree(transfer_path)

@override_settings(TIME_ZONE="US/Eastern")
def test_store_file_modification_dates(self):
"""Test store_file_modification_dates.

Expand All @@ -47,41 +50,44 @@ def test_store_file_modification_dates(self):
os.makedirs(dirname)
with open(path, "wb") as f:
f.write(path.encode("utf8"))
os.utime(path, (1339485682, 1339485682))
os.utime(path, (1049597970, 1049597970))

# Store file modification dates
store_file_modification_dates.main(self.transfer_uuid, self.temp_dir + "/")
store_file_modification_dates.main(
self.transfer_uuid, self.temp_dir + "/", get_current_timezone()
)

# Assert files have expected modification times
expected_time = "2003-04-06 02:59:30+00:00"
assert (
str(
models.File.objects.get(
pk="47813453-6872-442b-9d65-6515be3c5aa1"
).modificationtime
)
== "2012-06-12 07:21:22+00:00"
== expected_time
)
assert (
str(
models.File.objects.get(
pk="60e5c61b-14ef-4e92-89ec-9b9201e68adb"
).modificationtime
)
== "2012-06-12 07:21:22+00:00"
== expected_time
)
assert (
str(
models.File.objects.get(
pk="791e07ea-ad44-4315-b55b-44ec771e95cf"
).modificationtime
)
== "2012-06-12 07:21:22+00:00"
== expected_time
)
assert (
str(
models.File.objects.get(
pk="8a1f0b59-cf94-47ef-8078-647b77c8a147"
).modificationtime
)
== "2012-06-12 07:21:22+00:00"
== expected_time
)