o
    ßý°jü:  ã                   @  sŒ  U d dl m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 eejejhƒZh e£ej’ej’ZeedƒrRe ej¡ eeƒZ[h d£Zd6dd„Zd7dd„Zd8dd„Zd8dd„Zd9dd„Zd:dd„Z G dd„ dƒZ!G d d!„ d!e!ƒZ"G d"d#„ d#e!ƒZ#e"e#d$œZ$d%e%d&< ze&d'ƒ W n e'y®   e$d( e$d)< Y nw e$d* e$d)< d;d+d,„Z(				)d<d=d4d5„Z)dS )>é    )ÚannotationsN)Úchain)ÚPurePathé   )Ú_logÚreal_prefix>   ú.hgú.gitú.noxú.toxú.mypy_cacheú.pytest_cacheÚ__pycache__Úreturnút.Iterator[str]c                  c  sr   � t tj ¡ ƒD ].} t| ddƒ}|du s| t¡rqtj 	|¡s3|}tj 
|¡}||kr-n	tj 	|¡r |V  q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Úold© r    ú…/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/werkzeug/_reloader.pyÚ_iter_module_paths+   s   €ú€òr"   Úpathsúset[str]Úexclude_patternsÚNonec                 C  s    |D ]}|   t | |¡¡ qd S ©N)Údifference_updateÚfnmatchÚfilter)r#   r%   Úpatternr    r    r!   Ú_remove_by_pattern?   s   ÿr,   Úextra_filesút.Iterable[str]c           
   	   C  sú   t ƒ }tttjƒ| ƒD ]d}tj |¡}tj |¡r| |¡ qtj 	|¡di}t 
|¡D ]B\}}}| t¡s>tj |¡tv rC| ¡  q,d}|D ]}	|	 d¡rZd}| tj ||	¡¡ qG|sj|tj 	|¡ sj| ¡  q,|||< q,q| 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   r    r    r!   Ú_find_stat_pathsD   s6   
ÿ
€
ë
r?   c                 C  sz   t ƒ }tttjƒ| ƒD ]}tj |¡}tj |¡rtj |¡}| 	|¡ qt
ƒ D ]}| 	tj |¡¡ q(t||ƒ t|ƒS )z­Find 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   r0   r   r   r1   r"   r,   Ú_find_common_roots)r-   r%   r<   r   r    r    r!   Ú_find_watchdog_pathsw   s   

rA   c                   sh   i }t dd„ | D ƒtdd�D ]}|}|D ]}| |i ¡}q| ¡  qtƒ ‰d‡ ‡fdd„‰ ˆ |dƒ ˆS )Nc                 s  s   � | ]}t |ƒjV  qd S r'   )r   Úparts©Ú.0Úxr    r    r!   Ú	<genexpr>’   s   € z%_find_common_roots.<locals>.<genexpr>T)ÚkeyÚreverseÚnodeú t.Mapping[str, dict[str, t.Any]]r   útuple[str, ...]r   r&   c                   sH   |   ¡ D ]\}}ˆ |||f ƒ q| s |r"ˆ tjj|Ž ¡ d S d S d S r'   )Úitemsr1   r   r   r8   )rI   r   ÚprefixÚchild©Ú_walkÚrvr    r!   rP   œ   s
   ÿz!_find_common_roots.<locals>._walkr    )rI   rJ   r   rK   r   r&   )ÚsortedÚlenÚ
setdefaultr6   r/   )r#   r;   ÚchunksrI   Úchunkr    rO   r!   r@   �   s   

	r@   ú	list[str]c                  C  sx  t jdkrt jgt jdd… ¢S t jg} t jd }t jdd… }t jd }t|ddƒdu sFtjdkr„|j	dkr„tj
 |¡s„tj
 |› d	�¡r„tj
 |¡}tjdkr~tj
 |¡sdtj
 |› d	�¡rd|d	7 }tj
 t j¡d d	kr~tj
 |¡d d	kr~|  d¡ |  |¡ n1tj
 |¡r©t t|j	¡}tj
 tj
 |¡¡d }|dkr¨|d
|› �7 }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   Nr   Ú__main__Ú__package__ÚntÚ z.exeÚ.z-m)r   Úversion_infoÚ
executableÚ	orig_argvÚargvr   r   r   r   r[   r   Úexistsr0   ÚsplitextÚpopÚappendr   ÚtÚcastÚstrr4   ÚextendÚlstrip)rQ   Ú	py_scriptÚargsrZ   Ú	py_moduler   r    r    r!   Ú_get_args_for_reloading©   s>   





ÿþ

