get paid to paste

LOC and Comment totals

import BeautifulSoup
import os.path as op

#path of the csproj file
filepath = "absolute\path\to\csproj"
#the directory of the files
directory = op.split(filepath)[0]

num_of_comments = 0
num_of_LOC = 0
raw_num_of_lines = 0

with open(filepath) as f:
    
    soup = BeautifulSoup.BeautifulStoneSoup(f.read())

#find all the files in the projet (that get compiled)
compileTags = soup.findAll('compile')


#a list to store all the file names 
csNames = []

#loop over all the tags
for elem in compileTags:
    #find the ones with include in in
    for attr in elem.attrs:
        if attr[0] == 'include':
            csPath = attr[1]
            #print csPath
            csNames.append(csPath)

## now we've got all the relative positions, now to use the read the files
#since we've got the filepath of the csproj, and we assume the other files are
#in the same folder or lower, we can split filepath up and use it as a 
# root directory

#make sure each file exists. Race condition though, should use something 
# else isntead. see: http://stackoverflow.com/a/85237/541208
# hard crashes if the files don't exist.
for name in csNames:
    #could format the strings to speed things up a bit instead of conct'ing
    fullpath = r"{}\\{}".format(directory, name)
    assert op.exists(fullpath)
    print fullpath + ' exists'

#now, just have to check if a line in the cs files starts with a '\\' or not.
# you can make changes to allow it to search to '\* *\' comment blocks. I don't 
# use them at all.

    for name in csNames:
        with open(fullpath) as f:
            for line in f.readlines():
                #raw lines
                raw_num_of_lines += 1
                #lines with any code in it
                if line.strip():
                    num_of_LOC += 1
                if line.strip().startswith(r"//"):
                    #save the comment to a list. Only saving one total file.
                    # can easily customize it to keep stats for each file
                    #print 'a comment!'
                    num_of_comments += 1
                    

print "Found {} comments, where a comment was a line starting with //.".format(num_of_comments)
print "Found {} lines total, with {} lines with text in them.".format(raw_num_of_lines, num_of_LOC)

Pasted: Aug 26, 2012, 3:34:52 am
Views: 39