<s> import argparse <EOL> import ast <EOL> import sys <EOL> version_info = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> __version__ = '<STR_LIT:.>' . join ( map ( str , version_info ) ) <EOL> def stringify ( node ) : <EOL> if isinstance ( node , ast . Name ) : <EOL> return node . id <EOL> elif isinstance ( node , ast . Attribute ) : <EOL> return '<STR_LIT>' % ( stringify ( node . value ) , node . attr ) <EOL> elif isinstance ( node , ast . Subscript ) : <EOL> return '<STR_LIT>' % ( stringify ( node . value ) , stringify ( node . slice ) ) <EOL> elif isinstance ( node , ast . Index ) : <EOL> return stringify ( node . value ) <EOL> elif isinstance ( node , ast . Call ) : <EOL> return '<STR_LIT>' % ( stringify ( node . func ) , stringify ( node . args ) , stringify ( node . keywords ) ) <EOL> elif isinstance ( node , list ) : <EOL> return '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( stringify ( n ) for n in node ) ) <EOL> elif isinstance ( node , ast . Str ) : <EOL> return node . s <EOL> else : <EOL> return ast . dump ( node ) <EOL> class IllegalLine ( object ) : <EOL> def __init__ ( self , reason , node , filename ) : <EOL> self . reason = reason <EOL> self . lineno = node . lineno <EOL> self . filename = filename <EOL> self . node = node <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . filename , self . lineno , self . reason ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . reason , self . filename , self . lineno ) <EOL> def find_assignment_in_context ( variable , context ) : <EOL> if isinstance ( context , ( ast . FunctionDef , ast . Module , ast . For , ast . While , ast . With , ast . If ) ) : <EOL> for node in reversed ( list ( ast . iter_child_nodes ( context ) ) ) : <EOL> if isinstance ( node , ast . Assign ) : <EOL> if variable in ( stringify ( c ) for c in node . targets ) : <EOL> return node <EOL> if getattr ( context , '<STR_LIT>' , None ) : <EOL> return find_assignment_in_context ( variable , context . parent ) <EOL> else : <EOL> return None <EOL> class Checker ( ast . NodeVisitor ) : <EOL> def __init__ ( self , filename , * args , ** kwargs ) : <EOL> self . filename = filename <EOL> self . errors = [ ] <EOL> super ( Checker , self ) . __init__ ( * args , ** kwargs ) <EOL> def check_execute ( self , node ) : <EOL> if isinstance ( node , ast . BinOp ) : <EOL> if isinstance ( node . op , ast . Mod ) : <EOL> return IllegalLine ( '<STR_LIT>' , node , self . filename ) <EOL> elif isinstance ( node . op , ast . Add ) : <EOL> return IllegalLine ( '<STR_LIT>' , node , self . filename ) <EOL> elif isinstance ( node , ast . Call ) : <EOL> if isinstance ( node . func , ast . Attribute ) : <EOL> if node . func . attr == '<STR_LIT>' : <EOL> return IllegalLine ( '<STR_LIT>' , node , self . filename ) <EOL> elif isinstance ( node , ast . Name ) : <EOL> assignment = find_assignment_in_context ( node . id , node ) <EOL> if assignment is not None : <EOL> return self . check_execute ( assignment . value ) <EOL> def visit_Call ( self , node ) : <EOL> function_name = stringify ( node . func ) <EOL> if function_name . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> node . args [ <NUM_LIT:0> ] . parent = node <EOL> node_error = self . check_execute ( node . args [ <NUM_LIT:0> ] ) <EOL> if node_error : <EOL> self . errors . append ( node_error ) <EOL> elif function_name . lower ( ) == '<STR_LIT>' : <EOL> self . errors . append ( IllegalLine ( '<STR_LIT>' , node , self . filename ) ) <EOL> self . generic_visit ( node ) <EOL> def visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT>' + node . __class__ . __name__ <EOL> visitor = getattr ( self , method , self . generic_visit ) <EOL> return visitor ( node ) <EOL> def generic_visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> for field , value in ast . iter_fields ( node ) : <EOL> if isinstance ( value , list ) : <EOL> for item in value : <EOL> if isinstance ( item , ast . AST ) : <EOL> item . parent = node <EOL> self . visit ( item , ) <EOL> elif isinstance ( value , ast . AST ) : <EOL> value . parent = node <EOL> self . visit ( value ) <EOL> def check ( filename ) : <EOL> c = Checker ( filename = filename ) <EOL> with open ( filename , '<STR_LIT:r>' ) as fobj : <EOL> try : <EOL> parsed = ast . parse ( fobj . read ( ) , filename ) <EOL> c . visit ( parsed ) <EOL> except Exception : <EOL> raise <EOL> return c . errors <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' , <EOL> epilog = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:version>' , version = '<STR_LIT>' + __version__ ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:+>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> errors = [ ] <EOL> for fname in args . files : <EOL> these_errors = check ( fname ) <EOL> if these_errors : <EOL> print '<STR_LIT:\n>' . join ( str ( e ) for e in these_errors ) <EOL> errors . extend ( these_errors ) <EOL> if errors : <EOL> print '<STR_LIT>' % len ( errors ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