€
ro   c                   @  sf   e Zd ZdZ			dd dd„Zd!dd„Zdd„ Zd"dd„Zd"dd„Zd#dd„Z	d$dd„Z
d%dd„ZdS )&ÚReloaderLoopr]   Nr   r-   út.Iterable[str] | Noner%   Úintervalúint | floatr   r&   c                 C  s,   dd„ |pdD ƒ| _ t|pdƒ| _|| _d S )Nc                 S  s   h | ]}t j |¡’qS r    )r   r   r0   rC   r    r    r!   Ú	<setcomp>í   ó    z(ReloaderLoop.__init__.<locals>.<setcomp>r    )r-   r/   r%   rr   )Úselfr-   r%   rr   r    r    r!   Ú__init__ç   s   
zReloaderLoop.__init__c                 C  s   |   ¡  | S )zgDo any setup, then run one step of the watch to populate the
        initial filesystem state.
        )Úrun_step©rv   r    r    r!   Ú	__enter__ñ   s   zReloaderLoop.__enter__c                 C  ó   dS )z4Clean up any resources associated with the reloader.Nr    ©rv   Úexc_typeÚexc_valÚexc_tbr    r    r!   Ú__exit__ø   s   zReloaderLoop.__exit__c                 C  s   	 |   ¡  t | j¡ q)zfContinually run the watch step, sleeping for the configured
        interval after each step.
        )rx   ÚtimeÚsleeprr   ry   r    r    r!   Úrunü   s   þzReloaderLoop.runc                 C  r{   )z}Run one step for watching the filesystem. Called once to set
        up initial state, then repeatedly to update it.
        Nr    ry   r    r    r!   rx     s   zReloaderLoop.run_stepÚintc                 C  sJ   	 t dd| j› �ƒ tƒ }tj ¡ }d|d< tj||dd�}|dkr$|S q)	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_fdsrX   )r   r   ro   r   ÚenvironÚcopyÚ
subprocessÚcall)rv   rm   Únew_environÚ	exit_coder    r    r!   Úrestart_with_reloader
  s   
øz"ReloaderLoop.restart_with_reloaderÚfilenameri   c                 C  s   |   |¡ t d¡ d S )NrX   )Ú
log_reloadr   Úexit©rv   r‘   r    r    r!   Útrigger_reload  ó   
zReloaderLoop.trigger_reloadústr | bytesc                 C  s"   t j |¡}tdd|›d�ƒ d S )Nr…   z * Detected change in z, reloading)r   r   r0   r   r”   r    r    r!   r’     s   zReloaderLoop.log_reload)NNr   )r-   rq   r%   rq   rr   rs   r   r&   ©r   rp   ©r   r&   )r   r„   )r‘   ri   r   r&   ©r‘   r—   r   r&   )Ú__name__Ú
__module__Ú__qualname__r   rw   rz   r€   rƒ   rx   r�   r•   r’   r    r    r    r!   rp   ä   s    ü





