
    Eg!                         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j        e          ZdZ G d de          ZdS )    N)ABC)abstractmethod)system)DEVNULL)PIPE)sleep)request)URLError)WebDriverException)SubprocessStdAlias)utilsTc                   v   e Zd ZdZdeddfdedededej	        ej
        ej        ej        f                  dej	        e         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dS )Serviceac  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_file: (Optional) file descriptor (pos int) or file object with a valid file descriptor.
        subprocess.PIPE & subprocess.DEVNULL are also valid values.
    :param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
    r   N
executableportlog_fileenvstart_error_messagereturnc                 \   || _         |pt          j                    | _        t          s%|t
          k    rt          t          j        d          n|| _	        |pd| _
        |                    di           | _        | j                            dd          | _        |pt          j        | _        d S )Nwb popen_kwcreation_flagsr   )_pathr   	free_portr   _HAS_NATIVE_DEVNULLr   openosdevnullr   r   popr   r   environr   )selfr   r   r   r   r   kwargss          V/var/www/sysmax/venv/lib/python3.11/site-packages/selenium/webdriver/common/service.py__init__zService.__init__5   s      
-EO--	6IohZaNaNaRZ...go#6#<" 

:r22"m//0@!DD$"*    c                 <    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_urlG   s"     H-k49EEG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_argsL   s     ""VWWWr'   c                     | j         S N)r   r+   s    r%   pathzService.pathQ   s
    zr'   valuec                 .    t          |          | _        d S r1   )strr   )r#   r3   s     r%   r2   zService.pathU   s    ZZ


r'   c                     |                      | j                   d}	 |                                  |                                 rdS |dz  }t	          d           |dk    rt          d| j                   \)zStarts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        r   T   g      ?<   zCan not connect to the Service N)_start_processr   assert_process_still_runningis_connectabler   r   )r#   counts     r%   startzService.startY   s     	DJ'''	Y--///""$$ QJE#JJJ{{()W4:)W)WXXX	Yr'   c                 p    | j                                         }|rt          d| j         d|           dS )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_runningm   sL    l'')) 	r$%p
%p%pcn%p%pqqq	r 	rr'   c                 4    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_connectables   s     #DI...r'   c                     	 t          j        | j         d           n# t          $ r Y dS w xY wt	          d          D ](}|                                 s dS t          d           )dS )zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   r7   )r	   urlopenr,   r
   ranger;   r   )r#   _s     r%   send_remote_shutdown_commandz$Service.send_remote_shutdown_commandx   s    	Ot/:::;;;; 	 	 	FF	 r 	 	A&&(( !HHHH	 	s    
--c                 Z   | j         t          k    rW| j         t          k    rt          s@	 t	          | j         d          r| j                                          n# t          $ r Y nw xY w| j        <	 |                                  n# t          $ r Y nw xY w| 
                                 dS dS )zStops the service.closeN)r   r   r   r   hasattrrJ   	Exceptionr?   rH   	TypeError_terminate_processr+   s    r%   stopzService.stop   s    =D  $-7*B*BGZ*B4='22 *M'')))    <#113333   ##%%%%% $#s#   .A 
A%$A%0B 
BBc                    	 | j         j        | j         j        | j         j        }}}|||fD ]'}	 |                                 # t
          $ r Y $w xY w| j                                          	 | j                             d           dS # t          j	        $ r9 t                              dd           | j                                          Y dS w xY w# t          $ r  t                              dd           Y dS 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.
        r8   zTService process refused to terminate gracefully with SIGTERM, escalating to SIGKILL.T)exc_infoz"Error terminating service process.N)r?   stdinstdoutstderrrJ   AttributeError	terminatewait
subprocessTimeoutErrorloggererrorkillOSError)r#   rR   rS   rT   streams        r%   rN   zService._terminate_process   sB   	N$(L$68KT\M`66E/  LLNNNN%   DL""$$$$!!"%%%%%* $ $ $j!     !!######$  	N 	N 	NLL=LMMMMMM	NsS   +C AC 
AC AC -B	 	ACC CC &C>=C>c                 R    	 |                                   d S # t          $ r Y d S w xY wr1   )rO   rL   r+   s    r%   __del__zService.__del__   s:    	IIKKKKK 	 	 	DD	s    
&&r2   c                    |g}|                     |                                            | j                            dt	                      dk              }	 t          j        |f| j        || j        | j        t          | j
        d| j        | _        t                              d| j         d| j        j                    d	S # t           $ r  t"          $ r}|j        t$          j        k    r<t)          dt*          j                            | j                   d| j                   |j        t$          j        k    r<t)          dt*          j                            | j                   d| j                    d	}~wt4          $ rQ}t)          d
t*          j                            | j                   d| j         dt7          |                     d	}~ww xY w)zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdsWindows)r   rb   rS   rT   rR   creationflagszStarted executable: `z` in a child process with pid: 'z"' executable needs to be in PATH. z)' executable may have wrong permissions. NzThe executable z$ needs to be available in the path. 
)extendr/   r   r!   r   rX   Popenr   r   r   r   r?   rZ   debugr   pidrM   r]   errnoENOENTr   r   r2   basenamer   EACCESrL   r5   )r#   r2   cmdclose_file_descriptorserres         r%   r9   zService._start_process   s"   
 f

4))++,,,!%!2!2;I@U!V!V	%+	H0}}"1	 	 -	 	DL LLnnn\`\h\lnnooooo 	 	 	 		 		 		yEL(((r((44rrX\Xprr   yEL(((y((44yy_c_wyy    	 	 	$ I"'"2"24:">">  I  Idhd|  I  I  AD  EF  AG  AG  I  I  	s'   A+C GB#E;;GAGG)r   N)__name__
__module____qualname____doc__r   r5   intr   typingOptionalMappingAnyr&   propertyr,   r   Listr/   r2   setterr=   r:   boolr;   rH   rO   rN   r`   r9    r'   r%   r   r   )   s       	 	 '.GK48% %% % %	%
 _V^FJ
,BCD% $_S1% 
% % % %$ HS H H H XH X6;s#3 X X X ^X c    X 
[ #  $       [ Y Y Y Y(r r r r/ / / / /
   & & & &"N N N N6	 	 	 	#3 #4 # # # # # #r'   r   )rk   loggingr   rX   rx   abcr   r   platformr   r   r   timer   urllibr	   urllib.errorr
   selenium.common.exceptionsr   selenium.typesr   selenium.webdriver.commonr   	getLoggerrs   rZ   r   r   r   r'   r%   <module>r      sU  "   				                                                ! ! ! ! ! ! 9 9 9 9 9 9 - - - - - - + + + + + +		8	$	$  v v v v vc v v v v vr'   