首页 > 代码库 > Mac OS X代码量统计程序(Python版)

Mac OS X代码量统计程序(Python版)

方便统计各种平台项目的代码量,主要用到了find指令来进行处理的详情点击打开链接;

源代码下载点击打开链接

源代码如下:

# -*- coding: utf-8 -*- 
'''
Created on Jul 18, 2014

@author: Jayhomzhou

@note: 计算注释以及代码的总行数(即代码量)
'''
import subprocess

def countCodes(codePath, fileTypes):
    typeStrs = ''
    for ft in fileTypes:
        typeStrs += ' -name "*.' + ft +'" -or'
    if len(typeStrs) > 0:
        typeStrs = typeStrs[0:len(typeStrs)-3]#去除最后面的'-or'字符
    countOrder = 'find ' + codePath + ' ' + typeStrs + ' |xargs grep -v "^$"|wc -l'#grep -v "^$"是去掉空行
    print(countOrder)
    codeLinesCount = "0"
    
    pOrder = subprocess.Popen(countOrder, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in pOrder.stdout.readlines():
        codeLinesCount = line.decode().replace('\n','').strip()
        break;
    
    return codeLinesCount

if __name__ == '__main__':
    print("Counting...")
    codePath = input("Please input the codePath:\n")
    if len(codePath) == 0:
        codePath = './'
    fileTypes = input("Please input the file types:\niOS(m,h,cpp,xib,mm)\njava(java,xml)\nWP(cs,xaml)\n")
    if len(codePath) and len(fileTypes): 
        print('Line\'s Count:' + countCodes(codePath, fileTypes.split(',')))
    else:
        print('Command is empty!')
    print("Count end!")

Mac OS X代码量统计程序(Python版)