rp   c                      s,   e Zd ZdZd	‡ fdd„Zd
dd„Z‡  ZS )ÚStatReloaderLoopÚstatr   rp   c                   s   i | _ tƒ  ¡ S r'   )ÚmtimesÚsuperrz   ry   ©Ú	__class__r    r!   rz   $  s   
zStatReloaderLoop.__enter__r&   c              	   C  sn   t | j| jƒD ]-}zt |¡j}W n	 ty   Y qw | j |¡}|d u r+|| j|< q||kr4|  	|¡ qd S r'   )
r?   r-   r%   r   rŸ   Úst_mtimeÚOSErrorr    Úgetr•   )rv   r   ÚmtimeÚold_timer    r    r!   rx   (  s   ÿ

€ózStatReloaderLoop.run_stepr˜   r™   )r›   rœ   r�   r   rz   rx   Ú__classcell__r    r    r¢   r!   rž   !  s    rž   c                      sR   e Zd Zd‡ fdd„Zdd
d„Zd‡ fdd„Zdd„ Zddd„Zddd„Z‡  Z	S )ÚWatchdogReloaderLooprm   út.AnyÚkwargsr   r&   c           	        s  ddl m‰  ddl m‰ ddl m‰ ddl m‰ ddl m‰ ddl m} ddl m} dd	lm	} t
ƒ j|i |¤Ž | j‰G ‡ ‡‡‡‡‡fd
d„d|ƒ}|j ¡ }| d¡r[|d d… }d|› d�| _|ƒ | _dd„ | jD ƒ}|dddg|¢g dd„ tD ƒ¢| j¢d�| _t ¡ | _d S )Nr   )ÚEVENT_TYPE_CLOSED)ÚEVENT_TYPE_CREATED)ÚEVENT_TYPE_DELETED)ÚEVENT_TYPE_MODIFIED)ÚEVENT_TYPE_MOVED)ÚFileModifiedEvent)ÚPatternMatchingEventHandler)ÚObserverc                      s$   e Zd Zd‡ ‡‡‡‡‡fdd„ZdS )	z3WatchdogReloaderLoop.__init__.<locals>.EventHandlerÚeventr²   r   r&   c                   s&   |j ˆ ˆˆˆˆhvrd S ˆ|jƒ d S r'   )Ú
event_typeÚsrc_path)rv   rµ   ©r­   r®   r¯   r°   r±   r•   r    r!   Úon_any_eventH  s   ûz@WatchdogReloaderLoop.__init__.<locals>.EventHandler.on_any_eventN)rµ   r²   r   r&   )r›   rœ   r�   r¹   r    r¸   r    r!   ÚEventHandlerG  s    rº   Úobserveriøÿÿÿz
watchdog (ú)c                 s  s    � | ]}t j |¡s|V  qd S r'   )r   r   Úisdir)rD   Úpr    r    r!   rF   \  s   € z0WatchdogReloaderLoop.__init__.<locals>.<genexpr>z*.pyz*.pycz*.zipc                 S  s   g | ]}d |› d�‘qS )z*/z/*r    )rD   Údr    r    r!   Ú
<listcomp>`  ru   z1WatchdogReloaderLoop.__init__.<locals>.<listcomp>)ÚpatternsÚignore_patterns)Úwatchdog.eventsr­   r®   r¯   r°   r±   r²   r³   Úwatchdog.observersr´   r¡   rw   r•   r›   Úlowerr7   r   r»   r-   r5   r%   Úevent_handlerÚ	threadingÚEventÚshould_reload)	rv   rm   r¬   r²   r³   r´   rº   Úreloader_nameÚextra_patternsr¢   r¸   r!   rw   :  s4   

ÿþþzWatchdogReloaderLoop.__init__r‘   r—   c                 C  s   | j  ¡  |  |¡ d S r'   )rÉ   r/   r’   r”   r    r    r!   r•   f  s   
z#WatchdogReloaderLoop.trigger_reloadrp   c                   s   i | _ | j ¡  tƒ  ¡ S r'   )Úwatchesr»   Ústartr¡   rz   ry   r¢   r    r!   rz   m  s   

zWatchdogReloaderLoop.__enter__c                 C  s   | j  ¡  | j  ¡  d S r'   )r»   Ústopr8   r|   r    r    r!   r€   r  r–   zWatchdogReloaderLoop.__exit__c                 C  s6   | j j| jd�s|  ¡  | j j| jd�rt d¡ d S )N)ÚtimeoutrX   )rÉ   Úwaitrr   rx   r   r“   ry   r    r    r!   rƒ   v  s   ÿzWatchdogReloaderLoop.runc              	   C  sž   t | jƒ}t| j| jƒD ]*}|| jvr1z| jj| j|dd�| j|< W n ty0   d | j|< Y nw | 	|¡ q|D ]}| j 
|d ¡}|d urL| j |¡ q9d S )NT)Ú	recursive)r/   rÌ   rA   r-   r%   r»   ÚschedulerÆ   r¥   Údiscardre   Ú
unschedule)rv   Ú	to_deleter   Úwatchr    r    r!   rx   |  s"   

ÿü€üzWatchdogReloaderLoop.run_step)rm   r«   r¬   r«   r   r&   rš   r˜   r™   )
r›   rœ   r�   rw   r•   rz   r€   rƒ   rx   r©   r    r    r¢   r!   rª   9  s    
,
rª   )rŸ   Úwatchdogzdict[str, type[ReloaderLoop]]Úreloader_loopszwatchdog.observersrŸ   Úautor×   c                  C  s€   t jdu s
t j ¡ sdS zddl} W n
 ty   Y dS w |  t j¡}|d | j@ s>|d  | jO  < |  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   rX   )	r   ÚstdinÚisattyÚtermiosÚImportErrorÚ	tcgetattrÚECHOÚ	tcsetattrÚTCSANOW)rÜ   Ú
attributesr    r    r!   Úensure_echo_on¡  s   ÿþrã   Ú	main_funcút.Callable[[], None]rq   rr   rs   Úreloader_typeri   c                 C  s¾   ddl }|  |jdd„ ¡ t| |||d�}z>tj d¡dkrKtƒ  tj| dd	�}d
|_	|� | 
¡  | ¡  W d  ƒ W dS 1 sCw   Y  W dS t | ¡ ¡ W dS  ty^   Y dS w )z<Run the given function in an independent Python interpreter.r   Nc                  W  s
   t  d¡S )Nr   )r   r“   )rm   r    r    r!   Ú<lambda>¾  s   
 z#run_with_reloader.<locals>.<lambda>)r-   r%   rr   r‡   r†   r    )Útargetrm   T)ÚsignalÚSIGTERMrØ   r   rŠ   r¦   rã   rÇ   ÚThreadÚdaemonrÍ   rƒ   r   r“   r�   ÚKeyboardInterrupt)rä   r-   r%   rr   ræ   ré   Úreloaderrg   r    r    r!   Úrun_with_reloader´  s$   ÿ
&þÿrï   )r   r   )r#   r$   r%   r$   r   r&   )r-   r$   r%   r$   r   r.   )r#   r.   r   r.   )r   rW   r™   )NNr   rÙ   )rä   rå   r-   rq   r%   rq   rr   rs   ræ   ri   r   r&   )*Ú
__future__r   r)   r   rŒ   r   rÇ   r�   Útypingrg   Ú	itertoolsr   Úpathlibr   Ú	_internalr   ÚtupleÚbase_prefixÚbase_exec_prefixr   rM   Úexec_prefixÚhasattrr1   r   r3   r5   r"   r,   r?   rA   r@   ro   rp   rž   rª   rØ   Ú__annotations__Ú
__import__rÝ   rã   rï   r    r    r    r!   Ú<module>   sT    




3

;=\þÿ
û