首页 > 其他 > 详细

代码行数统计

时间:2015-10-15 18:37:02      阅读:215      评论:0      收藏:0      [点我收藏+]

最近工作不忙,写了个脚本统计代码行数.记录于此方便以后查阅.python版本2.7

 1 # -*- coding: cp936 -*-
 2 import os
 3 
 4 totalLineCounts = 0
 5 fileCounts = 0
 6 
 7 def GetLines(fullFilename):
 8     lineNum = 0
 9     endCommentLine = 0
10     startCommentLine = 0
11     totalBlankLineNum = 0
12     totalCommentLineNum = 0
13     efficientCodesLineNum = 0
14 
15     InCommentArea = 0  #防止重复统计.比如/* */块内包含有//开头的行
16     
17     for line in open(fullFilename).readlines():
18         line = line.strip()   #去除首尾的空格
19         lineNum += 1
20 
21         if InCommentArea:           
22             if line[-2:] == */:
23                 InCommentArea = 0   #出注释区
24                 endCommentLine = lineNum
25                 #print (‘注释区:%d行-%d行‘ % (startCommentLine,endCommentLine))
26                 totalCommentLineNum += (endCommentLine - startCommentLine + 1) 
27         else:                
28             if line == ‘‘:          #空行
29                 totalBlankLineNum += 1
30             elif line == //:      #以//开头的注释
31                 totalCommentLineNum += 1
32             elif line[0:2] == /*:
33                 InCommentArea = 1   #进入注释区
34                 startCommentLine = lineNum
35             else:
36                 efficientCodesLineNum += 1       
37     #print (‘空白行数量:%d,注释行数量:%d,有效代码行数量:%d,总行数:%d‘ % (totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum))
38     return [totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum]
39 
40 
41 exts = [cpp,h,txt]
42 #key:ext value:[totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum]
43 statistic = {cpp:[0,0,0,0],h:[0,0,0,0],txt:[0,0,0,0]}
44 
45 codesDir = D:\\SvnCodes\\main_branch\\code\\billing
46 #codesDir = ‘D:\Test\Sub1\sub1.1\sub1.1.1‘ 
47 if __name__ == __main__:
48     for curRoot,curSubDirs,curFiles in os.walk(codesDir):
49         #print curRoot,curSubDirs,curFiles
50         for file in curFiles:
51             fileExt = file.split(.)[-1]
52             if fileExt in exts:
53                 fullFilename = curRoot + os.path.sep + file
54                 r = GetLines(fullFilename)
55                 statistic[fileExt][0] += r[0]
56                 statistic[fileExt][1] += r[1]
57                 statistic[fileExt][2] += r[2]
58                 statistic[fileExt][3] += r[3]
59         
60     print {ext:[blank,comment,efficient,total]}
61     print statistic

 

代码行数统计

原文:http://www.cnblogs.com/sdu20112013/p/4882725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!