
    j;              !       ,   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJr  S SKrS	S
KJr  S	SKJrJrJrJ r   \(       a  S SKJ!r!  Sr"\RF                  " S5      r$SSSS\" 5       S SSSSS	SSS.S\\%-  S\%\
S\4   -  S\&\S4   S\'\%\4   S-  SSS\
\(\   /S4   S-  S\
\\%/\)4   S-  S \*S!\+S"\+S#\)S-  S$\+S%\+S&\)S'\)S(\+4 S) jjr,SSSS\" 5       S SSSSSS*.S\\%-  S\%\
S\4   -  S\&\S4   S\'\%\4   S-  SSS\
\(\   /\4   S-  S\
\\%/\)4   S-  S \*S!\+S"\+S#\)S-  S&\)S'\)S(\+4S+ jjr-\" S,5      r.S-\%S(\/\%   4S. jr0 SAS\%\
S\4   -  SS/S\&\S4   S\'\%\4   S-  S0\(\   S-  S(S14S2 jjr1S\%\
S\4   -  S(S/4S3 jr2 " S4 S15      r3S5\%S6\%S-  S\&\S4   S\'\%\4   S(S4
S7 jr4S8\%S(\4S9 jr5S(\%S-  4S: jr6\ Rn                  S6\%S-  S(\S;   4S< j5       r8S=\+S>\S(S4S? jr9SBS@ jr:g)C    N)Callable	Generator)import_module)get_context)SpawnProcess)Path)sleep)TYPE_CHECKINGAny   )DefaultFilter)Change
FileChangeawatchwatch)Literal)run_processarun_processdetect_target_typeimport_stringzwatchfiles.main autoi@  2      TF)argskwargstarget_typecallbackwatch_filtergrace_perioddebouncestepdebugsigint_timeoutsigkill_timeout	recursiveignore_permission_deniedpathstarget.r   r   r   z&Literal['function', 'command', 'auto']r   r   r    r!   r"   r#   r$   r%   r&   r'   returnc                    US:X  a  [        U 5      n[        R                  SX5        [        5         [	        XX5      nSnU(       a!  [        R                  SU5        [        U5         [        UUUUU	SUUS.6 H5  nU=(       a    U" U5        UR                  XS9  [	        XXU5      nUS-  nM7     UR                  5         U$ ! UR                  5         f = f)	u  
Run a process and restart it upon file changes.

`run_process` can work in two ways:

* Using `multiprocessing.Process` † to run a python function
* Or, using `subprocess.Popen` to run a command

!!! note

    **†** technically `multiprocessing.get_context('spawn').Process` to avoid forking and improve
    code reload/import.

Internally, `run_process` uses [`watch`][watchfiles.watch] with `raise_interrupt=False` so the function
exits cleanly upon `Ctrl+C`.

Args:
    *paths: matches the same argument of [`watch`][watchfiles.watch]
    target: function or command to run
    args: arguments to pass to `target`, only used if `target` is a function
    kwargs: keyword arguments to pass to `target`, only used if `target` is a function
    target_type: type of target. Can be `'function'`, `'command'`, or `'auto'` in which case
        [`detect_target_type`][watchfiles.run.detect_target_type] is used to determine the type.
    callback: function to call on each reload, the function should accept a set of changes as the sole argument
    watch_filter: matches the same argument of [`watch`][watchfiles.watch]
    grace_period: number of seconds after the process is started before watching for changes
    debounce: matches the same argument of [`watch`][watchfiles.watch]
    step: matches the same argument of [`watch`][watchfiles.watch]
    debug: matches the same argument of [`watch`][watchfiles.watch]
    sigint_timeout: the number of seconds to wait after sending sigint before sending sigkill
    sigkill_timeout: the number of seconds to wait after sending sigkill before raising an exception
    recursive: matches the same argument of [`watch`][watchfiles.watch]

Returns:
    number of times the function was reloaded.

```py title="Example of run_process running a function"
from watchfiles import run_process

def callback(changes):
    print('changes detected:', changes)

def foobar(a, b):
    print('foobar called with:', a, b)

if __name__ == '__main__':
    run_process('./path/to/dir', target=foobar, args=(1, 2), callback=callback)
```

