[关闭]
@cloverwang 2016-05-26T03:23:07.000000Z 字数 5199 阅读 2470

Python实现C代码统计工具(一)

Python 代码统计


声明

本文将基于Python2.7脚本语言,实现一个简易的C代码统计工具。

一. 问题提出

代码规模较大时,不易对其做出准确的评估。通过代码统计工具,可自动分析和统计软件项目中的文件行数、有效代码行数、注释行数及空白行数,提供准确而直观的代码量报告。基于这种定量报告,可以有针对性地提升代码质量。例如,分拆组合以消除巨型文件,对注释率过低的文件增加注释信息,等等。

为简单起见,本文仅仅统计C语言代码,即后缀名为ch的文件。并且,约定以下几条统计原则:
1. 当代码和注释位于同一行时,代码行数和注释行数均会加1。考虑到行注释的普遍性,因此代码行数、注释行数和空白行数的总和通常大于文件总行数。
2. 块注释中间和空白行计入空白行数。
3. "#if 0...endif"块视为代码行。

二. 代码实现

首先,定义两个存储统计结果的列表:

  1. rawCountInfo = [0, 0, 0, 0, 0]
  2. detailCountInfo = []

其中,rawCountInfo存储粗略的文件总行数信息,列表元素依次为文件行、代码行、注释行和空白行的总数,以及文件数目。detailCountInfo存储详细的统计信息,包括单个文件的行数信息和文件名,以及所有文件的行数总和。这是一个多维列表,存储内容示例如下:

  1. [['line.c', [33, 19, 15, 4]], ['test.c', [44, 34, 3, 7]]]

以下将给出具体的实现代码。为避免大段粘贴代码,以函数为片段简要描述。

  1. def CalcLines(lineList):
  2. lineNo, totalLines = 0, len(lineList)
  3. codeLines, commentLines, emptyLines = 0, 0, 0
  4. while lineNo < len(lineList):
  5. if lineList[lineNo].isspace(): #空行
  6. emptyLines += 1; lineNo += 1; continue
  7. regMatch = re.match('^([^/]*)/(/|\*)+(.*)$', lineList[lineNo].strip())
  8. if regMatch != None: #注释行
  9. commentLines += 1
  10. #代码&注释混合行
  11. if regMatch.group(1) != '':
  12. codeLines += 1
  13. elif regMatch.group(2) == '*' \
  14. and re.match('^.*\*/.+$', regMatch.group(3)) != None:
  15. codeLines += 1
  16. #行注释或单行块注释
  17. if '/*' not in lineList[lineNo] or '*/' in lineList[lineNo]:
  18. lineNo += 1; continue
  19. #跨行块注释
  20. lineNo += 1
  21. while '*/' not in lineList[lineNo]:
  22. if lineList[lineNo].isspace():
  23. emptyLines += 1
  24. else:
  25. commentLines += 1
  26. lineNo = lineNo + 1; continue
  27. commentLines += 1 #'*/'所在行
  28. else: #代码行
  29. codeLines += 1
  30. lineNo += 1; continue
  31. return [totalLines, codeLines, commentLines, emptyLines]

CalcLines()函数基于C语法判断文件行属性,按代码、注释或空行分别统计。参数lineList由readlines()读取文件得到,读到的每行末尾均含换行符。strip()可剔除字符串首尾的空白字符(包括换行符)。当通过print输出文件行内容时,可采用如下两种写法剔除多余的换行符:

  1. print '%s' %(line), #注意行末逗号
  2. print '%s' %(line.strip())

行尾包含换行符的问题也存在于readline()和read()调用,包括for line in file的语法。对于read()调用,可在读取文件后split('\n')得到不带换行符的行列表。注意,调用readlines()和read()时,会读入整个文件,文件位置指示器将指向文件尾端。此后再调用时,必须先通过file.seek(0)方法返回文件开头,否则读取的内容为空。

  1. def CountFileLines(filePath, isRaw=True):
  2. fileExt = os.path.splitext(filePath)
  3. if fileExt[1] != '.c' and fileExt[1] != '.h': #识别C文件
  4. return
  5. try:
  6. fileObj = open(filePath, 'r')
  7. except IOError:
  8. print 'Cannot open file (%s) for reading!', filePath
  9. else:
  10. lineList = fileObj.readlines()
  11. fileObj.close()
  12. if isRaw:
  13. global rawCountInfo
  14. rawCountInfo[:-1] = [x+y for x,y in zip(rawCountInfo[:-1], CalcLines(lineList))]
  15. rawCountInfo[-1] += 1
  16. else:
  17. detailCountInfo.append([filePath, CalcLines(lineList)])

