-
Notifications
You must be signed in to change notification settings - Fork 25
/
pack-ext.py
executable file
·69 lines (59 loc) · 2.71 KB
/
pack-ext.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
#!/usr/bin/python3
import sys
import os
import json
import zipfile
print("Browser Extension packager by Mte90")
print("The only parameter required is the folder path!")
def zipdir(path, name, browser):
zipf = zipfile.ZipFile(name, 'w', zipfile.ZIP_DEFLATED)
exclude_prefixes = ['__', '.', 'eslint', 'tests'] # list of exclusion prefixes
exclude_suffixes = ['.xpi', '.zip', '.py', 'ISSUE_TEMPLATE.md', 'README.md'] # list of exclusion suffix
for dirpath, dirnames, filenames in os.walk(path):
# exclude all dirs/files starting/endings
dirnames[:] = [dirname
for dirname in dirnames
if all([dirname.startswith(string) is False
for string in exclude_prefixes])
is True]
filenames[:] = [filename
for filename in filenames
if (all([filename.startswith(string) is False for string in exclude_prefixes]))
and (all([filename.endswith(string) is False for string in exclude_suffixes]))
is True]
for file_found in filenames:
zipf.write(os.path.join(dirpath, file_found))
zipf.close()
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
manifest = sys.argv[1] + '/manifest.json'
if os.path.isfile(manifest):
with open(manifest) as content:
data = json.load(content)
name = data['name'].replace(' ', '-') + '_v' + data['version']
zipdir(sys.argv[1], name + '.xpi', 'firefox')
print("- Firefox WebExtension Package done!")
# convert manifest v2 to v3
os.system('cp ' + sys.argv[1] + '/manifest.json ' + sys.argv[1] + '/__manifest.json ')
# change parameters
data['background']['service_worker'] = data['background']['scripts'][0]
del data['background']['scripts']
data['web_accessible_resources'] = [
{
'resources': data['web_accessible_resources'],
'matches': ["https://translate.wordpress.org/*"]
}
]
data['manifest_version'] = 3
with open(manifest, 'w') as new_manifest:
json.dump(data, new_manifest, indent=4)
zipdir(sys.argv[1], name + '.zip', 'chrome')
# restore the original manifest
os.system('rm ' + sys.argv[1] + '/manifest.json')
os.system('mv ' + sys.argv[1] + '/__manifest.json ' + sys.argv[1] + '/manifest.json')
print("- Chrome Extension Package done!")
else:
print("The file" + manifest + " not exist")
sys.exit()
else:
print("Path not found")
sys.exit()