forked from downthemall/downthemall-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.py
executable file
·514 lines (421 loc) · 14.2 KB
/
make.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env python
import os
import sys
import re
from optparse import OptionParser
from warnings import warn
from io import BytesIO
from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED
from glob import glob
from fnmatch import fnmatch
from time import strftime
from xml.dom.minidom import parseString as XML
from functools import wraps
try:
from xpisign.context import ZipFileMinorCompression as _Minor
Minor = _Minor
except ImportError:
warn("No optimal compression available")
class Minor(object):
""" Compatiblity stub"""
def __init__(self, *args):
pass
def __enter__(self):
pass
def __exit__(self, *args):
pass
try:
from Mozilla.CompareLocales import compareDirs as _compare_locales
compare_locales = _compare_locales
except ImportError:
warn("CompareLocales is not available!")
compare_locales = None
class Reset(object):
"""
Reset the tracked file-like object stream position when done
"""
def __init__(self, fp):
self.fp = fp
def __enter__(self):
self.pos = self.fp.tell()
def __exit__(self, *args):
self.fp.seek(self.pos, 0)
class WorkingDirectory(object):
"""
Change the working directory to make.py's path and restore when done
"""
def __enter__(self):
self.wd = os.getcwd()
try:
os.chdir(os.path.split(__file__)[0])
except:
pass
def __exit__(self, *args):
os.chdir(self.wd)
@staticmethod
def change(f):
"""
Decorator: Change the working directory before calling wrapped
function.
"""
@wraps(f)
def wrapper(*args, **kw):
with WorkingDirectory():
return f(*args, **kw)
return wrapper
NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
NS_EM = "http://www.mozilla.org/2004/em-rdf#"
RELEASE_ID = "{DDC359D1-844A-42a7-9AA1-88A850A938A8}"
FILES = ("install.rdf",
"icon*.png",
"bootstrap.js",
"MPL", "GPL", "LGPL", "LICENSE",
"chrome.manifest",
"chrome/content/about/about.xul",
"chrome/content/common/",
"chrome/content/dta/",
"chrome/content/integration/",
"chrome/content/preferences/",
"chrome/content/privacy/",
"chrome/content/mac/",
"chrome/content/unix/",
"chrome/content/win/",
"chrome/public/",
"chrome/skin/",
"chrome/locale/*/",
"modules/*.js*",
"modules/loaders/",
"modules/manager/",
"modules/support/",
)
TESTS = ("modules/testsupport/",
"tests"
)
EXCLUDED = ("chrome/locale/*/landingpage.dtd",
"chrome/locale/*/description.properties",
)
PLAIN = ("*.png",
"*.jpg",
"*.gif"
)
@WorkingDirectory.change
def check_locales(errors_only=False):
"""
Checks all chrome.manifest listed locales are complete and without errors.
"""
if not compare_locales:
return
listed = dict()
with open("chrome.manifest", "rb") as mp:
r = re.compile(r"^locale\s+.+?\s+(.+?)\s+(.+?)\s*$")
for l in mp:
m = r.match(l)
if not m:
continue
l, p = m.groups()
listed[l] = p
if not listed:
raise ValueError("did not read any locales")
if len(listed) == 1:
return
for x in ("en-US", "en", "de", "fr"):
baseloc = listed.pop(x, None)
if baseloc:
break
else:
raise ValueError("failed to determine base locale")
for l, p in listed.items():
if not os.path.isdir(p):
raise ValueError("Listed locale not available {}".format(l))
res = compare_locales(baseloc, p)
summary = res.getSummary()[None]
if "errors" in summary or (not errors_only and "missing" in summary):
raise ValueError("{}: {}\n{}".format(l, summary, res.details))
def filesort(f):
"""
Package file sort keys
"""
if f in ("install.rdf",):
return 0, f
if f in ("bootstrap.js", "chrome.manifest"):
return 1, f
if fnmatch(f, "icon*.png"):
return 2, f
if fnmatch(f, "modules/*"):
return 3, f
if f in ("MPL", "GPL", "LGPL", "LICENSE"):
return 1000, f
return 500, f
def files(*args, **kw):
"""
Generator over all file listing the given patterns.
All arguments will be considered patterns.
excluded keyword arg may specify a list of patterns that won't be returned
"""
excluded = kw.pop("excluded", ())
def items(f):
if os.path.isdir(f):
if not f.endswith("/"):
f += "/"
for i in files(f + "*"):
yield i
elif os.path.isfile(f) and not any(fnmatch(f, x) for x in excluded):
yield f
for p in args:
gg = glob(p)
if not gg:
raise ValueError("{} did not match anything!".format(p))
for g in gg:
for i in items(g):
yield i
def releaseversionjs(fp, **kw):
""" Preprocess version.js in release mode """
io = BytesIO()
with Reset(io):
for l in fp:
if "const ID = " in l:
print >>io, 'const ID = "{}"'.format(RELEASE_ID)
else:
print >>io, l,
return io
def droptests(fp, **kw):
""" Drop tests from chrome.manifest """
io = BytesIO()
with Reset(io):
for l in fp:
if "dta-tests" in l:
continue
print >>io, l,
return io
def localize(fp, **kw):
""" Generate em:localized """
def sort(f):
if "en-US" in f["locale"]:
return 0, f["locale"]
return 1, f["locale"]
locales = list()
for f in sorted(files("chrome/locale/*/description.properties")):
locale = dict(locale=(f.split("/", 3)[2],))
with open(f, "rb") as lp:
for l in lp:
l = unicode(l, "utf-8").strip()
if not l or l.startswith("#"):
continue
k, v = l.split("=", 1)
k = k.split(".")
k = k[-2] if len(k[-1]) < 3 else k[-1]
if not k or not v:
continue
if not k in locale:
locale[k] = list()
locale[k] += v,
locales += locale,
with Reset(fp):
rdf = XML(fp.read())
# kill old localized
for e in rdf.getElementsByTagNameNS(NS_EM, "localized"):
e.parentNode.removeChild(e)
def mapkey(k):
v = list()
for e in rdf.getElementsByTagNameNS(NS_EM, k):
v += e.firstChild.data,
return k, v
keys = ("locale", "name", "description", "creator", "homepageURL",
"developer", "translator", "contributor")
baseprops = dict(mapkey(k) for k in keys)
parent = rdf.getElementsByTagNameNS(NS_EM, "id")[0].parentNode
for props in sorted(locales, key=sort):
node = rdf.createElementNS(NS_EM, "em:localized")
desc = rdf.createElementNS(NS_RDF, "Description")
for k in keys:
vals = props.get(k, baseprops.get(k, list()))
for v in vals:
n = rdf.createElementNS(NS_EM, "em:" + k)
n.appendChild(rdf.createTextNode(v))
desc.appendChild(n)
parent.appendChild(rdf.createTextNode("\n\t\t"))
node.appendChild(desc)
parent.appendChild(node)
parent.appendChild(rdf.createTextNode("\n\t"))
io = BytesIO()
with Reset(io):
print >>io, rdf.toxml(encoding="utf-8")
rdf.unlink()
return io
def localized(fn):
""" Decorator: Wrap an install.rdf processor to also localize() """
@wraps(fn)
def wrapper(fp, **kw):
fp = fn(fp, **kw)
return localize(fp, **kw)
return wrapper
def releasify(fp, **kw):
with Reset(fp):
rdf = XML(fp.read())
node = rdf.getElementsByTagNameNS(NS_EM, "id")[0].childNodes[0]
node.data = RELEASE_ID
io = BytesIO()
with Reset(io):
print >>io, rdf.toxml(encoding="utf-8")
rdf.unlink()
return io
def set_uurl(fp, **kw):
""" Set the updateURL """
with Reset(fp):
rdf = XML(fp.read())
node = rdf.getElementsByTagNameNS(NS_EM, 'aboutURL')[0]
u = rdf.createElementNS(NS_EM, 'em:updateURL')
u.appendChild(rdf.createTextNode(kw.get("updateurl")))
node.parentNode.insertBefore(u, node)
node.parentNode.insertBefore(rdf.createTextNode('\n\t\t'), node)
io = BytesIO()
with Reset(io):
print >>io, rdf.toxml(encoding="utf-8")
rdf.unlink()
return io
@localized
def releaserdf(fp, **kw):
""" Preprocesses install.rdf for release mode """
with Reset(fp):
rdf = XML(fp.read())
node = rdf.getElementsByTagNameNS(NS_EM, 'version')[0].childNodes[0]
if not re.match(r"^[\d.]+$", node.data) or True:
raise ValueError("Invalid release version: {}".format(node.data))
return releasify(fp, *+kw)
@localized
def betardf(fp, **kw):
""" Preprocess install.rdf for beta mode """
with Reset(fp):
rdf = XML(fp.read())
node = rdf.getElementsByTagNameNS(NS_EM, 'version')[0].firstChild
if not re.match(r"^\d\.\db\d+$", node.data):
raise ValueError("Invalid beta version: {}".format(node.data))
return set_uurl(releasify(fp, **kw), **kw)
@localized
def nightlyrdf(fp, **kw):
""" Preprocesses install.rdf for nightly mode """
rdf = XML(fp.read())
# update the version
node = rdf.getElementsByTagNameNS(NS_EM, 'version')[0].childNodes[0]
node.data += strftime(".%Y%m%d.%Hh%Mm%Ss")
# a new name
node = rdf.getElementsByTagNameNS(NS_EM, 'name')[0].childNodes[0]
node.data += " *nightly*"
io = BytesIO()
with Reset(io):
print >>io, rdf.toxml(encoding="utf-8")
rdf.unlink()
return set_uurl(io, **kw)
@localized
def devrdf(fp, **kw):
""" Preprocesses install.rdf for default mode """
rdf = XML(fp.read())
node = rdf.getElementsByTagNameNS(NS_EM, 'name')[0].childNodes[0]
node.data += " *unofficial developer build*"
io = BytesIO()
with Reset(io):
print >>io, rdf.toxml(encoding="utf-8")
rdf.unlink()
return io
@WorkingDirectory.change
def pack(xpi, patterns, **kw):
""" Build the actual XPI """
packing = sorted(set(files(*patterns, excluded=EXCLUDED)),
key=filesort)
with ZipFile(xpi, "w", ZIP_DEFLATED) as zp:
def write(fn, mode, modifier=None):
with file(fn, "rb") as fp:
if modifier:
with modifier(fp, **kw) as mp:
zp.writestr(fn, mp.read(), mode)
else:
zp.writestr(fn, fp.read(), mode)
with Minor(zp):
for f in packing:
if f == "modules/version.js" and \
kw.get("type", None) in ("release", "beta"):
write(f, ZIP_DEFLATED, releaseversionjs)
elif f == "install.rdf":
t = kw.get("type", None)
if t == "release":
write(f, ZIP_DEFLATED, releaserdf)
elif t == "beta":
write(f, ZIP_DEFLATED, betardf)
elif t == "nightly":
write(f, ZIP_DEFLATED, nightlyrdf)
else:
write(f, ZIP_DEFLATED, devrdf)
elif not kw.get("tests", False) and f == "chrome.manifest":
write(f, ZIP_DEFLATED, droptests)
elif any(fnmatch(f, p) for p in PLAIN):
write(f, ZIP_STORED)
else:
write(f, ZIP_DEFLATED)
def create(args):
""" Process arguments and create the XPI """
parser = OptionParser()
parser.add_option("--force",
dest="force",
help="force overwrite output file if exists",
action="store_true",
default=False
)
parser.add_option("--release",
dest="type",
help="create release XPI",
action="store_const",
const="release"
)
parser.add_option("--beta",
dest="type",
help="create release XPI",
action="store_const",
const="beta"
)
parser.add_option("--nightly",
dest="type",
help="create nightly XPI",
action="store_const",
const="nightly"
)
parser.add_option("--updateURL",
dest="updateurl",
help="nightly update url",
type="string",
default=None
)
parser.add_option("--tests",
dest="tests",
help="ships tests as well",
action="store_true",
default=False
)
opts, args = parser.parse_args(args)
patterns = FILES
if opts.tests:
patterns += TESTS
if len(args) != 1:
raise ValueError("No distinct XPI name provided")
output = args[0]
if opts.type in ("nightly", "beta") and not opts.updateurl:
raise ValueError("Nightly/Beta requested but no update URL provided")
elif opts.type == "release" and opts.updateurl:
raise ValueError("Release versions cannot have an update URL")
if not opts.force and os.path.exists(output):
raise ValueError("Output file already exists")
check_locales(errors_only=True)
with BytesIO() as io:
try:
with Reset(io):
pack(io, patterns, **opts.__dict__)
except Exception as ex:
raise Exception("Failed packing: {}".format(ex)), None, \
sys.exc_info()[2]
try:
with open(output, "wb") as op:
op.write(io.read())
except Exception as ex:
raise Exception("Failed writing XPI: {}".format(ex)), None, \
sys.exc_info()[2]
if __name__ == "__main__":
create(sys.argv[1:])