
    k"g_                     L   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Zd dl	Z	d dl
mZ d dlmZmZ d dlmZ d dlmZ d dlm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 d dlmZ  e       Z  e       Z!dZ" ejF                  d      Z$g Z%da&	 d dl'Z'	 d dl)Z)d Z*d Z+d Z,d Z-d Z.d Z/ ed      d        Z0 ed      d        Z1d Z2d Z3d Z4d Z5 G d d      Z6 G d de6      Z7 G d  d!e8      Z9 G d" d#e6      Z:d$ Z;d% Z<d& Z=y# e($ r dZ'Y w xY w# e($ r dZ)Y w xY w)'    N)defaultdict)	lru_cachewraps)Path)
ModuleType)zipimporter)apps)request_finished)Signal)cached_property)get_version_tupleRUN_MAINzdjango.utils.autoreloadc                 8    | j                   j                  d      S )z7Return True if the given module is nested under Django.zdjango.)__name__
startswith)modules    P/var/www/html/djangosite/lib/python3.12/site-packages/django/utils/autoreload.pyis_django_moduler   1   s    ??%%i00    c                 r    t        t        j                        j                  t        |       j                  v S )z:Return True if the given file path is nested under Django.)r   django__file__parentparents)paths    r   is_django_pathr   6   s&     ''4:+=+===r   c                 .     t                fd       }|S )Nc                     	  | i | y # t         $ rr t        j                         at        \  }}}t	        |dd       t        j                  |      d   d   }n|j                  }|t        vrt        j                  |        w xY w)Nfilenamer   )
	Exceptionsysexc_info
_exceptiongetattr	traceback
extract_tbr   _error_filesappend)argskwargsetevtbr   fns         r   wrapperzcheck_errors.<locals>.wrapper<   s    	 	J#JBBr:t,4$//3B7:;;|+##H-	s
    A;B)r   )r/   r0   s   ` r   check_errorsr1   ;   s     
2Y ( Nr   c                  "    t         	t         d   y )N   )r$    r   r   raise_last_exceptionr5   T   s    m r   c                  6   t         rt        j                  j                         syt        j                  t        j                        } | d   t         j
                  z  s| dxx   t         j
                  z  cc<   t        t        d      r3t        j                  t        j                  t        j                        }nd}t        j                  t        j                  t         j                  |        |%t        j                  t        j                  |       yyy)z}
    Ensure that echo mode is enabled. Some tools such as PDB disable
    it which causes usability issues after reload.
    N   SIGTTOU)termiosr"   stdinisatty	tcgetattrECHOhasattrsignalr8   SIG_IGN	tcsetattrTCSANOW)	attr_listold_handlers     r   ensure_echo_onrE   Z   s    
 #))**,!!#)),IQ<',,&!$69% --GKK#))W__i@"MM&..+6 # 'r   c                      t        t        j                        } t        d t	        t        j                  j
                  |       D              }t        |t        t                    S )Nc              3   T   K   | ]   }t        |t        j                        s| " y wN)
isinstanceweakref
ProxyTypes).0ms     r   	<genexpr>z/iter_all_python_module_files.<locals>.<genexpr>s   s(      !W//0 	
s   &()	sortedr"   modulestuplemap__getitem__iter_modules_and_files	frozensetr(   )keysrP   s     r   iter_all_python_module_filesrW   m   sN    
 #++D S[[,,d3 G
 "'9\+BCCr   r3   )maxsizec                    g }| D ]  }t        |t              s|j                  dv r(t        |d      r|j	                  |j
                         Jt        |dd      X|j                  }|j                  sqt        |j                  t              r|j                  j                  n|j                  }|j	                  |        t               }t        j                  ||      D ]R  }|st!        |      }	 |j#                         s#	 |j+                         j-                         }
|j/                  |
       T t1        |      S # t$        $ r!}	t&        j)                  d|	|       Y d}	~	d}	~	ww xY w)z1Iterate through all modules needed to be watched.)__main____mp_main__r   __spec__Nz%"%s" raised when resolving path: "%s")rI   r   r   r>   r)   r   r%   r\   has_locationloaderr   archiveoriginset	itertoolschainr   exists
ValueErrorloggerdebugresolveabsoluteaddrU   )rP   extra_filessys_file_pathsr   specr`   resultsr   r   eresolved_paths              r   rT   rT   {   sU    N * &*-??99
 vz*%%foo66:t,4  dkk;7 ##[[ 
 !!&)3*6 eGOONK@ #H~	;;=  ! //1M"# W  	LL@!TJ	s   7E	E/E**E/c                     t        | D cg c]  }|j                   c}t        d      }i }|D ]-  }|}|D ]  }|j                  |i       } |j	                          / fdt         |d            S c c}w )a  
    Return a tuple of common roots that are shared between the given paths.
    File system watchers operate on directories and aren't cheap to create.
    Try to find the minimum set of directories to watch that encompass all of
    the files that need to be watched.
    T)keyreversec              3      K   | j                         D ]  \  }} |||fz         E d {     | st        |  y y 7 wrH   )itemsr   )noder   prefixchild_walks       r   ry   zcommon_roots.<locals>._walk   sN     !ZZ\ 	6MFEUDF9$4555	6+  6s   'A?Ar4   )rO   partslen
