
    d7                     >   U 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  eej        ej        h          Zh eej        ej        Z eed          re                    ej                    ee          Z[h dZdej        e         fd	Zd
ej        e         dej        e         ddfdZdej        e         dej        e         dej        e         fdZdej        e         dej        e         dej        e         fdZ d
ej        e         dej        e         fdZ!dej"        e         fdZ# G d d          Z$ G d de$          Z% G d de$          Z&e%e&dZ'ej(        eej)        e$         f         e*d<   	  e+d           e'd         e'd<   n# e,$ r e'd         e'd<   Y nw xY wd#dZ-	 	 	 	 d$dej.        g df         dej/        ej        e                  dej/        ej        e                  d ej0        e1e2f         d!eddfd"Z3dS )%    N)chain)PurePath   )_logreal_prefix>   .hg.git.nox.tox.mypy_cache.pytest_cache__pycache__returnc               #     K   t          t          j                                                  D ]} t	          | dd          }||                    t                    r0t          j        	                    |          sG|}t          j        
                    |          }||k    rn#t          j        	                    |          G|V  dS )z;Find the filesystem paths associated with imported modules.__file__N)listsysmodulesvaluesgetattr
startswith_ignore_alwaysospathisfiledirname)modulenameolds      2lib/python3.11/site-packages/werkzeug/_reloader.py_iter_module_pathsr!   &   s       s{))++,,  vz400 	4??>:: 	'..&& 	C7??4((Ds{  '..&& 	 JJJ     pathsexclude_patternsc                 `    |D ]*}|                      t          j        | |                     +d S N)difference_updatefnmatchfilter)r#   r$   patterns      r    _remove_by_patternr+   :   sB    # @ @ug > >????@ @r"   extra_filesc           	         t                      }t          t          t          j                  |           D ]z}t
          j                            |          }t
          j                            |          r|                    |           Wt
          j        	                    |          di}t          j
        |          D ]\  }}}|                    t                    s&t
          j                            |          t          v r|                                 [d}|D ]L}	|	                    d          r5d}|                    t
          j                            ||	                     M|s:|t
          j        	                    |                   s|                                 |||<   ||                    t'                                 t)          ||           |S )ax  Find paths for the stat reloader to watch. Returns imported
    module files, Python files under non-system paths. Extra files and
    Python files under extra directories can also be scanned.

    System paths have to be excluded for efficiency. Non-system paths,
    such as a project root or ``sys.path.insert``, should be the paths
    of interest to the user anyway.
    TF)z.pyz.pyc)setr   r   r   r   r   abspathr   addr   walkr   _stat_ignore_scanbasename_ignore_common_dirsclearendswithjoinupdater!   r+   )
r,   r$   r#   r   parent_has_pyrootdirsfileshas_pyr   s
             r    _find_stat_pathsr>   ?   s    EEEd38nnk22 ") ")wt$$7>>$ 	IIdOOO..5!# 	) 	)D$
  1227##D))-@@ 

F 8 8==11 8!FIIbgll466777  mBGOOD,A,AB 

"(M$1	)4 
LL#%%&&&u.///Lr"   c                    t                      }t          t          t          j                  |           D ]t}t
          j                            |          }t
          j                            |          rt
          j                            |          }|	                    |           ut                      D ]4}|	                    t
          j                            |                     5t          ||           t          |          S )zFind paths for the stat reloader to watch. Looks at the same
    sources as the stat reloader, but watches everything under
    directories instead of individual files.
    )r.   r   r   r   r   r   r/   r   r   r0   r!   r+   _find_common_roots)r,   r$   r;   r   s       r    _find_watchdog_pathsrA   u   s     55Dd38nnk22  wt$$7>>$ 	)7??4((D"$$ ( (&&''''t-...d###r"   c                 l   i }t          d | D             t          d          D ]3}|}|D ]}|                    |i           }|                                 4t	                      dt
          j        t          t          f         dt
          j	        t          df         dd ffd |d	           S )
Nc              3   >   K   | ]}t          |          j        V  d S r&   )r   parts.0xs     r    	<genexpr>z%_find_common_roots.<locals>.<genexpr>   s+      ;;(1+++;;;;;;r"   T)keyreversenoder   .r   c                     |                                  D ]\  }} |||fz              | s(                    t          j        j        |            d S d S r&   )itemsr0   r   r   r7   )rK   r   prefixchild_walkrvs       r    rP   z!_find_common_roots.<locals>._walk   sm    !ZZ\\ 	+ 	+MFEE%	)**** 	(FF27<&'''''	( 	(r"    )
sortedlen
setdefaultr5   r.   tMappingstrdictTuple)r#   r:   chunksrK   chunkrP   rQ   s        @@r    r@   r@      s     D;;U;;;dSSS   	. 	.E??5"--DD

	B(AIc4i( (S0A (d ( ( ( ( ( ( ( 
E$OOOIr"   c                     t           j        g} t           j        d         }t           j        dd         }t           j        d         }t	          |dd          _t
          j        dk    rJ|j        dk    r>t
          j        	                    |          st
          j        	                    | d          rt
          j        
                    |          }t
          j        dk    rt
          j        	                    |          s't
          j        	                    | d          r|dz  }t
          j                            t           j                  d         dk    r>t
          j                            |          d         dk    r|                     d           |                     |           nt
          j                            |          rpt          j        t"          |j                  }t
          j                            t
          j                            |                    d         }|dk    r|d	| z  }n|}|                     d
|                    d	          f           |                     |           | S )zpDetermine how the script was executed, and return the args needed
    to execute it again in a new process.
    r   r   N__main____package__nt z.exe.z-m)r   
executableargvr   r   r   r   r_   r   existsr/   splitextpopappendr   rV   castrX   r3   extendlstrip)rQ   	py_scriptargsr^   	py_moduler   s         r    _get_args_for_reloadingro      s&    .	BI8ABB<D{:&H
 x-- #1
4#1 B&#1 y))#1 GNNi---..	#1 GOOI..	7d? 
	 7>>),, $9@R@R@R1S1S $V#	   003v=G$$Y//2f< q			
		) 7>>)$$ 		"sH$899I7##BG$4$4Y$?$?@@CDz! (ZZZ'	 "I
		4))#../000IIdOOOIr"   c            	           e Zd ZdZ	 	 	 ddej        ej        e                  dej        ej        e                  dej        e	e
f         ddfdZdd	Zd
 ZddZddZde	fdZdeddfdZdeddfdZdS )ReloaderLoopra   Nr   r,   r$   intervalr   c                 f    d |pdD             | _         t          |pd          | _        || _        d S )Nc                 L    h | ]!}t           j                            |          "S rR   )r   r   r/   rE   s     r    	<setcomp>z(ReloaderLoop.__init__.<locals>.<setcomp>   s&    'V'V'Vq(:(:'V'V'Vr"   rR   )r,   r.   r$   rr   )selfr,   r$   rr   s       r    __init__zReloaderLoop.__init__   sB     (W'VKDUSU'V'V'V,/0@0FB,G,G r"   c                 .    |                                   | S )zgDo any setup, then run one step of the watch to populate the
        initial filesystem state.
        )run_steprv   s    r    	__enter__zReloaderLoop.__enter__   s     	r"   c                     dS )z4Clean up any resources associated with the reloader.NrR   rv   exc_typeexc_valexc_tbs       r    __exit__zReloaderLoop.__exit__   s    r"   c                 `    	 |                                   t          j        | j                   .)zfContinually run the watch step, sleeping for the configured
        interval after each step.
        )ry   timesleeprr   rz   s    r    runzReloaderLoop.run   s+    	&MMOOOJt}%%%	&r"   c                     dS )z}Run one step for watching the filesystem. Called once to set
        up initial state, then repeatedly to update it.
        NrR   rz   s    r    ry   zReloaderLoop.run_step   s	     	r"   c                     	 t          dd| j                    t                      }t          j                                        }d|d<   t          j        ||d          }|dk    r|S i)	z|Spawn a new Python interpreter with the same arguments as the
        current one, but running the reloader thread.
        Tinfoz * Restarting with trueWERKZEUG_RUN_MAINF)env	close_fds   )r   r   ro   r   environcopy
