首先,定義兩個存儲統計結果的列表:
rawCountInfo=[0,0,0,0,0]
detailCountInfo=[]
其中,rawCountInfo存儲粗略的文件總行數信息,列表元素依次為文件行、代碼行、注釋行和空白行的總數,以及文件數目。detailCountInfo存儲詳細的統計信息,包括單個文件的行數信息和文件名,以及所有文件的行數總和。這是一個多維列表,存儲內容示例如下:
[['line.c',[33,19,15,4]],['test.c',[44,34,3,7]]]
以下將給出具體的實現代碼。為避免大段粘貼代碼,以函數為片段簡要描述。
defCalcLines(lineList):
lineNo,totalLines=0,len(lineList)
codeLines,commentLines,emptyLines=0,0,0
whilelineNo
iflineList[lineNo].isspace():#空行
emptyLines+=1;lineNo+=1;continue
regMatch=re.match('^([^/]*)/(/|*)+(.*)$',lineList[lineNo].strip())
ifregMatch!=None:#注釋行
commentLines+=1
#代碼&注釋混合行
ifregMatch.group(1)!='':
codeLines+=1
elifregMatch.group(2)=='*'
andre.match('^.**/.+$',regMatch.group(3))!=None:
codeLines+=1
#行注釋或單行塊注釋
if'/*'notinlineList[lineNo]or'*/'inlineList[lineNo]:
lineNo+=1;continue
#跨行塊注釋
lineNo+=1
while'*/'notinlineList[lineNo]:
iflineList[lineNo].isspace():
emptyLines+=1
else:
commentLines+=1
lineNo=lineNo+1;continue
commentLines+=1#'*/'所在行
else:#代碼行
codeLines+=1
lineNo+=1;continue
return[totalLines,codeLines,commentLines,emptyLines]
CalcLines()函數基于C語法判斷文件行屬性,按代碼、注釋或空行分別統計。參數lineList由readlines()讀取文件得到,讀到的每行末尾均含換行符。strip()可剔除字符串首尾的空白字符(包括換行符)。當通過print輸出文件行內容時,可采用如下兩種寫法剔除多余的換行符:
print'%s'%(line),#注意行末逗號
print'%s'%(line.strip())
行尾包含換行符的問題也存在于readline()和read()調用,包括forlineinfile的語法。對于read()調用,可在讀取文件后split('')得到不帶換行符的行列表。注意,調用readlines()和read()時,會讀入整個文件,文件位置指示器將指向文件尾端。此后再調用時,必須先通過file.seek(0)方法返回文件開頭,否則讀取的內容為空。
defCountFileLines(filePath,isRaw=True):
fileExt=os.path.splitext(filePath)
iffileExt[1]!='.c'andfileExt[1]!='.h':#識別C文件
return
try:
fileObj=open(filePath,'r')
exceptIOError:
print'Cannotopenfile(%s)forreading!',filePath
else:
lineList=fileObj.readlines()
fileObj.close()
ifisRaw:
globalrawCountInfo
rawCountInfo[:-1]=[x+yforx,yinzip(rawCountInfo[:-1],CalcLines(lineList))]
rawCountInfo[-1]+=1
else:
detailCountInfo.append([filePath,CalcLines(lineList)])
CountFileLines()統計單個文件的行數信息,其參數isRaw指示統計報告是粗略還是詳細的。對于詳細報告,需要向detailCountInfo不斷附加單個文件的統計結果;而對于詳細報告,只需要保證rawCountInfo的元素值正確累加即可。
defReportCounterInfo(isRaw=True):
#Python2.5版本引入條件表達式(if-else)實現三目運算符,低版本可采用and-or的短路特性
#print'FileLinesCodeLinesCommentLinesEmptyLines%s'%(''ifisRawelse'FileName')
print'FileLinesCodeLinesCommentLinesEmptyLines%s'%(notisRawand'FileName'or'')
ifisRaw:
print'%-11d%-11d%-14d%-12d'%(rawCountInfo[0],rawCountInfo[1],
rawCountInfo[2],rawCountInfo[3],rawCountInfo[4])
return
total=[0,0,0,0]
#對detailCountInfo按第一列元素(文件名)排序,以提高輸出可讀性
#importoperator;detailCountInfo.sort(key=operator.itemgetter(0))
detailCountInfo.sort(key=lambdax:x[0])#簡潔靈活,但不如operator高效
foritemindetailCountInfo:
print'%-11d%-11d%-14d%-12d%s'%(item[1][0],item[1][1],item[1][2],item[1][3],item[0])
total[0]+=item[1][0];total[1]+=item[1][1]
total[2]+=item[1][2];total[3]+=item[1][3]
print'%-11d%-11d%-14d%-12d'%(total[0],total[1],total[2],total[3],len(detailCountInfo))
ReportCounterInfo()輸出統計報告。注意,詳細報告輸出前,先按文件名排序。
defCountDirLines(dirPath,isRawReport=True):
ifnotos.path.exists(dirPath):
printdirPath+'isnon-existent!'
return
ifnotos.path.isdir(dirPath):
printdirPath+'isnotadirectory!'
return
forroot,dirs,filesinos.walk(dirPath):
forfileinfiles:
CountFileLines(os.path.join(root,file),isRawReport)
ReportCounterInfo(isRawReport)
CountDirLines()統計當前目錄及其子目錄下所有文件的行數信息,并輸出統計報告。注意,os.walk()不一定按字母順序遍歷文件。在作者的WindowsXP主機上,os.walk()按文件名順序遍歷;而在LinuxRedhat主機上,os.walk()以"亂序"遍歷。
最后,添加簡單的命令行處理:
if__name__=='__main__':
DIR_PATH=r'E:PyTestlctest'
iflen(sys.argv)==1:#腳本名
CountDirLines(DIR_PATH)
sys.exit()
iflen(sys.argv)>=2:
ifint(sys.argv[1]):
CountDirLines(DIR_PATH,False)
else:
CountDirLines(DIR_PATH)
sys.exit()
以上內容為大家介紹了PythonC代碼統計工具的代碼實現,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。