~ubuntu-langpack/langpack-o-matic/main

« back to all changes in this revision

Viewing changes to lib/xpi.py

  • Committer: Martin Pitt
  • Date: 2011-12-16 11:06:30 UTC
  • Revision ID: martin.pitt@canonical.com-20111216110630-t6pjf2eyidu6y6yv
remove remaining XPI/Firefox handling, lucid and maverick switch to firefox-locale-* packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Integrate XPI extensions (such as Firefox translations)'''
2
 
 
3
 
# this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
4
 
# (C) 2011 Canonical Ltd.
5
 
 
6
 
import zipfile
7
 
import os, os
8
 
import shutil
9
 
from glob import glob
10
 
import xml.dom.minidom
11
 
from xml.parsers.expat import ExpatError
12
 
import tarfile
13
 
import zipfile
14
 
import tempfile
15
 
import logging
16
 
 
17
 
xpi_data_root = 'po2xpi/data'
18
 
 
19
 
def get_xpi_id(xpi):
20
 
    '''Return ID of given XPI.'''
21
 
 
22
 
    try:
23
 
        dom_doc = xml.dom.minidom.parseString(zipfile.ZipFile(xpi).open('install.rdf').read())
24
 
    except ExpatError as e:
25
 
        logging.error('Invalid XPI %s: %s', xpi, str(e))
26
 
        return None
27
 
 
28
 
    try:
29
 
        attr = dom_doc.getElementsByTagName('RDF:Description')[0].attributes['em:id']
30
 
    except IndexError:
31
 
        attr = dom_doc.getElementsByTagName('Description')[0].attributes['em:id']
32
 
 
33
 
    assert attr.value
34
 
    return attr.value
35
 
 
36
 
def _make_tar_from_xpi(xpi, output_tar, id):
37
 
    '''Unpack an XPI into a mozilla.tar.gz suitable for Firefox < 4.0 langpacks'''
38
 
 
39
 
    workdir = tempfile.mkdtemp()
40
 
    dest = os.path.join(workdir, 'usr/lib/firefox-addons/extensions', id)
41
 
    try:
42
 
        zip = zipfile.ZipFile(xpi)
43
 
        zip.extractall(dest)
44
 
        zip.close()
45
 
        if os.path.exists(output_tar):
46
 
            tar = tarfile.open(output_tar) 
47
 
            tar.extractall(workdir)
48
 
            tar.close()
49
 
        tar = tarfile.open(output_tar, 'w:gz') 
50
 
        tar.add(workdir, '.')
51
 
        tar.close()
52
 
    finally:
53
 
        shutil.rmtree(workdir)
54
 
 
55
 
def install_xpis(release_version, locale, source_pkg):
56
 
    '''Install XPIs for given locale into source package tree'''
57
 
 
58
 
    # Chinese langpacks are split, treat specially
59
 
    if locale.startswith('zh_'):
60
 
        lang = locale.split('.')[0].split('@')[0].replace('_', '-')
61
 
    else:
62
 
        lang = locale.split('_')[0].split('@')[0]
63
 
 
64
 
    xpis = glob(os.path.join(xpi_data_root, '%s/*/xpi/%s.xpi' % (release_version, lang))) + \
65
 
           glob(os.path.join(xpi_data_root, '%s/*/xpi/%s-*.xpi' % (release_version, lang)))
66
 
 
67
 
    destdir = os.path.join(source_pkg, 'data')
68
 
    for xpi in xpis:
69
 
        id = get_xpi_id(xpi)
70
 
        if not os.path.isdir(destdir):
71
 
            os.mkdir(destdir)
72
 
 
73
 
        if float(release_version) > 11:
74
 
            # Firefox 4.0 (in Ubuntu 11.04+) can use packed XPIs
75
 
            logging.debug('Copying %s (ID: %s) into %s', xpi, id, source_pkg)
76
 
            shutil.copy(xpi, os.path.join(destdir, id + '.xpi'))
77
 
        else:
78
 
            # older releases need the unpacked XPI; ship a readymade tarball in
79
 
            # the langpacks, which debian/rules will unpack
80
 
            logging.debug('Unpacking %s (ID: %s) into %s', xpi, id, source_pkg)
81
 
            _make_tar_from_xpi(xpi, os.path.join(destdir, 'mozilla.tar.gz'), id)
82
 
 
83
 
    # Add Firefox search plugins
84
 
    if float(release_version) > 10.09:
85
 
        search_plugins = glob(os.path.join('extra-files', 'searchplugins', release_version, lang)) + \
86
 
                         glob(os.path.join('extra-files', 'searchplugins', release_version, '%s-*' % lang))
87
 
        if search_plugins:
88
 
            output_tar = os.path.join(destdir, 'searchplugins.tar')
89
 
            logging.debug('Creating Firefox search plugins pack %s', output_tar)
90
 
            tar = tarfile.open(output_tar, 'w')
91
 
            for lang in search_plugins:
92
 
                tar.add(lang, os.path.join('usr/lib/firefox-addons/searchplugins',
93
 
                            os.path.basename(lang)))
94
 
            tar.close()
95
 
 
96
 
if __name__ == '__main__':
97
 
    import tempfile, unittest
98
 
 
99
 
    class _T(unittest.TestCase):
100
 
        def setUp(self):
101
 
            self.all_xpis = glob(os.path.join(xpi_data_root, '*/*/xpi/*.xpi'))
102
 
            self.assertNotEqual(len(self.all_xpis), 0)
103
 
 
104
 
            self.workdir = tempfile.mkdtemp()
105
 
 
106
 
        def tearDown(self):
107
 
            shutil.rmtree(self.workdir)
108
 
 
109
 
        def test_xpi_id(self):
110
 
            '''get_xpi_id()'''
111
 
 
112
 
            for xpi in self.all_xpis:
113
 
                id = get_xpi_id(xpi)
114
 
                if id:
115
 
                    self.assertTrue(id.endswith('.mozilla.org') or id.endswith('.ubuntu.com'))
116
 
                    self.assertTrue(os.path.basename(xpi).split('.')[0].split('-')[0] in id)
117
 
 
118
 
        def test_install_xpis_unavailable(self):
119
 
            '''install_xpis() for unavailable langpack'''
120
 
 
121
 
            # valid release, unknown locale
122
 
            install_xpis('11.04', 'zz', self.workdir)
123
 
            installed = os.listdir(os.path.join(self.workdir))
124
 
            self.assertEqual(installed, [])
125
 
 
126
 
            # invalid release, known locale
127
 
            install_xpis('10.03', 'de', self.workdir)
128
 
            installed = os.listdir(os.path.join(self.workdir))
129
 
            self.assertEqual(installed, [])
130
 
 
131
 
        def test_install_xpis_nocountry(self):
132
 
            '''install_xpis() for language without country'''
133
 
 
134
 
            install_xpis('11.04', 'de_DE.UTF-8', self.workdir)
135
 
            installed = os.listdir(os.path.join(self.workdir, 'data'))
136
 
            self.assertEqual(len(installed), 1, 'installed: ' + ' '.join(installed))
137
 
            self.assertTrue('-de@' in installed[0])
138
 
 
139
 
        def test_install_xpis_unpacked(self):
140
 
            '''install_xpis() for Firefox < 4.0 (unpacked XPI)'''
141
 
 
142
 
            install_xpis('10.04', 'es_ES.UTF-8', self.workdir)
143
 
            installed = os.listdir(os.path.join(self.workdir, 'data'))
144
 
            self.assertEqual(len(installed), 1, 'installed: ' + ' '.join(installed))
145
 
            self.assertEqual(installed[0], 'mozilla.tar.gz')
146
 
            tar = tarfile.open(os.path.join(self.workdir, 'data',
147
 
                'mozilla.tar.gz'))
148
 
            names = [n.startswith('./') and n[2:] or n for n in tar.getnames()]
149
 
            tar.close()
150
 
            self.assertTrue('usr/lib/firefox-addons/extensions/langpack-es-ES@firefox.mozilla.org/chrome/es-ES.jar'
151
 
                    in names)
152
 
            self.assertTrue('usr/lib/firefox-addons/extensions/langpack-es-AR@firefox.mozilla.org/chrome/es-AR.jar'
153
 
                    in names)
154
 
 
155
 
        def test_install_xpis_country(self):
156
 
            '''install_xpis() for country specific language'''
157
 
 
158
 
            install_xpis('11.04', 'es_AR.UTF-8', self.workdir)
159
 
            installed = os.listdir(os.path.join(self.workdir, 'data'))
160
 
            self.assertTrue(len(installed) > 1, 'installed: ' + ' '.join(installed))
161
 
            searching = ['es-AR@', 'es-ES@']
162
 
            for i in installed:
163
 
                for s in list(searching):
164
 
                    if s in i:
165
 
                        searching.remove(s)
166
 
            self.assertEqual(searching, [], 'should have installed ' + ' '.join(searching))
167
 
 
168
 
        def test_install_xpis_zh_cn(self):
169
 
            '''install_xpis() for Chinese split (CN)'''
170
 
 
171
 
            install_xpis('11.04', 'zh_CN.UTF-8', self.workdir)
172
 
            installed = os.listdir(os.path.join(self.workdir, 'data'))
173
 
            self.assertEqual(len(installed), 1, 'installed: ' + ' '.join(installed))
174
 
            self.assertTrue('-zh-CN@' in installed[0])
175
 
 
176
 
        def test_install_xpis_zh_tw(self):
177
 
            '''install_xpis() for Chinese split (TW)'''
178
 
 
179
 
            install_xpis('11.04', 'zh_TW.UTF-8', self.workdir)
180
 
            installed = os.listdir(os.path.join(self.workdir, 'data'))
181
 
            self.assertEqual(len(installed), 1, 'installed: ' + ' '.join(installed))
182
 
            self.assertTrue('-zh-TW@' in installed[0])
183
 
 
184
 
    unittest.main()