]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Add flag to ignore POT-Creation-Date for updates (#999)
authorJoe Portela <jportela14@gmail.com>
Thu, 27 Jul 2023 11:24:25 +0000 (07:24 -0400)
committerGitHub <noreply@github.com>
Thu, 27 Jul 2023 11:24:25 +0000 (14:24 +0300)
babel/messages/catalog.py
babel/messages/frontend.py
tests/messages/test_catalog.py
tests/messages/test_frontend.py

index fe9f45bb397902c1e7f5ff106d1bd06c3f1d7549..8faf046d3c49128b34de13c974a8740090c38981 100644 (file)
@@ -769,6 +769,7 @@ class Catalog:
         no_fuzzy_matching: bool = False,
         update_header_comment: bool = False,
         keep_user_comments: bool = True,
+        update_creation_date: bool = True,
     ) -> None:
         """Update the catalog based on the given template catalog.
 
@@ -907,7 +908,8 @@ class Catalog:
 
         # Make updated catalog's POT-Creation-Date equal to the template
         # used to update the catalog
-        self.creation_date = template.creation_date
+        if update_creation_date:
+            self.creation_date = template.creation_date
 
     def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str:
         """Converts a message key to a string suitable for fuzzy matching."""
index af7e1d11da518827463c6912a1fbbdba886c3181..6ed1fe5bde73c90ac10e6aeaab50d1b440c18d2e 100644 (file)
@@ -727,11 +727,13 @@ class update_catalog(Command):
          'don\'t update the catalog, just return the status. Return code 0 '
          'means nothing would change. Return code 1 means that the catalog '
          'would be updated'),
+        ('ignore-pot-creation-date=', None,
+         'ignore changes to POT-Creation-Date when updating or checking'),
     ]
     boolean_options = [
         'omit-header', 'no-wrap', 'ignore-obsolete', 'init-missing',
         'no-fuzzy-matching', 'previous', 'update-header-comment',
-        'check',
+        'check', 'ignore-pot-creation-date',
     ]
 
     def initialize_options(self):
@@ -749,6 +751,7 @@ class update_catalog(Command):
         self.update_header_comment = False
         self.previous = False
         self.check = False
+        self.ignore_pot_creation_date = False
 
     def finalize_options(self):
         if not self.input_file:
@@ -837,7 +840,8 @@ class update_catalog(Command):
 
             catalog.update(
                 template, self.no_fuzzy_matching,
-                update_header_comment=self.update_header_comment
+                update_header_comment=self.update_header_comment,
+                update_creation_date=not self.ignore_pot_creation_date,
             )
 
             tmpname = os.path.join(os.path.dirname(filename),
index 8f5b4974f2452063037390815f3a015dbf1f117b..4e133304f6ca0009b58de820ea7e544770744187 100644 (file)
@@ -273,6 +273,17 @@ including versions of Lorem Ipsum."
         localized_catalog.update(template)
         assert template.creation_date == localized_catalog.creation_date
 
+    def test_update_po_ignores_pot_creation_date(self):
+        template = catalog.Catalog()
+        localized_catalog = copy.deepcopy(template)
+        localized_catalog.locale = 'de_DE'
+        assert template.mime_headers != localized_catalog.mime_headers
+        assert template.creation_date == localized_catalog.creation_date
+        template.creation_date = datetime.datetime.now() - \
+            datetime.timedelta(minutes=5)
+        localized_catalog.update(template, update_creation_date=False)
+        assert template.creation_date != localized_catalog.creation_date
+
     def test_update_po_keeps_po_revision_date(self):
         template = catalog.Catalog()
         localized_catalog = copy.deepcopy(template)
index 8e1e670ca7e99f807b81f17fa308f26a61483423..821738d23d5edfc2d6eee729a68fc2b0ab4bff3c 100644 (file)
@@ -16,7 +16,7 @@ import shutil
 import sys
 import time
 import unittest
-from datetime import datetime
+from datetime import datetime, timedelta
 from io import StringIO
 
 import pytest
@@ -1179,6 +1179,60 @@ msgstr[2] ""
             catalog = read_po(infp)
             assert len(catalog) == 4  # Catalog was updated
 
+    def test_update_pot_creation_date(self):
+        template = Catalog()
+        template.add("1")
+        template.add("2")
+        template.add("3")
+        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+        with open(tmpl_file, "wb") as outfp:
+            write_po(outfp, template)
+        po_file = os.path.join(i18n_dir, 'temp1.po')
+        self.cli.run(sys.argv + ['init',
+                                 '-l', 'fi',
+                                 '-o', po_file,
+                                 '-i', tmpl_file
+                                 ])
+        with open(po_file) as infp:
+            catalog = read_po(infp)
+            assert len(catalog) == 3
+        original_catalog_creation_date = catalog.creation_date
+
+        # Update the template creation date
+        template.creation_date -= timedelta(minutes=3)
+        with open(tmpl_file, "wb") as outfp:
+            write_po(outfp, template)
+
+        self.cli.run(sys.argv + ['update',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file])
+
+        with open(po_file) as infp:
+            catalog = read_po(infp)
+            # We didn't ignore the creation date, so expect a diff
+            assert catalog.creation_date != original_catalog_creation_date
+
+        # Reset the "original"
+        original_catalog_creation_date = catalog.creation_date
+
+        # Update the template creation date again
+        # This time, pass the ignore flag and expect the times are different
+        template.creation_date -= timedelta(minutes=5)
+        with open(tmpl_file, "wb") as outfp:
+            write_po(outfp, template)
+
+        self.cli.run(sys.argv + ['update',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file,
+                                 '--ignore-pot-creation-date'])
+
+        with open(po_file) as infp:
+            catalog = read_po(infp)
+            # We ignored creation date, so it should not have changed
+            assert catalog.creation_date == original_catalog_creation_date
+
     def test_check(self):
         template = Catalog()
         template.add("1")
@@ -1237,6 +1291,54 @@ msgstr[2] ""
                                      '-o', po_file,
                                      '-i', tmpl_file])
 
+    def test_check_pot_creation_date(self):
+        template = Catalog()
+        template.add("1")
+        template.add("2")
+        template.add("3")
+        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+        with open(tmpl_file, "wb") as outfp:
+            write_po(outfp, template)
+        po_file = os.path.join(i18n_dir, 'temp1.po')
+        self.cli.run(sys.argv + ['init',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file
+                                 ])
+
+        # Update the catalog file
+        self.cli.run(sys.argv + ['update',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file])
+
+        # Run a check without introducing any changes to the template
+        self.cli.run(sys.argv + ['update',
+                                 '--check',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file])
+
+        # Run a check after changing the template creation date
+        template.creation_date = datetime.now() - timedelta(minutes=5)
+        with open(tmpl_file, "wb") as outfp:
+            write_po(outfp, template)
+
+        # Should fail without --ignore-pot-creation-date flag
+        with pytest.raises(BaseError):
+            self.cli.run(sys.argv + ['update',
+                                     '--check',
+                                     '-l', 'fi_FI',
+                                     '-o', po_file,
+                                     '-i', tmpl_file])
+        # Should pass with --ignore-pot-creation-date flag
+        self.cli.run(sys.argv + ['update',
+                                 '--check',
+                                 '-l', 'fi_FI',
+                                 '-o', po_file,
+                                 '-i', tmpl_file,
+                                 '--ignore-pot-creation-date'])
+
     def test_update_init_missing(self):
         template = Catalog()
         template.add("1")