
    8h_                          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	 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 d d
lmZ d dlmZ d dlmZ  ej2                  e      Z G d de      Zy)    N)ABC)abstractmethod)IOBase)system)PIPE)sleep)request)URLError)WebDriverException)SubprocessStdAlias)utilsc                   l   e Zd ZdZ	 	 	 	 ddedededej                  ej                  ej                  ej                  f      ddf
dZedefd	       Zedej                  e   fd
       Zedefd       Zej$                  deddfd       ZddZddZdefdZddZddZddZddZdeddfdZy)Servicea(  The abstract base class for all service objects.  Services typically
    launch a child program in a new process as an interim process to
    communicate with a browser.

    :param executable: install path of the executable.
    :param port: Port for the service to run on, defaults to 0 where the operating system will decide.
    :param log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
    :param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
    N
executableport
log_outputenvreturnc                    t        |t              rt        |dd      | _        nU|t        j
                  u rd | _        n;||t        j                  u r t        t        j                  d      | _        n|| _        || _	        |xs t        j                         | _        |j                  di       | _        | j                  j                  dd      | _        |xs t        j                   | _        y )Nza+zutf-8)encodingwbpopen_kwcreation_flagsr   )
isinstancestropenr   
subprocessSTDOUTDEVNULLosdevnull_pathr   	free_portr   popr   r   environr   )selfr   r   r   r   kwargss         x/home/stella.sw7ft.com/public_html/site_rebuilder/venv/lib/python3.12/site-packages/selenium/webdriver/common/service.py__init__zService.__init__1   s     j#&":tgFDO:,,,"DO:1C1C#C"2::t4DO(DO
-EOO-	

:r2"mm//0@!D$"**    c                 H    dt        j                  d| j                         S )zGets the url of the Service.zhttp://	localhost)r   join_host_portr   r&   s    r(   service_urlzService.service_urlI   s#     --k499EFGGr*   c                     t        d      )z7A List of program arguments (excluding the executable).z2This method needs to be implemented in a sub class)NotImplementedErrorr.   s    r(   command_line_argszService.command_line_argsN   s     ""VWWr*   c                     | j                   S N)r"   r.   s    r(   pathzService.pathS   s    zzr*   valuec                 $    t        |      | _        y r4   )r   r"   )r&   r6   s     r(   r5   zService.pathW   s    Z
r*   c                     | j                  | j                         d}	 | j                          | j                         ry|dz  }t	        d       |dk(  rt        d| j                         O)zStarts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        r      g      ?<   zCan not connect to the Service N)_start_processr"   assert_process_still_runningis_connectabler   r   )r&   counts     r(   startzService.start[   sl     	DJJ'--/""$QJE#J{(+J4::,)WXX r*   c                 r    | j                   j                         }|rt        d| j                   d|       y)z1Check if the underlying process is still running.zService z' unexpectedly exited. Status code was: N)processpollr   r"   )r&   return_codes     r(   r<   z$Service.assert_process_still_runningo   s;    ll'')$x

|;bcnbo%pqq r*   c                 @    t        j                  | j                        S )zfEstablishes a socket connection to determine if the service running
        on the port is accessible.)r   r=   r   r.   s    r(   r=   zService.is_connectableu   s     ##DII..r*   c                     	 t        j                  | j                   d       t	        d      D ]  }| j                         s yt        d       ! y# t        $ r Y yw xY w)zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   r9   )r	   urlopenr/   r
   ranger=   r   )r&   _s     r(   send_remote_shutdown_commandz$Service.send_remote_shutdown_commandz   s`    	OOt//0	:; r 	A&&(!H	  		s   "A 	AAc                    | j                   t        k7  rnt        | j                   t              r| j                   j	                          n9t        | j                   t
              rt        j                  | j                          | j                  "	 | j                          | j                          yy# t        $ r Y w xY w)zStops the service.N)r   r   r   r   closeintr    rA   rJ   	TypeError_terminate_processr.   s    r(   stopzService.stop   s     ??d"$//62%%'DOOS1)<<#113 ##% $  s   B1 1	B=<B=c                 *   	 | j                   j                  | j                   j                  | j                   j                  }}}|||fD ]  }	 |j	                           | j                   j                          	 | j                   j                  d       y# t
        $ r Y Xw xY w# t        j                  $ r4 t        j                  dd       | j                   j                          Y yw xY w# t        $ r t        j                  dd       Y yw xY w)a#  Terminate the child process.

        On POSIX this attempts a graceful SIGTERM followed by a SIGKILL,
        on a Windows OS kill is an alias to terminate.  Terminating does
        not raise itself if something has gone wrong but (currently)
        silently ignores errors here.
        r:   zTService process refused to terminate gracefully with SIGTERM, escalating to SIGKILL.T)exc_infoz"Error terminating service process.N)rA   stdinstdoutstderrrL   AttributeError	terminatewaitr   TimeoutExpiredloggererrorkillOSError)r&   rS   rT   rU   streams        r(   rO   zService._terminate_process   s    	N$(LL$6$68K8KT\\M`M`66E/ LLN
 LL""$$!!"%	 & 
 ,, $j!   !!#$  	NLL=LM	NsU   A
C/ BC/ :B% 	B"C/ !B""C/ %AC,)C/ +C,,C/ / DDc                 D    	 | j                          y # t        $ r Y y w xY wr4   )rP   	Exceptionr.   s    r(   __del__zService.__del__   s"    	IIK 		s    	r5   c                 N   |g}|j                  | j                                | j                  j                  dt	               dk7        }	 d}t	               dk(  rOt        j                         }t
        j                  t
        j                  z  |_	        t
        j                  |_        t        j                  |f| j                  || j                  | j                  t        | j                   |d| j                  | _        t$        j'                  d| j(                  | j"                  j*                         y# t,        $ r  t.        $ rZ}|j0                  t0        j2                  k(  r7t5        dt6        j8                  j;                  | j(                         d      | d}~ww xY w)zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdsWindowsN)r   rc   rT   rU   rS   creationflagsstartupinfoz8Started executable: `%s` in a child process with pid: %s'z(' executable may have wrong permissions.)extendr2   r   r$   r   r   STARTUPINFOCREATE_NEW_CONSOLESTARTF_USESHOWWINDOWdwFlagsSW_HIDEwShowWindowPopenr   r   r   r   rA   rZ   debugr"   pidrN   r]   errnoEACCESr   r    r5   basename)r&   r5   cmdclose_file_descriptors
start_infoerrs         r(   r;   zService._start_process   s^   
 f

4))+,!%!2!2;I@U!V	Jx9$'335
%/%B%BZEdEd%d
")3););
&%++
HH0"11&
 --
DL LLSUYU_U_aeamamaqaqr 	 	yyELL((((455]^ 	s   C,D8 8F$
AFF$)Nr   NN)r   N)__name__
__module____qualname____doc__r   rM   r   typingOptionalMappingAnyr)   propertyr/   r   Listr2   r5   setterr?   r<   boolr=   rJ   rP   rO   ra   r;    r*   r(   r   r   &   s/    )-GK%% % '	%
 __V^^FJJ

,BCD% 
%0 HS H H X6;;s#3 X X c   
[[ #  $    Y(r/ /
& N6	"3 "4 "r*   r   )rr   loggingr    r   r}   abcr   r   ior   platformr   r   timer   urllibr	   urllib.errorr
   selenium.common.exceptionsr   selenium.typesr   selenium.webdriver.commonr   	getLoggerry   rZ   r   r   r*   r(   <module>r      sW   "   	          ! 9 - +			8	$yc yr*   