首页 > 编程 > Python > 正文

使用python写的代码行数统计程序

2019-11-17 04:57:24
字体:
来源:转载
供稿:网友

  原文见:使用python写的代码行数统计程序
因为最近在作的项目很非凡,所使用的语言是一个公司内部的IDE环境,而这个IDE所产生的代码并不是以文本方式存放的,都是放在二进制文件中,而且由于 这门语言外界几乎接触不到,所以没有针对它的代码统计程序,当一个模块完成后要统计代码行数会很困难,要统计的话必须先把代码编辑器中的内容拷贝到一个文 本类型的文件中。
正好一直在关注python,还没有用python写过程序,今天就利用中午休息的时间写了一个简单的代码统计程序。
对输入的路径作递归,查找代码文件,对每一个代码文件计算它的注释行数,空行数,真正的代码行数。
自己用的程序,就写的粗糙了,也没加异常处理。
主要的python脚本文件LineCount.py的内容如下:
使用python写的代码行数统计程序import sys;
使用python写的代码行数统计程序import os;
使用python写的代码行数统计程序
使用python写的代码行数统计程序class LineCount:
使用python写的代码行数统计程序    def trim(self,docstring):
使用python写的代码行数统计程序        if not docstring:
使用python写的代码行数统计程序            return ''
使用python写的代码行数统计程序        lines = docstring.eXPandtabs().splitlines()
使用python写的代码行数统计程序        
使用python写的代码行数统计程序        indent = sys.maxint
使用python写的代码行数统计程序        for line in lines[1:]:
使用python写的代码行数统计程序            stripped = line.lstrip()
使用python写的代码行数统计程序            if stripped:
使用python写的代码行数统计程序                indent = min(indent, len(line) - len(stripped))
使用python写的代码行数统计程序        
使用python写的代码行数统计程序        trimmed = [lines[0].strip()]
使用python写的代码行数统计程序        if indent < sys.maxint:
使用python写的代码行数统计程序            for line in lines[1:]:
使用python写的代码行数统计程序                trimmed.append(line[indent:].rstrip())
使用python写的代码行数统计程序        
使用python写的代码行数统计程序        while trimmed and not trimmed[-1]:
使用python写的代码行数统计程序            trimmed.pop()
使用python写的代码行数统计程序        while trimmed and not trimmed[0]:
使用python写的代码行数统计程序            trimmed.pop(0)
使用python写的代码行数统计程序        
使用python写的代码行数统计程序        return '/n'.join(trimmed)
使用python写的代码行数统计程序    
使用python写的代码行数统计程序    def FileLineCount(self,filename):
使用python写的代码行数统计程序        (filepath,tempfilename) = os.path.split(filename);
使用python写的代码行数统计程序        (shotname,extension) = os.path.splitext(tempfilename);
使用python写的代码行数统计程序        if extension == '.txt' or extension == '.hol' : # file type 
使用python写的代码行数统计程序            file = open(filename,'r');
使用python写的代码行数统计程序            self.sourceFileCount += 1;
使用python写的代码行数统计程序            allLines = file.readlines();
使用python写的代码行数统计程序            file.close();
使用python写的代码行数统计程序            
使用python写的代码行数统计程序            lineCount    =0;
使用python写的代码行数统计程序            commentCount = 0;
使用python写的代码行数统计程序            blankCount   = 0;
使用python写的代码行数统计程序            codeCount    = 0;
使用python写的代码行数统计程序            for eachLine in allLines:
使用python写的代码行数统计程序                if eachLine != " " :
使用python写的代码行数统计程序                    eachLine = eachLine.replace(" ",""); #remove space
使用python写的代码行数统计程序                    eachLine = self.trim(eachLine);      #remove tabIndent
使用python写的代码行数统计程序                    if  eachLine.find('--') == 0 :  #LINECOMMENT 
使用python写的代码行数统计程序                        commentCount += 1;
使用python写的代码行数统计程序                    else :
使用python写的代码行数统计程序                        if eachLine == "":
使用python写的代码行数统计程序                            blankCount += 1;
使用python写的代码行数统计程序                        else :
使用python写的代码行数统计程序                            codeCount += 1;
使用python写的代码行数统计程序                lineCount = lineCount + 1;
使用python写的代码行数统计程序            self.all += lineCount;
使用python写的代码行数统计程序            self.allComment += commentCount;
使用python写的代码行数统计程序            self.allBlank += blankCount;
使用python写的代码行数统计程序            self.allSource += codeCount;
使用python写的代码行数统计程序            PRint filename;
使用python写的代码行数统计程序            print '           Total      :',lineCount ;
使用python写的代码行数统计程序            print '           Comment    :',commentCount;
使用python写的代码行数统计程序            print '           Blank      :',blankCount;
使用python写的代码行数统计程序            print '           Source     :',codeCount;
使用python写的代码行数统计程序                    
使用python写的代码行数统计程序    def CalulateCodeCount(self,filename):
使用python写的代码行数统计程序        if os.path.isdir(filename) :
使用python写的代码行数统计程序            if not filename.endswith('//'):
使用python写的代码行数统计程序                filename += '//'; 
使用python写的代码行数统计程序            for file in os.listdir(filename):
使用python写的代码行数统计程序                if os.path.isdir(filename + file):
使用python写的代码行数统计程序                    self.CalulateCodeCount(filename + file);
使用python写的代码行数统计程序                else:
使用python写的代码行数统计程序                    self.FileLineCount(filename + file);
使用python写的代码行数统计程序        else:
使用python写的代码行数统计程序            self.FileLineCount(self,filename);
使用python写的代码行数统计程序
使用python写的代码行数统计程序    # Open File
使用python写的代码行数统计程序    def __init__(self):
使用python写的代码行数统计程序        self.all = 0;
使用python写的代码行数统计程序        self.allComment =0;
使用python写的代码行数统计程序        self.allBlank = 0;
使用python写的代码行数统计程序        self.allSource = 0;
使用python写的代码行数统计程序        self.sourceFileCount = 0;
使用python写的代码行数统计程序        filename = raw_input('Enter file name: ');
使用python写的代码行数统计程序        self.CalulateCodeCount(filename);
使用python写的代码行数统计程序        if self.sourceFileCount == 0 :
使用python写的代码行数统计程序            print 'No Code File';
使用python写的代码行数统计程序            pass;
使用python写的代码行数统计程序        print '/n';
使用python写的代码行数统计程序        print '*****************  All Files  **********************';
使用python写的代码行数统计程序        print '    Files      :',self.sourceFileCount;
使用python写的代码行数统计程序        print '    Total      :',self.all;
使用python写的代码行数统计程序        print '    Comment    :',self.allComment;
使用python写的代码行数统计程序        print '    Blank      :',self.allBlank;
使用python写的代码行数统计程序        print '    Source     :',self.allSource;
使用python写的代码行数统计程序        print '****************************************************';
使用python写的代码行数统计程序
使用python写的代码行数统计程序myLineCount = LineCount();


可以看到extension == '.txt' or extension == '.hol'这句是判定文件的后缀,来确定是否要计算代码行数。
if  eachLine.find('--') == 0 :这句来判定当前行是不是单行注释(我们的这门语言不支持块注释)。
为了能在其他机器上运行,使用了py2exe来把python脚本生成可执行的exe,setup.py脚本内容如下:
使用python写的代码行数统计程序from distutils.core import setup
使用python写的代码行数统计程序import py2exe
使用python写的代码行数统计程序
使用python写的代码行数统计程序setup(
使用python写的代码行数统计程序   
使用python写的代码行数统计程序    version = "0.0.1",
使用python写的代码行数统计程序    description = "LineCount",
使用python写的代码行数统计程序    name = "LineCount",
使用python写的代码行数统计程序
使用python写的代码行数统计程序    console = ["LineCount.py"],
使用python写的代码行数统计程序    )
使用python写的代码行数统计程序

不过生成exe后程序臃肿很多,有3M多。
感觉使用python确实是件很舒服的事。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表