1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozprofile/tests/addon_stubs.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +import os 1.7 +import tempfile 1.8 +import zipfile 1.9 + 1.10 +import mozfile 1.11 +import mozhttpd 1.12 + 1.13 + 1.14 +here = os.path.dirname(os.path.abspath(__file__)) 1.15 + 1.16 +# stubs is a dict of the form {'addon id': 'install manifest content'} 1.17 +stubs = { 1.18 + 'test-addon-1@mozilla.org': 'test_addon_1.rdf', 1.19 + 'test-addon-2@mozilla.org': 'test_addon_2.rdf', 1.20 + 'test-addon-3@mozilla.org': 'test_addon_3.rdf', 1.21 + 'test-addon-4@mozilla.org': 'test_addon_4.rdf', 1.22 + 'test-addon-invalid-no-id@mozilla.org': 'test_addon_invalid_no_id.rdf', 1.23 + 'test-addon-invalid-version@mozilla.org': 'test_addon_invalid_version.rdf', 1.24 + 'test-addon-invalid-no-manifest@mozilla.org': None, 1.25 + 'test-addon-invalid-not-wellformed@mozilla.org': 'test_addon_invalid_not_wellformed.rdf', 1.26 + 'test-addon-unpack@mozilla.org': 'test_addon_unpack.rdf'} 1.27 + 1.28 + 1.29 +def generate_addon(addon_id, path=None, name=None, xpi=True): 1.30 + """ 1.31 + Method to generate a single addon. 1.32 + 1.33 + :param addon_id: id of an addon to generate from the stubs dictionary 1.34 + :param path: path where addon and .xpi should be generated 1.35 + :param name: name for the addon folder or .xpi file 1.36 + :param xpi: Flag if an XPI or folder should be generated 1.37 + 1.38 + Returns the file-path of the addon's .xpi file 1.39 + """ 1.40 + 1.41 + if not addon_id in stubs.keys(): 1.42 + raise IOError('Requested addon stub "%s" does not exist' % addon_id) 1.43 + 1.44 + # Generate directory structure for addon 1.45 + try: 1.46 + tmpdir = path or tempfile.mkdtemp() 1.47 + addon_dir = os.path.join(tmpdir, name or addon_id) 1.48 + os.mkdir(addon_dir) 1.49 + except IOError: 1.50 + raise IOError('Could not generate directory structure for addon stub.') 1.51 + 1.52 + # Write install.rdf for addon 1.53 + if stubs[addon_id]: 1.54 + install_rdf = os.path.join(addon_dir, 'install.rdf') 1.55 + with open(install_rdf, 'w') as f: 1.56 + manifest = os.path.join(here, 'install_manifests', stubs[addon_id]) 1.57 + f.write(open(manifest, 'r').read()) 1.58 + 1.59 + if not xpi: 1.60 + return addon_dir 1.61 + 1.62 + # Generate the .xpi for the addon 1.63 + xpi_file = os.path.join(tmpdir, (name or addon_id) + '.xpi') 1.64 + with zipfile.ZipFile(xpi_file, 'w') as x: 1.65 + x.write(install_rdf, install_rdf[len(addon_dir):]) 1.66 + 1.67 + # Ensure we remove the temporary folder to not install the addon twice 1.68 + mozfile.rmtree(addon_dir) 1.69 + 1.70 + return xpi_file 1.71 + 1.72 + 1.73 +def generate_manifest(addon_list, path=None): 1.74 + tmpdir = path or tempfile.mkdtemp() 1.75 + addons = [generate_addon(addon, path=tmpdir) for addon in addon_list] 1.76 + 1.77 + manifest = os.path.join(tmpdir, 'manifest.ini') 1.78 + with open(manifest, 'w') as f: 1.79 + for addon in addons: 1.80 + f.write('[' + addon + ']\n') 1.81 + 1.82 + return manifest