-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunbundle.py
53 lines (43 loc) · 1.26 KB
/
unbundle.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
# this file is taken from Matt Basta's code
# https://github.com/mattbasta/amo-validator/blob/master/extras/unbundle.py
import sys
import os
import zipfile
from zipfile import ZipFile
from StringIO import StringIO
source = sys.argv[1]
target = sys.argv[2]
if not target.endswith("/"):
target = "%s/" % target
def _unbundle(path, target):
zf = ZipFile(path, 'r')
contents = zf.namelist()
for item in contents:
sp = item.split("/")
if not sp[-1]:
continue
if "__MACOSX" in item:
continue
print item, ">", target + item
cpath = target + "/".join(sp[:-1])
if not os.path.exists(cpath):
os.makedirs(cpath)
if item.endswith((".jar", ".xpi", ".zip")):
now = target + item
path_item = item.split("/")
path_item[-1] = "_" + path_item[-1]
path = target + "/".join(path_item)
buff = StringIO(zf.read(item))
_unbundle(buff, path + "/")
else:
f = open(target + item, 'w')
f.write(zf.read(item))
f.close()
zf.close()
if not os.path.exists(target):
os.mkdir(target)
try:
_unbundle(source, target)
except:
erf = open(target + "failed", 'w')
erf.close()