subprocesscall)rv   rm   new_environ	exit_codes       r    restart_with_reloaderz"ReloaderLoop.restart_with_reloader  sv    	!:ty::;;;*,,D*//++K/5K+,"+OOOIA~ !  	!r"   filenamec                 X    |                      |           t          j        d           d S Nr   )
log_reloadr   exitrv   r   s     r    trigger_reloadzReloaderLoop.trigger_reload  s%    !!!r"   c                 l    t           j                            |          }t          dd|d           d S )Nr   z * Detected change in z, reloading)r   r   r/   r   r   s     r    r   zReloaderLoop.log_reload  s7    7??8,,VEhEEEFFFFFr"   )NNr   )r   rq   r   N)__name__
__module____qualname__r   rV   OptionalIterablerX   Unionintfloatrw   r{   r   r   ry   r   r   r   rR   r"   r    rq   rq      s:       D 488<()	! !Z
30! *QZ_5! '#u*%	!
 
! ! ! !     & & & &   !s ! ! ! !s t    G3 G4 G G G G G Gr"   rq   c                   0     e Zd ZdZdef fdZddZ xZS )StatReloaderLoopstatr   c                 R    i | _         t                                                      S r&   )mtimessuperr{   rv   	__class__s    r    r{   zStatReloaderLoop.__enter__  s     *,ww  """r"   Nc                    t          | j        | j                  D ]o}	 t          j        |          j        }n# t          $ r Y )w xY w| j                            |          }||| j        |<   T||k    r| 	                    |           pd S r&   )
r>   r,   r$   r   r   st_mtimeOSErrorr   getr   )rv   r   mtimeold_times       r    ry   zStatReloaderLoop.run_step  s    $T%5t7LMM 	* 	*D.    {t,,H $)D!x *##D)))	* 	*s   8
AAr   )r   r   r   r   rq   r{   ry   __classcell__r   s   @r    r   r     s]        D#< # # # # # #* * * * * * * *r"   r   c                   v     e Zd Zdej        dej        ddf fdZdeddfdZdef fdZ	d	 Z
dd
ZddZ xZS )WatchdogReloaderLooprm   kwargsr   Nc                    ddl m} ddlm}  t	                      j        |i | | j         G fdd|          }|j                                        }|	                    d          r
|d d         }d| d	| _
         |            | _        d
 | j        D             } |dddg|g d t          D             | j                  | _        d| _        d S )Nr   )Observer)PatternMatchingEventHandlerc                       e Zd Z fdZdS )3WatchdogReloaderLoop.__init__.<locals>.EventHandlerc                 (     |j                    d S r&   )src_path)rv   eventr   s     r    on_any_eventz@WatchdogReloaderLoop.__init__.<locals>.EventHandler.on_any_event9  s    u~.....r"   N)r   r   r   r   )r   s   r    EventHandlerr   8  s.        / / / / / / /r"   r   observeriz
