0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/buildscripts/errorcodes.py

159 lines
4.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import os
import sys
import re
2010-07-19 21:37:19 +02:00
import utils
assertNames = [ "uassert" , "massert" ]
def assignErrorCodes():
cur = 10000
for root in assertNames:
2010-09-03 17:58:53 +02:00
for x in utils.getAllSourceFiles():
print( x )
didAnything = False
fixed = ""
for line in open( x ):
s = line.partition( root + "(" )
if s[1] == "" or line.startswith( "#define " + root):
fixed += line
continue
fixed += s[0] + root + "( " + str( cur ) + " , " + s[2]
cur = cur + 1
didAnything = True
if didAnything:
out = open( x , 'w' )
out.write( fixed )
out.close()
2010-07-19 21:37:19 +02:00
codes = []
2011-06-21 23:23:01 +02:00
def readErrorCodes( callback, replaceZero = False ):
ps = [ re.compile( "(([umsg]asser(t|ted))) *\(( *)(\d+)" ) ,
re.compile( "((User|Msg|MsgAssertion)Exceptio(n))\(( *)(\d+)" ) ,
re.compile( "(((verify))) *\(( *)(\d+)" )
]
2011-06-21 23:23:01 +02:00
2010-09-03 17:58:53 +02:00
for x in utils.getAllSourceFiles():
2011-06-21 23:23:01 +02:00
needReplace = [False]
lines = []
lastCodes = [0]
lineNum = 1
2011-06-21 23:23:01 +02:00
for line in open( x ):
2011-06-21 23:23:01 +02:00
for p in ps:
2011-06-21 23:23:01 +02:00
def repl( m ):
m = m.groups()
start = m[0]
spaces = m[3]
code = m[4]
2011-06-21 23:23:01 +02:00
if code == '0' and replaceZero :
code = getNextCode( lastCodes )
lastCodes.append( code )
code = str( code )
needReplace[0] = True
print( "Adding code " + code + " to line " + x + ":" + str( lineNum ) )
else :
codes.append( ( x , lineNum , line , code ) )
callback( x , lineNum , line , code )
return start + "(" + spaces + code
2011-06-21 23:23:01 +02:00
line = re.sub( p, repl, line )
2011-06-21 23:23:01 +02:00
if replaceZero : lines.append( line )
lineNum = lineNum + 1
if replaceZero and needReplace[0] :
print( "Replacing file " + x )
of = open( x + ".tmp", 'w' )
of.write( "".join( lines ) )
of.close()
2011-10-07 23:10:54 +02:00
os.remove(x)
2011-06-21 23:23:01 +02:00
os.rename( x + ".tmp", x )
2011-06-21 23:23:01 +02:00
def getNextCode( lastCodes = [0] ):
highest = [max(lastCodes)]
def check( fileName , lineNum , line , code ):
code = int( code )
if code > highest[0]:
highest[0] = code
readErrorCodes( check )
return highest[0] + 1
def checkErrorCodes():
seen = {}
errors = []
def checkDups( fileName , lineNum , line , code ):
if code in seen:
print( "DUPLICATE IDS" )
print( "%s:%d:%s %s" % ( fileName , lineNum , line.strip() , code ) )
print( "%s:%d:%s %s" % seen[code] )
errors.append( seen[code] )
seen[code] = ( fileName , lineNum , line , code )
2011-06-21 23:23:01 +02:00
readErrorCodes( checkDups, True )
return len( errors ) == 0
2010-07-19 21:37:19 +02:00
def getBestMessage( err , start ):
err = err.partition( start )[2]
if not err:
return ""
err = err.partition( "\"" )[2]
if not err:
return ""
err = err.rpartition( "\"" )[0]
if not err:
return ""
return err
def genErrorOutput():
if os.path.exists( "docs/errors.md" ):
i = open( "docs/errors.md" , "r" )
2011-05-12 23:22:43 +02:00
out = open( "docs/errors.md" , 'wb' )
2010-07-19 21:37:19 +02:00
out.write( "MongoDB Error Codes\n==========\n\n\n" )
prev = ""
seen = {}
codes.sort( key=lambda x: x[0]+"-"+x[3] )
for f,l,line,num in codes:
if num in seen:
continue
seen[num] = True
if f.startswith( "./" ):
f = f[2:]
if f != prev:
out.write( "\n\n" )
out.write( f + "\n----\n" )
prev = f
url = "http://github.com/mongodb/mongo/blob/master/" + f + "#L" + str(l)
2010-07-19 21:37:19 +02:00
out.write( "* " + str(num) + " [code](" + url + ") " + getBestMessage( line , str(num) ) + "\n" )
out.write( "\n" )
out.close()
if __name__ == "__main__":
ok = checkErrorCodes()
print( "ok:" + str( ok ) )
2009-12-28 23:12:49 +01:00
print( "next: " + str( getNextCode() ) )
2010-07-19 21:37:19 +02:00
if ok:
genErrorOutput()