From 17f1b6a34c9af454c190eb2f3bc9674cfcdcab85 Mon Sep 17 00:00:00 2001 From: "Douglas Cerna (Soy Douglas)" Date: Wed, 18 Sep 2024 16:29:58 +0000 Subject: [PATCH 1/3] Extract expected time into separate variable --- tests/MCPClient/test_store_file_modification.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/MCPClient/test_store_file_modification.py b/tests/MCPClient/test_store_file_modification.py index bb27e9cc3d..0db4a18605 100644 --- a/tests/MCPClient/test_store_file_modification.py +++ b/tests/MCPClient/test_store_file_modification.py @@ -53,13 +53,14 @@ def test_store_file_modification_dates(self): store_file_modification_dates.main(self.transfer_uuid, self.temp_dir + "/") # Assert files have expected modification times + expected_time = "2012-06-12 07:21:22+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( @@ -67,7 +68,7 @@ def test_store_file_modification_dates(self): pk="60e5c61b-14ef-4e92-89ec-9b9201e68adb" ).modificationtime ) - == "2012-06-12 07:21:22+00:00" + == expected_time ) assert ( str( @@ -75,7 +76,7 @@ def test_store_file_modification_dates(self): pk="791e07ea-ad44-4315-b55b-44ec771e95cf" ).modificationtime ) - == "2012-06-12 07:21:22+00:00" + == expected_time ) assert ( str( @@ -83,5 +84,5 @@ def test_store_file_modification_dates(self): pk="8a1f0b59-cf94-47ef-8078-647b77c8a147" ).modificationtime ) - == "2012-06-12 07:21:22+00:00" + == expected_time ) From 7d8a3d8866464132bd2b9a670439b9fccb246e66 Mon Sep 17 00:00:00 2001 From: "Douglas Cerna (Soy Douglas)" Date: Wed, 18 Sep 2024 16:33:00 +0000 Subject: [PATCH 2/3] Update test to use a non UTC time zone This test case represents a time zone and file modification time combination that makes the job to fail in Archivematica 1.15.x. In this commit the test fails because the expected time is now time zone aware but the job has not been updated to use the current time zone yet. ```console FAILED test_store_file_modification.py::TestStoreFileModification::test_store_file_modification_dates - AssertionError: assert '2003-04-06 07:59:30+00:00' == '2003-04-06 02:59:30+00:00' - 2003-04-06 02:59:30+00:00 ? ^ + 2003-04-06 07:59:30+00:00 ? ^ ``` --- tests/MCPClient/test_store_file_modification.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/MCPClient/test_store_file_modification.py b/tests/MCPClient/test_store_file_modification.py index 0db4a18605..4f4a5f8a77 100644 --- a/tests/MCPClient/test_store_file_modification.py +++ b/tests/MCPClient/test_store_file_modification.py @@ -4,6 +4,7 @@ import store_file_modification_dates from django.test import TestCase +from django.test import override_settings from main import models THIS_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -25,6 +26,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. @@ -47,13 +49,13 @@ 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 + "/") # Assert files have expected modification times - expected_time = "2012-06-12 07:21:22+00:00" + expected_time = "2003-04-06 02:59:30+00:00" assert ( str( models.File.objects.get( From 9282a5a1ebac1a6e16fd830c14fe774db19216c2 Mon Sep 17 00:00:00 2001 From: "Douglas Cerna (Soy Douglas)" Date: Wed, 18 Sep 2024 16:41:00 +0000 Subject: [PATCH 3/3] Update job to create timezone aware timestamps --- .../store_file_modification_dates.py | 15 +++++++-------- tests/MCPClient/test_store_file_modification.py | 5 ++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/MCPClient/lib/clientScripts/store_file_modification_dates.py b/src/MCPClient/lib/clientScripts/store_file_modification_dates.py index 581b44b2af..0dab5ad6d9 100755 --- a/src/MCPClient/lib/clientScripts/store_file_modification_dates.py +++ b/src/MCPClient/lib/clientScripts/store_file_modification_dates.py @@ -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) @@ -57,7 +55,7 @@ def main(transfer_uuid, shared_directory_path): 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 @@ -65,10 +63,11 @@ def main(transfer_uuid, shared_directory_path): def call(jobs): + timezone = get_current_timezone() 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) job.set_status(0) diff --git a/tests/MCPClient/test_store_file_modification.py b/tests/MCPClient/test_store_file_modification.py index 4f4a5f8a77..f6639e7f14 100644 --- a/tests/MCPClient/test_store_file_modification.py +++ b/tests/MCPClient/test_store_file_modification.py @@ -5,6 +5,7 @@ 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__)) @@ -52,7 +53,9 @@ def test_store_file_modification_dates(self): 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"