Code modification statistics Tool (Python)

Source: Internet
Author: User

This tool can be used to compare the number of lines modified by the Code.

Usage:

1: Install python.

2: Create a folder in any directory named "old" and put all the files before modification into this folder.

3: Create a folder in the same directory and name it "new". Put all the modified files in the folder.

4: copy the tool and name it diff. py.

5: Double-click diff. py to get the comparison result.

6: You can use beyond compare to compare files on both sides and manually count the number of modified lines to verify the result.

7: sometimes it is found that the number of rows modified by this tool is less than beyond compare, because this tool uses the Alignment Method with the minimum number of rows modified.

Note: After setting the PRINTPATH in the file to Etrue, you can print the alignment line numbers of the files on both sides. The row numbers of the old files are on the left and the row numbers of the files on the right.

import osimport os.pathimport stringfrom os.path import joinPRINTPATH = Falsedef 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 linesdef 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()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.