代码修改统计工具(python)

该工具可以用来比较代码修改的行数。

使用方法:

1:安装python。

2:在任意一个目录建立一个文件夹,命名为"old",将修改前的文件都放入该文件夹。

3:在相同目录建立一个文件夹,命名为"new",将修改后的文件都放入该文件夹。

4:将本工具拷下来,命名为diff.py。

5:双击diff.py即可得到比较结果。

6:可用beyond compare比较两边文件,手动数一下修改的行数来验证结果。

7:有时会发现该工具统计的修改行数比beyond compare少,这是因为该工具取的是修改行数最小的对齐方式。

注:将文件中的PRINTPATH设为Etrue后可打印两边文件的对齐行号,左边为旧文件的行号,右边为文件的行号。

import os
import os.path
import string
from os.path import join

PRINTPATH = False

def calDiff(oldFile, newFile, fileName):
    commLen = []
    oldLines = 0
    newLines = 0
    newContents = []
    oldContents = []
    minDiffs = []
# minDiffs[[[a, b, c, d, e, f],...],...]
# each item in this list contains the information of one format of aligning the old and new lines.
# a: index of old lines(blank lines excluded & begin from 0).
# b: index of new lines(blank lines excluded & begin from 0).
# c: index of old lines(blank lines included & begin from 1). (PRINTPATH enabled)
# d: index of new lines(blank lines included & begin from 1). (PRINTPATH enabled)
# e: minimum different lines when the c line and the d line are aligned.
# f: index of previous aligned lines. (PRINTPATH enabled)

    oldLineNum = 0
    for eachLine in oldFile:
        oldLineNum += 1
        if not eachLine.isspace():
            oldLines += 1
            oldContents.append([oldLineNum, string.strip(eachLine)])

    newLineNum = 0
    for eachLine in newFile:
        newLineNum += 1
        if not eachLine.isspace():
            newLines += 1
            newContents.append([newLineNum, string.strip(eachLine)])    

    for i in range(oldLines):       
        for j in range(newLines):
            value = 0
            equal = (oldContents[i][1] == newContents[j][1])
            if j == 0:
                if equal:
                    value = 1
                    commLen.append([1])
                else:
                    commLen.append([0])
            elif i == 0:
                if equal:
                    value = 1
                    commLen[0].append(1)
                else:
                    commLen[0].append(0)
            else:
                if equal:
                    value = commLen[i-1][j-1] + 1
                else:
                    value = max(commLen[i-1][j], commLen[i][j-1])
                commLen[i].append(value)
            if equal and value > 0:
                if PRINTPATH:
                    if len(minDiffs) < value:
                        minDiffs.append([[i,j,oldContents[i][0],newContents[j][0]]])
                    else:
                        minDiffs[value-1].append([i,j,oldContents[i][0],newContents[j][0]])
                else:
                    if len(minDiffs) < value:
                        minDiffs.append([[i,j]])
                    else:
                        minDiffs[value-1].append([i,j])

    del commLen
    del oldContents
    del newContents

    if PRINTPATH:
        minDiffs.append([[oldLines,newLines,oldLineNum,newLineNum]])
    else:
        minDiffs.append([[oldLines,newLines]])

    for i in range(len(minDiffs[0])):
        minValue = max(minDiffs[0][i][0], minDiffs[0][i][1])
        minDiffs[0][i].append(minValue) 
        if PRINTPATH:
            minDiffs[0][i].append(0)

    minDiffsNum = len(minDiffs)
    for i in range(1, minDiffsNum):
        curMinDiffNum = len(minDiffs[i])
        prevMinDiffNum = len(minDiffs[i-1])
        for j in range(curMinDiffNum):
            minValue = oldLines + newLines
            final = 0
            for k in range(prevMinDiffNum):
                if minDiffs[i][j][0] > minDiffs[i-1][k][0] and minDiffs[i][j][1] > minDiffs[i-1][k][1]:
                    temp = max(minDiffs[i][j][0]-minDiffs[i-1][k][0]-1, minDiffs[i][j][1]-minDiffs[i-1][k][1]-1)
                    if PRINTPATH:
                        temp += minDiffs[i-1][k][4]
                    else:
                        temp += minDiffs[i-1][k][2]
                    if temp < minValue:
                        minValue = temp
                        final = k         
            minDiffs[i][j].append(minValue)
            if PRINTPATH:
                minDiffs[i][j].append(final)

    if PRINTPATH:
        row = len(minDiffs) - 1
        column = 0
        print "/*begin path*/"
        while row >= 0:
            print minDiffs[row][column][2], ' ',
            print minDiffs[row][column][3]
            column = minDiffs[row][column][5]
            row -= 1
        print "/*end path*/"
    
    if PRINTPATH:
        print fileName, " min diff lines ", minDiffs[len(minDiffs)-1][0][4]
    else:
        print  fileName, " min diff lines ", minDiffs[len(minDiffs)-1][0][2]
    del minDiffs
    
def totalLines(filePath):
    try:
        f = open(filePath, 'r')
    except IOError, e:
        print
        print e
        return None

    lines = 0
    for eachLine in f:
        if not eachLine.isspace():
            lines += 1
    f.close()
    return lines

