-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_md5_local.py
67 lines (47 loc) · 1.68 KB
/
get_md5_local.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
import hashlib
import os
import io
import sys
from xml.dom.minidom import Document
def writeFileInfo(doc, filelist, filename, dirname, md5string):
file = doc.createElement('file')
filelist.appendChild(file)
file.setAttribute("filename", filename)
file.setAttribute("dir", dirname)
file.setAttribute("md5", md5string)
def getFileMd5(filename):
m = hashlib.md5()
file = io.FileIO(filename,'r')
bytes = file.read(1024)
while(bytes != b''):
m.update(bytes)
bytes = file.read(1024)
file.close()
return m.hexdigest()
def main():
if 2 > len(sys.argv) :
print("Please input the file name to save!")
return
saveFileName = sys.argv[1]
doc = Document()
filelist = doc.createElement('filelist')
doc.appendChild(filelist)
appName = os.path.basename(__file__)
for root, dirs, files in os.walk("."):
for f in files:
full_file = os.path.join(root,f)
dirname = os.path.dirname(full_file)
if dirname == "." :
dirname = ""
else :
dirname = dirname[2:] #remove ./ in dirname
dirname = dirname.replace("\\", '/')
filename = os.path.basename(full_file)
#the file does not written into the config
if appName == filename or saveFileName == filename or "user.ini" == filename or "get_latest_gv.exe" == filename:
continue
writeFileInfo(doc, filelist, filename, dirname, getFileMd5(full_file))
# save the config file
with open(saveFileName, 'wb') as f:
f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))
main()