watchdog ()c                 P    g | ]#}t           j                            |          !|$S rR   )r   r   isdir)rF   ps     r    
<listcomp>z1WatchdogReloaderLoop.__init__.<locals>.<listcomp>H  s+    NNNRW]]1=M=MN!NNNr"   z*.pyz*.pycz*.zipc                     g | ]}d | d	S )z*/z/*rR   )rF   ds     r    r   z1WatchdogReloaderLoop.__init__.<locals>.<listcomp>L  s     :::*q***:::r"   )patternsignore_patternsF)watchdog.observersr   watchdog.eventsr   r   rw   r   r   lowerr6   r   r   r,   r4   r$   event_handlershould_reload)
rv   rm   r   r   r   r   reloader_nameextra_patternsr   r   s
           @r    rw   zWatchdogReloaderLoop.__init__1  sR   //////??????$)&))),	/ 	/ 	/ 	/ 	/ 	/ 	/6 	/ 	/ 	/ !)//11!!*-- 	/)#2#.M1111	 

 ONT%5NNN)\gw@@::&9:::&
 
 
 #r"   r   c                 >    d| _         |                     |           d S )NT)r   r   r   s     r    r   z#WatchdogReloaderLoop.trigger_reloadR  s$     "!!!!!r"   c                     i | _         | j                                         t                                                      S r&   )watchesr   startr   r{   r   s    r    r{   zWatchdogReloaderLoop.__enter__Y  s4    +-ww  """r"   c                 j    | j                                          | j                                          d S r&   )r   stopr7   r}   s       r    r   zWatchdogReloaderLoop.__exit__^  s0    r"   c                     | j         s4|                                  t          j        | j                   | j         4t          j        d           d S r   )r   ry   r   r   rr   r   r   rz   s    r    r   zWatchdogReloaderLoop.runb  sL    $ 	&MMOOOJt}%%% $ 	& 	r"   c                    t          | j                  }t          | j        | j                  D ]f}|| j        vrF	 | j                            | j        |d          | j        |<   n# t          $ r d | j        |<   Y nw xY w|	                    |           g|D ]9}| j        
                    |d           }|| j                            |           :d S )NT)	recursive)r.   r   rA   r,   r$   r   scheduler   r   discardrg   