def main():
    oldRoot = "old"
    newRoot = "new"
    oldFiles = []
    newFiles = []
    for root, dirs, files in os.walk(oldRoot):
        for i in range(len(files)):
            oldFiles.append(join(root[4:], files[i]))
    
    for root, dirs, files in os.walk(newRoot):
        for i in range(len(files)):
            newFiles.append(join(root[4:], files[i]))

    oldFile = ""
    newFile = ""

    for file in oldFiles:
        if file in newFiles:
            newFiles.remove(file) 
            try:
                oldFile = open(join(oldRoot, file), 'r')
                newFile = open(join(newRoot, file), 'r')
            except IOError, e:
                if not oldFile.close:
                    oldFile.close()
                print e
                raw_input('Press ENTER key to exit')
                return None
            calDiff(oldFile, newFile, file)
            oldFile.close()
            newFile.close()
        else:
            result = totalLines(join(oldRoot, file))
            if result is not None:
                print file, " is deleted. Total lines : ", result

    for file in newFiles:
        result = totalLines(join(newRoot, file))
        if result is not None:
            print file, " is added. Total lines : ", result
            
    raw_input('Press ENTER key to exit')


if __name__ == '__main__':
    main()


通过例子介绍使用方法如下: 1.差异统计 统计某一个版本的代码包相对于一个原始的基线代码包,变动的代码量 以及变动的代码量中各语言非空非注释(NBNC)的结果 缺省执的就是差异统计,直接跟上两个代码包的目录即可 实际使用中,可能会有文件名和目录名大小写不一致的情况,如果希望忽略 文件名大小写的差异,需要使用 --ignore-case 参数,否则两个一样的文件 一个会算作删除,一个会算作新增 Diffcount [test\sp1] and [test\sp2] result: LANG ADD MOD DEL A&M BLK CMT NBNC RATE ----------------------------------------------------------------------- C 44 7 26 51 8 11 35 1.00 Pascal 0 0 25 0 0 0 0 0.23 Java 7 4 11 11 0 3 9 0.41 Config 31 4 0 35 1 0 34 0.12 XML 126 0 0 126 2 0 124 0.12 ----------------------------------------------------------------------- Convert all NBNC lines to standard C Total: 57.65 (standard C lines) ADD MOD DEL A&M BLK CMT NBNC RATE 的 含义分别为: 新增、修改、删除、新增+修改、空、注释、非空非注释、标准C折算率 2.代码统计: 如果需要,可以把diffcount当作普通的代码统计工具统计一个代码代码统计使用 -c (或者--count-only)参数, 在diffcount目录下执结果如下: Counting package [test\count] result: LANG TOTAL BLK CMT NBNC RATE ----------------------------------------------------------------------- C 203 46 61 101 1.00 C++ 57 7 25 25 0.42 Pascal 117 24 17 79 0.23 Java 71 7 24 40 0.41 ASM 129 34 12 85 2.50 C# 18 1 1 17 0.20 Basic 447 140 20 295 0.21 Perl 97 4 26 74 0.16 TCL/TK 91 12 26 54 0.50 Config 116 13 0 103 0.12 XML 126 2 0 124 0.12 ----------------------------------------------------------------------- Convert all NBNC lines to standard C Total: 490.00 (standard C lines) 如果想显示每一个文件的具体信息,请使用 --print-files-info 参数
通过例子介绍使用方法如下: 1.差异统计 统计某一个版本的代码包相对于一个原始的基线代码包,变动的代码量 以及变动的代码量中各语言非空非注释(NBNC)的结果 diffcount 缺省执的就是差异统计,直接跟上两个代码包的目录即可 在diffcount目录下执: diffcount test\sp1 test\sp2 实际使用中,可能会有文件名和目录名大小写不一致的情况,如果希望忽略 文件名大小写的差异,需要使用 --ignore-case 参数,否则两个一样的文件 一个会算作删除,一个会算作新增 G:\diffcount>diffcount test\sp1 test\sp2 Diffcount [test\sp1] and [test\sp2] result: LANG ADD MOD DEL A&M BLK CMT NBNC RATE ----------------------------------------------------------------------- C 44 7 26 51 8 11 35 1.00 Pascal 0 0 25 0 0 0 0 0.23 Java 7 4 11 11 0 3 9 0.41 Config 31 4 0 35 1 0 34 0.12 XML 126 0 0 126 2 0 124 0.12 ----------------------------------------------------------------------- Convert all NBNC lines to standard C Total: 57.65 (standard C lines) ADD MOD DEL A&M BLK CMT NBNC RATE 的 含义分别为: 新增、修改、删除、新增+修改、空、注释、非空非注释、标准C折算率 2.代码统计: 如果需要,可以把diffcount当作普通的代码统计工具统计一个代码代码统计使用 -c (或者--count-only)参数, 在diffcount目录下执 diffcount -c test\count 执结果如下: G:\diffcount>diffcount -c test\count Counting package [test\count] result: LANG TOTAL BLK CMT NBNC RATE ----------------------------------------------------------------------- C 203 46 61 101 1.00 C++ 57 7 25 25 0.42 Pascal 117 24 17 79 0.23 Java 71 7 24 40 0.41 ASM 129 34 12 85 2.50 C# 18 1 1 17 0.20 Basic 447 140 20 295 0.21 Perl 97 4 26 74 0.16 TCL/TK 91 12 26 54 0.50 Config 116 13 0 103 0.12 XML 126 2 0 124 0.12 ----------------------------------------------------------------------- Convert
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值