fromdjango.confimportsettingsfromdjango.core.management.baseimportBaseCommand#### pyflakes.scripts.pyflakes, modified:## - return array of warnings instead of printing them## - honour pyflakes:ignore commentsimportcompiler,sysimportosimportpyflakes.checkerdefcheck(codeString,filename):""" Check the Python source given by C{codeString} for flakes. @param codeString: The Python source to check. @type codeString: C{str} @param filename: The name of the file the source came from, used to report errors. @type filename: C{str} @return: The number of warnings emitted. @rtype: C{int} """# Since compiler.parse does not reliably report syntax errors, use the# built in compiler first to detect those.try:try:compile(codeString,filename,"exec")exceptMemoryError:# Python 2.4 will raise MemoryError if the source can't be# decoded.ifsys.version_info[:2]==(2,4):raiseSyntaxError(None)raiseexcept(SyntaxError,IndentationError),value:msg=value.args[0](lineno,offset,text)=value.lineno,value.offset,value.text# If there's an encoding problem with the file, the text is None.iftextisNone:# Avoid using msg, since for the only known case, it contains a# bogus message that claims the encoding the file declared was# unknown.return["%s: problem decoding source"%(filename,)]else:line=text.splitlines()[-1]ifoffsetisnotNone:offset=offset-(len(text)-len(line))return['%s:%d: %s'%(filename,lineno,msg)]else:# Okay, it's syntactically valid. Now parse it into an ast and check# it.tree=compiler.parse(codeString)w=pyflakes.checker.Checker(tree,filename)lines=codeString.split('\n')messages=[messageformessageinw.messagesiflines[message.lineno-1].find('pyflakes:ignore')<0]messages.sort(lambdaa,b:cmp(a.lineno,b.lineno))returnmessagesdefcheckPath(filename):""" Check the given path, printing out any warnings detected. @return: the number of warnings printed """try:returncheck(file(filename,'U').read()+'\n',filename)exceptIOError,msg:return["%s: %s"%(filename,msg.args[1])]defcheckPaths(filenames):warnings=[]forarginfilenames:ifos.path.isdir(arg):fordirpath,dirnames,filenamesinos.walk(arg):forfilenameinfilenames:iffilename.endswith('.py'):warnings.extend(checkPath(os.path.join(dirpath,filename)))else:warnings.extend(checkPath(arg))returnwarnings#### pyflakes.scripts.pyflakes ends.classCommand(BaseCommand):help="Run pyflakes syntax checks."args='[filename [filename [...]]]'defhandle(self,*filenames,**options):ifnotfilenames:filenames=getattr(settings,'PYFLAKES_DEFAULT_ARGS',['.'])warnings=checkPaths(filenames)forwarninginwarnings:printwarningifwarnings:print'Total warnings: %d'%len(warnings)raiseSystemExit(1)