-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_model_info.py
67 lines (55 loc) · 2 KB
/
update_model_info.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import hashlib
import os
import sys
import subprocess
DEBUG = False
def get_file_size_mb(file_path):
try:
file_size = os.path.getsize(file_path)
if DEBUG: print(file_size)
return file_size / (1000000.)
except Exception as err:
print(err)
def get_file_md5(file_path):
if not os.path.isfile(file_path):
return ""
myhash = hashlib.md5()
f = open(file_path, "rb")
while True:
b = f.read(8096)
if not b:
break
myhash.update(b)
f.close()
if DEBUG: print(myhash.hexdigest())
return myhash.hexdigest()
def main():
output_file_name = "README.md"
work_dir = sys.argv[1] if len(sys.argv) > 1 else "."
output = subprocess.Popen(['ls -l ' + work_dir],stdout=subprocess.PIPE, shell=True).communicate()
summary_str = ""
summary_header = "| fileName | fileAuthor | modifyDate | MB | md5 |\n"
summary_format = "|:--|:--|:--|:--|:--|\n"
summary_str += summary_header + summary_format
print output[0]
each_file_lines = output[0].split("\n")[1:]
for line_idx in range(len(each_file_lines)):
line = each_file_lines[line_idx]
line_info_list = line.split()
if DEBUG: print line_info_list
author = "/".join(line_info_list[2:4]) if len(line_info_list) > 3 else "AuthorIsNull"
size_mb = int(line_info_list[4]) / 1000000. if len(line_info_list) > 4 else "SizeIsNull"
modify_date = "-".join(line_info_list[5:8]) if len(line_info_list) > 7 else "DateIsNull"
name = line_info_list[8] if len(line_info_list) > 8 else "NameIsNull"
md5 = get_file_md5(name)
summary_line_list = [name, author, modify_date, size_mb, md5]
summary_line_list = map(str, summary_line_list)
summary_line = "|" + " | ".join(summary_line_list) + " |\n"
summary_str += summary_line
if DEBUG: print(summary_line)
with open(output_file_name, "w") as out_file:
out_file.write(summary_str)
if __name__ == "__main__":
main()