CountFileLines()统计单个文件的行数信息,其参数isRaw指示统计报告是粗略还是详细的。对于详细报告,需要向detailCountInfo不断附加单个文件的统计结果;而对于详细报告,只需要保证rawCountInfo的元素值正确累加即可。

  1. def ReportCounterInfo(isRaw=True):
  2. #Python2.5版本引入条件表达式(if-else)实现三目运算符,低版本可采用and-or的短路特性
  3. #print 'FileLines CodeLines CommentLines EmptyLines %s' %('' if isRaw else 'FileName')
  4. print 'FileLines CodeLines CommentLines EmptyLines %s' %(not isRaw and 'FileName' or '')
  5. if isRaw:
  6. print '%-11d%-11d%-14d%-12d<Total:%d Files>' %(rawCountInfo[0], rawCountInfo[1],\
  7. rawCountInfo[2], rawCountInfo[3], rawCountInfo[4])
  8. return
  9. total = [0, 0, 0, 0]
  10. #对detailCountInfo按第一列元素(文件名)排序,以提高输出可读性
  11. #import operator; detailCountInfo.sort(key=operator.itemgetter(0))
  12. detailCountInfo.sort(key=lambda x:x[0]) #简洁灵活,但不如operator高效
  13. for item in detailCountInfo:
  14. print '%-11d%-11d%-14d%-12d%s' %(item[1][0], item[1][1], item[1][2], item[1][3], item[0])
  15. total[0] += item[1][0]; total[1] += item[1][1]
  16. total[2] += item[1][2]; total[3] += item[1][3]
  17. print '%-11d%-11d%-14d%-12d<Total:%d Files>' %(total[0], total[1], total[2], total[3], len(detailCountInfo))

ReportCounterInfo()输出统计报告。注意,详细报告输出前,先按文件名排序。

  1. def CountDirLines(dirPath, isRawReport=True):
  2. if not os.path.exists(dirPath):
  3. print dirPath + ' is non-existent!'
  4. return
  5. if not os.path.isdir(dirPath):
  6. print dirPath + ' is not a directory!'
  7. return
  8. for root, dirs, files in os.walk(dirPath):
  9. for file in files:
  10. CountFileLines(os.path.join(root, file), isRawReport)
  11. ReportCounterInfo(isRawReport)

CountDirLines()统计当前目录及其子目录下所有文件的行数信息,并输出统计报告。注意,os.walk()不一定按字母顺序遍历文件。在作者的Windows XP主机上,os.walk()按文件名顺序遍历;而在Linux Redhat主机上,os.walk()以"乱序"遍历。

最后,添加简单的命令行处理:

  1. if __name__ == '__main__':
  2. DIR_PATH = r'E:\PyTest\lctest'
  3. if len(sys.argv) == 1: #脚本名
  4. CountDirLines(DIR_PATH)
  5. sys.exit()
  6. if len(sys.argv) >= 2:
  7. if int(sys.argv[1]):
  8. CountDirLines(DIR_PATH, False)
  9. else:
  10. CountDirLines(DIR_PATH)
  11. sys.exit()

三. 效果验证

为验证上节的代码实现,建立lctest调试目录。该目录下包含line.c及和《为C函数自动添加跟踪语句》一文中的test.c文件。其中,line.c内容如下:

  1. #include <stdio.h>
  2. /* {{{ comment */
  3. /***********
  4. Multiline
  5. Comment
  6. ***********/
  7. int test(int a/*comment*/, int b)
  8. {
  9. int a2; int b2; //comment
  10. a2 = 1;
  11. b2 = 2;
  12. }
  13. /* {{{ test3 */
  14. int test3(int a,
  15. int b) /*test2 has been deleted,
  16. so this is test3. */
  17. {int a3 = 1; int b3 = 2;
  18. if(a3)
  19. {/*comment*/
  20. a3 = 0;
  21. }
  22. //comment
  23. b3 = 0;
  24. }
  25. /* }}} */
  26. //comment //comment
  27. /*FALSE*/ #if M_DEFINED
  28. #error Defination!
  29. #endif

以不同的命令行参数运行CLineCounter.py,输出如下:

  1. E:\PyTest>CLineCounter.py
  2. FileLines CodeLines CommentLines EmptyLines
  3. 77 53 18 11 <Total:2 Files>
  4. E:\PyTest>CLineCounter.py 0
  5. FileLines CodeLines CommentLines EmptyLines
  6. 77 53 18 11 <Total:2 Files>
  7. E:\PyTest>CLineCounter.py 1
  8. FileLines CodeLines CommentLines EmptyLines FileName
  9. 33 19 15 4 E:\PyTest\lctest\line.c
  10. 44 34 3 7 E:\PyTest\lctest\test.c
  11. 77 53 18 11 <Total:2 Files>

经人工校验,统计信息正确。

接着,在实际工程中运行python CLineCounter.py 1,截取部分运行输出如下:

  1. [wangxiaoyuan_@localhost ~]$ python CLineCounter.py 1
  2. FileLines CodeLines CommentLines EmptyLines FileName
  3. 99 21 58 24 /sdb1/wangxiaoyuan/include/Dsl_Alloc.h
  4. 120 79 28 24 /sdb1/wangxiaoyuan/include/Dsl_Backtrace.h
  5. ... ... ... ... ... ... ... ...
  6. 139 89 24 26 /sdb1/wangxiaoyuan/source/Dsl_Tbl_Map.c
  7. 617 481 64 78 /sdb1/wangxiaoyuan/source/Dsl_Test_Suite.c
  8. 797 569 169 82 /sdb1/wangxiaoyuan/source/xDSL_Common.c
  9. 15450 10437 3250 2538 <Total:40 Files>

四. 后记

本文所实现的C代码统计工具较为简陋,后续将重构代码并添加控制选项。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注