
    6h                    
   S SK J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
JrJr   SSKJr  \R                    \R"                  S:X  Ga:  S SKrS SKr\R(                  rSrS	rS	rS
r\R2                  " S5      R4                  rSrSrSrSrSr S r!Sr"Sr#Sr$\R(                  RJ                  r& " S S\RN                  5      r(\RR                  " \RT                  5      r+\RX                  RZ                  r.\.R^                  r0\Rb                  \RT                  \RT                  \Rd                  \RT                  \RT                  \Rf                  /\0l4        \Rf                  \0l5        \.Rl                  r6\Rf                  /\6l4        \Rn                  \6l5        \.Rp                  r8\Rf                  \Rd                  \RT                  \+\RR                  " \(5      /\8l4        \Rn                  \8l5        \.Rr                  r9\Rf                  \Rd                  \RT                  \+\RR                  " \(5      /\9l4        \Rn                  \9l5        \.Rt                  r:/ \:l4        \RT                  \:l5        \.Rv                  r;\RT                  /\;l4        S\;l5        \.Rx                  r=\RT                  \Rd                  \RT                  \RT                  \RR                  " \Rb                  5      \RT                  \Rd                  /\=l4        \RT                  \=l5        \.R|                  r>\.R~                  r?\Rf                  \RR                  " \(5      \+\Rn                  /\?l4        \Rn                  \?l5        \@" \.SS5      rA\AbK  \Rf                  \RR                  " \(5      \+\RT                  \Rn                  /\Al4        \Rn                  \Al5        \.R                  rB\Rf                  \RT                  \Rn                  /\Bl4        \RT                  \Bl5        \.R                  rD\+\Rn                  \Rn                  \Rb                  /\Dl4        \Rf                  \Dl5        \.R                  rE\Rf                  \RR                  " \(5      /\El4        \Rn                  \El5        SrFSrG\G(       a  S rHOS rHS rI " S S\J5      rK " S S\K5      rL " S S \K5      rM " S! S"\K5      rN " S# S$\K5      rO " S% S&\K5      rP " S' S(\K5      rQ " S) S*5      rR " S+ S,5      rS " S- S.\R5      rTS/ rU " S0 S1\R5      rVSAS2 jrW " S3 S4\R5      rX " S5 S6\S5      rY " S7 S8\Y5      rZ " S9 S:\Y5      r[ " S; S<\[\Z5      r\ " S= S>\S5      r] " S? S@5      r^g! \ a
    SSKJr   GN<f = f)B    )annotationsN   )capabilitiescompatencodingload)bser)pybserntl        i   @         i   l    i     i  c                  x    \ rS rSrS\4S\4S\R                  4S\R                  4S\R                  4/rS r	Sr
g	)

OVERLAPPEDO   InternalInternalHighOffset
OffsetHighhEventc                J    SU l         SU l        SU l        SU l        SU l        g Nr   r   r   r   r   r   selfs    E/usr/lib/python3/dist-packages/hgext/fsmonitor/pywatchman/__init__.py__init__OVERLAPPED.__init__X   s&    DM !DDKDODK    r   N)__name__
__module____qualname____firstlineno__	ULONG_PTRwintypesDWORDHANDLE_fields_r   __static_attributes__ r!   r   r   r   O   sB    #Y'x~~&8>>*x'
	r!   r   GetOverlappedResultEx   Fc           	         [        S[        R                  " S[        R                  " 5       5      < SXS S  -  < 35        g )N[z%a, %d %b %Y %H:%M:%Sz] )printtimestrftimegmtimefmtargss     r   logr8      s/     5t{{}E1g	
r!   c                    g Nr,   r5   s     r   r8   r8          r!   c           	         [         R                  " 5       n[        [        [        -  [
        -  SU SUSS5         UR                  [        U5        $ ! [        U5        f = f)z7expand a win32 error code into a human readable messageNr   )ctypesc_char_pFormatMessageFORMAT_MESSAGE_FROM_SYSTEMFORMAT_MESSAGE_ALLOCATE_BUFFERFORMAT_MESSAGE_IGNORE_INSERTSvalue	LocalFree)errbufs     r   _win32_strerrorrG      sZ     //
C"
(	)
'	( 			
yy#	#s   A Ac                  *    \ rS rSrSS jrS rS rSrg)WatchmanError   Nc                    Xl         X l        g r:   )msgcmd)r   rL   rM   s      r   r   WatchmanError.__init__   s    r!   c                    Xl         g r:   )rM   )r   rM   s     r   
setCommandWatchmanError.setCommand   s    r!   c                v    U R                   (       a  U R                  < SU R                   < 3$ U R                  $ )Nz, while executing rM   rL   r   s    r   __str__WatchmanError.__str__   s%    88/3xxBBxxr!   rS   NN)r"   r#   r$   r%   r   rP   rT   r+   r,   r!   r   rI   rI      s    r!   rI   c                      \ rS rSrSrg)BSERv1Unsupportedi  r,   Nr"   r#   r$   r%   r+   r,   r!   r   rX   rX         r!   rX   c                      \ rS rSrSrg)UseAfterForki  r,   NrY   r,   r!   r   r\   r\     rZ   r!   r\   c                  ,   ^  \ rS rSrSU 4S jjrSrU =r$ )WatchmanEnvironmentErrori	  c                6   > [         TU ]  U SU SU 3U5        g )Nz: errno=z errmsg=superr   )r   rL   errnoerrmsgrM   	__class__s        r   r   !WatchmanEnvironmentError.__init__
  s#    C5xx@#Fr!   r,   r:   r"   r#   r$   r%   r   r+   __classcell__rd   s   @r   r^   r^   	  s    G Gr!   r^   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )SocketConnectErrori  c                L   > [         TU ]  SU< SU< 35        Xl        X l        g )Nzunable to connect to z: )ra   r   sockpathexc)r   rl   rm   rd   s      r   r   SocketConnectError.__init__  s    (CHI r!   )rm   rl   rf   rh   s   @r   rj   rj     s     r!   rj   c                      \ rS rSrSrSrg)SocketTimeouti  ax  A specialized exception raised for socket timeouts during communication to/from watchman.
