U
    d5                     @   s  d Z ddlZddlZedkr:ejd ejtkr:ejd= ddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ zddlZW n ek
r   dZY nX ddlZddlmZmZ ejrdd	lmZmZmZ ejd
kZe Z g Z!da"e# Z$da%da&da'd$e(ddddZ)ddddZ*e+ddddZ,eg df ddddZ-ee+e.f ddddZ/ee+e.f e+ddddZ0dddd Z1d!Z2ddd"d#Z3edkre3  dS )%a7  Automatically restart the server when a source file is modified.

Most applications should not access this module directly.  Instead,
pass the keyword argument ``autoreload=True`` to the
`tornado.web.Application` constructor (or ``debug=True``, which
enables this setting and several others).  This will enable autoreload
mode as well as checking for changes to templates and static
resources.  Note that restarting is a destructive operation and any
requests in progress will be aborted when the process restarts.  (If
you want to disable autoreload while using other debug-mode features,
pass both ``debug=True`` and ``autoreload=False``).

This module can also be used as a command-line wrapper around scripts
such as unit test runners.  See the `main` method for details.

The command-line wrapper and Application debug modes can be used together.
This combination is encouraged as the wrapper catches syntax errors and
other import-time failures, while debug mode catches changes once
the server has started.

This module will not work correctly when `.HTTPServer`'s multi-process
mode is used.

Reloading loses any Python interpreter command-line arguments (e.g. ``-u``)
because it re-executes Python using ``sys.executable`` and ``sys.argv``.
Additionally, modifying these variables will cause reloading to behave
incorrectly.

    N__main__)ioloop)gen_log)process)exec_in)CallableDict)ListOptionalUnionwin32F  )
check_timereturnc                 C   s\   t j }|tkrdS dt|< ttdkr4td i }tt	|}t 
|| }|  dS )zBegins watching source files for changes.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.
    NT   z=tornado.autoreload started more than once in the same process)r   IOLoopcurrent	_io_loopslenr   warning	functoolspartial_reload_on_updateZPeriodicCallbackstart)r   io_loopmodify_timescallbackZ	scheduler r   6/tmp/pip-unpacked-wheel-fekwu36z/tornado/autoreload.pyr   u   s    

r   )r   c                  C   s   t  } | t |   dS )zWait for a watched file to change, then restart the process.

    Intended to be used at the end of scripts like unit test runners,
    to run the tests again after any source file changes (but see also
    the command-line interface in `main`)
    N)r   r   Zadd_callbackr   )r   r   r   r   wait   s    
r   )filenamer   c                 C   s   t |  dS )zTAdd a file to the watch list.

    All imported modules are watched by default.
    N)_watched_filesadd)r    r   r   r   watch   s    r#   )fnr   c                 C   s   t |  dS )a  Add a function to be called before reloading the process.

    Note that for open file and socket handles it is generally
    preferable to set the ``FD_CLOEXEC`` flag (using `fcntl` or
    `os.set_inheritable`) instead of using a reload hook to close them.
    N)_reload_hooksappend)r$   r   r   r   add_reload_hook   s    r'   )r   r   c                 C   s   t rd S t d k	rd S ttj D ]N}t|tj	s8q&t
|dd }|sJq&|ds^|drj|d d }t| | q&tD ]}t| | qzd S )N__file__z.pycz.pyo)_reload_attemptedr   Ztask_idlistsysmodulesvalues
isinstancetypes
ModuleTypegetattrendswith_check_filer!   )r   modulepathr   r   r   r      s    r   )r   r6   r   c                 C   s^   zt |j}W n tk
r&   Y d S X || kr<|| |< d S | | |krZtd| t  d S )Nz%s modified; restarting server)osstatst_mtime	Exceptionr   info_reload)r   r6   modifiedr   r   r   r4      s    r4   c               	   C   s<  da tD ]
} |   qtjdkr.ttjdd trHtd k	s>t	t
}t}nttjd dd }tj}|r|d|jg|dd   }nDdtj }tjd d	krtjd
d	|s|tjd
d	 tjd
< tsttjg|  td nVzttjtjg|  W n: tk
r6   ttjtjtjg|  td Y nX d S )NTr   r   r   __spec__-mr   . 
PYTHONPATH)r*   r%   r,   platformsignal	setitimerITIMER_REAL_autoreload_is_main_original_argvAssertionError_original_specr2   r-   argvnamer7   pathsepr6   environget
startswith
_has_execv
subprocessPopen
executable_exitexecvOSErrorspawnvP_NOWAIT)r$   specrK   Zpath_prefixr   r   r   r<      s<    

  
r<   z|Usage:
  python -m tornado.autoreload -m module.to.run [args...]
  python -m tornado.autoreload path/to/script.py [args...]
c               
   C   s$  ddl } d | j_atj}| | j_attjd dd}| | j_atjdd t_t	tjdkrtjd dkrd	}tjd
 }tjdd= nFt	tjd
krd}tjd }tjdd t_nt
ttjd td z^|d	krddl}|j|ddd n8|dkr.t| }|abt| t t  W 5 Q R X W n tk
rb } ztd|j W 5 d}~X Y n tk
r } z^tjddd tt d
 D ]\}	}
}}t|	 qt |t!r|j"dk	rt|j" W 5 d}~X Y nX td |t_|d	krt#$|}|dk	rt|%  t&  dS )a  Command-line wrapper to re-run a script whenever its source changes.

    Scripts may be specified by filename or module name::

        python -m tornado.autoreload -m tornado.test.runtests
        python -m tornado.autoreload tornado/test/runtests.py

    Running a script with this wrapper is similar to calling
    `tornado.autoreload.wait` at the end of the script, but this wrapper
    can catch import-time problems like syntax errors that would otherwise
    prevent the script from reaching its call to `wait`.
    r   NTr   r>      r   r?   r5      script)file)Zrun_nameZ	alter_syszScript exited with status %sz%Script exited with uncaught exception)exc_infozScript exited normally)'Ztornado.autoreloadZ
autoreloadrG   r,   rK   rH   r2   r-   rJ   r   print_USAGEstderrexitrunpyZ
run_moduleopenr(   __package__r   readglobals
SystemExitr   r;   coder:   r   	traceback
extract_tbr_   r#   r/   SyntaxErrorr    pkgutil
get_loaderget_filenamer   )tornadoZoriginal_argvZoriginal_specmoder5   r]   rd   fer    linenorL   lineloaderr   r   r   main  sT    




"



rx   )r   )4__doc__r7   r,   __name__r6   dirnamer(   r   rn   rk   r0   rR   weakrefrq   r   Ztornado.logr   r   Ztornado.utilr   rD   ImportErrortypingr   r   TYPE_CHECKINGr	   r
   r   rC   rQ   setr!   r%   r*   WeakKeyDictionaryr   rG   rH   rJ   intr   r   strr#   r'   floatr   r4   r<   ra   rx   r   r   r   r   <module>   sX   


;W
