Saturday, 7 February 2009

Extracting review comments with Python

Often I need to exchange code over email with temmates who cannot access our source control repository due to network issues. In many cases, especially after a code review, I find that they have removed the review comments that I put in and I have no way to figure where I commented on what. This is also partly due to the not-so-good practice where I put the comments in the code using '//@@' as an indicator. To get over such cases, I wrote a simple Python script that reads all my Java source files and extracts these lines with '//@@' and puts them in a text file. This text file then serves as a rough guideline for me to review the code again.

Here is the script, hope it proves useful to someone else.



import re
import os

class Parser:
def __init__(self,pattern,outfilename):
self.pattern=re.compile(pattern)
self.outfile=open(outfilename,'a')

def walkDir(self,base_dir):
for t in os.walk(base_dir):
for f in t[2]:
self.processFile("/".join((t[0], f)))
self.outfile.close()

def processFile(self,filename):
self.outfile.write('Processiing file :'+filename+'\n')
self.outfile.write('=============================\n')
file=open(filename,'r')
for line in file:
if(self.pattern.match(line) is not None):
self.outfile.write(line+'\n')


if __name__=="__main__":
p = Parser('^.*//@@.*','testresult.txt')
p.walkDir('./code_with_review_comments')







Let me know how useful you find it!

0 comments: