-
Notifications
You must be signed in to change notification settings - Fork 10
/
makelog.py
71 lines (64 loc) · 2.4 KB
/
makelog.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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os, time, sys
'''
script to make an entire log from report(fail|pass|error) folder.
'''
DEFAULT_ALL_LOG_NAME = 'alllog.txt'
LOGCAT_FILE_NAME = 'logcat.txt'
TIMESTAMP_FORMAT = '%Y-%m-%d_%H:%M:%S'
def show_usage():
print 'usage:'
print '\tpython makelog.py [-h|--help] <-d REPORT_DIRECTORY_PATH> [-f ENTIRE_LOG_FILE_NAME]\n\n'
print 'Process the paramters of makelog'
print 'optional arguments:'
print '\t-h, --help Show this help message and exit\n'
print '\t-d REPORT_DIRECTORY_PATH Set the directory path of report\n'
print '\t-f ENTIRE_LOG_FILE_NAME Set the file name of entire log with the related or absolute path\n'
exit(1)
def callback(fs):
name = os.path.basename(fs)
p,f = name.split('@')
return int(time.mktime(time.strptime(f, TIMESTAMP_FORMAT)))
def gen_all(wd):
gen = os.walk(wd)
root_path, target_dir, _ = gen.next()
for td in target_dir:
inner_path, case_dir , __ = os.walk(os.path.join(root_path,td)).next()
for cd in case_dir:
yield os.path.abspath(os.path.join(inner_path, cd))
def make_log(dirs, fname):
log_path = None
if os.path.isabs(fname):
log_path = fname
else:
log_path = os.path.join(os.getcwd(), fname)
#if os.path.exists(log_path): raise Exception('%s already exists' % log_path)
a = sorted(gen_all(dirs), key=callback)
with open(log_path, 'wa+') as l:
for f in a:
lf = os.path.join(os.path.join(f, 'logs'), LOGCAT_FILE_NAME)
content = None
try:
with open(lf, 'r') as ff:
content = ff.read()
l.write(content)
except:
print 'file not exists. ignore it.'
print '\n%s saved success.' % log_path
if __name__ == '__main__':
report_dir = None
entire_log_name = DEFAULT_ALL_LOG_NAME
if '-h' in sys.argv or '--help' in sys.argv:
show_usage()
if '-f' in sys.argv:
index = sys.argv.index('-f')
entire_log_name = sys.argv[int(index)+1]
if '-d' not in sys.argv:
show_usage()
if '-d' in sys.argv:
index = sys.argv.index('-d')
report_dir = os.path.abspath(sys.argv[int(index)+1])
if not os.path.exists(report_dir) and not os.path.isdir(report_dir):
show_usage()
make_log(report_dir, entire_log_name)