Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 #!/usr/bin/env python
3 import os
4 import tempfile
5 import zipfile
7 import mozfile
8 import mozhttpd
11 here = os.path.dirname(os.path.abspath(__file__))
13 # stubs is a dict of the form {'addon id': 'install manifest content'}
14 stubs = {
15 'test-addon-1@mozilla.org': 'test_addon_1.rdf',
16 'test-addon-2@mozilla.org': 'test_addon_2.rdf',
17 'test-addon-3@mozilla.org': 'test_addon_3.rdf',
18 'test-addon-4@mozilla.org': 'test_addon_4.rdf',
19 'test-addon-invalid-no-id@mozilla.org': 'test_addon_invalid_no_id.rdf',
20 'test-addon-invalid-version@mozilla.org': 'test_addon_invalid_version.rdf',
21 'test-addon-invalid-no-manifest@mozilla.org': None,
22 'test-addon-invalid-not-wellformed@mozilla.org': 'test_addon_invalid_not_wellformed.rdf',
23 'test-addon-unpack@mozilla.org': 'test_addon_unpack.rdf'}
26 def generate_addon(addon_id, path=None, name=None, xpi=True):
27 """
28 Method to generate a single addon.
30 :param addon_id: id of an addon to generate from the stubs dictionary
31 :param path: path where addon and .xpi should be generated
32 :param name: name for the addon folder or .xpi file
33 :param xpi: Flag if an XPI or folder should be generated
35 Returns the file-path of the addon's .xpi file
36 """
38 if not addon_id in stubs.keys():
39 raise IOError('Requested addon stub "%s" does not exist' % addon_id)
41 # Generate directory structure for addon
42 try:
43 tmpdir = path or tempfile.mkdtemp()
44 addon_dir = os.path.join(tmpdir, name or addon_id)
45 os.mkdir(addon_dir)
46 except IOError:
47 raise IOError('Could not generate directory structure for addon stub.')
49 # Write install.rdf for addon
50 if stubs[addon_id]:
51 install_rdf = os.path.join(addon_dir, 'install.rdf')
52 with open(install_rdf, 'w') as f:
53 manifest = os.path.join(here, 'install_manifests', stubs[addon_id])
54 f.write(open(manifest, 'r').read())
56 if not xpi:
57 return addon_dir
59 # Generate the .xpi for the addon
60 xpi_file = os.path.join(tmpdir, (name or addon_id) + '.xpi')
61 with zipfile.ZipFile(xpi_file, 'w') as x:
62 x.write(install_rdf, install_rdf[len(addon_dir):])
64 # Ensure we remove the temporary folder to not install the addon twice
65 mozfile.rmtree(addon_dir)
67 return xpi_file
70 def generate_manifest(addon_list, path=None):
71 tmpdir = path or tempfile.mkdtemp()
72 addons = [generate_addon(addon, path=tmpdir) for addon in addon_list]
74 manifest = os.path.join(tmpdir, 'manifest.ini')
75 with open(manifest, 'w') as f:
76 for addon in addons:
77 f.write('[' + addon + ']\n')
79 return manifest