|
1 #!/usr/bin/env python |
|
2 |
|
3 import os |
|
4 import tempfile |
|
5 import zipfile |
|
6 |
|
7 import mozfile |
|
8 import mozhttpd |
|
9 |
|
10 |
|
11 here = os.path.dirname(os.path.abspath(__file__)) |
|
12 |
|
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'} |
|
24 |
|
25 |
|
26 def generate_addon(addon_id, path=None, name=None, xpi=True): |
|
27 """ |
|
28 Method to generate a single addon. |
|
29 |
|
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 |
|
34 |
|
35 Returns the file-path of the addon's .xpi file |
|
36 """ |
|
37 |
|
38 if not addon_id in stubs.keys(): |
|
39 raise IOError('Requested addon stub "%s" does not exist' % addon_id) |
|
40 |
|
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.') |
|
48 |
|
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()) |
|
55 |
|
56 if not xpi: |
|
57 return addon_dir |
|
58 |
|
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):]) |
|
63 |
|
64 # Ensure we remove the temporary folder to not install the addon twice |
|
65 mozfile.rmtree(addon_dir) |
|
66 |
|
67 return xpi_file |
|
68 |
|
69 |
|
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] |
|
73 |
|
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') |
|
78 |
|
79 return manifest |