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