-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
96 lines (77 loc) · 2.45 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
#!/usr/bin/python3
#
# Create zipped-up extension package from the sources directory
# in a single, easy to run operation that doesn't pollute the
# src directory.
import json
import shutil
import zipfile
##############################################
# Settings - Edit This First
# Include Version Number
INCLUDE_VERSION = True
#INCLUDE_VERSION = False
# Targets - The output filenames to save to
TARGETS = ["GobStopper{version_str}.xpi"]
# List of files to include (relative to the "src" directory)
SRC_ROOT = "src/"
SOURCES = [
"manifest.json",
"background.js",
"content.js",
"icon.svg",
]
##############################################
# Utilities
# Read version string from manifest
# > returns: (str) Version number string
def getVersionString():
if INCLUDE_VERSION:
# Read manifest to find the current version number
with open("src/manifest.json") as f:
manifest = json.load(f)
# Read the version string
versionStr = manifest["version"]
# Reformat it for use in a filename
# - Prefix with hypen
# - Use underscores instead of dots (so extensions don't get weird)
return "-%s" % (versionStr.replace('.', '_'))
else:
# Don't include a version number
return ""
# Check if the given filename is writeable
# < fileN: (str) Path to check
# > returns: (bool) Whether file can be written to
def probeFilenameWriteable(fileN):
try:
with open(fileN, 'w') as f:
return True
# TODO: return False otherwise?
except OSError:
return False
# Write the files specified in SOURCES into a zip file
# < fileN: (str) Name/path of zip file to write to
def writeZipFile(fileN):
with zipfile.ZipFile(fileN, 'w') as zf:
for fileN in SOURCES:
path = SRC_ROOT + fileN
zf.write(path, fileN)
# Force overwrite the dst file (in spite of Windows File Locks)
# < src: (str) Temporary file name the data is currently stored in
# < dst: (str) The filename that the ZIP file should actually have
def replaceZipFile(*, src="tempName.zip", dst="realName.zip"):
print(f"Copying '{src}' to '{dst}'...")
shutil.move(src, dst)
##############################################
# Write zipfiles
versionStr = getVersionString()
for targetName in TARGETS:
targetName = targetName.format(version_str=versionStr)
print(f"Creating {targetName}...")
if probeFilenameWriteable(targetName):
writeZipFile(targetName)
else:
tempName = targetName + ".new.zip"
writeZipFile(tempName)
replaceZipFile(src=tempName, dst=targetName)
input("Done! Press Enter to continue...")