<s> """<STR_LIT>""" <EOL> import argparse <EOL> import os <EOL> import sys <EOL> import imp <EOL> import runpy <EOL> from io import StringIO <EOL> from pkgutil import ImpImporter , ImpLoader <EOL> import runpy <EOL> import sys <EOL> import tempfile <EOL> import lib2to3 <EOL> from lib2to3 . refactor import RefactoringTool , get_fixers_from_package <EOL> fixes = get_fixers_from_package ( '<STR_LIT>' ) <EOL> rt = RefactoringTool ( fixes ) <EOL> PACKAGES = [ ] <EOL> DIRS = [ ] <EOL> def maybe_2to3 ( filename , modname = None ) : <EOL> """<STR_LIT>""" <EOL> need_2to3 = False <EOL> filename = os . path . abspath ( filename ) <EOL> if any ( filename . startswith ( d ) for d in DIRS ) : <EOL> need_2to3 = True <EOL> elif modname is not None and any ( modname . startswith ( p ) for p in PACKAGES ) : <EOL> need_2to3 = True <EOL> if not need_2to3 : <EOL> return filename <EOL> outfilename = '<STR_LIT>' . join ( os . path . split ( filename ) ) <EOL> if ( not os . path . exists ( outfilename ) or <EOL> os . stat ( filename ) . st_mtime > os . stat ( outfilename ) . st_mtime ) : <EOL> try : <EOL> with open ( filename ) as file : <EOL> contents = file . read ( ) <EOL> contents = rt . refactor_docstring ( contents , filename ) <EOL> tree = rt . refactor_string ( contents , filename ) <EOL> except Exception as err : <EOL> raise ImportError ( "<STR_LIT>" % filename ) <EOL> outfile = open ( outfilename , '<STR_LIT:wb>' ) <EOL> outfile . write ( str ( tree ) . encode ( '<STR_LIT:utf8>' ) ) <EOL> outfile . close ( ) <EOL> return outfilename <EOL> class ToThreeImporter ( ImpImporter ) : <EOL> def find_module ( self , fullname , path = None ) : <EOL> subname = fullname . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> if subname != fullname and self . path is None : <EOL> return None <EOL> if self . path is None : <EOL> path = None <EOL> else : <EOL> path = [ os . path . realpath ( self . path ) ] <EOL> try : <EOL> file , filename , etc = imp . find_module ( subname , path ) <EOL> except ImportError : <EOL> return None <EOL> if file and etc [ <NUM_LIT:2> ] == imp . PY_SOURCE : <EOL> outfilename = maybe_2to3 ( filename , modname = fullname ) <EOL> if outfilename != filename : <EOL> file . close ( ) <EOL> filename = outfilename <EOL> file = open ( filename , '<STR_LIT:rb>' ) <EOL> return ImpLoader ( fullname , file , filename , etc ) <EOL> sys . path_hooks . append ( ToThreeImporter ) <EOL> for key in sys . path_importer_cache : <EOL> if sys . path_importer_cache [ key ] is None : <EOL> sys . path_importer_cache [ key ] = ToThreeImporter ( key ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , metavar = '<STR_LIT>' ) <EOL> args , rest = parser . parse_known_args ( ) <EOL> if args . package : <EOL> PACKAGES . extend ( args . package ) <EOL> if args . dir : <EOL> DIRS . extend ( os . path . abspath ( d ) for d in args . dir ) <EOL> if not PACKAGES and not DIRS : <EOL> DIRS . append ( os . getcwd ( ) ) <EOL> if args . m : <EOL> sys . argv [ <NUM_LIT:1> : ] = rest <EOL> runpy . run_module ( args . m , run_name = '<STR_LIT:__main__>' , alter_sys = True ) <EOL> elif rest : <EOL> sys . argv = rest <EOL> converted = maybe_2to3 ( rest [ <NUM_LIT:0> ] ) <EOL> with open ( converted ) as f : <EOL> new_globals = dict ( __name__ = '<STR_LIT:__main__>' , <EOL> __file__ = rest [ <NUM_LIT:0> ] ) <EOL> exec ( f . read ( ) , new_globals ) <EOL> else : <EOL> import code <EOL> code . interact ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
