1
1
#!/usr/bin/env python
2
-
3
2
import os
4
3
import sys
5
4
import re
5
+ import ConfigParser
6
+
7
+ #sourceroot = "/Users/epc/Documents/github/epc/mongo"
8
+ #errorsrst = "/Users/epc/Documents/github/epc/docs/draft/messages/errors.txt"
9
+ #errorsCSV = "/Users/epc/Documents/github/epc/docs/draft/messages/errors.csv"
10
+ #errorsTitle = "MongoDB Error and Message Codes"
11
+
12
+ config = ConfigParser .SafeConfigParser ()
13
+ config .read ('errorcodes.conf' )
14
+
15
+ sourceroot = config .get ('errorcodes' ,'source' )
16
+ resultsRoot = config .get ('errorcodes' , 'outputDir' )
17
+ generateCSV = config .get ('errorcodes' ,'generateCSV' )
18
+ errorsTitle = config .get ('errorcodes' , 'Title' )
19
+ errorsFormat = config .get ('errorcodes' , 'Format' )
20
+
21
+ default_domain = "\n \n .. default-domain:: mongodb\n \n "
22
+
23
+ sys .path .append (sourceroot + "/buildscripts" )
24
+
25
+ # get mongodb/buildscripts/utils.py
6
26
import utils
7
27
8
- sourceroot = "/Users/epc/Documents/github/epc/mongo"
9
- errorsrst = "/Users/epc/Documents/github/epc/docs/draft/messages/errors.txt"
10
- errorsCSV = "/Users/epc/Documents/github/epc/docs/draft/messages/errors.csv"
11
- errorsTitle = "MongoDB Error and Message Codes"
12
28
13
29
assertNames = [ "uassert" , "massert" , "fassert" , "fassertFailed" ]
14
30
@@ -58,99 +74,27 @@ def assignErrorCodes():
58
74
59
75
codes = []
60
76
61
- def XreadErrorCodes ( callback , replaceZero = False ):
62
-
77
+ def readErrorCodes ():
78
+ """Open each source file in sourceroot and scan for potential error messages."""
63
79
quick = [ "assert" , "Exception" ]
64
80
65
- ps = [ re .compile ( "(([umsgf]asser(t|ted))) *\(( *)(\d+)" ) ,
66
- re .compile ( "((User|Msg|MsgAssertion)Exceptio(n))\(( *)(\d+)" ),
67
- re .compile ( "streamNotGood" ),
68
- re .compile ( "((fassertFailed)()) *\(( *)(\d+)" )
69
- ]
70
-
71
- bad = [ re .compile ( "\sassert *\(" ) ]
72
- arr = []
73
- for x in utils .getAllSourceFiles (arr ,sourceroot ):
74
-
75
- needReplace = [False ]
76
- lines = []
77
- lastCodes = [0 ]
78
- lineNum = 1
79
-
80
- for line in open ( x ):
81
-
82
- found = False
83
- for zz in quick :
84
- if line .find ( zz ) >= 0 :
85
- found = True
86
- break
87
-
88
- if found :
89
-
90
- if x .find ( "src/mongo/" ) >= 0 :
91
- for b in bad :
92
- if len (b .findall ( line )) > 0 :
93
- print ( x )
94
- print ( line )
95
- raise Exception ( "you can't use a bare assert" )
96
-
97
- for p in ps :
98
-
99
- def repl ( m ):
100
- m = m .groups ()
101
-
102
- start = m [0 ]
103
- spaces = m [3 ]
104
- code = m [4 ]
105
- if code == '0' and replaceZero :
106
- code = getNextCode ( lastCodes )
107
- lastCodes .append ( code )
108
- code = str ( code )
109
- needReplace [0 ] = True
110
-
111
- print ( "Adding code " + code + " to line " + x + ":" + str ( lineNum ) )
112
-
113
- else :
114
- codes .append ( ( x , lineNum , line , code ) )
115
- callback ( x , lineNum , line , code )
116
-
117
- return start + "(" + spaces + code
118
-
119
- line = re .sub ( p , repl , line )
120
- # end if ps loop
121
-
122
- if replaceZero : lines .append ( line )
123
- lineNum = lineNum + 1
124
-
125
- if replaceZero and needReplace [0 ] :
126
- print ( "Replacing file " + x )
127
- of = open ( x + ".tmp" , 'w' )
128
- of .write ( "" .join ( lines ) )
129
- of .close ()
130
- os .remove (x )
131
- os .rename ( x + ".tmp" , x )
132
-
133
-
134
- def readErrorCodes ( callback , replaceZero = False ):
135
-
136
- quick = [ "assert" , "Exception" ]
137
-
138
- # ps = [ re.compile( "(([umsgf]asser(t|ted))) *\(( *)(\d+)" ) ,
139
- # re.compile( "((DB|User|Msg|MsgAssertion)Exceptio(n))\(( *)(\d+)" ),
140
- # re.compile( "((fassertFailed)()) *\(( *)(\d+)" )
141
- # ]
142
81
ps = [ re .compile ( "(([wum]asser(t|ted))) *\(( *)(\d+) *,\s*(\" \S[^\" ]+\S\" )\s*,?.*" ) ,
143
82
re .compile ( "(([wum]asser(t|ted))) *\(( *)(\d+) *,\s*([\S\s+<\(\)\" ]+) *,?.*" ) ,
144
83
re .compile ( '((msgasser(t|ted))) *\(( *)(\d+) *, *(\" \S[^,^\" ]+\S\" ) *,?' ) ,
84
+ re .compile ( '((msgasser(t|ted)NoTrace)) *\(( *)(\d+) *, *(\" \S[^,^\" ]+\S\" ) *,?' ) ,
145
85
re .compile ( "((fasser(t|ted))) *\(( *)(\d+)()" ) ,
146
86
re .compile ( "((DB|User|Msg|MsgAssertion)Exceptio(n))\(( *)(\d+) *,? *(\S+.+\S) *,?" ),
147
- re .compile ( "((fassertFailed)()) *\(( *)(\d+)()" )
87
+ re .compile ( "((fassertFailed)()) *\(( *)(\d+)()" ),
88
+ re .compile ( "(([wum]asser(t|ted)))\s*\(([^\d]*)(\d+)\s*,?()" ),
89
+ re .compile ( "((msgasser(t|ted)))\s*\(([^\d]*)(\d+)\s*,?()" ),
90
+ re .compile ( "((msgasser(t|ted)NoTrace))\s*\(([^\d]*)(\d+)\s*,?()" ),
91
+
148
92
]
149
93
150
94
bad = [ re .compile ( "\sassert *\(" ) ]
151
95
arr = []
152
96
for x in utils .getAllSourceFiles (arr ,sourceroot ):
153
-
97
+ sys . stderr . write ( "Analyzing: {} \n " . format ( x ))
154
98
needReplace = [False ]
155
99
lines = []
156
100
lastCodes = [0 ]
@@ -184,35 +128,13 @@ def repl( m ):
184
128
spaces = m [3 ]
185
129
code = m [4 ]
186
130
message = m [5 ]
187
- # if code == '0' and replaceZero :
188
- # code = getNextCode( lastCodes )
189
- # lastCodes.append( code )
190
- # code = str( code )
191
- # needReplace[0] = True
192
- #
193
- # print( "Adding code " + code + " to line " + x + ":" + str( lineNum ) )
194
- #
195
- # else :
196
131
codes .append ( ( x , lineNum , line , code , message , severity ) )
197
- print ("x(" + x + ") lineNum(" + str (lineNum ) + ") line(" + line .strip (stripChars ) + ") spaces(" + spaces + ") code(" + code + ")" )
198
- callback ( x , lineNum , line , code )
199
132
200
133
return start + "(" + spaces + code
201
134
202
135
line = re .sub ( p , repl , line )
203
- print ("line(" + line + ")" )
204
- # end if ps loop
205
136
206
- # if replaceZero : lines.append( line )
207
137
lineNum = lineNum + 1
208
- #
209
- # if replaceZero and needReplace[0] :
210
- # print( "Replacing file " + x )
211
- # of = open( x + ".tmp", 'w' )
212
- # of.write( "".join( lines ) )
213
- # of.close()
214
- # os.remove(x)
215
- # os.rename( x + ".tmp", x )
216
138
217
139
218
140
@@ -252,17 +174,25 @@ def getBestMessage( err , start ):
252
174
return err
253
175
254
176
def genErrorOutput ():
255
-
256
- if os .path .exists (errorsrst ):
257
- i = open (errorsrst , "r" )
258
- out = open ( errorsrst , 'wb' )
259
- titleLen = len (errorsTitle )
260
- out .write (":orphan:\n " )
261
- out .write ("=" * titleLen + "\n " )
262
- out .write (errorsTitle + "\n " )
263
- out .write ("=" * titleLen + "\n " )
264
-
265
- out .write ("\n \n .. default-domain:: mongodb\n \n " );
177
+ """Sort and iterate through codes printing out error codes and messages in RST format."""
178
+ sys .stderr .write ("Generating RST files\n " );
179
+ separatefiles = False
180
+ if errorsFormat == 'single' :
181
+ errorsrst = resultsRoot + "/errors.txt"
182
+ if os .path .exists (errorsrst ):
183
+ i = open (errorsrst , "r" )
184
+ out = open ( errorsrst , 'wb' )
185
+ sys .stderr .write ("Generating single file: {}\n " .format (errorsrst ))
186
+ titleLen = len (errorsTitle )
187
+ out .write (":orphan:\n " )
188
+ out .write ("=" * titleLen + "\n " )
189
+ out .write (errorsTitle + "\n " )
190
+ out .write ("=" * titleLen + "\n " )
191
+ out .write (default_domain );
192
+ elif errorsFormat == 'separate' :
193
+ separatefiles = True
194
+ else :
195
+ raise Exception ("Unknown output format: {}" .format (errorsFormat ))
266
196
267
197
prev = ""
268
198
seen = {}
@@ -271,28 +201,31 @@ def genErrorOutput():
271
201
stripChars = " " + "\n "
272
202
273
203
# codes.sort( key=lambda x: x[0]+"-"+x[3] )
274
- codes .sort ( key = lambda x : x [3 ]+ "-" + x [ 0 ] )
204
+ codes .sort ( key = lambda x : int ( x [3 ]) )
275
205
for f ,l ,line ,num ,message ,severity in codes :
276
206
if num in seen :
277
207
continue
278
208
seen [num ] = True
279
209
280
- # if f.startswith( "./" ):
281
210
if f .startswith (sourceroot ):
282
211
f = f [sourcerootOffset + 1 :]
283
-
284
212
fn = f .rpartition ("/" )[2 ]
285
-
286
213
url = ":source:`" + f + "#L" + str (l ) + "`"
287
-
288
-
289
- out .write (".. line: {}\n \n " .format (line .strip (stripChars )))
290
214
215
+ if separatefiles :
216
+ outputFile = "{}/{:d}.txt" .format (resultsRoot ,int (num ))
217
+ out = open (outputFile , 'wb' )
218
+ out .write (default_domain )
219
+ sys .stderr .write ("Generating file: {}\n " .format (outputFile ))
220
+
221
+ out .write (".. line: {}\n \n " .format (line .strip (stripChars )))
291
222
out .write (".. error:: {}\n \n " .format (num ))
292
- if message :
223
+ if message != '' :
293
224
out .write (" :message: {}\n " .format (message .strip (stripChars )))
294
225
else :
295
- out .write (" :message: {}\n " .format (getBestMessage ( line , str (num )).strip (stripChars )))
226
+ message = getBestMessage (line ,str (num )).strip (stripChars )
227
+ if message != '' :
228
+ out .write (" :message: {}\n " .format (message ))
296
229
if severity :
297
230
if severity in severityTexts :
298
231
out .write (" :severity: {}\n " .format (severityTexts [severity ]))
@@ -301,18 +234,23 @@ def genErrorOutput():
301
234
else :
302
235
out .write (" :severity: {}\n " .format (severity ))
303
236
304
- out .write (" :module: {}\n \n " .format (url ) )
305
-
306
-
307
- out .write ( "\n " )
308
- out .close ()
237
+ out .write (" :module: {}\n " .format (url ) )
238
+ if separatefiles :
239
+ out .write ("\n " )
240
+ out .close ()
241
+
242
+ if separatefiles == False :
243
+ out .write ( "\n " )
244
+ out .close ()
309
245
310
246
def genErrorOutputCSV ():
247
+ """Parse through codes array and generate a csv file."""
248
+ sys .stderr .write ("Writing to {}\n " .format (errorsCSV ))
311
249
if os .path .exists (errorsCSV ):
312
250
i = open (errorsCSV ,"r" );
313
251
314
252
out = open (errorsCSV , 'wb' )
315
- out .write ('"Error","Text","Module","Line"' + "\n " )
253
+ out .write ('"Error","Text","Module","Line","Message","Severity" ' + "\n " )
316
254
317
255
prev = ""
318
256
seen = {}
@@ -337,10 +275,8 @@ def genErrorOutputCSV():
337
275
out .close ()
338
276
339
277
if __name__ == "__main__" :
340
- ok = checkErrorCodes ()
341
- print ( "ok:" + str ( ok ) )
342
- # print( "next: " + str( getNextCode() ) )
343
- if ok :
344
- genErrorOutput ()
345
- genErrorOutputCSV ()
278
+ readErrorCodes ()
279
+ genErrorOutput ()
280
+ if (generateCSV == 'yes' ):
281
+ genErrorOutputCSV ()
346
282
0 commit comments