-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdb_label_year_month.py
executable file
·103 lines (95 loc) · 4.68 KB
/
gdb_label_year_month.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
#!/usr/bin/env python3
# XXX This is not a Bunsen analysis, just a standalone script to label
# a GDB buildbot repository with year_month.txt files for use with the
# 'timeslice' option in +gdb/commit_logs. You probably don't need to
# use this unless you've decided to prepare your own GDB dataset and
# are having difficulty doing it all at once.
import sys
import os
import dateutil.parser
import lzma
# XXX Duplicates +gdb/parse_dejagnu openfile_or_xz():
def openfile_or_xz(path):
if os.path.isfile(path):
return open(path, mode='rt')
elif os.path.isfile(path+'.xz'):
return lzma.open(path+'.xz', mode='rt')
return open(path, mode='rt') # XXX trigger default error
# XXX Duplicates +gdb/commit_logs find_file_or_xz():
def find_file_or_xz(testdir, name):
if os.path.isfile(os.path.join(testdir, name)):
return os.path.join(testdir, name)
if os.path.isfile(os.path.join(testdir, name+'.xz')):
return os.path.join(testdir, name+'.xz')
return None
# XXX Duplicates +gdb/commit_logs is_testdir():
def is_testdir(testdir):
if not os.path.isfile(os.path.join(testdir,'README.txt')):
return False
if find_file_or_xz(testdir,'gdb.log') is None:
return False
if find_file_or_xz(testdir,'gdb.sum') is None:
return False
return True
# XXX Duplicates some of the traversal code in +gdb/commit_logs
# and the parsing code in +gdb/parse_dejagnu annotate_dejagnu_log():
if __name__=='__main__':
log_src = sys.argv[1]
for logdir in os.listdir(log_src):
logdir = os.path.join(log_src, logdir)
if not os.path.isdir(logdir): continue
for bigdir in os.listdir(logdir):
# TODO: also check if bigdir is a testdir
bigdir = os.path.join(logdir, bigdir)
if not os.path.isdir(bigdir): continue
for testdir in os.listdir(bigdir):
test_sha = testdir
testdir = os.path.join(bigdir, testdir)
if not os.path.isdir(testdir): continue
if not is_testdir(testdir): continue
if os.path.isfile(os.path.join(testdir, "year_month.txt")):
#pass # XXX to test how long it takes to scan everything from scratch
continue
logfile = os.path.join(testdir, "gdb.log")
f = openfile_or_xz(logfile)
year_month = None
if True: # this is slow but more thorough
try:
relevant_lines = f.readlines()
except UnicodeDecodeError: # yep, it happens
relevant_lines = []
if False: # this is fast but ugly and doesn't work with lzma
relevant_lines = []
first = f.readline() # Read the first line.
second = f.readline()
third = f.readline()
relevant_lines += [first,second,third]
f.seek(-2, os.SEEK_END) # Jump to the second last byte.
while f.read(1) != b"\n": # Until EOL is found...
f.seek(-2, os.SEEK_CUR) # ...jump back the read byte plus one more.
last = f.readline() # Read last line.
relevant_lines += [last]
for line in relevant_lines:
if (line.startswith("Test Run By") and " on " in line) \
or (" completed at " in line):
if line.startswith("Test Run By"):
t1 = line.rfind(" on ") + len(" on ")
else:
t1 = line.find(" completed at ") \
+ len(" completed at ")
datestamp = line[t1:].strip()
try:
datestamp = dateutil.parser.parse(datestamp)
# XXX datetime is a bit too brittle in practice.
#datestamp = datetime.strptime(datestamp, datestamp_format)
year_month = datestamp.strftime("%Y-%m")
except ValueError:
print("WARNING: unknown datestamp in line --",
line, file=sys.stderr)
f.close()
if year_month is not None:
print("FOUND {} for {}".format(year_month, testdir))
with open(os.path.join(testdir,"year_month.txt"), 'w') as f:
f.write(year_month)
else:
print("NO YEAR_MONTH FOUND FOR {}".format(testdir))