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