forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crash_report_to_plist.py
executable file
·176 lines (146 loc) · 3.87 KB
/
crash_report_to_plist.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Convert a UKCrashReport into a usable plist file.
# Uses the rather spiffy Python Lex-Yacc tool available here:
# http://www.dabeaz.com/ply/
# and checked in under ply/
import ply.lex as lex
import ply.yacc as yacc
import datetime
import time
tokens = (
'QUOTED_WORD', 'WORD','EQUALS', 'BEGIN_BLOCK','END_BLOCK', 'REAL', 'SEMI', "BEGIN_ARRAY", "END_ARRAY", "COMMA", "BEGIN_TUPLE", "END_TUPLE", "DATE", "TIME"
)
# Tokens
t_WORD = r'[a-zA-Z0-9_.\-+]+'
t_QUOTED_WORD = r'"[^"]*"'
t_EQUALS = r'='
t_BEGIN_BLOCK = r'{'
t_END_BLOCK = r'}'
t_REAL = r'[0-9]*\.[0-9][0-9]*(e[+\-][0-9]+)'
t_SEMI = r';'
t_BEGIN_ARRAY = r'\('
t_END_ARRAY = r'\)'
t_COMMA = r','
t_BEGIN_TUPLE = "<"
t_END_TUPLE = ">"
t_DATE = r'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
t_TIME = r'[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
# Ignored characters
t_ignore = " \t"
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
import ply.lex as lex
lex.lex()
# Parsing rules
precedence = (
)
# dictionary of names
names = { }
def p_plist(t):
'plist : block_value'
t[0] = t[1]
print t[0]
def p_dict_value(t):
'''dict_value : dict_value key EQUALS value SEMI
|'''
if len(t) == 1:
t[0] = ""
else:
t[0] = "%s<key>%s</key>\n%s\n" % (t[1], t[2], t[4])
def p_key(t):
'''key : WORD
| quoted_phrase'''
t[0] = t[1]
def p_quoted_phrase(t):
'quoted_phrase : QUOTED_WORD'
t[0] = t[1][1:-1]
def p_value(t):
'''value : block_value
| array_value
| tuple_value
| real_value
| ambiguous_value
| date_value
| string_value'''
t[0] = t[1]
def p_tuple_value(t):
'tuple_value : BEGIN_TUPLE tuple_entries END_TUPLE'
t[0] = "<array>%s</array>" % t[2]
def p_tuple_entries(t):
'''tuple_entries : value tuple_entries
| value'''
if len(t) == 2:
t[0] = t[1]
else:
t[0] = "%s\n%s" % (t[1], t[2])
def p_array_value(t):
'array_value : BEGIN_ARRAY array_entries END_ARRAY'
t[0] = "<array>%s</array>" % t[2]
def p_array_entries(t):
'''array_entries : value COMMA array_entries
| value
|'''
if len(t) == 1:
t[0] = ""
elif len(t) == 2:
t[0] = t[1]
else:
t[0] = "%s\n%s" % (t[1], t[3])
def p_ambiguous_value(t):
'ambiguous_value : WORD'
try:
i = int(t[1])
t[0] = "<integer>%s</integer>" % i
except ValueError:
t[0] = "<string>%s</string>" % t[1]
def RejiggerDate(d):
parts = d.split(" ")
tz = parts[-1]
nozonedate = " ".join(parts[:-1])
st = time.strptime(nozonedate, "%Y-%m-%d %H:%M:%S")
dt = datetime.datetime(*st[:6])
i = int(tz)
hdiff = i / 100
mdiff = abs(i) % 100
if i < 0:
mdiff *= -1
sdiff = hdiff * 3600 + mdiff * 60
dt -= datetime.timedelta(0, sdiff)
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
def p_date_value(t):
'date_value : DATE TIME WORD'
t[0] = "<date>%s</date>" % RejiggerDate("%s %s %s" % (t[1], t[2], t[3]))
def p_string_value(t):
'string_value : quoted_phrase'
t[0] = "<string>%s</string>" % t[1]
def p_real_value(t):
'real_value : REAL'
t[0] = "<real>%s</real>" % t[1]
def p_block_value(t):
'block_value : BEGIN_BLOCK dict_value END_BLOCK'
t[0] = "<dict>\n%s</dict>" % t[2]
def p_error(t):
print("Syntax error at ", t)
import ply.yacc as yacc
yacc.yacc()
s = None
while 1:
try:
line = raw_input()
if s is None:
line.strip()
if line == "Preferences:":
s = ""
else:
s += line
except EOFError:
break
print '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">'''
yacc.parse(s)
print "</plist>"