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

Proposed by Данило Шеган on 2010-11-18
Status: Merged
Approved by: Gavin Panella on 2010-11-18
Approved revision: no longer in the source branch.
Merged at revision: 11946
Proposed branch: lp:~danilo/launchpad/bzr-export-pofile-gathering
Merge into: lp:launchpad
Diff against target: 143 lines (+77/-31)
2 files modified
lib/lp/translations/scripts/tests/test_translations_to_branch.py (+40/-0)
lib/lp/translations/scripts/translations_to_branch.py (+37/-31)
To merge this branch: bzr merge lp:~danilo/launchpad/bzr-export-pofile-gathering
Reviewer Review Type Date Requested Status
Gavin Panella (community) 2010-11-18 Approve on 2010-11-18
Review via email: mp+41227@code.launchpad.net

Commit Message

[r=allenap][ui=none][no-qa] Factor out gathering of POFiles for translations export to bzr branches.

Description of the Change

Factor out gathering of POFiles for translations export to a bzr branch. Other than the pofile gathering logic, loop has been mostly unindented and no code has changed there.

Reason is to simplify the next step, fix for bug 669831.

Tests: bin/test -cvvt translations_to_branch

To post a comment you must log in.
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/translations/scripts/tests/test_translations_to_branch.py'
2--- lib/lp/translations/scripts/tests/test_translations_to_branch.py 2010-10-06 11:46:51 +0000
3+++ lib/lp/translations/scripts/tests/test_translations_to_branch.py 2010-11-18 20:45:10 +0000
4@@ -3,6 +3,8 @@
5
6 """Acceptance test for the translations-export-to-branch script."""
7
8+import datetime
9+import pytz
10 import re
11 from textwrap import dedent
12
13@@ -278,6 +280,44 @@
14 "Launchpad Translations on behalf of %s" % branch.owner.name,
15 committer.getBzrCommitterID())
16
17+ def test_findChangedPOFiles(self):
18+ # Returns all POFiles changed in a productseries after a certain
19+ # date.
20+ date_in_the_past = (
21+ datetime.datetime.now(pytz.UTC) - datetime.timedelta(1))
22+ pofile = self.factory.makePOFile()
23+
24+ exporter = ExportTranslationsToBranch(test_args=[])
25+ self.assertEquals(
26+ [pofile],
27+ list(exporter._findChangedPOFiles(
28+ pofile.potemplate.productseries,
29+ changed_since=date_in_the_past)))
30+
31+ def test_findChangedPOFiles_all(self):
32+ # If changed_since date is passed in as None, all POFiles are
33+ # returned.
34+ pofile = self.factory.makePOFile()
35+ exporter = ExportTranslationsToBranch(test_args=[])
36+ self.assertEquals(
37+ [pofile],
38+ list(exporter._findChangedPOFiles(
39+ pofile.potemplate.productseries, changed_since=None)))
40+
41+ def test_findChangedPOFiles_unchanged(self):
42+ # If a POFile has been changed before changed_since date,
43+ # it is not returned.
44+ pofile = self.factory.makePOFile()
45+ date_in_the_future = (
46+ datetime.datetime.now(pytz.UTC) + datetime.timedelta(1))
47+
48+ exporter = ExportTranslationsToBranch(test_args=[])
49+ self.assertEquals(
50+ [],
51+ list(exporter._findChangedPOFiles(
52+ pofile.potemplate.productseries,
53+ date_in_the_future)))
54+
55
56 class TestExportToStackedBranch(TestCaseWithFactory):
57 """Test workaround for bzr bug 375013."""
58
59=== modified file 'lib/lp/translations/scripts/translations_to_branch.py'
60--- lib/lp/translations/scripts/translations_to_branch.py 2010-10-02 11:41:43 +0000
61+++ lib/lp/translations/scripts/translations_to_branch.py 2010-11-18 20:45:10 +0000
62@@ -163,6 +163,21 @@
63
64 return None
65
66+ def _findChangedPOFiles(self, source, changed_since):
67+ """Return an iterator of POFiles changed since `changed_since`.
68+
69+ :param source: a `ProductSeries`.
70+ :param changed_since: a datetime object.
71+ """
72+ subset = getUtility(IPOTemplateSet).getSubset(
73+ productseries=source, iscurrent=True)
74+ for template in subset:
75+ for pofile in template.pofiles:
76+ if (changed_since is None or
77+ pofile.date_changed > changed_since):
78+ yield pofile
79+
80+
81 def _exportToBranch(self, source):
82 """Export translations for source into source.translations_branch.
83
84@@ -195,37 +210,28 @@
85 change_count = 0
86
87 try:
88- subset = getUtility(IPOTemplateSet).getSubset(
89- productseries=source, iscurrent=True)
90- for template in subset:
91- base_path = os.path.dirname(template.path)
92-
93- for pofile in template.pofiles:
94- has_changed = (
95- changed_since is None or
96- pofile.date_changed > changed_since)
97- if not has_changed:
98- continue
99-
100- language_code = pofile.getFullLanguageCode()
101- self.logger.debug("Exporting %s." % language_code)
102-
103- pofile_path = os.path.join(
104- base_path, language_code + '.po')
105- pofile_contents = pofile.export()
106-
107- committer.writeFile(pofile_path, pofile_contents)
108- change_count += 1
109-
110- # We're not actually writing any changes to the
111- # database, but it's not polite to stay in one
112- # transaction for too long.
113- if self.txn:
114- self.txn.commit()
115-
116- # We're done with this POFile. Don't bother caching
117- # anything about it any longer.
118- template.clearPOFileCache()
119+ for pofile in self._findChangedPOFiles(source, changed_since):
120+ base_path = os.path.dirname(pofile.potemplate.path)
121+
122+ language_code = pofile.getFullLanguageCode()
123+ self.logger.debug("Exporting %s." % language_code)
124+
125+ pofile_path = os.path.join(
126+ base_path, language_code + '.po')
127+ pofile_contents = pofile.export()
128+
129+ committer.writeFile(pofile_path, pofile_contents)
130+ change_count += 1
131+
132+ # We're not actually writing any changes to the
133+ # database, but it's not polite to stay in one
134+ # transaction for too long.
135+ if self.txn:
136+ self.txn.commit()
137+
138+ # We're done with this POFile. Don't bother caching
139+ # anything about it any longer.
140+ pofile.potemplate.clearPOFileCache()
141
142 if change_count > 0:
143 self.logger.debug("Writing to branch.")