Skip to content

Commit 3e4856a

Browse files
committed
Create zip info based on pre-generated zip
Before, when I tried constructing the zip metadata from scratch, FF would give a corrupt file error when trying to load the xpi.
1 parent 51480ad commit 3e4856a

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

utils/create_xpi.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,42 @@
77
xpiName = sys.argv[1]
88
exclusionsFile = sys.argv[2]
99
exclusions = []
10+
tmpfile = '../pkg/tmp.xpi'
1011

1112
with open(exclusionsFile) as f:
1213
for line in f:
1314
exclusions.extend(glob.glob(line.strip()))
1415

1516
xpiFile = zipfile.ZipFile(xpiName, mode='w')
1617

17-
def constructZipInfo(filename):
18-
"""Create deterministic ZIP metadata for a given file"""
19-
info = zipfile.ZipInfo(filename, time.gmtime(1378343307))
20-
info.compress_type = 0
21-
info.comment = ''
22-
info.extra = ''
23-
info.create_system = 3
24-
info.create_version = 20
25-
info.extract_version = 20
26-
if os.path.isfile(filename):
27-
info.external_attr = 0664 << 16L
28-
elif os.path.isdir(filename):
29-
info.external_attr = 04775 << 16L
30-
# info.external_attr = 2176057344L
31-
info.volume = 0
32-
return info
18+
def createTmpZipInfo():
19+
"""
20+
Create a non-deterministic zip in order to use the file info
21+
generated to create a deterministic zip
22+
"""
23+
xpiFileTmp = zipfile.ZipFile(tmpfile, mode='w')
24+
for root,subfolders,files in os.walk('.'):
25+
for fi in files:
26+
filename = os.path.join(root,fi)
27+
if filename not in map(lambda x: './'+x, exclusions):
28+
xpiFileTmp.write(filename)
29+
xpiFileTmp.close()
30+
return xpiFileTmp.infolist()
3331

34-
for root,subfolders,files in os.walk('.'):
35-
for fi in files:
36-
filename = os.path.join(root,fi)
37-
if filename not in map(lambda x: './'+x, exclusions):
38-
print("Adding to zip: "+filename)
39-
info = constructZipInfo(filename)
40-
xpiFile.writestr(info, open(filename).read())
32+
def constructZipDet():
33+
"""
34+
Create a deterministic zip by setting timestamps and
35+
system/version info to hard-coded values
36+
"""
37+
tmpInfo = createTmpZipInfo()
38+
for info in tmpInfo:
39+
info.date_time = time.gmtime(1378343307)
40+
info.create_system = 3
41+
info.create_version = 20
42+
info.extract_version = 20
43+
print("adding to zip: "+info.filename)
44+
xpiFile.writestr(info, open(info.filename).read())
4145

46+
constructZipDet()
4247
xpiFile.close()
48+
os.remove(tmpfile)

0 commit comments

Comments
 (0)