~ubuntu-china-devs/ubuntu-chinese/langpack-o-matic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python

'''Integration test for "import" script.'''

# this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
#
# (C) 2010 Canonical Ltd.

import subprocess
import unittest
import shutil
import tempfile
import os.path
import sys

class_domain_expected = {
        '': 'pg_dump-8.4',
        'gnome-': 'gnome-session-2.0',
        'kde-': 'knetworkconf'
}

class _T(unittest.TestCase):
    def setUp(self):
        self.outdir = tempfile.mkdtemp()
        self.srcroot = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
        self.mirror = 'file://%s/test/archive' % self.srcroot
        self.full_tar = os.path.join(self.srcroot, 'test', 'test-full.tar.gz')
        self.update_tar = os.path.join(self.srcroot, 'test', 'test-update.tar.gz')
        self.import_path = os.path.join(self.srcroot, 'import')
        self.updated_packages = os.path.join(self.srcroot, 'updated-packages')
        assert not os.path.exists(self.updated_packages), 'updated-packages already exists, please clean up first'

    def tearDown(self):
        shutil.rmtree(self.outdir)
        if os.path.exists(self.updated_packages):
            os.unlink(self.updated_packages)

    def test_full_classes_nostatic(self):
        '''full tarball, classes, no static'''

        i = subprocess.Popen([self.import_path, '--no-static', '--mirror', self.mirror,
            self.full_tar, 'lucid', self.outdir], stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        out, err = i.communicate()
        self.assertEqual(i.returncode, 0, 'import script failed:\n' + err)
        self.assertEqual(err, '')

        updated = open(self.updated_packages).read()

        for lang in ('de', 'en', 'pt', 'zh-hans', 'zh-hant'):
            for cls in ('', 'gnome-', 'kde-'):
                # base packs should be created and have the expected files
                dir = os.path.join(self.outdir, 'sources-base',
                        'language-pack-%s%s-base' % (cls, lang))
                self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
                change = open(os.path.join(dir, 'debian', 'changelog')).readline()
                self.assertEqual(change, 'language-pack-%s%s-base (1:10.04+20100101) lucid; urgency=low\n' % (cls, lang))
                locs = os.listdir(os.path.join(dir, 'data'))
                try:
                    locs.remove('extra.tar')
                except ValueError:
                    pass
                self.assert_(len(locs) >= 1)
                for loc in locs:
                    self.assertEqual(os.listdir(os.path.join(dir, 'data', loc,
                        'LC_MESSAGES')), [class_domain_expected[cls] + '.po'])

                self.assert_('sources-base/language-pack-%s%s-base\n' % (cls, lang) in updated)

                # update packs should be created and be empty
                dir = os.path.join(self.outdir, 'sources-update',
                        'language-pack-%s%s' % (cls, lang))
                self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
                change = open(os.path.join(dir, 'debian', 'changelog')).readline()
                self.assertEqual(change, 'language-pack-%s%s (1:10.04+20100101) lucid; urgency=low\n' % (cls, lang))
                self.assertEqual(os.listdir(os.path.join(dir, 'data')), [])

                self.assert_('sources-update/language-pack-%s%s\n' % (cls, lang) in updated)

            # support packs should be created
            dir = os.path.join(self.outdir, 'sources-support',
                    'language-support-%s' % lang)
            self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
            change = open(os.path.join(dir, 'debian', 'changelog')).readline()
            self.assertEqual(change, 'language-support-%s (1:10.04+20100101) lucid; urgency=low\n' % lang)
            self.assert_('sources-support/language-support-%s\n' % lang in updated)

    def test_update_classes_nostatic(self):
        '''update tarball, classes, no static'''

        # import full tarball first
        i = subprocess.Popen([self.import_path, '--no-static', '--mirror', self.mirror,
            self.full_tar, 'lucid', self.outdir], stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        out, err = i.communicate()
        self.assertEqual(i.returncode, 0, 'import script failed:\n' + err)
        self.assertEqual(err, '')

        os.unlink(self.updated_packages)

        # now import update tarball: this introduces a new language 'es' with
        # gnome-sessino-2.0, thus we should get a new -gnome-base with empty
        # updates and empty corresponding non-class packages. It also updates
        # en_GB's gnome-session-2.0 (-gnome-en update) and introduces en_GB's
        # jockey.po (-en update)
        i = subprocess.Popen([self.import_path, '--no-static', '--mirror', self.mirror,
            self.update_tar, 'lucid', self.outdir], stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        out, err = i.communicate()
        self.assertEqual(i.returncode, 0, 'import script failed:\n' + err)
        self.assertEqual(err, '')

        updated = open(self.updated_packages).read()
        # 6 for es (2x support, 2x -gnome, 2x non-class), -gnome-en and -en
        self.assertEqual(len(updated.strip().splitlines()), 8)

        # po files landed in the right place
        f = os.path.join(self.outdir, 'sources-base',
            'language-pack-gnome-es-base', 'data', 'es', 'LC_MESSAGES',
            'gnome-session-2.0.po')
        contents = open(f).read()
        self.assert_('"es"' in contents)
        self.assert_('newstring' in contents)

        f = os.path.join(self.outdir, 'sources-update', 'language-pack-en',
                'data', 'en_GB', 'LC_MESSAGES', 'jockey.po')
        contents = open(f).read()
        self.assert_('"en_GB"' in contents)

        # check changelogs
        for p in ('gnome-es-base', 'gnome-es', 'es-base', 'es', 'gnome-en', 'en'):
            dir = os.path.join(self.outdir, 
                    'base' in p and 'sources-base' or 'sources-update', 
                    'language-pack-%s' % p)
            self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
            change = open(os.path.join(dir, 'debian', 'changelog')).readline()
            self.assertEqual(change, 'language-pack-%s (1:10.04+20100202) lucid; urgency=low\n' % p)

    def test_full_noclasses_nostatic(self):
        '''full tarball, no classes, no static'''

        i = subprocess.Popen([self.import_path, '--no-classes', '--no-static',
            '--mirror', self.mirror, self.full_tar, 'lucid', self.outdir],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = i.communicate()
        self.assertEqual(i.returncode, 0, 'import script failed:\n' + err)
        self.assertEqual(err, '')

        updated = open(self.updated_packages).read()

        self.failIf('-kde' in updated)
        self.failIf('-gnome' in updated)

        for lang in ('de', 'en', 'pt', 'zh-hans', 'zh-hant'):
            # base packs should be created and have the expected files
            dir = os.path.join(self.outdir, 'sources-base',
                    'language-pack-%s-base' % lang)
            self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
            change = open(os.path.join(dir, 'debian', 'changelog')).readline()
            self.assertEqual(change, 'language-pack-%s-base (1:10.04+20100101) lucid; urgency=low\n' % lang)
            locs = os.listdir(os.path.join(dir, 'data'))
            try:
                locs.remove('extra.tar')
            except ValueError:
                pass
            self.assert_(len(locs) >= 1)
            for loc in locs:
                self.assertEqual(set(os.listdir(os.path.join(dir, 'data', loc,
                    'LC_MESSAGES'))), set([d + '.po' for d in class_domain_expected.itervalues()]))

            self.assert_('sources-base/language-pack-%s-base\n' % lang in updated)

            # update packs should be created and be empty
            dir = os.path.join(self.outdir, 'sources-update', 'language-pack-%s' % lang)
            self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
            change = open(os.path.join(dir, 'debian', 'changelog')).readline()
            self.assertEqual(change, 'language-pack-%s (1:10.04+20100101) lucid; urgency=low\n' % lang)
            self.assertEqual(os.listdir(os.path.join(dir, 'data')), [])

            self.assert_('sources-update/language-pack-%s\n' % lang in updated)

            # support packs should be created
            dir = os.path.join(self.outdir, 'sources-support',
                    'language-support-%s' % lang)
            self.assert_(os.path.isfile(os.path.join(dir, 'debian', 'control')))
            change = open(os.path.join(dir, 'debian', 'changelog')).readline()
            self.assertEqual(change, 'language-support-%s (1:10.04+20100101) lucid; urgency=low\n' % lang)
            self.assert_('sources-support/language-support-%s\n' % lang in updated)

    def test_deb_validity(self):
        '''Source and binary deb validity'''

        i = subprocess.Popen([self.import_path, '--no-static', '--mirror', self.mirror,
            self.update_tar, 'lucid', self.outdir], stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        out, err = i.communicate()
        self.assertEqual(i.returncode, 0, 'import script failed:\n' + err)
        self.assertEqual(err, '')

        updated = open(self.updated_packages).read()

        def _check_pkg(p):
            pkg = os.path.join(self.outdir, 'support' in p and 'sources-support'
                    or ('base' in p and 'sources-base' or 'sources-update'), p)
            self._dpkg_build(pkg)
            prefix = os.path.join(self.outdir, 'sources-base',
                'language-pack-gnome-es-base_10.04+20100202')
            self._dpkg_lintian(prefix + '.dsc')
            self._dpkg_lintian(prefix + '_all.deb')
            contents = self._dpkg_list(prefix + '_all.deb')
            self.assert_('./usr/share/doc/language-pack-gnome-es-base/copyright'
                    in contents)
            return contents

        contents = _check_pkg('language-pack-gnome-es-base')
        self.assert_('./usr/share/locale-langpack/es/LC_MESSAGES/gnome-session-2.0.mo'
                in contents)
        _check_pkg('language-pack-gnome-es')
        _check_pkg('language-support-es')
        _check_pkg('language-support-writing-es')

    def _dpkg_build(self, pkgdir):
        '''Build source and binary packages from pkgdir.
        
        Return (pkgname, version) tuple.
        '''
        env = os.environ.copy()
        try:
            del env['LANG']
        except KeyError:
            pass
        dpkg = subprocess.Popen(['dpkg-buildpackage', '-us', '-uc'],
                cwd=pkgdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                env=env)
        out, err = dpkg.communicate()
        #self.assertEqual(err, '')
        self.assertEqual(dpkg.returncode, 0)

    def _dpkg_lintian(self, path):
        '''Run lintian on path and ensure it has no output.'''

        lintian = subprocess.Popen(['lintian', path], stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT)
        out = lintian.communicate()[0]
        self.assertEqual(lintian.returncode, 0)
        for l in out.splitlines():
            # this is a special case for Ubuntu
            if 'unknown-section translations' in l:
                continue
            # XXX cosmetical error, needs fixing at some point
            if 'package-relation-with-self replaces' in l:
                continue
            self.fail('lintian error: ' + l)

    def _dpkg_list(self, path):
        '''Return list of files in given deb'''

        dpkg = subprocess.Popen(['dpkg', '-c', path],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = dpkg.communicate()
        self.assertEqual(err, '')
        self.assertEqual(dpkg.returncode, 0)
        return [l.split()[-1] for l in out.strip().splitlines()]

unittest.main()