unschedule)rv   	to_deleter   watchs       r    ry   zWatchdogReloaderLoop.run_stepi  s   %%	()94;PQQ 	$ 	$D4<' 	..)-)?)?*DD *@ * *DL&&  . . . *.DL&&&	. d#### 	0 	0DL$$T400E 0((///		0 	0s   *A&&A=<A=r   )r   r   r   rV   Anyrw   rX   r   rq   r{   r   r   ry   r   r   s   @r    r   r   0  s        #ae #qu # # # # # # #B"s "t " " " "#< # # # # # #
     0 0 0 0 0 0 0 0r"   r   )r   watchdogreloader_loopszwatchdog.observersr   autor   c                  d   t           j        t           j                                        sdS 	 ddl} n# t          $ r Y dS w xY w|                     t           j                  }|d         | j        z  s=|dxx         | j        z  cc<   |                     t           j        | j        |           dS dS )zuEnsure that echo mode is enabled. Some tools such as PDB disable
    it which causes usability issues after a reload.Nr   r   )	r   stdinisattytermiosImportError	tcgetattrECHO	tcsetattrTCSANOW)r   
attributess     r    ensure_echo_onr     s     y 	 0 0 2 2     ""39--Ja=7<' B1%#)W_jAAAAAB Bs   3 
A A	main_funcrr   reloader_typec                    ddl }|                     |j        d            t          |         |||          }	 t          j                            d          dk    rkt                       t          j        | d          }d	|_	        |5   |j
                     |                                 ddd           dS # 1 swxY w Y   dS t          j        |                                           dS # t          $ r Y dS w xY w)
z<Run the given function in an independent Python interpreter.r   Nc                  *    t          j        d          S )Nr   )r   r   )rm   s    r    <lambda>z#run_with_reloader.<locals>.<lambda>  s     r"   )r,   r$   rr   r   r   rR   )targetrm   T)signalSIGTERMr   r   r   r   r   	threadingThreaddaemonr   r   r   r   r   KeyboardInterrupt)r   r,   r$   rr   r   r   reloaderrV   s           r    run_with_reloaderr     s]    MMM
MM&.";";<<<m,2BX  H:>>-..&8 	7 	;;;AAH   			                  HX335566666   s<   AC0 
$B;.C0 ;B??C0 B?C0 &C0 0
C>=C>r   )NNr   r   )4r(   r   r   r   r   r   typingrV   	itertoolsr   pathlibr   	_internalr   tuplebase_prefixbase_exec_prefixr   rN   exec_prefixhasattrr0   r   r2   r4   IteratorrX   r!   Setr+   r   r>   rA   r@   Listro   rq   r   r   r   DictType__annotations__
__import__r   r   Callabler   r   r   r   r   rR   r"   r    <module>r     s     				     



                            )=>??	7>	73:	7s	7
73  
JJsE&MM 
   AJsO    (@aeCj @AE#J @4 @ @ @ @
3s3/0uSz3Z_3 3 3 3l$s$/0uSz$Z_$ $ $ $0ajo !*S/    03 3 3 3 3l:G :G :G :G :G :G :G :Gz* * * * *| * * *0N0 N0 N0 N0 N0< N0 N0 N0d $5 5sAF<001   
8J#$$$ ,J7N6  4 4 4+F3N64B B B B* 0448$% z"d(#AJsO, jC1 gc5j!	
  
     s   F( (F;:F;