This makes it easier to implement non-blocking loops as callers can easily distinguish
between a routine timeout and an actual error condition.

Note that catching WatchmanError will also catch this as it is a super-class, so backwards
compatibility in exception handling is preserved.
r,   Nr"   r#   r$   r%   __doc__r+   r,   r!   r   rp   rp     s    r!   rp   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )CommandErrori  zJerror returned by watchman

self.msg is the message returned by watchman.
c                .   > [         TU ]  SU< 3U5        g )Nzwatchman command error: r`   )r   rL   rM   rd   s      r   r   CommandError.__init__%  s    >Dr!   r,   r:   )r"   r#   r$   r%   rr   r   r+   rg   rh   s   @r   rt   rt     s    
E Er!   rt   c                  :    \ rS rSrSrSrS rS rS rS r	S r
S	rg)
	Transporti)  z.communication transport to the watchman serverNc                    [        5       e)ztear it downNotImplementedErrorr   s    r   closeTransport.close.      !##r!   c                    [        5       e)zread size bytesrz   )r   sizes     r   	readBytesTransport.readBytes2  r~   r!   c                    [        5       e)zwrite some datarz   )r   rF   s     r   writeTransport.write6  r~   r!   c                    g r:   r,   r   rC   s     r   
setTimeoutTransport.setTimeout:  r;   r!   c                   U R                   c  / U l         [        U R                   5      S:X  a>  SU R                   S   ;   a+  U R                   S   R                  SS5      u  pU/U l         U$  U R                  S5      nSU;   a;  SR	                  U R                   5      nUR                  SS5      u  pU/U l         X1-   $ U R                   R                  U5        Mo  )zoread a line
Maintains its own buffer, callers of the transport should not mix
calls to readBytes and readLine.
r      
r   r   r!   )rF   lensplitr   joinappend)r   linebresults       r   readLineTransport.readLine=  s    
 88DH txx=A%488A;"6))%3ITsDHKt$Az$((+GGE1-	3}$HHOOA r!   )rF   )r"   r#   r$   r%   rr   rF   r|   r   r   r   r   r+   r,   r!   r   rx   rx   )  s#    8
C$$$r!   rx   c                  4    \ rS rSrSrSrS rS rS rS r	Sr
g)	CodeciV  z.communication encoding for the watchman serverNc                    Xl         g r:   	transport)r   r   s     r   r   Codec.__init__[  s    "r!   c                    [        5       er:   rz   r   s    r   receiveCodec.receive^      !##r!   c                    [        5       er:   rz   r   r7   s     r   send
Codec.senda  r   r!   c                :    U R                   R                  U5        g r:   )r   r   r   s     r   r   Codec.setTimeoutd  s    !!%(r!   r   )r"   r#   r$   r%   rr   r   r   r   r   r   r+   r,   r!   r   r   r   V  s    8I#$$)r!   r   c                  :    \ rS rSrSrSrS rS rS rS r	S r
S	rg)
UnixSocketTransportih  z"local unix domain socket transportNc                l   Xl         X l        [        R                  " [        R                  [        R                  5      n UR                  U R                  5        UR                  U R                   5        X0l        g ! [         a+  nUR                  5         [        U R                   U5      eS nAff = fr:   )rl   timeoutsocketAF_UNIXSOCK_STREAM
settimeoutconnectsockOSErrorr|   rj   )r   rl   r   r   es        r   r   UnixSocketTransport.__init__m  sz     }}V^^V-?-?@	7OODLL)LL'I 	7JJL$T]]A66	7s   <A> >
B3&B..B3c                j    U R                   (       a"  U R                   R                  5         S U l         g g r:   )r   r|   r   s    r   r|   UnixSocketTransport.closez  s#    99IIOODI r!   c                Z    Xl         U R                  R                  U R                   5        g r:   )r   r   r   r   s     r   r   UnixSocketTransport.setTimeout  s    		T\\*r!   c                     U R                   R                  U5      /nUS   (       d  [        S5      eUS   $ ! [        R                   a    [        S5      ef = f)Nr   empty watchman responseztimed out waiting for response)r   recvrI   r   r   rp   )r   r   rF   s      r   r   UnixSocketTransport.readBytes  sZ    	B99>>$'(Cq6#$=>>q6M~~ 	B @AA	Bs	   58  Ac                     U R                   R                  U5        g ! [        R                   a    [	        S5      ef = f)Nztimed out sending query command)r   sendallr   r   rp   )r   datas     r   r   UnixSocketTransport.write  s;    	CIId#~~ 	C ABB	Cs     >)r   rl   r   )r"   r#   r$   r%   rr   r   r   r|   r   r   r   r+   r,   r!   r   r   r   h  s%    ,D7
+BCr!   r   c                ~   [        SU5        US:w  a  [        UR                  X45      nU[        :X  a  O~U[        :X  a  [        [        5        OdU[        :X  a  [        [        5        gU[        :X  a   [        5       n[        S[        U5      5        g[        5       n[        S[        U5      5        g[        XUS5      $ )a  Windows 7 and earlier does not support GetOverlappedResultEx. The
alternative is to use GetOverlappedResult and wait for read or write
operation to complete. This is done be using CreateEvent and
WaitForSingleObjectEx. CreateEvent, WaitForSingleObjectEx
and GetOverlappedResult are all part of Windows API since WindowsXP.
This is the exact same implementation that can be found in the watchman
source code (see get_overlapped_result_ex_impl in stream_win.c). This
way, maintenance should be simplified.
z"Preparing to wait for maximum %dmsr   Fz WaitForSingleObjectEx failed: %szUnexpected error: %s)r8   WaitForSingleObjectExr   WAIT_OBJECT_0WAIT_IO_COMPLETIONSetLastErrorWAIT_TIMEOUTWAIT_FAILEDGetLastErrorrG   GetOverlappedResult)pipeolapnbytesmillis	alertablewaitReturnCoderE   s          r   _get_overlapped_result_ex_implr     s     ,f5{.t{{FN]*11 +,|+ &{*.C2OC4HI .C&(<=t6599r!   c                  B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)WindowsNamedPipeTransporti  zconnect to a named pipec           	        Xl         [        [        R                  " US-  5      5      U l        S U l        [        R                  (       a  [        R                  " U5      n[        U[        [        -  SS [        [        S 5      U l        [!        5       nU R                  ["        :X  d  U R                  S:X  a-  S U l        [%        U R                   U R'                  SU5      5      e[)        S SSS 5      U l        [!        5       nU R*                  c  U R-                  SU5        [.        U l        [        R2                  " S5      S:X  d  U R0                  c  [4        U l        g g )	N  r    TFzCreateEvent failedWATCHMAN_WIN7_COMPAT1)rl   intmathceilr   _iobufr   PYTHON3osfsencode
CreateFileGENERIC_READGENERIC_WRITEOPEN_EXISTINGFILE_FLAG_OVERLAPPEDr   r   INVALID_HANDLE_VALUErj   _make_win_errCreateEvent	_waitable_raise_win_errr-   _get_overlapped_result_exgetenvr   )r   rl   r   rE   s       r   r   "WindowsNamedPipeTransport.__init__  s    499Wt^45>>{{8,H=( 
	 n99,,		QDI$T]]D4F4Fr34OPP %T4=n>>! 4c:)>&II,-4--5-KD* 6r!   c                $    U R                  X5      er:   )r   r   rL   rE   s      r   r   (WindowsNamedPipeTransport._raise_win_err  s      **r!   c                4    [        SX[        U5      4-  5      $ )Nz%s win32 error code: %d %s)IOErrorrG   r   s      r   r   'WindowsNamedPipeTransport._make_win_err  s!    (Coc6J+KK
 	
r!   c                    U R                   (       a   [        S5        [        U R                   5        S U l         U R                  b  [        U R                  5        S U l        g )NzClosing pipe)r   r8   CloseHandler   r   s    r   r|   WindowsNamedPipeTransport.close  sA    99		"	>>%'r!   c                *    [        US-  5      U l        g )Nr   )r   r   r   s     r   r   $WindowsNamedPipeTransport.setTimeout  s    54<(r!   c                   U R                   (       aS  U[        U R                   5      :  a  U R                   nSU l        U$ U R                   SU nU R                   US U l         U$ [        R                  " U5      n[        5       nU R                  Ul        [        SU5        [        U R                  X1SU5      nU(       d)  [        5       nU[        :w  a  U R                  SU-  U5        [        R                  5       nU R!                  U R                  XGU(       a  SOU R"                  S5      (       dk  [        5       n[%        U R                  U5        U[&        :X  a#  [        S5        [)        SU R"                  -  5      e[        SU5        U R                  S	U5        UR*                  nUS:X  a!  [%        U R                  U5        [-        S
5      eUSU n[/        Xq5      nX:X  a  U$ X8S U l         USU $ )zA read can block for an unbounded amount of time, even if the
kernel reports that the pipe handle is signalled, so we need to
always perform our reads asynchronously
Nzmade read buff of size %dzfailed to read %d bytesr   TzGetOverlappedResultEx timedoutz%timed out after waiting %dms for readz&GetOverlappedResultEx reports error %dzerror while waiting for readz'Async read yielded 0 bytes; unpossible!)r   r   rF   r=   create_string_bufferr   r   r   r8   ReadFiler   r   ERROR_IO_PENDINGr   r'   r(   r   r   
CancelIoExr   rp   rC   r   min)	r   r   resrF   r   	immediaterE   nreadreturned_sizes	            r   r   #WindowsNamedPipeTransport.readBytes  s    ;;s4;;''kk
++et$C++de,DKJ ))$/|nn'. TYY4>	.C&&##$=$DcJ --IItAd
 
 .Ctyy$'l"45#;dllJ  8#> >DA:
 tyy$'CDD &5kE(!J .)>M""r!   c                   [        5       nU R                  Ul        [        U R                  [
        R                  " U5      [        U5      S U5      nU(       d>  [        5       nU[        :w  a*  U R                  S[        U5      U R                  4-  U5        [        R                  5       nU R                  U R                  X%U(       a  SOU R                  S5      (       a"  [        SUR                   5        UR                   $ [        5       n[#        U R                  U5        U[$        :X  a  ['        SU R                  -  5      eU R                  S[        U5      -  U5        g )Nz%failed to write %d bytes to handle %rr   Tzmade write of %d bytesz&timed out after waiting %dms for writez)error while waiting for write of %d bytes)r   r   r   	WriteFiler   r=   r>   r   r   r   r   r'   r(   r   r   r8   rC   r   r   rp   )r   r   r   r   rE   nwrotes         r   r   WindowsNamedPipeTransport.write<  s   |nnIIvt,c$it
	 .C&&##;4y$)),- !))IIt)Qt
 
 (&,,7<<n 	499d#,84<<G  	7#d)CS	
r!   )r   r   r   rF   r   rl   r   N)r"   r#   r$   r%   rr   r   r   r   r|   r   r   r   r+   r,   r!   r   r   r     s,    !!LF+

	)B#H%
r!   r   c                T    U (       a  U $ [         R                  R                  SS5      $ )NWATCHMAN_BINARYwatchman)r   environget)binpaths    r   _default_binpathr	  d  s"     ::>>+Z88r!   c                  B    \ rS rSrSrSrSrSS jrS rS r	S r
S	 rS
rg)CLIProcessTransportim  a  open a pipe to the cli to talk to the service
This intended to be used only in the test harness!

The CLI is an oddball because we only support JSON input
and cannot send multiple commands through the same instance,
so we spawn a new process for each command.

We disable server spawning for this implementation, again, because
it is intended to be used only in our test harness.  You really
should not need to use the CLI transport for anything real.

While the CLI can output in BSER, our Transport interface doesn't
support telling this instance that it should do so.  That effectively
limits this implementation to JSON input and output only at this time.

It is the responsibility of the caller to set the send and
receive codecs appropriately.
NTc                <    Xl         X l        [        U5      U l        g r:   )rl   r   r	  r  )r   rl   r   r  s       r   r   CLIProcessTransport.__init__  s     '0r!   c                \   U R                   (       a  U R                   R                  b  U R                   R                  5         U R                   R                  R	                  5         U R                   R
                  R	                  5         U R                   R                  5         S U l         g g r:   )procpidkillstdinr|   stdoutwaitr   s    r   r|   CLIProcessTransport.close  si    99yy}}(		 IIOO!!#II""$IINNDI r!   c                   U R                   (       a  U R                   $ U R                  SU R                   3SSSSSS/n[        R                  " U[        R
                  [        R
                  S9U l         U R                   $ )	Nz--sockname=z--logfile=/BOGUSz--statefile=/BOGUSz
--no-spawnz
--no-localz--no-prettyz-j)r  r  )r  r  rl   
subprocessPopenPIPEr   s     r   _connectCLIProcessTransport._connect  sp    9999LL$--) 	
 $$


	 yyr!   c                    U R                  5         U R                  R                  R                  U5      nU(       d  [	        S5      eU$ )NzEOF on CLI process transport)r  r  r  readrI   )r   r   r   s      r   r   CLIProcessTransport.readBytes  s9    ii##D) >??
r!   c                    U R                   (       a  U R                  5         SU l         U R                  5       nUR                  R	                  U5      nUR                  R                  5         SU l         U$ )NFT)closedr|   r  r  r   )r   r   r  r   s       r   r   CLIProcessTransport.write  sT    ;;JJLDK}}jjt$


r!   )r  r   r  rl   r   r:   )r"   r#   r$   r%   rr   r  r   r   r|   r  r   r   r+   r,   r!   r   r  r  m  s,    & DF1
$r!   r  c                  >   ^  \ rS rSrSrU 4S jrS rS rS rSr	U =r
$ )	BserCodeci  z<use the BSER encoding.  This is the default, preferred codecc                <   > [         TU ]  U5        X l        X0l        g r:   )ra   r   _value_encoding_value_errors)r   r   value_encodingvalue_errorsrd   s       r   r   BserCodec.__init__  s    #-)r!   c                V    [         R                  " UU R                  U R                  S9$ )Nr'  r(  r	   loadsr%  r&  r   responses     r   _loadsBserCodec._loads  s(    zz//++
 	
r!   c                   U R                   R                  [        5      /nUS   (       d  [        S5      e[        R
                  " US   5      u  p#n[        US   5      nXE:  aD  UR                  U R                   R                  XE-
  5      5        U[        US   5      -  nXE:  a  MD  SR                  U5      n U R                  U5      nU$ ! [         a  n[        SU-  5      eS nAff = f)Nr   r   r   r!   "watchman response decode error: %s)r   r   	sniff_lenrI   r	   pdu_infor   r   r   r0  
ValueError)	r   rF   _1_2elenrlenr/  r   r   s	            r   r   BserCodec.receive  s    ~~''	231v 9::}}SV,3q6{kJJt~~//<=CBL D k 88C=	J++h'CJ 	J Dq HII	Js   ;C 
C+C&&C+c                `    [         R                  " U6 nU R                  R                  U5        g r:   )r	   dumpsr   r   r   r7   rM   s      r   r   BserCodec.send  s"    jj$S!r!   )r%  r&  )r"   r#   r$   r%   rr   r   r0  r   r   r+   rg   rh   s   @r   r#  r#    s     F*

