Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29335865
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-04-20 20:38:32


#-*-coding:utf-8-*-
"""
统计一个PY代码里面代码的行数,注释的行数还有空行个数
版本:2010-04-20 号
可以实现输入一个目录统计此目录下面全部的py文件有多少行、有多少注释、有多少代码
author:hkebao@126.com
"
""
import os,sys,time

class CodeCount:
    def __init__(self):
        self.lineCount = 0;
        self.commentCount =0;
        self.blankCount = 0;
        self.codeCount = 0;
        self.sourceFileCount = 0;
        filename = raw_input('Enter file name: ');
        self.CalulateCodeCount(filename);
        if self.sourceFileCount == 0 :
            print 'No Code File';
            pass;
        
        
        print filename;
        print 'total: ',self.lineCount
        print 'comment: ',self.commentCount
        print 'blank: ' , self.blankCount
        print 'source: ',self.codeCount
        
    
    def CalulateCodeCount(self,filename):
        if os.path.isdir(filename) :
            if not filename.endswith('\\'):
                filename += '\\';
            for file in os.listdir(filename):
                if os.path.isdir(filename + file):
                    self.CalulateCodeCount(filename + file);
                else:
                    self.FileLineCount(filename + file);
        else:
            self.FileLineCount(self,filename);

    def trims(self,docString):
        if not docString:
            return ''
        lines = docString.expandtabs().splitlines()
        indent = sys.maxint
        for line in lines[0:]:
            stripped = line.lstrip()
            if stripped:
                indent = min(indent, len(line) - len(stripped))
            trimmed = [lines[0].strip()]
            if indent < sys.maxint:
                for line in lines[1:]:
                    trimmed.append(line[indent:].rstrip())
            
            while trimmed and not trimmed[-1]:
                trimmed.pop()
            while trimmed and not trimmed[0]:
                trimmed.pop(0)
            
            return '\n'.join(trimmed)
    
    
    def FileLineCount(self,filename):
        (filepath,tmp) = os.path.split(filename)
        (shortname,extension) = os.path.splitext(tmp)
        if extension == '.py':
            file = open(filename,'r')
            allLines = file.readlines()
            self.sourceFileCount += 1;
            file.close()
            commentstart = False
            i = 0
            for eachline in allLines:
                if eachline != " ":
                    eachline = eachline.replace(" ","")
                    eachline = self.trims(eachline)
                    if eachline.find('#') == 0:
                        self.commentCount += 1
                    elif commentstart:
                        i += 1;
                        self.commentCount += 1
                        if eachline.count('"""') > 0 and i > 1:
                            commentstart = False;i = 0
                    elif eachline[0:3] == '"""' :
                        self.commentCount += 1
                        if eachline.count('"""') > 1:pass
                        else:commentstart = True;i=1
                    else:
                        if eachline == "":
                            self.blankCount += 1
                        else:
                            self.codeCount += 1
                self.lineCount = self.lineCount+1;
    
            
if __name__ == '__main__':
    CodeCount()




使用方法:Enter file name: E:\JaveProj\Opensource\src\cu
阅读(1009) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~