-
Notifications
You must be signed in to change notification settings - Fork 6
/
genwgtu.py
145 lines (110 loc) · 4.36 KB
/
genwgtu.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import zipfile
import hashlib
import json
import re
from xml.dom.minidom import Document
def md5sum(str):
hash = hashlib.md5()
hash.update(str)
return hash.hexdigest()
def generateXml(newId, oldVersion, delNameList):
doc = Document()
wgtu = doc.createElement("wgtu")
wgtu.setAttribute("appid", newId)
doc.appendChild(wgtu)
basis = doc.createElement("basis")
basis.setAttribute("version", oldVersion)
wgtu.appendChild(basis)
if (len(delNameList) > 0 ):
remove = doc.createElement("remove")
for it in delNameList:
item = doc.createElement("item")
item.setAttribute("path", it)
remove.appendChild(item)
wgtu.appendChild(remove)
return doc.toprettyxml(indent="\t", encoding="utf-8")
def compareWgt(oldZf, newZf):
newNameSet = set(newZf.namelist())
oldNameSet = set(oldZf.namelist())
addNameList = list(newNameSet - oldNameSet)
#print "addNameList:", addNameList
delNameList = list(oldNameSet - newNameSet)
#print "delNameList:", delNameList
regexp = re.compile(r'(.*/).*')
delDirList = [regexp.sub("\g<1>", x) for x in delNameList if x.find("/") >= 0]
#remove duplate item
delDirList = list(set(delDirList))
#print "delDirList:", delDirList
def isRealDel(dir):
for x in newZf.namelist():
if x.startswith(dir):
return False
return True
realDelDirList = [x for x in delDirList if isRealDel(x)]
#print "realDelDirList:", realDelDirList
def isSubDelItem(name):
for x in realDelDirList:
if name.startswith(x):
return True
return False
delNameList = [x for x in delNameList if not isSubDelItem(x)]
#print "delNameList:", delNameList
delNameDirList = delNameList + realDelDirList
#print "delNameDirList:", delNameDirList
commNameList = list(newNameSet & oldNameSet)
#print "commNameList:", commNameList
newNameMd5TupList = zip(commNameList, [md5sum(newZf.read(x)) for x in commNameList])
oldNameMd5TupList = zip(commNameList, [md5sum(oldZf.read(x)) for x in commNameList])
modNameMd5TupSet = set(newNameMd5TupList) - set(oldNameMd5TupList)
modNameList = [x for x, y in modNameMd5TupSet]
#print "modNameList:", modNameList
return addNameList, delNameDirList, modNameList
def main(oldWgt, newWgt, outWgtu = None):
try:
oldZf = zipfile.ZipFile(oldWgt, "r")
newZf = zipfile.ZipFile(newWgt, "r")
except IOError, e:
print >> sys.stderr, e
return False
newManifest = json.loads(newZf.read("manifest.json"), "utf-8")
oldManifest = json.loads(oldZf.read("manifest.json"), "utf-8")
newInfo = {"id": newManifest["id"], "version": newManifest["version"]["name"] }
oldInfo = {"id": oldManifest["id"], "version": oldManifest["version"]["name"] }
if (newInfo["id"] != oldInfo["id"]):
print >> sys.stderr, "app id are not equals in thier mainfest.json"
return False
if (newInfo["version"] <= oldInfo["version"]):
print >> sys.stderr, "new app version must greater than old app"
return False
addNameList, delNameDirList, modNameList = compareWgt(oldZf, newZf)
if (len(addNameList) == 0 and len(delNameDirList) == 0 and len(modNameList) == 0):
print >> sys.stderr, "two wgt files are identical, do not need generate wgtu package."
return False
if (outWgtu is None):
outWgtu = "update-diff-" + oldInfo["version"] + "-" + newInfo["version"] + ".wgtu"
try:
outZf = zipfile.ZipFile(outWgtu, "w", zipfile.ZIP_DEFLATED)
for name in modNameList:
outZf.writestr("www/" + name, newZf.read(name))
for name in addNameList:
outZf.writestr("www/" + name, newZf.read(name))
xml = generateXml(newInfo["id"], oldInfo["version"], delNameDirList)
outZf.writestr("update.xml", xml)
outZf.close()
print "output: " + outWgtu
except IOError, e:
print >> sys.stderr, e
return False
finally:
oldZf.close()
newZf.close()
if __name__ == "__main__":
myargv = sys.argv[1:]
if (len(myargv) > 3 or len(myargv) < 2):
print >> sys.stderr, "\nUsage: %s <old.wgt> <new.wgt> [ -o out.wgtu]\n" % sys.argv[0]
exit(1)
else:
main(*myargv)