<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import os <EOL> import sys <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if sys . path [ <NUM_LIT:0> ] == os . path . dirname ( __file__ ) : <EOL> del sys . path [ <NUM_LIT:0> ] <EOL> import functools <EOL> import logging <EOL> import os <EOL> import pkgutil <EOL> import sys <EOL> import traceback <EOL> import types <EOL> import subprocess <EOL> import weakref <EOL> from tornado import ioloop <EOL> from tornado . log import gen_log <EOL> from tornado import process <EOL> from tornado . util import exec_in <EOL> try : <EOL> import signal <EOL> except ImportError : <EOL> signal = None <EOL> _has_execv = sys . platform != '<STR_LIT:win32>' <EOL> _watched_files = set ( ) <EOL> _reload_hooks = [ ] <EOL> _reload_attempted = False <EOL> _io_loops = weakref . WeakKeyDictionary ( ) <EOL> def start ( io_loop = None , check_time = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> io_loop = io_loop or ioloop . IOLoop . current ( ) <EOL> if io_loop in _io_loops : <EOL> return <EOL> _io_loops [ io_loop ] = True <EOL> if len ( _io_loops ) > <NUM_LIT:1> : <EOL> gen_log . warning ( "<STR_LIT>" ) <EOL> if _has_execv : <EOL> add_reload_hook ( functools . partial ( io_loop . close , all_fds = True ) ) <EOL> modify_times = { } <EOL> callback = functools . partial ( _reload_on_update , modify_times ) <EOL> scheduler = ioloop . PeriodicCallback ( callback , check_time , io_loop = io_loop ) <EOL> scheduler . start ( ) <EOL> def wait ( ) : <EOL> """<STR_LIT>""" <EOL> io_loop = ioloop . IOLoop ( ) <EOL> start ( io_loop ) <EOL> io_loop . start ( ) <EOL> def watch ( filename ) : <EOL> """<STR_LIT>""" <EOL> _watched_files . add ( filename ) <EOL> def add_reload_hook ( fn ) : <EOL> """<STR_LIT>""" <EOL> _reload_hooks . append ( fn ) <EOL> def _reload_on_update ( modify_times ) : <EOL> if _reload_attempted : <EOL> return <EOL> if process . task_id ( ) is not None : <EOL> return <EOL> for module in list ( sys . modules . values ( ) ) : <EOL> if not isinstance ( module , types . ModuleType ) : <EOL> continue <EOL> path = getattr ( module , "<STR_LIT>" , None ) <EOL> if not path : <EOL> continue <EOL> if path . endswith ( "<STR_LIT>" ) or path . endswith ( "<STR_LIT>" ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> _check_file ( modify_times , path ) <EOL> for path in _watched_files : <EOL> _check_file ( modify_times , path ) <EOL> def _check_file ( modify_times , path ) : <EOL> try : <EOL> modified = os . stat ( path ) . st_mtime <EOL> except Exception : <EOL> return <EOL> if path not in modify_times : <EOL> modify_times [ path ] = modified <EOL> return <EOL> if modify_times [ path ] != modified : <EOL> gen_log . info ( "<STR_LIT>" , path ) <EOL> _reload ( ) <EOL> def _reload ( ) : <EOL> global _reload_attempted <EOL> _reload_attempted = True <EOL> for fn in _reload_hooks : <EOL> fn ( ) <EOL> if hasattr ( signal , "<STR_LIT>" ) : <EOL> signal . setitimer ( signal . ITIMER_REAL , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> path_prefix = '<STR_LIT:.>' + os . pathsep <EOL> if ( sys . path [ <NUM_LIT:0> ] == '<STR_LIT>' and <EOL> not os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) . startswith ( path_prefix ) ) : <EOL> os . environ [ "<STR_LIT>" ] = ( path_prefix + <EOL> os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> if not _has_execv : <EOL> subprocess . Popen ( [ sys . executable ] + sys . argv ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> try : <EOL> os . execv ( sys . executable , [ sys . executable ] + sys . argv ) <EOL> except OSError : <EOL> os . spawnv ( os . P_NOWAIT , sys . executable , <EOL> [ sys . executable ] + sys . argv ) <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> _USAGE = """<STR_LIT>""" <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> original_argv = sys . argv <EOL> sys . argv = sys . argv [ : ] <EOL> if len ( sys . argv ) >= <NUM_LIT:3> and sys . argv [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> mode = "<STR_LIT>" <EOL> module = sys . argv [ <NUM_LIT:2> ] <EOL> del sys . argv [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> elif len ( sys . argv ) >= <NUM_LIT:2> : <EOL> mode = "<STR_LIT>" <EOL> script = sys . argv [ <NUM_LIT:1> ] <EOL> sys . argv = sys . argv [ <NUM_LIT:1> : ] <EOL> else : <EOL> print ( _USAGE , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> if mode == "<STR_LIT>" : <EOL> import runpy <EOL> runpy . run_module ( module , run_name = "<STR_LIT:__main__>" , alter_sys = True ) <EOL> elif mode == "<STR_LIT>" : <EOL> with open ( script ) as f : <EOL> global __file__ <EOL> __file__ = script <EOL> global __package__ <EOL> del __package__ <EOL> exec_in ( f . read ( ) , globals ( ) , globals ( ) ) <EOL> except SystemExit as e : <EOL> logging . basicConfig ( ) <EOL> gen_log . info ( "<STR_LIT>" , e . code ) <EOL> except Exception as e : <EOL> logging . basicConfig ( ) <EOL> gen_log . warning ( "<STR_LIT>" , exc_info = True ) <EOL> for ( filename , lineno , name , line ) in traceback . extract_tb ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) : <EOL> watch ( filename ) <EOL> if isinstance ( e , SyntaxError ) : <EOL> watch ( e . filename ) <EOL> else : <EOL> logging . basicConfig ( ) <EOL> gen_log . info ( "<STR_LIT>" ) <EOL> sys . argv = original_argv <EOL> if mode == '<STR_LIT>' : <EOL> loader = pkgutil . get_loader ( module ) <EOL> if loader is not None : <EOL> watch ( loader . get_filename ( ) ) <EOL> wait ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
