← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~danilo/launchpad/bzr-export-pofile-gathering into lp:launchpad/devel

 

Данило Шеган has proposed merging lp:~danilo/launchpad/bzr-export-pofile-gathering into lp:launchpad/devel.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)


Factor out gathering of POFiles for translations export to a bzr branch. To simplify the next step, fix for bug 669831.

Tests: bin/test -cvvt translations_to_branch
-- 
https://code.launchpad.net/~danilo/launchpad/bzr-export-pofile-gathering/+merge/41227
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~danilo/launchpad/bzr-export-pofile-gathering into lp:launchpad/devel.
=== modified file 'lib/lp/translations/scripts/tests/test_translations_to_branch.py'
--- lib/lp/translations/scripts/tests/test_translations_to_branch.py	2010-10-06 11:46:51 +0000
+++ lib/lp/translations/scripts/tests/test_translations_to_branch.py	2010-11-18 20:44:31 +0000
@@ -3,6 +3,8 @@
 
 """Acceptance test for the translations-export-to-branch script."""
 
+import datetime
+import pytz
 import re
 from textwrap import dedent
 
@@ -278,6 +280,44 @@
             "Launchpad Translations on behalf of %s" % branch.owner.name,
             committer.getBzrCommitterID())
 
+    def test_findChangedPOFiles(self):
+        # Returns all POFiles changed in a productseries after a certain
+        # date.
+        date_in_the_past = (
+            datetime.datetime.now(pytz.UTC) - datetime.timedelta(1))
+        pofile = self.factory.makePOFile()
+
+        exporter = ExportTranslationsToBranch(test_args=[])
+        self.assertEquals(
+            [pofile],
+            list(exporter._findChangedPOFiles(
+                pofile.potemplate.productseries,
+                changed_since=date_in_the_past)))
+
+    def test_findChangedPOFiles_all(self):
+        # If changed_since date is passed in as None, all POFiles are
+        # returned.
+        pofile = self.factory.makePOFile()
+        exporter = ExportTranslationsToBranch(test_args=[])
+        self.assertEquals(
+            [pofile],
+            list(exporter._findChangedPOFiles(
+                pofile.potemplate.productseries, changed_since=None)))
+
+    def test_findChangedPOFiles_unchanged(self):
+        # If a POFile has been changed before changed_since date,
+        # it is not returned.
+        pofile = self.factory.makePOFile()
+        date_in_the_future = (
+            datetime.datetime.now(pytz.UTC) + datetime.timedelta(1))
+
+        exporter = ExportTranslationsToBranch(test_args=[])
+        self.assertEquals(
+            [],
+            list(exporter._findChangedPOFiles(
+                pofile.potemplate.productseries,
+                date_in_the_future)))
+
 
 class TestExportToStackedBranch(TestCaseWithFactory):
     """Test workaround for bzr bug 375013."""

=== modified file 'lib/lp/translations/scripts/translations_to_branch.py'
--- lib/lp/translations/scripts/translations_to_branch.py	2010-10-02 11:41:43 +0000
+++ lib/lp/translations/scripts/translations_to_branch.py	2010-11-18 20:44:31 +0000
@@ -163,6 +163,21 @@
 
         return None
 
+    def _findChangedPOFiles(self, source, changed_since):
+        """Return an iterator of POFiles changed since `changed_since`.
+
+        :param source: a `ProductSeries`.
+        :param changed_since: a datetime object.
+        """
+        subset = getUtility(IPOTemplateSet).getSubset(
+            productseries=source, iscurrent=True)
+        for template in subset:
+            for pofile in template.pofiles:
+                if (changed_since is None or
+                    pofile.date_changed > changed_since):
+                    yield pofile
+
+
     def _exportToBranch(self, source):
         """Export translations for source into source.translations_branch.
 
@@ -195,37 +210,28 @@
         change_count = 0
 
         try:
-            subset = getUtility(IPOTemplateSet).getSubset(
-                productseries=source, iscurrent=True)
-            for template in subset:
-                base_path = os.path.dirname(template.path)
-
-                for pofile in template.pofiles:
-                    has_changed = (
-                        changed_since is None or
-                        pofile.date_changed > changed_since)
-                    if not has_changed:
-                        continue
-
-                    language_code = pofile.getFullLanguageCode()
-                    self.logger.debug("Exporting %s." % language_code)
-
-                    pofile_path = os.path.join(
-                        base_path, language_code + '.po')
-                    pofile_contents = pofile.export()
-
-                    committer.writeFile(pofile_path, pofile_contents)
-                    change_count += 1
-
-                    # We're not actually writing any changes to the
-                    # database, but it's not polite to stay in one
-                    # transaction for too long.
-                    if self.txn:
-                        self.txn.commit()
-
-                    # We're done with this POFile.  Don't bother caching
-                    # anything about it any longer.
-                    template.clearPOFileCache()
+            for pofile in self._findChangedPOFiles(source, changed_since):
+                base_path = os.path.dirname(pofile.potemplate.path)
+
+                language_code = pofile.getFullLanguageCode()
+                self.logger.debug("Exporting %s." % language_code)
+
+                pofile_path = os.path.join(
+                    base_path, language_code + '.po')
+                pofile_contents = pofile.export()
+
+                committer.writeFile(pofile_path, pofile_contents)
+                change_count += 1
+
+                # We're not actually writing any changes to the
+                # database, but it's not polite to stay in one
+                # transaction for too long.
+                if self.txn:
+                    self.txn.commit()
+
+                # We're done with this POFile.  Don't bother caching
+                # anything about it any longer.
+                pofile.potemplate.clearPOFileCache()
 
             if change_count > 0:
                 self.logger.debug("Writing to branch.")


Follow ups