11#!/usr/bin/env python
2-
32import os
43import sys
54import 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
626import utils
727
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"
1228
1329assertNames = [ "uassert" , "massert" , "fassert" , "fassertFailed" ]
1430
@@ -58,99 +74,27 @@ def assignErrorCodes():
5874
5975codes = []
6076
61- def XreadErrorCodes ( callback , replaceZero = False ):
62-
77+ def readErrorCodes ():
78+ """Open each source file in sourceroot and scan for potential error messages."""
6379 quick = [ "assert" , "Exception" ]
6480
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- # ]
14281 ps = [ re .compile ( "(([wum]asser(t|ted))) *\(( *)(\d+) *,\s*(\" \S[^\" ]+\S\" )\s*,?.*" ) ,
14382 re .compile ( "(([wum]asser(t|ted))) *\(( *)(\d+) *,\s*([\S\s+<\(\)\" ]+) *,?.*" ) ,
14483 re .compile ( '((msgasser(t|ted))) *\(( *)(\d+) *, *(\" \S[^,^\" ]+\S\" ) *,?' ) ,
84+ re .compile ( '((msgasser(t|ted)NoTrace)) *\(( *)(\d+) *, *(\" \S[^,^\" ]+\S\" ) *,?' ) ,
14585 re .compile ( "((fasser(t|ted))) *\(( *)(\d+)()" ) ,
14686 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+
14892 ]
14993
15094 bad = [ re .compile ( "\sassert *\(" ) ]
15195 arr = []
15296 for x in utils .getAllSourceFiles (arr ,sourceroot ):
153-
97+ sys . stderr . write ( "Analyzing: {} \n " . format ( x ))
15498 needReplace = [False ]
15599 lines = []
156100 lastCodes = [0 ]
@@ -184,35 +128,13 @@ def repl( m ):
184128 spaces = m [3 ]
185129 code = m [4 ]
186130 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 :
196131 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 )
199132
200133 return start + "(" + spaces + code
201134
202135 line = re .sub ( p , repl , line )
203- print ("line(" + line + ")" )
204- # end if ps loop
205136
206- # if replaceZero : lines.append( line )
207137 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 )
216138
217139
218140
@@ -252,17 +174,25 @@ def getBestMessage( err , start ):
252174 return err
253175
254176def 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 ))
266196
267197 prev = ""
268198 seen = {}
@@ -271,28 +201,31 @@ def genErrorOutput():
271201 stripChars = " " + "\n "
272202
273203# 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 ]) )
275205 for f ,l ,line ,num ,message ,severity in codes :
276206 if num in seen :
277207 continue
278208 seen [num ] = True
279209
280- # if f.startswith( "./" ):
281210 if f .startswith (sourceroot ):
282211 f = f [sourcerootOffset + 1 :]
283-
284212 fn = f .rpartition ("/" )[2 ]
285-
286213 url = ":source:`" + f + "#L" + str (l ) + "`"
287-
288-
289- out .write (".. line: {}\n \n " .format (line .strip (stripChars )))
290214
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 )))
291222 out .write (".. error:: {}\n \n " .format (num ))
292- if message :
223+ if message != '' :
293224 out .write (" :message: {}\n " .format (message .strip (stripChars )))
294225 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 ))
296229 if severity :
297230 if severity in severityTexts :
298231 out .write (" :severity: {}\n " .format (severityTexts [severity ]))
@@ -301,18 +234,23 @@ def genErrorOutput():
301234 else :
302235 out .write (" :severity: {}\n " .format (severity ))
303236
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 ()
309245
310246def genErrorOutputCSV ():
247+ """Parse through codes array and generate a csv file."""
248+ sys .stderr .write ("Writing to {}\n " .format (errorsCSV ))
311249 if os .path .exists (errorsCSV ):
312250 i = open (errorsCSV ,"r" );
313251
314252 out = open (errorsCSV , 'wb' )
315- out .write ('"Error","Text","Module","Line"' + "\n " )
253+ out .write ('"Error","Text","Module","Line","Message","Severity" ' + "\n " )
316254
317255 prev = ""
318256 seen = {}
@@ -337,10 +275,8 @@ def genErrorOutputCSV():
337275 out .close ()
338276
339277if __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 ()
346282
0 commit comments