<s> TEMPLATE_EXTENSIONS = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> PYTHON_SOURCE_EXTENSIONS = [ <EOL> "<STR_LIT>" , <EOL> ] <EOL> TEMPLATE_ENCODING = "<STR_LIT>" <EOL> PYTHON_ENCODING = "<STR_LIT>" <EOL> import os <EOL> import sys <EOL> import re <EOL> from optparse import OptionParser <EOL> USAGE = """<STR_LIT>""" <EOL> _POST_FORM_RE = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> _FORM_CLOSE_RE = re . compile ( r'<STR_LIT>' ) <EOL> _TOKEN_RE = re . compile ( '<STR_LIT>' ) <EOL> def get_template_dirs ( ) : <EOL> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> dirs = set ( ) <EOL> if ( '<STR_LIT>' in settings . TEMPLATE_LOADERS <EOL> or '<STR_LIT>' in settings . TEMPLATE_LOADERS ) : <EOL> dirs . update ( map ( unicode , settings . TEMPLATE_DIRS ) ) <EOL> if ( '<STR_LIT>' in settings . TEMPLATE_LOADERS <EOL> or '<STR_LIT>' in settings . TEMPLATE_LOADERS ) : <EOL> from django . template . loaders . app_directories import app_template_dirs <EOL> dirs . update ( app_template_dirs ) <EOL> return dirs <EOL> def make_template_info ( filename , root_dirs ) : <EOL> """<STR_LIT>""" <EOL> return Template ( filename , <EOL> [ filename [ len ( d ) + <NUM_LIT:1> : ] for d in root_dirs if filename . startswith ( d ) ] ) <EOL> class Template ( object ) : <EOL> def __init__ ( self , absolute_filename , relative_filenames ) : <EOL> self . absolute_filename , self . relative_filenames = absolute_filename , relative_filenames <EOL> def content ( self ) : <EOL> try : <EOL> return self . _content <EOL> except AttributeError : <EOL> fd = open ( self . absolute_filename ) <EOL> try : <EOL> content = fd . read ( ) . decode ( TEMPLATE_ENCODING ) <EOL> except UnicodeDecodeError , e : <EOL> message = '<STR_LIT>' % ( <EOL> e [ <NUM_LIT:4> ] , self . absolute_filename . encode ( '<STR_LIT>' , '<STR_LIT:ignore>' ) ) <EOL> raise UnicodeDecodeError ( * ( e . args [ : <NUM_LIT:4> ] + ( message , ) ) ) <EOL> fd . close ( ) <EOL> self . _content = content <EOL> return content <EOL> content = property ( content ) <EOL> def post_form_info ( self ) : <EOL> """<STR_LIT>""" <EOL> forms = { } <EOL> form_line = <NUM_LIT:0> <EOL> for ln , line in enumerate ( self . content . split ( "<STR_LIT:\n>" ) ) : <EOL> if not form_line and _POST_FORM_RE . search ( line ) : <EOL> form_line = ln + <NUM_LIT:1> <EOL> forms [ form_line ] = False <EOL> if form_line and _TOKEN_RE . search ( line ) : <EOL> forms [ form_line ] = True <EOL> form_line = <NUM_LIT:0> <EOL> if form_line and _FORM_CLOSE_RE . search ( line ) : <EOL> form_line = <NUM_LIT:0> <EOL> return forms . items ( ) <EOL> def includes_template ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for r in t . relative_filenames : <EOL> if re . search ( r'<STR_LIT>' + re . escape ( r ) + r'<STR_LIT>' , self . content ) : <EOL> return True <EOL> return False <EOL> def related_templates ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _related_templates <EOL> except AttributeError : <EOL> pass <EOL> retval = set ( [ self ] ) <EOL> for t in self . all_templates : <EOL> if t . includes_template ( self ) : <EOL> retval = retval . union ( t . related_templates ( ) ) <EOL> self . _related_templates = retval <EOL> return retval <EOL> def __repr__ ( self ) : <EOL> return repr ( self . absolute_filename ) <EOL> def __eq__ ( self , other ) : <EOL> return self . absolute_filename == other . absolute_filename <EOL> def __hash__ ( self ) : <EOL> return hash ( self . absolute_filename ) <EOL> def get_templates ( dirs ) : <EOL> """<STR_LIT>""" <EOL> templates = set ( ) <EOL> for root in dirs : <EOL> for ( dirpath , dirnames , filenames ) in os . walk ( root ) : <EOL> for f in filenames : <EOL> if len ( [ True for e in TEMPLATE_EXTENSIONS if f . endswith ( e ) ] ) > <NUM_LIT:0> : <EOL> t = make_template_info ( os . path . join ( dirpath , f ) , dirs ) <EOL> t . all_templates = templates <EOL> templates . add ( t ) <EOL> return templates <EOL> def get_python_code ( paths ) : <EOL> """<STR_LIT>""" <EOL> retval = [ ] <EOL> for p in paths : <EOL> if not os . path . isdir ( p ) : <EOL> raise Exception ( "<STR_LIT>" % p ) <EOL> for ( dirpath , dirnames , filenames ) in os . walk ( p ) : <EOL> for f in filenames : <EOL> if len ( [ True for e in PYTHON_SOURCE_EXTENSIONS if f . endswith ( e ) ] ) > <NUM_LIT:0> : <EOL> fn = os . path . join ( dirpath , f ) <EOL> fd = open ( fn ) <EOL> content = [ l . decode ( PYTHON_ENCODING ) for l in fd . readlines ( ) ] <EOL> fd . close ( ) <EOL> retval . append ( ( fn , content ) ) <EOL> return retval <EOL> def search_python_list ( python_code , template_names ) : <EOL> """<STR_LIT>""" <EOL> retval = [ ] <EOL> for tn in template_names : <EOL> retval . extend ( search_python ( python_code , tn ) ) <EOL> retval = list ( set ( retval ) ) <EOL> retval . sort ( ) <EOL> return retval <EOL> def search_python ( python_code , template_name ) : <EOL> """<STR_LIT>""" <EOL> retval = [ ] <EOL> for fn , content in python_code : <EOL> for ln , line in enumerate ( content ) : <EOL> if ( ( u'<STR_LIT>' % template_name ) in line ) or ( ( u"<STR_LIT>" % template_name ) in line ) : <EOL> retval . append ( ( fn , ln + <NUM_LIT:1> ) ) <EOL> return retval <EOL> def main ( pythonpaths ) : <EOL> template_dirs = get_template_dirs ( ) <EOL> templates = get_templates ( template_dirs ) <EOL> python_code = get_python_code ( pythonpaths ) <EOL> for t in templates : <EOL> form_matches = t . post_form_info ( ) <EOL> num_post_forms = len ( form_matches ) <EOL> form_lines_without_token = [ ln for ( ln , has_token ) in form_matches if not has_token ] <EOL> if num_post_forms == <NUM_LIT:0> : <EOL> continue <EOL> to_search = [ rf for rt in t . related_templates ( ) for rf in rt . relative_filenames ] <EOL> found = search_python_list ( python_code , to_search ) <EOL> print t . absolute_filename <EOL> for r in t . relative_filenames : <EOL> print u"<STR_LIT>" % r <EOL> print u"<STR_LIT>" % num_post_forms <EOL> print u"<STR_LIT>" % ( num_post_forms - len ( form_lines_without_token ) ) <EOL> if form_lines_without_token : <EOL> print u"<STR_LIT>" <EOL> for ln in form_lines_without_token : <EOL> print "<STR_LIT>" % ( t . absolute_filename , ln ) <EOL> print <EOL> print u"<STR_LIT>" <EOL> for r in to_search : <EOL> print u"<STR_LIT:U+0020>" + r <EOL> print <EOL> print u"<STR_LIT>" <EOL> if len ( found ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> else : <EOL> for fn , ln in found : <EOL> print "<STR_LIT>" % ( fn , ln ) <EOL> print <EOL> print "<STR_LIT>" <EOL> parser = OptionParser ( usage = USAGE ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> options , args = parser . parse_args ( ) <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> settings = getattr ( options , '<STR_LIT>' , None ) <EOL> if settings is None : <EOL> if os . environ . get ( "<STR_LIT>" , None ) is None : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> os . environ [ "<STR_LIT>" ] = settings <EOL> main ( args ) </s>
