]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/tests/testpo.py
Updating POT files
[working/Evergreen.git] / build / i18n / tests / testpo.py
1 #!/usr/bin/env python
2
3 import filecmp
4 import glob
5 import os
6 import re
7 import shutil
8 import subprocess
9 import sys
10 import unittest
11
12 class TestPOFramework(unittest.TestCase):
13
14     po_sources = ('../../Open-ILS/web/opac/locale/en-US/*.dtd',
15         '../../Open-ILS/xul/staff_client/chrome/locale/en-US/*.properties',
16         '../../Open-ILS/examples/fm_IDL.xml',
17         '../../Open-ILS/src/extras/ils_events.xml',
18         '../../Open-ILS/src/sql/Pg/950.data.seed-values.sql')
19
20     po_tmp_files = ('tests/tmp/po/en-US/test.properties.pot', 'tests/tmp/po/ll-LL/temp.properties.po')
21     pot_dir = 'tests/tmp/po/en-US'
22     po_in_dir = 'POINDIR=tests/tmp/po'
23     po_out_dir = 'POOUTDIR=tests/tmp/po'
24     locale = 'll-LL'
25     locale_dir = 'tests/tmp/po/ll-LL'
26     project_dir = 'tests/tmp/locale'
27     po_tmp_dirs = (project_dir + '/' + locale, project_dir, locale_dir, pot_dir, project_dir, 'tests/tmp')
28     newdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
29
30     def setUp(self):
31         os.chdir(self.newdir)
32         self.tearDown()
33         devnull = open('/dev/null', 'w')
34         os.mkdir('tests/tmp')
35         os.mkdir(self.project_dir)
36         subprocess.Popen(('cp', '-r', 'po', 'tests/tmp'), 0, None, None, devnull, devnull).wait()
37         subprocess.Popen(('make', 'LOCALE=ll-LL', 'POTSRC=tests/tmp/po/en-US', self.po_in_dir, self.po_out_dir, 'newpot'), 0, None, None, devnull, devnull).wait()
38         subprocess.Popen(('make', 'LOCALE=ll-LL', 'POTSRC=tests/tmp/po/en-US', self.po_in_dir, self.po_out_dir, 'newpo'), 0, None, None, devnull, devnull).wait()
39         subprocess.Popen(('make', 'LOCALE=ll-LL', 'PROJECT=tests/tmp/locale', self.po_in_dir, 'newproject'), 0, None, None, devnull, devnull).wait()
40         devnull.close()
41
42     def tearDown(self):
43         pass
44 #        devnull = open('/dev/null', 'w')
45 #        subprocess.Popen(('cp', '-r', 'tests/tmp', 'done'), 0, None, None, devnull, devnull).wait()
46 #        for root, dirs, files in os.walk(os.path.join(self.newdir, 'tests/tmp'), topdown=False):
47 #            for name in files:
48 #                os.remove(os.path.join(root, name))
49 #            for name in dirs:
50 #                os.rmdir(os.path.join(root, name))
51 #        if os.access(os.path.join(self.newdir, 'tests/tmp'), os.F_OK):
52 #            os.rmdir(os.path.join(self.newdir, 'tests/tmp'))
53
54     def testnewpofiles(self):
55         # Create a brand new set of PO files from our en-US project files.
56         # Compare the files generated in the po/ll-LL directory with
57         # the number expected by a manual count of our known sources.
58         po_files = []
59         for po_dir in self.po_sources:
60             for path in glob.glob(po_dir):
61                 po_files.append(os.path.basename(path) + '.po')
62         po_files.sort()
63         new_pofiles = os.listdir(self.locale_dir)
64         new_pofiles.sort()
65         self.assertEqual(len(po_files), len(new_pofiles))
66
67     def testnewprojectfiles(self):
68         # Create a brand new set of project files from PO files.
69         # Compare the files created with a manual count of our known sources.
70         moz_files = []
71         for po_dir in self.po_sources:
72             for path in glob.glob(po_dir):
73                 moz_files.append(os.path.basename(path))
74         moz_files.sort()
75         new_mozfiles = os.listdir(self.locale_dir)
76         new_mozfiles.sort()
77         self.assertEqual(len(moz_files), len(new_mozfiles))
78
79     def testtranslatedfile(self):
80         # "Translate" strings in a PO file, then generate the project
81         # files to ensure that the translated string appears in the output.
82
83         # Create the "translated" PO file
84         commonpo = os.path.join(self.locale_dir, 'common.properties.po')
85         testpo = os.path.join(self.locale_dir, 'test.properties.po')
86         commonfile = open(commonpo)
87         testfile = open(testpo, 'w')
88         for line in commonfile:
89             line = re.sub(r'^msgstr ""', r'msgstr "abcdefg"', line)
90             testfile.write(line)
91         commonfile.close()
92         testfile.close()
93         os.remove(commonpo)
94         os.rename(testpo, commonpo)
95
96         # Create the "translated" properties file
97         commonprops = os.path.join(self.project_dir, self.locale, 'common.properties')
98         testprops = os.path.join(self.locale_dir, 'test.properties')
99         commonfile = open(commonprops)
100         testfile = open(testprops, 'w')
101         for line in commonfile:
102             line = re.sub(r'^(.*?)=.*?$', r'\1=abcdefg', line)
103             testfile.write(line)
104         commonfile.close()
105         testfile.close()
106
107         # Regenerate the project files to get the translated strings in place
108         devnull = open('/dev/null', 'w')
109         subprocess.Popen(('make', 'LOCALE=ll-LL', self.po_in_dir, self.po_out_dir, 'updateproject'), 0, None, None, devnull, devnull).wait()
110
111         self.assertEqual(filecmp.cmp(commonprops, testprops), 1)
112
113     def testupdatepo(self):
114         # Add strings to a POT file, then ensure that the updated PO files
115         # include the new strings
116
117         # Create the "template" PO file
118         commonpo = os.path.join(self.locale_dir, 'common.properties.po')
119         testpo = os.path.join(self.locale_dir, 'test.properties.po')
120         commonfile = open(commonpo)
121         testfile = open(testpo, 'w')
122         for line in commonfile:
123             line = re.sub(r'common.properties$', r'test.properties', line)
124             testfile.write(line)
125         commonfile.close()
126         testfile.close()
127
128         # Create the test POT file
129         commonpot = os.path.join(self.pot_dir, 'common.properties.pot')
130         testpot = os.path.join(self.pot_dir, 'test.properties.pot')
131         commonfile = open(commonpot)
132         testfile = open(testpot, 'w')
133         for line in commonfile:
134             line = re.sub(r'common.properties$', r'test.properties', line)
135             testfile.write(line)
136         commonfile.close()
137         testfile.write("\n#: common.testupdatepo")
138         testfile.write('\nmsgid "TESTUPDATEPO"')
139         testfile.write('\nmsgstr ""')
140         testfile.close()
141
142         # Update the PO files to get the translated strings in place
143         devnull = open('/dev/null', 'w')
144         subprocess.Popen(('make', 'LOCALE=ll-LL', self.po_in_dir, self.po_out_dir, 'updatepo'), 0, None, None, devnull, devnull).wait()
145
146         commonprops = os.path.join(self.locale_dir, 'common.properties.po')
147         tempprops = os.path.join(self.locale_dir, 'temp.properties.po')
148         testprops = os.path.join(self.locale_dir, 'test.properties.po')
149
150         # Munge the common file to make it what we expect it to be
151         commonfile = open(commonprops, 'a+')
152         commonfile.write("\n#: common.testupdatepo")
153         commonfile.write('\nmsgid "TESTUPDATEPO"')
154         commonfile.write('\nmsgstr ""')
155         commonfile.close()
156
157         shutil.copyfile(commonprops, tempprops)
158         commonfile = open(commonprops, 'w')
159         tempfile = open(testpot)
160         for line in tempfile:
161             line = re.sub(r'common.properties$', r'test.properties', line)
162             line = re.sub(r'^"Project-Id-Version: .*"$', r'"Project-Id-Version: PACKAGE VERSION\\n"', line)
163             commonfile.write(line)
164         commonfile.write("\n")
165         commonfile.close()
166         tempfile.close()
167
168         # Compare the updated PO files - they should be the same
169         self.assertEqual(filecmp.cmp(commonprops, testprops), 1)
170
171 if __name__ == '__main__':
172     unittest.main()