michael@0: #!/usr/bin/env python michael@0: michael@0: import os michael@0: import tempfile michael@0: import zipfile michael@0: michael@0: import mozfile michael@0: import mozhttpd michael@0: michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: # stubs is a dict of the form {'addon id': 'install manifest content'} michael@0: stubs = { michael@0: 'test-addon-1@mozilla.org': 'test_addon_1.rdf', michael@0: 'test-addon-2@mozilla.org': 'test_addon_2.rdf', michael@0: 'test-addon-3@mozilla.org': 'test_addon_3.rdf', michael@0: 'test-addon-4@mozilla.org': 'test_addon_4.rdf', michael@0: 'test-addon-invalid-no-id@mozilla.org': 'test_addon_invalid_no_id.rdf', michael@0: 'test-addon-invalid-version@mozilla.org': 'test_addon_invalid_version.rdf', michael@0: 'test-addon-invalid-no-manifest@mozilla.org': None, michael@0: 'test-addon-invalid-not-wellformed@mozilla.org': 'test_addon_invalid_not_wellformed.rdf', michael@0: 'test-addon-unpack@mozilla.org': 'test_addon_unpack.rdf'} michael@0: michael@0: michael@0: def generate_addon(addon_id, path=None, name=None, xpi=True): michael@0: """ michael@0: Method to generate a single addon. michael@0: michael@0: :param addon_id: id of an addon to generate from the stubs dictionary michael@0: :param path: path where addon and .xpi should be generated michael@0: :param name: name for the addon folder or .xpi file michael@0: :param xpi: Flag if an XPI or folder should be generated michael@0: michael@0: Returns the file-path of the addon's .xpi file michael@0: """ michael@0: michael@0: if not addon_id in stubs.keys(): michael@0: raise IOError('Requested addon stub "%s" does not exist' % addon_id) michael@0: michael@0: # Generate directory structure for addon michael@0: try: michael@0: tmpdir = path or tempfile.mkdtemp() michael@0: addon_dir = os.path.join(tmpdir, name or addon_id) michael@0: os.mkdir(addon_dir) michael@0: except IOError: michael@0: raise IOError('Could not generate directory structure for addon stub.') michael@0: michael@0: # Write install.rdf for addon michael@0: if stubs[addon_id]: michael@0: install_rdf = os.path.join(addon_dir, 'install.rdf') michael@0: with open(install_rdf, 'w') as f: michael@0: manifest = os.path.join(here, 'install_manifests', stubs[addon_id]) michael@0: f.write(open(manifest, 'r').read()) michael@0: michael@0: if not xpi: michael@0: return addon_dir michael@0: michael@0: # Generate the .xpi for the addon michael@0: xpi_file = os.path.join(tmpdir, (name or addon_id) + '.xpi') michael@0: with zipfile.ZipFile(xpi_file, 'w') as x: michael@0: x.write(install_rdf, install_rdf[len(addon_dir):]) michael@0: michael@0: # Ensure we remove the temporary folder to not install the addon twice michael@0: mozfile.rmtree(addon_dir) michael@0: michael@0: return xpi_file michael@0: michael@0: michael@0: def generate_manifest(addon_list, path=None): michael@0: tmpdir = path or tempfile.mkdtemp() michael@0: addons = [generate_addon(addon, path=tmpdir) for addon in addon_list] michael@0: michael@0: manifest = os.path.join(tmpdir, 'manifest.ini') michael@0: with open(manifest, 'w') as f: michael@0: for addon in addons: michael@0: f.write('[' + addon + ']\n') michael@0: michael@0: return manifest