一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python实现代码行数自动统计实例

时间:2015-01-15 编辑:简简单单 来源:一聚教程网

此功能用到了2个常用的Python标准库os和sys模块就解决。

可能是因为这段时间在做的一个Python项目,用的是一个公司内部的IDE环境,而这个IDE环境产生的py代码并不是以文本方式存储,都是放在二进制文件里面的。

由于这门语言外界几乎接触不到,所以没有针对它的代码统计程序。一个模块完成后要统计代码行数会很困难的,要统计的话只能手工来操作,这不符合我们程序员的一惯风格。

在这期间我一直在关注python语言,但是还没有动手真正的写python程序 。今天就利用中午休息的时间写了一个简单的代码统计程序。对输入路径作递归查找代码文件,对每一个代码文件计算注释行数、空行数、真正的代码行数。

统计代码行数Python代码如下:

 代码如下 复制代码
#coding=utf-8
#iplaypython.py
import sys;
import os;

class LineCount:
    def trim(self,docstring):
        if not docstring:
            return ''
        lines = docstring.expandtabs().splitlines()
       
        indent = sys.maxint
        for line in lines[1:]:
            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,tempfilename) = os.path.split(filename);
        (shotname,extension) = os.path.splitext(tempfilename);
        if extension == '.txt' or extension == '.hol' : # file type
            file = open(filename,'r');
            self.sourceFileCount += 1;
            allLines = file.readlines();
            file.close();
           
            lineCount    =0;
            commentCount = 0;
            blankCount   = 0;
            codeCount    = 0;
            for eachLine in allLines:
                if eachLine != " " :
                    eachLine = eachLine.replace(" ",""); #remove space
                    eachLine = self.trim(eachLine);      #remove tabIndent
                    if  eachLine.find('--') == 0 :  #LINECOMMENT
                        commentCount += 1;
                    else :
                        if eachLine == "":
                            blankCount += 1;
                        else :
                            codeCount += 1;
                lineCount = lineCount + 1;
            self.all += lineCount;
            self.allComment += commentCount;
            self.allBlank += blankCount;
            self.allSource += codeCount;
            print filename;
            print '           Total      :',lineCount ;
            print '           Comment    :',commentCount;
            print '           Blank      :',blankCount;
            print '           Source     :',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(filename);

    # Open File
    def __init__(self):
        self.all = 0;
        self.allComment =0;
        self.allBlank = 0;
        self.allSource = 0;
        self.sourceFileCount = 0;
        filename = raw_input('Enter file name: ');
        self.CalulateCodeCount(filename);
        if self.sourceFileCount == 0 :
            print 'No Code File';
            pass;
        print 'n';
        print '*****************  All Files  **********************';
        print '    Files      :',self.sourceFileCount;
        print '    Total      :',self.all;
        print '    Comment    :',self.allComment;
        print '    Blank      :',self.allBlank;
        print '    Source     :',self.allSource;
        print '****************************************************';

myLineCount = LineCount();



我们看到代码中 extension == '.txt' or extension == '.hol'这句代码是用来判定文件的后缀名,可以确定是否要计算代码行数。

if  eachLine.find('--') == 0 :是用来判定当前行是不是单行注释,为了能在其他机器上运行,使用了py2exe来把python脚本生成可执行的exe。

 代码如下 复制代码
from distutils.core import setup
import py2exe

setup(
 
    version = "0.0.1",
    description = "LineCount",
    name = "LineCount",
 
    console = ["LineCount.py"],
    )




利用Python统计源代码行数以及对源代码排版

近段时间写的一个软件需要做软件著作权登记,需要统计源代码行数以及提交部分源代码,刚好利用Python写了这么一个小工具。

用法如下:

在windows命令行窗口,使用python python_count_line.py source_code_root_directory即可。

其中,source_code_root_directory为需要统计的源代码的根目录,源代码目录中如果有其它子目录,也可以递归遍历。

源代码如下:

 代码如下 复制代码
import os,sys

def count_line(filename):
    """ count the line number of file"""
    with open(filename) as file:
        lines = file.readlines()
        file.close()
        return len(lines)

def read_lines_and_write_to_target(source_filename,target_file,begin_linenum):
    linenum = begin_linenum
    with open(source_filename) as source_file:
        lines = source_file.readlines()
        for i in range(len(lines)):
            target_file.write("%4d"%linenum+"  ")
            target_file.write(lines[i])
            linenum = linenum+1
        source_file.close()
    return linenum
       
def flatten(lst):
    for x in lst:
        if isinstance(x,list):
            for x in flatten(x):
                yield x
        else:
            yield x

def count_files(root_dir,file_filter):
    process_list = get_process_files(root_dir,file_filter)

    process_list = list(flatten(process_list))
    print "Flatten Process List",
    print process_list

    line_count = 0
    for file in process_list:
        line_count = line_count + count_line(file)
        print file,"line_number",line_count
   
    #Generate Source Files in One
    line_count = 1
    target_filename=root_dir+""+"result.txt"
    with open(target_filename,"w") as target_file:
        for file in process_list:
            target_file.write("///////"+file+"///////n")
            line_count = read_lines_and_write_to_target(file,target_file,line_count)
        target_file.close()
    print "Result is generated in target file:",target_filename