J&" "r!   r#  c                      \ rS rSrSrS rSrg)ImmutableBserCodeci  Ouse the BSER encoding, decoding values using the newer
immutable object supportc                X    [         R                  " USU R                  U R                  S9$ )NFr+  r,  r.  s     r   r0  ImmutableBserCodec._loads  s+    zz//++	
 	
r!   r,   N)r"   r#   r$   r%   rr   r0  r+   r,   r!   r   rA  rA    s     
r!   rA  c                  8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )Bser2WithFallbackCodeci  zuse BSER v2 encodingc                  > [         TU ]  XU5        [        R                  (       a  SnOSnU R	                  SUS/0/5        U R                  5       nSU;   a  [        S5      eUS   S   (       a  SU l        S	U l        g S
U l        S	U l        g )Nrequiredoptionalversionzbser-v2errorz[The watchman server version does not support Python 3. Please upgrade your watchman server.r      r   r   )	ra   r   r   r   r   r   rX   bser_versionbser_capabilities)r   r   r'  r(  
bserv2_keyr   rd   s         r   r   Bser2WithFallbackCodec.__init__  s    LA>>#J#J		9zI;789||~l"#0 
 '	2 !D%&D" !D%&D"r!   c                \   U R                   R                  [        5      /nUS   (       d  [        S5      e[        R
                  " US   5      u  p#n[        U S5      (       a/  [        U R                  U5      U l        U R                  U-  U l
        [        US   5      nXE:  aD  UR                  U R                   R                  XE-
  5      5        U[        US   5      -  nXE:  a  MD  SR                  U5      n U R                  U5      nU$ ! [         a  n[        SU-  5      eS nAff = f)Nr   r   rM  r   r!   r3  )r   r   r4  rI   r	   r5  hasattrmaxrM  rN  r   r   r   r   r0  r6  )	r   rF   recv_bser_versionrecv_bser_capabilitiesr9  r:  r/  r   r   s	            r   r   Bser2WithFallbackCodec.receive  s   ~~''	231v 9:::>--A:O744(( #D$5$57H ID $ 6 69O OD3q6{kJJt~~//<=CBL D k 88C=	J++h'CJ 	J Dq HII	Js   ;D 
D+D&&D+c                    [        U S5      (       a,  [        R                  " UU R                  U R                  S.6nO[        R                  " U6 nU R
                  R                  U5        g )NrM  )rJ  r   )rR  r	   r=  rM  rN  r   r   r>  s      r   r   Bser2WithFallbackCodec.send  sT    4((**))!33C **d#CS!r!   )rN  rM  r   )
r"   r#   r$   r%   rr   r   r   r   r+   rg   rh   s   @r   rF  rF    s    '0J0	" 	"r!   rF  c                      \ rS rSrSrSrg)ImmutableBser2Codeci)  rB  r,   Nrq   r,   r!   r   rZ  rZ  )  s      	r!   rZ  c                  <   ^  \ rS rSrSrSrU 4S jrS rS rSr	U =r
$ )	JsonCodeci0  z<Use json codec.  This is here primarily for testing purposesNc                8   > [         TU ]  U5        SS KnX l        g r   )ra   r   json)r   r   r^  rd   s      r   r   JsonCodec.__init__5  s    #	r!   c                    U R                   R                  5       n [        R                  (       a  UR	                  S5      nU R
                  R                  U5      $ ! [         a  n[        X!5        e S nAff = f)Nzutf-8)	r   r   r   r   decoder^  r-  	Exceptionr1   )r   r   r   s      r   r   JsonCodec.receive<  s]    ~~&&(	 ~~{{7+99??4(( 	!N	s   A A 
A8'A33A8c                    U R                   R                  " U6 n[        R                  (       a  UR	                  S5      nU R
                  R                  US-   5        g )Nasciir   )r^  r=  r   r   encoder   r   r>  s      r   r   JsonCodec.sendK  sA    iioot$ >>**W%CS5[)r!   )r^  )r"   r#   r$   r%   rr   r^  r   r   r   r+   rg   rh   s   @r   r\  r\  0  s    FD* *r!   r\  c                      \ rS rSrSrSrSrSrSrSr	Sr
0 r0 r/ rSS/rSrSrSr         SS jrS rS rS	 rS
 rS rS rS rS rS rS rS rSS jrSS jrS r SS jr!S r"Sr#g)clientiU  z3Handles the communication with the watchman serviceNr8   subscriptionc
                   Xl         X l        X`l        [        U	5      U l        [
        R                  " U5      (       a  [        U[        5      (       a  X0l	        OU=(       d    [        R                  " S5      =(       d    SnUS:X  a   [        R                  S:X  a  [        U l	        O<US:X  a  [        U l	        O*US:X  a  [        U l	        Uc  SnUc  UnO[!        SU-  5      e[#        U=(       d    [        R                  " S5      =(       d    S5      n[#        U=(       d    [        R                  " S5      =(       d    S5      nU R%                  U5      U l        U R%                  U5      U l        US	L aT  [*        R,                  (       a0  [.        R0                  " 5       U l        [.        R4                  U l        g S U l        S U l        g Xpl        US	L a  [.        R4                  U l        g Xl        g )
NWATCHMAN_TRANSPORTlocalr   clir^  zinvalid transport %sWATCHMAN_ENCODINGr	   F)rl   r   useImmutableBserr	  r  inspectisclass
issubclassrx   r   r   r   namer   r   r  rI   str_parseEncoding	recvCodec	sendCodecr   r   r   get_local_encodingvalueEncodingdefault_local_errorsvalueErrors)
r   rl   r   r   sendEncodingrecvEncodingrp  rz  r|  r  s
             r   r   client.__init__f  s    ! 0'0??9%%*Y	*J*J&N!ORYY/C%DOIG#4!:g%!4e#!4'#)L'#/L#$:Y$FGGDBII&9:Df
 DBII&9:Df
 ,,\:,,\: E!~~%-%@%@%B"#+#@#@ %)"#' !.e##+#@#@ #. r!   c                   ^ ^ UU 4S jnU$ )Nc                @   > T" U TR                   TR                  5      $ r:   )rz  r|  )r   codecr   s    r   
make_codec)client._makeBSERCodec.<locals>.make_codec  s    D$6$68H8HIIr!   r,   )r   r  r  s   `` r   _makeBSERCodecclient._makeBSERCodec  s    	J r!   c                z   US:X  a;  U R                   (       a  U R                  [        5      $ U R                  [        5      $ US:X  a[  [        R
                  (       a  [        S5      eU R                   (       a  U R                  [        5      $ U R                  [        5      $ US:X  a  [        $ [        SU-  5      e)Nr	   zbser-v1zrPython 3 does not support the BSER v1 encoding: specify "bser" or omit the sendEncoding and recvEncoding argumentsr^  zinvalid encoding %s)rp  r  rZ  rF  r   r   rX   rA  r#  r\  rI   )r   encs     r   rv  client._parseEncoding  s    &=$$**+>??&&'=>>I~~'  
 $$**+=>>&&y11F] 5 ;<<r!   c                B    U R                   (       a  [        X5      $ X!;   $ r:   )rp  rR  )r   r   rt  s      r   _haspropclient._hasprop  s      6((~r!   c                   [         R                  " S5      nU(       a  U$ U R                  SS/n [        [        R
                  [        R
                  S9n[         R                  S:X  a<  [        R                  " 5       nU=R                  [        R                  -  sl        XCS'   [        R                  " U40 UD6nUR                  5       u  pxUR                  5       n	U	(       a  [        SU	-  5      e[        R                   " U5      n
S	U
;   a  [        S
U
S	   -  5      eU
S   $ ! [         a  n[        SU-  5      eS nAff = f)NWATCHMAN_SOCKz--output-encoding=bserzget-sockname)r  stderrr   startupinfoz&"watchman" executable not in PATH (%s)zwatchman exited with code %drK  zget-sockname error: %ssockname)r   r   r  dictr  r  rt  STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWr  r   rI   communicatepollr	   r-  )r   pathrM   r7   r  pr   r  r  exitcoder   s              r   _resolvesocknameclient._resolvesockname  s$    yy)K||5~F	N!zD ww$ )446##z'F'FF# '2]#  --A
 668 > IJJF#f 86'? JKKj!!  	N H1 LMM	Ns   BD& &
E0D>>Ec                    U R                   (       a/  U R                  [        R                  " 5       :w  a  [	        S5      egU R
                  c  U R                  5       U l        0 nU R                  [        :X  a  U R                  US'   U R                  " U R
                  U R                  40 UD6U l        U R                  U R                  5      U l        U R                  U R                  5      U l         [        R                  " 5       U l        g)zestablish transport connectionz@do not re-use a connection after fork; open a new client insteadNr  )recvConnr  r   getpidr\   rl   r  r   r  r  r   tportrx  sendConnrw  )r   kwargss     r   r  client._connect  s     ==xx299;&"V  ==  113DM>>00 $F9 ^^DMM4<<J6J
 tzz2tzz299;r!   c                $    U R                  5         g r:   r|   r   s    r   __del__client.__del__	      

r!   c                &    U R                  5         U $ r:   )r  r   s    r   	__enter__client.__enter__  s    r!   c                $    U R                  5         g r:   r  )r   exc_type	exc_valueexc_tracebacks       r   __exit__client.__exit__  r  r!   c                    U R                   (       a0  U R                   R                  5         S U l         S U l        S U l        g g r:   )r  r|   r  r  r   s    r   r|   client.close  s3    ::JJDJ DM DM	 r!   c                   U R                  5         U R                  R                  5       nU R                  US5      (       a  [	        US   5      eU R                  US5      (       a  U R
                  R                  US   5        U R                  US5      (       a  US   nX R                  ;  a  / U R                  U'   U R                  U   R                  U5        [        R                  R                  [        R                  R                  US   5      5      nX0R                  ;  a  0 U R                  U'   X R                  U   ;  a  / U R                  U   U'   U R                  U   U   R                  U5        U$ )a  receive the next PDU from the watchman service

If the client has activated subscriptions or logs then
this PDU may be a unilateral PDU sent by the service to
inform the client of a log event or subscription change.

It may also simply be the response portion of a request
initiated by query.

There are clients in production that subscribe and call
this in a loop to retrieve all subscription responses,
so care should be taken when making changes here.
rK  r8   rj  root)r  r  r   r  rt   logsr   subsr   r  normpathnormcasesub_by_root)r   r   subr  s       r   r   client.receive  s<    	&&(==))vg//==''IIVE]+==00(C99$!#		#IIcN!!&) 77##BGG$4$4VF^$DED+++)+  &**400.0  &s+T"3'..v6r!   c                Z    SU;   a  US   (       a  gU R                    H
  nX!;   d  M
    g   g)N
unilateralTF)r  )r   r   ks      r   isUnilateralResponseclient.isUnilateralResponseA  s/    33|#4Ax ! r!   c                :    U R                   nU(       a  / U l         U$ )zRetrieve buffered log data

If remove is true the data will be removed from the buffer.
Otherwise it will be left in the buffer
)r  )r   remover   s      r   getLogclient.getLogJ  s     iiDI
r!   c                   Ub  [         R                  R                  [         R                  R                  U5      5      nX0R                  ;  a  gXR                  U   ;  a  gU R                  U   U   nU(       a,  U R                  U   U	 XR
                  ;   a  U R
                  U	 U$ XR
                  ;  a  gU R
                  U   nU(       a  U R
                  U	 U$ )a  Retrieve the data associated with a named subscription

If remove is True (the default), the subscription data is removed
from the buffer.  Otherwise the data is returned but left in
the buffer.

Returns None if there is no data associated with `name`

If root is not None, then only return the subscription
data that matches both root and name.  When used in this way,
remove processing impacts both the unscoped and scoped stores
for the subscription data.
N)r   r  r  r  r  r  )r   rt  r  r  r  s        r   getSubscriptionclient.getSubscriptionU  s     77##BGG$4$4T$:;D+++++D11""4(.C$$T*4099$		$Jyy iio		$
r!   c                   [        S5        U R                  5          U R                  R                  U5        U R	                  5       nU R                  U5      (       a(  U R	                  5       nU R                  U5      (       a  M(  U$ ! [         a'  n[        SUR                  UR                  U5      eSnAf[         a  nUR                  U5        e SnAff = f)a  Send a query to the watchman service and return the response

This call will block until the response is returned.
If any unilateral responses are sent by the service in between
the request-response they will be buffered up in the client object
and NOT returned via this method.
zcalling client.queryz,I/O error communicating with watchman daemonN)r8   r  r  r   r   r  r   r^   rb   strerrorrI   rP   )r   r7   r   eeexs        r   queryclient.queryx  s     	"#	MMt$,,.C++C00lln ++C00 J 	 +>	   	MM$	s*   A'B B 
C"B44CCCc                    U R                  SU=(       d    / U=(       d    / S.5      nU R                  US5      (       d*  [        R                  " X15        SU;   a  [	        US   5      eU$ )z!Perform a server capability checkrJ  )rI  rH  r   rK  )r  r  r   
synthesizert   )r   rI  rH  r   s       r   capabilityCheckclient.capabilityCheck  s`    jjHNBO
 }}S.11 ##C2#~"3w<00