As well as using a `callback` function, changes can be accessed from within the target function,
using the `WATCHFILES_CHANGES` environment variable.

```py title="Example of run_process accessing changes"
from watchfiles import run_process

def foobar(a, b, c):
    # changes will be an empty list "[]" the first time the function is called
    changes = os.getenv('WATCHFILES_CHANGES')
    changes = json.loads(changes)
    print('foobar called due to changes:', changes)

if __name__ == '__main__':
    run_process('./path/to/dir', target=foobar, args=(1, 2, 3))
```

Again with the target as `command`, `WATCHFILES_CHANGES` can be used
to access changes.

```bash title="example.sh"
echo "changers: ${WATCHFILES_CHANGES}"
```

```py title="Example of run_process running a command"
from watchfiles import run_process

if __name__ == '__main__':
    run_process('.', target='./example.sh')
```
r   running "%s" as %sr   3sleeping for %s seconds before watching for changesF)r   r!   r"   r#   raise_interruptr&   r'   )r$   r%   r   )r   loggerr#   catch_sigtermstart_processr	   r   stop)r)   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   processreloadschangess                     K/var/www/reloj-farallon/venv/lib/python3.13/site-packages/watchfiles/run.pyr   r      s    @ f(0
LL%v;OF>GGJLYl%!%=	
G *'*LLLX#FwOGqLG	
 	N 	s   )A
