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