1. import BeautifulSoup
  2. import os.path as op
  3. #path of the csproj file
  4. filepath = "absolute\path\to\csproj"
  5. #the directory of the files
  6. directory = op.split(filepath)[0]
  7. num_of_comments = 0
  8. num_of_LOC = 0
  9. raw_num_of_lines = 0
  10. with open(filepath) as f:
  11. soup = BeautifulSoup.BeautifulStoneSoup(f.read())
  12. #find all the files in the projet (that get compiled)
  13. compileTags = soup.findAll('compile')
  14. #a list to store all the file names
  15. csNames = []
  16. #loop over all the tags
  17. for elem in compileTags:
  18. #find the ones with include in in
  19. for attr in elem.attrs:
  20. if attr[0] == 'include':
  21. csPath = attr[1]
  22. #print csPath
  23. csNames.append(csPath)
  24. ## now we've got all the relative positions, now to use the read the files
  25. #since we've got the filepath of the csproj, and we assume the other files are
  26. #in the same folder or lower, we can split filepath up and use it as a
  27. # root directory
  28. #make sure each file exists. Race condition though, should use something
  29. # else isntead. see: http://stackoverflow.com/a/85237/541208
  30. # hard crashes if the files don't exist.
  31. for name in csNames:
  32. #could format the strings to speed things up a bit instead of conct'ing
  33. fullpath = r"{}\\{}".format(directory, name)
  34. assert op.exists(fullpath)
  35. print fullpath + ' exists'
  36. #now, just have to check if a line in the cs files starts with a '\\' or not.
  37. # you can make changes to allow it to search to '\* *\' comment blocks. I don't
  38. # use them at all.
  39. for name in csNames:
  40. with open(fullpath) as f:
  41. for line in f.readlines():
  42. #raw lines
  43. raw_num_of_lines += 1
  44. #lines with any code in it
  45. if line.strip():
  46. num_of_LOC += 1
  47. if line.strip().startswith(r"//"):
  48. #save the comment to a list. Only saving one total file.
  49. # can easily customize it to keep stats for each file
  50. #print 'a comment!'
  51. num_of_comments += 1
  52. print "Found {} comments, where a comment was a line starting with //.".format(num_of_comments)
  53. print "Found {} lines total, with {} lines with text in them.".format(raw_num_of_lines, num_of_LOC)