setdefaultclearrQ   )pathsx
path_partstreechunksrv   chunkry   s          @r   common_rootsr      s     %0Q0c4HJD  	.E??5"-D	. 	

 tR!!# 1s   A7c               #      K   t         j                  D ]_  } t        |       } | j                         s| j	                         j                         }|j                         r|j                   \| a yw)zZ
    Yield absolute directories from sys.path, ignoring entries that don't
    exist.
    N)r"   r   r   rd   rh   ri   is_filer   )r   rp   s     r   sys_path_directoriesr      sb     
  	 Dz{{}//1  "&&&	 s   A4A6c                     ddl } t        t        j                  d         }|j	                  d      }t        j
                  gt        j                  D cg c]  }d|z  	 c}z   }t        j                  j                  dv r4|j                  d t        j                  j                         D               t        | dd      |j                         sz| j                  }|j                  dk(  s|j                  j                  d	      r|j                   r|j                   }n|j                  }|d
|gz  }|t        j                  dd z  }|S |j                         s{|j                         r|gt        j                  dd S |j#                  d|j                  z        }|j                         rg ||t        j                  dd S t%        d|z        |t        j                  z  }|S c c}w )z
    Return the executable. This contains a workaround for Windows if the
    executable is reported to not have the .exe extension which can cause bugs
    on reloading.
    r   Nz.exez-W%s)cpythonpypyc              3   D   K   | ]  \  }}|d u rd| nd| d|   yw)Tz-X=Nr4   )rL   rr   values      r   rN   z&get_child_arguments.<locals>.<genexpr>   s:      
U  4-bJr#aw-??
s    r\   rZ   z	.__main__z-mr3   z%s-script.pyzScript %s does not exist.)rZ   r   r"   argvwith_suffix
executablewarnoptionsimplementationnameextend	_xoptionsru   r%   rd   r\   endswithr   	with_nameRuntimeError)rZ   	py_scriptexe_entrypointor*   rm   r   script_entrypoints           r   get_child_argumentsr      s    SXXa[!I**62NNN3??CavzCCD
"55 
!mm113
 	
 xT*6~?T?T?V  II#tyy'9'9+'FDKK;;D99Dt K    ""2SXXab\22%//0OP##%<T<,<sxx|<<6BCCK= Ds   G,c                 Z    t         j                  d|        t        j                  d       y )Nz%s changed, reloading.r7   )rf   infor"   exit)r   s    r   trigger_reloadr   	  s    
KK((3HHQKr   c                      i t         j                  t        di} t               }	 t	        j
                  || d      }|j                  dk7  r|j                  S 4)NtrueF)env	close_fdsr7   )osenvironDJANGO_AUTORELOAD_ENVr   
