]> git.evergreen-ils.org Git - Evergreen.git/blob - build/i18n/tests/testpo.py
Move POT files into en-US subdirectory (clutter BAD).
[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         devnull = open('/dev/null', 'w')
44         subprocess.Popen(('cp', '-r', 'tests/tmp', 'done'), 0, None, None, devnull, devnull).wait()
45         for root, dirs, files in os.walk(os.path.join(self.newdir, 'tests/tmp'), topdown=False):
46             for name in files:
47                 os.remove(os.path.join(root, name))
48             for name in dirs:
49                 os.rmdir(os.path.join(root, name))
50         if os.access(os.path.join(self.newdir, 'tests/tmp'), os.F_OK):
51             os.rmdir(os.path.join(self.newdir, 'tests/tmp'))
52
53     def testnewpofiles(self):
54         # Create a brand new set of PO files from our en-US project files.
55         # Compare the files generated in the po/ll-LL directory with
56         # the number expected by a manual count of our known sources.
57         po_files = []
58         for po_dir in self.po_sources:
59             for path in glob.glob(po_dir):
60                 po_files.append(os.path.basename(path) + '.po')
61         po_files.sort()
62         new_pofiles = os.listdir(self.locale_dir)
63         new_pofiles.sort()
64         self.assertEqual(len(po_files), len(new_pofiles))
65
66     def testnewprojectfiles(self):
67         # Create a brand new set of project files from PO files.
68         # Compare the files created with a manual count of our known sources.
69         moz_files = []
70         for po_dir in self.po_sources:
71             for path in glob.glob(po_dir):
72                 moz_files.append(os.path.basename(path))
73         moz_files.sort()
74         new_mozfiles = os.listdir(self.locale_dir)
75         new_mozfiles.sort()
76         self.assertEqual(len(moz_files), len(new_mozfiles))
77
78     def testtranslatedfile(self):
79         # "Translate" strings in a PO file, then generate the project
80         # files to ensure that the translated string appears in the output.
81
82         # Create the "translated" PO file
83         commonpo = os.path.join(self.locale_dir, 'common.properties.po')
84         testpo = os.path.join(self.locale_dir, 'test.properties.po')
85         commonfile = open(commonpo)
86         testfile = open(testpo, 'w')
87         for line in commonfile:
88             line = re.sub(r'^msgstr ""', r'msgstr "abcdefg"', line)
89             testfile.write(line)
90         commonfile.close()
91         testfile.close()
92         os.remove(commonpo)
93         os.rename(testpo, commonpo)
94
95         # Create the "translated" properties file
96         commonprops = os.path.join(self.project_dir, self.locale, 'common.properties')
97         testprops = os.path.join(self.locale_dir, 'test.properties')
98         commonfile = open(commonprops)
99         testfile = open(testprops, 'w')
100         for line in commonfile:
101             line = re.sub(r'^(.*?)=.*?$', r'\1=abcdefg', line)
102             testfile.write(line)
103         commonfile.close()
104         testfile.close()
105
106         # Regenerate the project files to get the translated strings in place
107         devnull = open('/dev/null', 'w')
108         subprocess.Popen(('make', 'LOCALE=ll-LL', self.po_in_dir, self.po_out_dir, 'updateproject'), 0, None, None, devnull, devnull).wait()
109
110         self.assertEqual(filecmp.cmp(commonprops, testprops), 1)
111
112     def testupdatepo(self):
113         # Add strings to a POT file, then ensure that the updated PO files
114         # include the new strings
115
116         # Create the "template" PO file
117         commonpo = os.path.join(self.locale_dir, 'common.properties.po')
118         testpo = os.path.join(self.locale_dir, 'test.properties.po')
119         commonfile = open(commonpo)
120         testfile = open(testpo, 'w')
121         for line in commonfile:
122             line = re.sub(r'common.properties$', r'test.properties', line)
123             testfile.write(line)
124         commonfile.close()
125         testfile.close()
126
127         # Create the test POT file
128         commonpot = os.path.join(self.pot_dir, 'common.properties.pot')
129         testpot = os.path.join(self.pot_dir, 'test.properties.pot')
130         commonfile = open(commonpot)
131         testfile = open(testpot, 'w')
132         for line in commonfile:
133             line = re.sub(r'common.properties$', r'test.properties', line)
134             testfile.write(line)
135         commonfile.close()
136         testfile.write("\n#: common.testupdatepo")
137         testfile.write('\nmsgid "TESTUPDATEPO"')
138         testfile.write('\nmsgstr ""')
139         testfile.close()
140
141         # Update the PO files to get the translated strings in place
142         devnull = open('/dev/null', 'w')
143         subprocess.Popen(('make', 'LOCALE=ll-LL', self.po_in_dir, self.po_out_dir, 'updatepo'), 0, None, None, devnull, devnull).wait()
144
145         commonprops = os.path.join(self.locale_dir, 'common.properties.po')
146         tempprops = os.path.join(self.locale_dir, 'temp.properties.po')
147         testprops = os.path.join(self.locale_dir, 'test.properties.po')
148
149         # Munge the common file to make it what we expect it to be
150         commonfile = open(commonprops, 'a+')
151         commonfile.write("\n#: common.testupdatepo")
152         commonfile.write('\nmsgid "TESTUPDATEPO"')
153         commonfile.write('\nmsgstr ""')
154         commonfile.close()
155
156         shutil.copyfile(commonprops, tempprops)
157         commonfile = open(commonprops, 'w')
158         tempfile = open(testpot)
159         for line in tempfile:
160             line = re.sub(r'common.properties$', r'test.properties', line)
161             line = re.sub(r'^"Project-Id-Version: .*"$', r'"Project-Id-Version: PACKAGE VERSION\\n"', line)
162             commonfile.write(line)
163         commonfile.write("\n")
164         commonfile.close()
165         tempfile.close()
166
167         # Compare the updated PO files - they should be the same
168         self.assertEqual(filecmp.cmp(commonprops, testprops), 1)
169
170 if __name__ == '__main__':
171     unittest.main()