tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

addon_stubs.py (2275B)


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