subprocessrun
returncode)new_environr*   ps      r   restart_with_reloaderr     sP    ?RZZ?!6?K D
NN4[EB<<1<< r   c                   d    e Zd Zd Zd ZddZd Zd Zd Zd Z	e
d        Zd	 Zed
        Zd Zy)BaseReloaderc                 |    t               | _        t        t               | _        t	        j
                         | _        y rH   )ra   rk   r   directory_globs	threadingEvent_stop_conditionselfs    r   __init__zBaseReloader.__init__  s(    5*3/(0r   c                     t        |      }	 |j                         }t        j	                  d||       | j
                  |   j                  |       y # t        $ r t        j	                  d|d       Y y w xY w)Nz6Unable to watch directory %s as it cannot be resolved.T)r#   zWatching dir %s with glob %s.)r   ri   FileNotFoundErrorrf   rg   r   rj   )r   r   globs      r   	watch_dirzBaseReloader.watch_dir  sw    Dz	==?D 	4dDAT"&&t, ! 	LLH  
 	s   A !A76A7c              #      K   t               E d{    | j                  E d{    |rC| j                  j                         D ]%  \  }}|D ]  }|j	                  |      E d{     ' yy7 ^7 L7 w)zq
        Yield all files that need to be watched, including module files and
        files within globs.
        N)rW   rk   r   ru   r   )r   include_globs	directorypatternspatterns        r   watched_fileszBaseReloader.watched_files+  s~     
 0111####'+';';'A'A'C 7#	8' 7G(~~g66677  	2# 7s1   A5A/A5A1?A5$A3%A51A53A5c                     |j                         r.|j                  j                  d      ry|j                         r.t        j	                  d       y)a  
        Wait until Django reports that the apps have been loaded. If the given
        thread has terminated before the apps are ready, then a SyntaxError or
        other non-recoverable error has been raised. In that case, stop waiting
        for the apps_ready event and continue processing.

        Return True if the thread is alive and the ready event has been
        triggered, or False if the thread is terminated while waiting for the
        event.
        皙?timeoutTz8Main Django thread has terminated before apps are ready.F)is_aliveready_eventwaitrf   rg   )r   app_regdjango_main_threads      r   wait_for_apps_readyz BaseReloader.wait_for_apps_ready7  sJ     !))+""'''4 !))+ LLSTr   c                    t         j                  d       | j                  t        |       ddlm} 	  |       j                   t         j                  d       t        j                  |        | j                          y # t        $ r Y Gw xY w)NzWaiting for apps ready_event.r   )get_resolverz>Apps ready_event triggered. Sending autoreload_started signal.)sender)rf   rg   r   r	   django.urlsr   urlconf_moduler!   autoreload_startedsendrun_loop)r   r   r   s      r   r   zBaseReloader.runI  sr    45  '9:,	N))
 	UVt,  	 	s   B   	BBc                     | j                         }| j                  s	 t        |       | j                  s| j	                          y # t        $ r Y w xY wrH   )tickshould_stopnextStopIterationstop)r   tickers     r   r   zBaseReloader.run_loopZ  sL    ""V ""
 			 ! s   A 	AAc                     t        d      )aX  
        This generator is called in a loop from run_loop. It's important that
        the method takes care of pausing or otherwise waiting for a period of
        time. This split between run_loop() and tick() is to improve the
        testability of the reloader implementations by decoupling the work they
        do from the loop.
        z!subclasses must implement tick().NotImplementedErrorr   s    r   r   zBaseReloader.tickc  s     ""EFFr   c                     t        d      )Nz/subclasses must implement check_availability().r   clss    r   check_availabilityzBaseReloader.check_availabilitym  s    !"STTr   c                     t         j                  | |      }t        j                  d||       t	        d |D              st        |       y y )N)r   	file_pathz+%s notified as changed. Signal results: %s.c              3   &   K   | ]	  }|d      yw)r3   Nr4   )rL   ress     r   rN   z3BaseReloader.notify_file_changed.<locals>.<genexpr>t  s     -c3q6-s   )file_changedr   rf   rg   anyr   )r   r   rn   s      r   notify_file_changedz BaseReloader.notify_file_changedq  sC    ##44#@BD'R-W--4  .r   c                 6    | j                   j                         S rH   )r   is_setr   s    r   r   zBaseReloader.should_stopx  s    ##**,,r   c                 8    | j                   j                          y rH   )r   ra   r   s    r   r   zBaseReloader.stop|  s      "r   N)T)r   
__module____qualname__r   r   r   r   r   r   r   classmethodr   r   propertyr   r   r4   r   r   r   r     sZ    1
-
7$"G U U! - -#r   r   c                   ,    e Zd ZdZd Zd Zed        Zy)StatReloaderr3   c              #   F  K   i }	 | j                         D ]d  \  }}|j                  |      }|||<   |t        j                  d||       6||kD  s<t        j                  d|||       | j	                  |       f t        j                  | j                         d  w)Nz File %s first seen with mtime %sz-File %s previous mtime: %s, current mtime: %s)snapshot_filesgetrf   rg   r   timesleep
SLEEP_TIME)r   mtimesfilepathmtimeold_times        r   r   zStatReloader.tick  s     #'#6#6#8 7%!::h/#(x #LL!CXuUX%LLG  	 ,,X67 JJt'# s   AB!AB!c              #      K   t               }| j                         D ]9  }||v r	 |j                         j                  }|j                  |       ||f ; y # t        $ r Y Hw xY wwrH   )ra   r   statst_mtimeOSErrorrj   )r   
seen_filesfiler   s       r   r   zStatReloader.snapshot_files  sq     U
&&( 		Dz!		,, NN4 +		
  s(   #A)A A)	A&#A)%A&&A)c                      y)NTr4   r   s    r   r   zStatReloader.check_availability  s    r   N)r   r   r   r   r   r   r   r   r4   r   r   r   r     s%    J*  r   r   c                       e Zd Zy)WatchmanUnavailableN)r   r   r   r4   r   r   r  r    s    r   r  c                        e Zd Z fdZed        Zd Zed        Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Z fdZddZed        Z xZS )WatchmanReloaderc                     t        t              | _        t        j                         | _        t        t        j                  j                  dd            | _
        t        | 1          y )NDJANGO_WATCHMAN_TIMEOUT   )r   ra   rootsr   r   processed_requestintr   r   r   client_timeoutsuperr   r   	__class__s    r   r   zWatchmanReloader.__init__  sF     %
!*!2!"**..1JA"NOr   c                 B    t        j                  | j                        S )Nr   )
pywatchmanclientr  r   s    r   r  zWatchmanReloader.client  s      )<)<==r   c                    |j                         s=|j                  j                         st        j                  d|       y |j                  }| j                  j                  dt        |j                                     }d|v rt        j                  d|d          t        j                  d|       |d   |j                  d      fS )Nz>Unable to watch root dir %s as neither it or its parent exist.zwatch-projectwarningzWatchman warning: %sz!Watchman watch-project result: %swatchrelative_path)
rd   r   rf   r  r  querystrri   rg   r   )r   rootresults      r   _watch_rootzWatchmanReloader._watch_root  s     {{};;%%'T ;;D""?C4HINN16)3DE8&Ag

? ;;;r   c                 @    | j                   j                  d|      d   S )Nclock)r  r  )r   r  s     r   
_get_clockzWatchmanReloader._get_clock  s    {{  $/88r   c                     | j                  |      \  }}ddddgddgg|g}|dg| j                  |      dd}|r||d	<   t        j                  d
|||       | j                  j                  d|||       y )Nallofanyoftypeflr   T)
expressionfieldssincededup_resultsrelative_rootz8Issuing watchman subscription %s, for root %s. Query: %s	subscribe)r  r   rf   rg   r  r  )r   r   r   r'  r  rel_pathonly_files_expressionr  s           r   
_subscribezWatchmanReloader._subscribe  s    )))4h vsmfc]3!
 0h__T*!	
 %-E/"F		
 	+tT59r   c                 J   |j                         sp|j                  j                         st        j                  d|       y d|j                  z  }|D cg c]  }|j                  d| }}|j                  }d|dg}nd}d|g}| j                  ||d||       y c c}w )N?Unable to watch directory %s as neither it or its parent exist.zfiles-parent-%s/r   	wholenamefiles:)rd   r   rf   r  r   r/  )r   r   	filenamesrw   r   r'  s         r   _subscribe_dirzWatchmanReloader._subscribe_dir  s    !##**,U &7FNWX(INNH=XIX!((I )[9JF ),J	fi#@*M Ys   B c                 r   d}|j                         sj|j                  j                         st        j                  d|       yd|j                  z  }|D cg c]  }|j                  d| }}|j                  }dg}|D ]  }|j                  d|dg        | j                  ||d	||       yc c}w )
a  
        Watch a directory with a specific glob. If the directory doesn't yet
        exist, attempt to watch the parent directory and amend the patterns to
        include this. It's important this method isn't called more than one per
        directory when updating all subscriptions. Subsequent calls will
        overwrite the named subscription, so it must include all possible glob
        expressions.
        r   r1  Nzglob-parent-%sr2  r#  matchr3  r5  )rd   r   rf   r  r   r)   r/  )r   r   r   rw   r   r'  s         r   _watch_globzWatchmanReloader._watch_glob   s     !##**,U %	6FKST9>>7;THT!((IY
 	?Gw=>	?	fi#@*M Us   B4c                     | j                   j                         }|D cg c]  }|j                   }}t        t	                     }t        g |||      S c c}w rH   )r   rV   r   listr   rU   )r   r   extra_directoriesr%  watched_file_dirs	sys_pathss         r   watched_rootszWatchmanReloader.watched_roots  s_     00557/<=!QXX==-/0	M,M/@M9MNN >s   Ac                 X   t        | j                  d            }t        | j                  |            }t        j                  dt        |             t        j                  d|       t        |      D ]  }| j                  |        | j                  j                         D ]  \  }}| j                  ||        t        |d       }t        j                  |d       D ]=  \  }}| j                  ||D cg c]  }t        |j!                  |             c}       ? y c c}w )NF)r   zWatching %s fileszFound common roots: %sc                     | j                   S rH   r   r   s    r   <lambda>z2WatchmanReloader._update_watches.<locals>.<lambda>+  s
    188 r   )rr   c                     | j                   S rH   rC  rD  s    r   rE  z2WatchmanReloader._update_watches.<locals>.<lambda>,  s
    ahh r   )r<  r   r   r@  rf   rg   r{   rO   r  r   ru   r:  rb   groupbyr7  r  relative_to)	r   r   found_rootsr  r   r   sorted_filesgroupr   s	            r   _update_watchesz WatchmanReloader._update_watches   s
   T//e/DE"4#5#5m#DE(#m*<=-{;;' 	#DT"	##'#7#7#=#=#? 	2IxY1	2 m1CD ) 1 1,DV W 	Iu5IaCi 89I	 Js   <!D'c                 v    	 | j                          y # t        $ r}| j                  |      r Y d }~y d }~ww xY wrH   )rL  r!   check_server_status)r   exs     r   update_watcheszWatchmanReloader.update_watches2  s9    	  " 	''+ ,	s    	838c                 >   | j                   j                  |      }|sy t        j                  d|       |D ]d  }t	        |d   j                  dd      d         }t        j                  d|       |j                  dg       D ]  }| j                  ||z          f y )Nz%Watchman subscription %s has results.subscriptionr5  r3   zFound root directory %sr4  )r  getSubscriptionrf   rg   r   splitr   r   )r   subrR  r  root_directoryr  s         r   _check_subscriptionz$WatchmanReloader._check_subscription:  s    {{2237<cB" 	@F
 "&"8">">sA"Fq"IJNLL2NC

7B/ @(($)>?@	@r   c                 b    t         j                  d       | j                  j                          y )Nz0Request processed. Setting update_watches event.)rf   rg   r  ra   )r   r+   s     r   request_processedz"WatchmanReloader.request_processedI  s!    GH""$r   c              #     K   t        j                  | j                         | j                          	 | j                  j                         r*| j                          | j                  j                          	 | j                  j                          t        | j                  j                  j                               D ]  }| j                  |        	 d  t'        j(                  d       # t        j                  $ r Y /t        j                  $ r1}t         j#                  d|       | j%                  |       Y d }~md }~ww xY ww)Nz+Watchman error: %s, checking server status.r   )r
   connectrY  rP  r  r   r}   r  receiver<  subsrV   rW  r  SocketTimeoutWatchmanErrorrf   rg   rN  r   r   )r   rU  rO  s      r   r   zWatchmanReloader.tickM  s       !7!78%%,,.##%&&,,.	2##%   0 0 5 5 78 2C,,S12JJsO!  ++ ++ -JBO((,,-s=   A5E8C0 AE0EEE'E>EEEc                 V    | j                   j                          t        |           y rH   )r  closer  r   r  s    r   r   zWatchmanReloader.stopb  s    r   c                     	 | j                   j                  d       y# t        $ r t        t	        |            |w xY w)z'Return True if the server is available.versionT)r  r  r!   r  r  )r   inner_exs     r   rN  z$WatchmanReloader.check_server_statusf  sC    	CKKi(   	C%c(m4(B	Cs    =c                    t         st        d      t        j                  d      }	 |j                         }t        |d         }t        j                  d|       |dk  rt        d      y # t        $ r t        d      w xY w)	Nzpywatchman not installed.r   r   z'Cannot connect to the watchman service.rc  zWatchman version %s)   	   z"Watchman 4.9 or later is required.)r  r  r  capabilityCheckr!   r   rf   rg   )r   r  r  rc  s       r   r   z#WatchmanReloader.check_availabilityn  s    %&ABB""3/	Q++-F $F9$56 	*G4V%&JKK   	Q%&OPP	Qs   A. .BrH   )r   r   r   r   r   r  r  r   r   r/  r7  r:  r@  rL  rP  rW  rY  r   r   rN  r   r   __classcell__)r  s   @r   r  r    s     > ><0 9 9:2N"N4O$@%* L Lr   r  c                  r    	 t         j                          t               S # t        $ r t               cY S w xY w)z7Return the most suitable reloader for this environment.)r  r   r  r   r4   r   r   get_reloaderrk    s7    ++-   ~s     66c                     t                t        |      }t        j                  |||d      }d|_        |j                          | j                  s| j                  |       | j                  sy y )Nzdjango-main-thread)targetr*   r+   r   T)rE   r1   r   Threaddaemonstartr   r   )reloader	main_funcr*   r+   r   s        r   start_djangors    se    Y'I"))tF9M !%""'( ""r   c                    t        j                   t         j                  d        	 t        j                  j	                  t
              dk(  rEt               }t        j                  d|j                  j                         t        || g|i | y t               }t        j                  |       y # t        $ r Y y w xY w)Nc                  ,    t        j                  d      S )Nr   )r"   r   )r*   s    r   rE  z#run_with_reloader.<locals>.<lambda>  s     r   r   z!Watching for file changes with %s)r?   SIGTERMr   r   r   r   rk  rf   r   r  r   rs  r   r"   r   KeyboardInterrupt)rr  r*   r+   rq  	exit_codes        r   run_with_reloaderry    s    
MM&..";<::>>/0F:#~HKK3X5G5G5P5P 9>t>v>-/IHHY s   A*B2 B2 2	B>=B>)>rb   loggingr   r?   r   r"   r   r   r&   rJ   collectionsr   	functoolsr   r   pathlibr   typesr   	zipimportr   r   django.appsr	   django.core.signalsr
   django.dispatchr   django.utils.functionalr   django.utils.versionr   r   r   r   	getLoggerrf   r(   r$   r9   ImportErrorr  r   r   r1   r5   rE   rW   rT   r   r   r   r   r   r   r   r   r  r  rk  rs  ry  r4   r   r   <module>r     sa     	   
     # &   !   0 " 3 2X x" 			4	5
 


1
>
27&D 1. .b 1" "< ")X
 f# f#R(< (V	, 	NL| NLb)a  G  Js$   D D DDD#"D#