-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_ffx.py
executable file
·94 lines (82 loc) · 3.38 KB
/
pack_ffx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env python
import os
import json
import zipfile
import argparse
import re
ver = '0.1.2'
abt = \
"""
Script to convert a Chrome / Opera WebExtension to a Firefox-compatible WebExtension archive.
Creates a compressed zip archive named from manifest keys <version_name>.xpi or <name>_firefox_<version>.xpi.
By Steven Eardley | Cottage Labs LLP | steve@cottagelabs.com
"""
parser = argparse.ArgumentParser(description=abt, version=ver)
parser.add_argument("-d",
"--dir",
default="..",
help="directory to pack (with manifest.json at its root)."
"Defaults to .. so this script can used in a submodule")
parser.add_argument("-i",
"--id",
default="firefox_extension@cottagelabs.com",
help="extension ID to be added to the manifest. Default is firefox_extension@cottagelabs.com")
parser.add_argument("-m",
"--min_version",
default="45.0",
help="version to put as strict_min_version for Firefox. Defaults to 45.0")
args = parser.parse_args()
ffx_manifest_extras = {
'applications': {
'gecko': {
'id': args.id,
'strict_min_version': args.min_version
}
}
}
chrome_manifest = None
try:
with open(args.dir + '/manifest.json') as f:
try:
chrome_manifest = json.load(f)
# change version string to firefox
try:
chrome_manifest["version_name"] = chrome_manifest["version_name"].replace('chrome', 'firefox')
except KeyError:
# no version_name, pass
pass
# add firefox keys to manifest
chrome_manifest.update(ffx_manifest_extras)
except ValueError:
print "Error: could not parse manifest file."
exit(1)
except IOError:
print "Error: problem reading manifest file or file not found."
exit(1)
if chrome_manifest is None:
print "Error: no usable manifest file found."
exit(1)
try:
name = chrome_manifest['version_name']
except KeyError:
name = "{0}_firefox_{1}".format(chrome_manifest['name'], chrome_manifest['version'])
archive_filename = '{0}.xpi'.format(name)
with zipfile.ZipFile(file=archive_filename, mode='w') as xpi_zip:
archive_root = args.dir if args.dir.endswith('/') else args.dir + '/'
match_root = re.compile('^{0}'.format(archive_root))
# write all files apart from the manifest
for root, dirs, files in os.walk(args.dir):
# exclude hidden files and directories
files = [f for f in files if not f.startswith('.')]
dirs[:] = [d for d in dirs if not d.startswith('.')]
for fi in files:
# add all files to archive except any old .xpi, the old manifest and this script
if fi != 'manifest.json' and fi != 'pack_ffx.py' and not fi.endswith(".xpi"):
xpi_zip.write(filename=os.path.join(root, fi),
arcname=match_root.sub('', os.path.join(root, fi)),
compress_type=zipfile.ZIP_DEFLATED)
# write the updated manifest
xpi_zip.writestr('manifest.json', json.dumps(chrome_manifest), compress_type=zipfile.ZIP_DEFLATED)
print "Done. New archive at {0} including:\n".format(archive_filename)
xpi_zip.printdir()
xpi_zip.close()