This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.py
183 lines (150 loc) · 4.92 KB
/
build.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from glob2 import glob
from operator import itemgetter
import json
import os
import re
import shutil
import sys
import subprocess
import time
import traceback
BUILD_DIR = 'build'
FILES_DIR = os.path.join(BUILD_DIR, 'files')
DEFAULT_NAME = 'EXTENSION_NAME'
PRIVATE_KEY = '../[Keys]/Private Key.pem'
INCLUDES = [
'_locales/**/messages.json',
'css/**/*.css',
'img/**/*.gif',
'img/**/*.jpg',
'img/**/*.png',
'img/**/*.svg',
'js/**/*.js',
'**/*LICENSE',
'manifest.json',
'LICENSE',
'README.md',
'options-page.html',
]
IGNORE_TYPES = [
'.min.css',
'.min.js',
]
def build_package():
""" Copies all the included files to the build directory """
# Erase and rebuild the build directory
if os.path.exists(BUILD_DIR):
shutil.rmtree(BUILD_DIR)
mkdir(BUILD_DIR)
mkdir(FILES_DIR)
for include in INCLUDES:
# Glob all the files!
files = glob(include)
for file in files:
# Filter out ignored file types
if split_compound_ext(file)[1].lower() in IGNORE_TYPES:
continue
# Copy the file
newfile = os.path.join(FILES_DIR, file)
newdir = os.path.dirname(newfile)
if not os.path.exists(newdir):
mkdir(newdir)
shutil.copy(file, newfile)
def find_build_executable():
executable = None
if sys.platform == 'win32':
# Opera ignores --pack-extension, so I'll just use Chrome instead.
import winreg
try:
CHROME_KEY = r"Software\Clients\StartMenuInternet\Google Chrome\shell\open\command"
executable = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE, CHROME_KEY).strip('"')
except WindowsError:
pass
else:
raise NotImplementedError()
return executable
def fix_manifest():
""" Removes testing info from the manifest file """
manifest = ''
resource_pattern = r',[^"}]+("web_accessible_resources"\s*:\s*\[\s*((".+"\s*,\s*)*(".+")?)\s*\])'
with open(os.path.join(FILES_DIR, 'manifest.json'), 'r', encoding='utf-8-sig') as file:
manifest = file.read()
manifest = re.sub(resource_pattern, fix_resources, manifest)
with open(os.path.join(FILES_DIR, 'manifest.json'), 'w', encoding='utf-8-sig') as file:
file.write(manifest)
def fix_resources(match):
""" Regex replace routing that filters out files not in the build directory """
files = [ file.strip().strip('"') for file in match.group(2).split(',') ]
# Keep only the files that exist
files = [ file for file in files if os.path.exists(os.path.join(FILES_DIR, file)) ]
if len(files) == 0:
return ''
else:
filestring = ', '.join([ '"%s"' % file for file in files ])
return match.group(0).replace(match.group(2), filestring)
def get_extension_name():
""" Grabs the extension name from the manifest """
with open('manifest.json', 'r', encoding='utf-8-sig') as file:
manifest = json.load(file)
name = manifest['name'] if 'name' in manifest else DEFAULT_NAME
# If the extension name is localized, grab the name from the default locale
match = re.match(r'__MSG_(.+)__', name)
if match:
locale = manifest['default_locale'] if 'default_locale' in manifest else 'en_US'
with open(os.path.join('_locales', locale, 'messages.json'), 'r', encoding='utf-8-sig') as file:
messages = json.load(file)
key = match.group(1)
name = messages[key]['message'] if key in messages else DEFAULT_NAME
return name
def package_extension(path, key):
""" Uses Chrome to compile the build directory into an extension package """
path = os.path.abspath(path)
key = os.path.abspath(key)
subprocess.check_output([find_build_executable(), '--pack-extension={0}'.format(path), '--pack-extension-key={0}'.format(key)])
def mkdir(dir, max_retries=10):
""" Makes dir, retrying if it failed due to a permission error """
try:
safe_mkdir(dir)
except PermissionError:
if max_retries > 0:
mkdir(dir, max_retries - 1)
else:
raise
def safe_mkdir(dir):
""" Makes dir and any missing parent directories """
if not os.path.exists(dir):
head, tail = os.path.split(dir)
if tail:
safe_mkdir(head)
os.mkdir(dir)
elif head:
os.mkdir(dir)
def split_compound_ext(path):
""" splitext with support for compound extensions (ex: foo.js.map into foo, .js.map) """
path, ext1 = os.path.splitext(path)
path, ext2 = os.path.splitext(path)
return path, ext2 + ext1
def main():
# Copy all the necessary files into the build directory
build_package()
fix_manifest()
# Get the desired package name and the name Chrome will give to the package
name = get_extension_name()
buildname = FILES_DIR.rstrip('/').rpartition('/')[2]
chrome_name = os.path.join(BUILD_DIR, '{0}.crx'.format(name))
opera_name = os.path.join(BUILD_DIR, '{0}.nex'.format(name))
try:
# Build the package, then rename it to the desired name
package_extension(FILES_DIR, PRIVATE_KEY)
shutil.move(buildname + '.crx', chrome_name)
shutil.copy(chrome_name, opera_name)
except subprocess.CalledProcessError as e:
# If Chrome fails to package the extension, print its output so we know why.
print(e.output)
raise e
if __name__ == '__main__':
try:
main()
except:
traceback.print_exc()
input('Press Enter...')