r!   c                p    U R                   R                  U5        U R                  R                  U5        g r:   )r  r   r  r   s     r   r   client.setTimeout  s&      '  'r!   )r  r  r  rw  r  rx  r  rl   r   r  r   rp  rz  r|  )	Ng      ?NNNFFFN)T)TNrV   )$r"   r#   r$   r%   rr   rl   r   rx  rw  r  r  r  r  r  r  r  rp  r  r   r  rv  r  r  r  r  r  r  r|   r   r  r  r  r  r  r   r+   r,   r!   r   ri  ri  U  s    =HIIIHHDKD(JE
C  >/@=(
("T4!%N	!F@(r!   ri  r:   )_
__future__r   rq  r   r   r   r  r2   r   r   r   r   r   r	   r5  ImportErrorr
   rt  r=   ctypes.wintypesr'   r   r   r   r   c_void_prC   r   r@   rA   rB   r   r   r   r   INFINITEr   WPARAMr&   	Structurer   POINTERr(   LPDWORDwindllkernel32	_kernel32CreateFileAr   LPSTRLPVOIDr)   argtypesrestyper   BOOLr   r   r   r   FormatMessageAr?   rD   r   getattrr-   r   CreateEventAr   r   r4  
_debuggingr8   rG   rb  rI   rX   r\   r^   rj   rp   rt   rx   r   r   r   r   r	  r  r#  rA  rF  rZ  r\  ri  r,   r!   r   <module>r     s  : #   	    2 2
! 	MM
 77d?HLM%M!??2.44!+%/"$.!KLM#H " &&IV%%   nnX^^,G&&I&&JJ "J''K$OO,K"--K!!Hz"H  }}H##Iz"I !I))LL#>>L))L%^^,LL,,Mx~~&M %NNM##I#77z"	$  #+--#I/FM(OONN:&NNMM*
& )1%%;;&"
 %-NN!((K	K #//K %%J#??FNN:,FGJ!J 	 

,I 	 		= 	G} G
 M E= E* *Z) )$(C) (CV%:Pf
	 f
R9F) FR$" $"N

 

<"Y <"~	02D 	"* "*JT( T(I  !  !s   U+ +U;:U;