def get_process_files(root_dir,file_filter):
    """ process all files in directory that matches file_filter """
    cur_dir = os.path.abspath(root_dir)
    file_list = os.listdir(cur_dir)
    print file_list
    process_list=[]
    #processing the file_list, retrieve all target files to process_list
    for file in file_list:
        fullfile = cur_dir+""+file
        print fullfile
        if os.path.isfile(fullfile):
            print "is file"
            found_flag = False
            for filter in file_filter:
                if file.rfind(filter) != -1:
                    print "match!",file
                    process_list.append(fullfile)
                    found_flag = True
            if found_flag==False:
                print "pass this file",fullfile
        elif os.path.isdir(fullfile):
            print "is dir,recursive"
            dir_extra_list = get_process_files(fullfile,file_filter)
            print dir_extra_list
            if len(dir_extra_list)!=0:
                process_list.append(dir_extra_list)
        else:
            print "not defined"
    return process_list


if __name__ == '__main__':
    print sys.argv
    root_dir = sys.argv[1]
    print "INFO: Root_Dir:",root_dir
    file_filter = [".cpp",".h"]
    count_files(root_dir,file_filter)


   

统计某文件夹下源代码行数 Python脚本


写了一个统计某文件夹下各种源代码行数的脚本

仅支持c/c++ java python

有需要可以自己更改程序,我把这个脚本的拓展性做的很高

很容易使之支持其他文本型语言代码

或者给我Email我帮你做 onezeros@yahoo.cn

对于MFC自动生成的代码应该排除在外

可以用一种不太严密的方法:先统计各种mfc生成工程代码数量

然后在分析文件夹时根据mfc文件的一些特征判断目标文件夹是否为mfc工程

如果是,就减去生成的代码量

 代码如下 复制代码
#!/usr/bin/python
#codelines.py short for files in directory
import sys
import os
import os.path

class Element:
    _fileamount=0#number of files
    _codelines=0 #line number of files
    _commandlines=0;#standalone command lines
   
extension={'c':0,'cpp':0,'C':0,'cc':0,'cxx':0,
        'h':0,'hpp':0,'hxx':0,'java':1,'py':2}  
language=['c/c++','java','py']
statistics=[Element() for x in range(0,len(language))]

#count lines in a file
def fileparser(file,ext):
    global extension,statistics
    try:
        f=open(file,'r')
    except:
        print("failed to open "+file)
    print("parsing file "+file)
    #print("extension "+str(extension[ext]))
    statistics[extension[ext]]._fileamount+=1
    try:
        context=f.readlines()
        i=-1;
        while i             i+=1
            line=context[i].strip()
            if line=='':
                continue
            else:
                if extension[ext]==0 or ext=='java':#c/cpp#java
                    if line[0:2]=='//':
                        statistics[extension[ext]]._commandlines+=1
                    elif line[0:2]=='/*':
                        while line.find('*/')==-1:
                            i+=1;
                            line=context[i].strip()
                            statistics[extension[ext]]._commandlines+=1
                        statistics[extension[ext]]._commandlines+=1
                    else:
                        statistics[extension[ext]]._codelines+=1
                elif ext=='py':#python
                    if line[0]=='#':
                        statistics[extension[ext]]._commandlines+=1
                    else:
                        statistics[extension[ext]]._codelines+=1
    except:
        pass
    f.close()
    return
#find source code file by analyzing extension
def extparser(file):
    global extension
    ext=os.path.splitext(file)[1][1:]
    #print("extension "+ext)
    if ext in extension:
        #print("in  extparing  "+file)
        fileparser(file,ext)
    else:
        pass
    return
   
#parse a directory to find files
def dirparser(directory):
    print("parsing directory "+directory)
    try:
        dirlist=os.listdir(directory)
        for i in dirlist:
            fullpath=directory+'//'+i
            if(os.path.isdir(fullpath)==True):
                dirparser(fullpath)
            else:
                extparser(fullpath)
    except:
        pass
    return

if __name__ == "__main__":
    if(os.path.isdir(sys.argv[1])==False):
        print('%s is not a correct path',sys.argv[1]);
        sys.exit(0)
    dirparser(sys.argv[1])
    length=[len('language:  '),len('file amount: '),
        len('code amount: '),len('command amount')]
    print('language:  file amount:  code amount:  command amount')
    for i,lang in enumerate(language):
        print(repr(lang).ljust(length[0]),
            repr(statistics[i]._fileamount).ljust(length[1]),
            repr(statistics[i]._codelines).ljust(length[1]),
            repr(statistics[i]._commandlines).ljust(length[1]))
   
    print('Done')

热门栏目