C C)r   r   r   r   r   r    r!   r"   r#   r&   r'   c           
        #    SSK nUS:X  a  [        U 5      n[        R                  SX5        [	        5         [
        R                  R                  [        XX5      I Sh  vN nSnU(       a4  [        R                  SU5        [
        R                  " U5      I Sh  vN   [        UUUUU	U
US.6  Sh  vN nUb(  U" U5      nUR                  U5      (       a
  UI Sh  vN   [
        R                  R                  UR                  5      I Sh  vN   [
        R                  R                  [        XXU5      I Sh  vN nUS-  nM   N N N Np NA N
 [
        R                  R                  UR                  5      I Sh  vN    U$ 7f)a  
Async equivalent of [`run_process`][watchfiles.run_process], all arguments match those of `run_process` except
`callback` which can be a coroutine.

Starting and stopping the process and watching for changes is done in a separate thread.

As with `run_process`, internally `arun_process` uses [`awatch`][watchfiles.awatch], however `KeyboardInterrupt`
cannot be caught and suppressed in `awatch` so these errors need to be caught separately, see below.

```py title="Example of arun_process usage"
import asyncio
from watchfiles import arun_process

async def callback(changes):
    await asyncio.sleep(0.1)
    print('changes detected:', changes)

def foobar(a, b):
    print('foobar called with:', a, b)

async def main():
    await arun_process('.', target=foobar, args=(1, 2), callback=callback)

if __name__ == '__main__':
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('stopped via KeyboardInterrupt')
```
r   Nr   r,   r-   )r   r!   r"   r#   r&   r'   r   )inspectr   r/   r#   r0   anyio	to_threadrun_syncr1   r	   r   isawaitabler2   )r)   r   r   r   r   r   r    r!   r"   r#   r&   r'   r(   r8   r3   r4   r5   rs                     r6   r   r      sK    Z f(0
LL%v;OOO,,]FQU^^GGJLYkk,'''	!!9 g !A""1%%oo&&w||44400UYcjkk1/ _
 	( 4k" //
"
"7<<
000Ns   AF E!<FEF2E6E7E:'F!E"0FE-F EFFEFFF-FFFspawncmdc                     SS K nUR                  5       R                  R                  5       S:g  n[        R
                  " XS9$ )Nr   windows)posix)platformunamesystemlowershlexsplit)r?   rC   rB   s      r6   	split_cmdrI      s4    NN##))+y8E;;s((    zLiteral['function', 'command']r5   CombinedProcessc                 R   Uc  SnO;[         R                  " U VVs/ s H  u  pgUR                  5       U/PM     snn5      nU[        R                  S'   US:X  a]  U=(       d    0 n[        U [        5      (       a  U [        5       X#4n[        n0 nOU n[        R                  XUS9n	U	R                  5         O`U(       d  U(       a  [        R                  S5        [        U [        5      (       d   S5       e[        U 5      n
[        R                   " U
5      n	[#        U	5      $ s  snnf )Nz[]WATCHFILES_CHANGESfunction)r)   r   r   z-ignoring args and kwargs for "command" targetz+target must be a string to run as a command)jsondumpsraw_strosenviron
isinstancestrget_tty_pathrun_functionspawn_contextProcessstartr/   warningrI   
subprocessPopenrK   )r)   r   r   r   r5   changes_env_varcptarget_r3   
popen_argss              r6   r1   r1      s     **7%K741qyy{A&67%KL'6BJJ#$ j 2fc""<>47D"GFG''w&'Q6NNJK&#&&U(UU&v&
"":.7##/ &Ls   D#
c                     [        U [        5      (       d  gU R                  S5      (       a  g[        R                  " SU 5      (       a  gg)a&  
Used by [`run_process`][watchfiles.run_process], [`arun_process`][watchfiles.arun_process]
and indirectly the CLI to determine the target type with `target_type` is `auto`.

Detects the target type - either `function` or `command`. This method is only called with `target_type='auto'`.

The following logic is employed:

* If `target` is not a string, it is assumed to be a function
* If `target` ends with `.py` or `.sh`, it is assumed to be a command
* Otherwise, the target is assumed to be a function if it matches the regex `[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)+`

If this logic does not work for you, specify the target type explicitly using the `target_type` function argument
or `--target-type` command line argument.

Args:
    target: The target value

Returns:
    either `'function'` or `'command'`
rN   )z.pyz.shcommandz[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)+)rT   rU   endswithre	fullmatch)r)   s    r6   r   r     s>    , fc""		(	(	8&	A	ArJ   c                       \ rS rSrSS jrSS\S\SS4S jjrS\4S jr\	S\4S	 j5       r
S
\SS4S jr\	S\S-  4S j5       rSrg)rK   i>  c                 8    Xl         U R                  c   S5       eg )Nzprocess not yet spawned_ppid)selfr`   s     r6   __init__CombinedProcess.__init__?  s    xx#>%>>#rJ   r$   r%   r*   Nc                    [         R                  R                  SS 5        U R                  5       (       a  [        R                  S5        [         R                  " U R                  [        R                  5         U R                  U5        U R                  cV  [        R                  S5        [         R                  " U R                  [        R                  5        U R                  U5        g [        R                  S5        g [        R                  SU R                  5        g ! [        R                   a    [        R                  SU5         Nf = f)NrM   zstopping process...z!SIGINT timed out after %r secondsz+process has not terminated, sending SIGKILLzprocess stoppedz#process already dead, exit code: %d)rR   rS   popis_aliver/   r#   killrl   signalSIGINTjoinr\   TimeoutExpiredr[   exitcodeSIGKILL)rm   r$   r%   s      r6   r2   CombinedProcess.stopC  s    


+T2==??LL./GGDHHfmm,		.) }}$LM&..1		/*./NN@$--P ,,  BNS	s   ;D& &*EEc                     [        U R                  [        5      (       a  U R                  R                  5       $ U R                  R	                  5       S L $ N)rT   rk   r   rr   pollrm   s    r6   rr   CombinedProcess.is_alive[  s;    dgg|,,77##%%77<<>T))rJ   c                 .    U R                   R                  $ r|   rj   r~   s    r6   rl   CombinedProcess.pida  s     ww{{rJ   timeoutc                     [        U R                  [        5      (       a  U R                  R                  U5        g U R                  R	                  U5        g r|   )rT   rk   r   rv   wait)rm   r   s     r6   rv   CombinedProcess.joinf  s3    dgg|,,GGLL!GGLL!rJ   c                     [        U R                  [        5      (       a  U R                  R                  $ U R                  R                  $ r|   )rT   rk   r   rx   
returncoder~   s    r6   rx   CombinedProcess.exitcodel  s3    dgg|,,77###77%%%rJ   )rk   )r`   z&SpawnProcess | subprocess.Popen[bytes])r   r   )__name__
__module____qualname____firstlineno__rn   intr2   boolrr   propertyrl   rv   rx   __static_attributes__r   rJ   r6   rK   rK   >  s~    ?Q3 QS Q Q0*$ * S  "C "D " &#* & &rJ   rN   tty_pathc                 t    [        U5         [        U 5      nU" U0 UD6  S S S 5        g ! , (       d  f       g = fr|   )set_ttyr   )rN   r   r   r   funcs        r6   rW   rW   t  s-    		X&df 
		s   )
7dotted_pathc                     U R                  S5      R                  SS5      u  p[	        U5      n [        XB5      $ ! [         a  n[        SU  S35      UeSnAff = f! [         a  n[        SU SU S	35      UeSnAff = f)
z
Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the
last name in the path. Raise ImportError if the import fails.
 .r   "z!" doesn't look like a module pathNzModule "z" does not define a "z" attribute)striprsplit
ValueErrorImportErrorr   getattrAttributeError)r   module_path
class_nameemodules        r6   r   r   z  s    
V"-"3"3C"8"?"?Q"G ;'Fgv**  VAk]*LMNTUUV  gH[M1FzlR]^_effgs,   #< 
A 
AAA
B (A;;B c                       [         R                  " [        R                  R	                  5       5      $ ! [
         a     g[         a     gf = f)zf
Return the path to the current TTY, if any.

Virtually impossible to test in pytest, hence no cover.
z/dev/ttyN)rR   ttynamesysstdinfilenoOSErrorr   r   rJ   r6   rV   rV     s@    zz#))**,--  s   14 
A 	AA)NNNc              #      #    U (       a%   [        U 5       nU[        l        S v   S S S 5        g S v   g ! , (       d  f       g = f! [         a    S v    g f = f7fr|   )openr   r   r   )r   ttys     r6   r   r     sF     	h3	   	    		sF   AA 4A A
AA AA AAAAsignum_framec                 b    [         R                  S[        R                  " U 5      5        [        e)Nz-received signal %s, raising KeyboardInterrupt)r/   r[   rt   SignalsKeyboardInterrupt)r   r   s     r6   raise_keyboard_interruptr     s!    
NNBFNNSYDZ[
rJ   c                      [         R                  S[        R                  " 5       5        [        R                  " [        R
                  [        5        g)a  
Catch SIGTERM and raise KeyboardInterrupt instead. This means watchfiles will stop quickly
on `docker compose stop` and other cases where SIGTERM is sent.

Without this the watchfiles process will be killed while a running process will continue uninterrupted.
z8registering handler for SIGTERM on watchfiles process %dN)r/   r#   rR   getpidrt   SIGTERMr   r   rJ   r6   r0   r0     s,     LLKRYY[Y
MM&..":;rJ   r|   )r*   N);
contextlibrO   loggingrR   rf   rG   rt   r\   r   collections.abcr   r   	importlibr   multiprocessingr   multiprocessing.contextr   pathlibr   timer	   typingr
   r   r9   filtersr   mainr   r   r   r   r   __all__	getLoggerr/   rU   tupledictsetr   floatr   r   r   rX   listrI   r1   r   rK   rW   r   rV   contextmanagerr   r   r0   r   rJ   r6   <module>r      s      	 	    
 / # ' 0   %  " 3 3
N			,	- $(<B9=9F%*}3J}(38$$} S/} cNT!	}
 :} J($./$6} FC=$./$6} } } } $;} } } } #}  	!}F $(<B8<9F%*M3JM(38$$M S/M cNT!	M
 :M J(#-.5M FC=$./$6M M M M $;M M #M 	Md G$)3 )49 ) '+!$(38$$!$1!$ S/!$ cNT!	!$
 _t#!$ !$HsXc3h%77 <\ @3& 3&l3 #* E#s(O UYZ]_bZbUc hl gs gs g"cDj   cDj Y/?%@  S # $ 
<rJ   