<s> import os , sys , time <EOL> try : <EOL> import thread <EOL> except ImportError : <EOL> import dummy_thread as thread <EOL> try : <EOL> import threading <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> import termios <EOL> except ImportError : <EOL> termios = None <EOL> RUN_RELOADER = True <EOL> _mtimes = { } <EOL> _win = ( sys . platform == "<STR_LIT:win32>" ) <EOL> def code_changed ( ) : <EOL> global _mtimes , _win <EOL> for filename in filter ( lambda v : v , map ( lambda m : getattr ( m , "<STR_LIT>" , None ) , sys . modules . values ( ) ) ) : <EOL> if filename . endswith ( "<STR_LIT>" ) or filename . endswith ( "<STR_LIT>" ) : <EOL> filename = filename [ : - <NUM_LIT:1> ] <EOL> if not os . path . exists ( filename ) : <EOL> continue <EOL> stat = os . stat ( filename ) <EOL> mtime = stat . st_mtime <EOL> if _win : <EOL> mtime -= stat . st_ctime <EOL> if filename not in _mtimes : <EOL> _mtimes [ filename ] = mtime <EOL> continue <EOL> if mtime != _mtimes [ filename ] : <EOL> _mtimes = { } <EOL> return True <EOL> return False <EOL> def ensure_echo_on ( ) : <EOL> if termios : <EOL> fd = sys . stdin . fileno ( ) <EOL> attr_list = termios . tcgetattr ( fd ) <EOL> if not attr_list [ <NUM_LIT:3> ] & termios . ECHO : <EOL> attr_list [ <NUM_LIT:3> ] |= termios . ECHO <EOL> termios . tcsetattr ( fd , termios . TCSANOW , attr_list ) <EOL> def reloader_thread ( ) : <EOL> ensure_echo_on ( ) <EOL> while RUN_RELOADER : <EOL> if code_changed ( ) : <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def restart_with_reloader ( ) : <EOL> while True : <EOL> args = [ sys . executable ] + [ '<STR_LIT>' % o for o in sys . warnoptions ] + sys . argv <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> args = [ '<STR_LIT>' % arg for arg in args ] <EOL> new_environ = os . environ . copy ( ) <EOL> new_environ [ "<STR_LIT>" ] = '<STR_LIT:true>' <EOL> exit_code = os . spawnve ( os . P_WAIT , sys . executable , args , new_environ ) <EOL> if exit_code != <NUM_LIT:3> : <EOL> return exit_code <EOL> def python_reloader ( main_func , args , kwargs ) : <EOL> if os . environ . get ( "<STR_LIT>" ) == "<STR_LIT:true>" : <EOL> thread . start_new_thread ( main_func , args , kwargs ) <EOL> try : <EOL> reloader_thread ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> else : <EOL> try : <EOL> sys . exit ( restart_with_reloader ( ) ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> def jython_reloader ( main_func , args , kwargs ) : <EOL> from _systemrestart import SystemRestart <EOL> thread . start_new_thread ( main_func , args ) <EOL> while True : <EOL> if code_changed ( ) : <EOL> raise SystemRestart <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def main ( main_func , args = None , kwargs = None ) : <EOL> if args is None : <EOL> args = ( ) <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> reloader = jython_reloader <EOL> else : <EOL> reloader = python_reloader <EOL> reloader ( main_func , args , kwargs ) </s>
