a
    xd!                     @   s   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eZdZG dd deZdS )    N)ABC)abstractmethod)system)DEVNULL)PIPE)sleep)request)URLError)WebDriverException)SubprocessStdAlias)utilsTc                   @   s  e Zd ZdZdeddfeeeej	ej
ejejf  ej	e ddddZeeddd	Zeeje dd
dZeedddZejeddddZddddZddddZedddZddddZddddZddddZddddZeddd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                 K   sj   || _ |pt | _ts,|tkr,ttjdn|| _	|p8d| _
|di | _| jdd| _|pbtj| _d S )Nwb popen_kwcreation_flagsr   )_pathr   Z	free_portr   _HAS_NATIVE_DEVNULLr   openosdevnullr   r   popr   r   environr   )selfr   r   r   r   r   kwargs r!   Y/var/www/html/Ranjet/env/lib/python3.9/site-packages/selenium/webdriver/common/service.py__init__5   s    	
zService.__init__)r   c                 C   s   dt d| j S )zGets the url of the Service.zhttp://	localhost)r   Zjoin_host_portr   r   r!   r!   r"   service_urlG   s    zService.service_urlc                 C   s   t ddS )z7A List of program arguments (excluding the executable).z2This method needs to be implemented in a sub classN)NotImplementedErrorr%   r!   r!   r"   command_line_argsL   s    zService.command_line_argsc                 C   s   | j S N)r   r%   r!   r!   r"   pathQ   s    zService.path)valuer   c                 C   s   t || _d S r)   )strr   )r   r+   r!   r!   r"   r*   U   s    c                 C   sP   |  | j d}|   |  r"qL|d7 }td |dkrtd| j qdS )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   countr!   r!   r"   startY   s    zService.startc                 C   s(   | 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   Zreturn_coder!   r!   r"   r0   m   s    
z$Service.assert_process_still_runningc                 C   s   t | jS )zfEstablishes a socket connection to determine if the service running
        on the port is accessible.)r   r1   r   r%   r!   r!   r"   r1   s   s    zService.is_connectablec                 C   sR   zt | j d W n ty*   Y dS 0 tdD ]}|  sD qNtd q4dS )zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   r-   )r   urlopenr&   r	   ranger1   r   )r   _r!   r!   r"   send_remote_shutdown_commandx   s    z$Service.send_remote_shutdown_commandc                 C   s|   | j tkrF| j tkrtsFzt| j dr0| j   W n tyD   Y n0 | jdurxz|   W n t	yn   Y n0 | 
  dS )zStops the service.closeN)r   r   r   r   hasattrr;   	Exceptionr4   r:   	TypeError_terminate_processr%   r!   r!   r"   stop   s    
zService.stopc              	   C   s   z| j j| j j| j j  }}}|||fD ]$}z|  W q( tyJ   Y q(0 q(| j   z| j d W n, tj	y   t
jddd | j   Y n0 W n  ty   t
jddd Y n0 dS )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)r4   stdinstdoutstderrr;   AttributeError	terminatewait
subprocessTimeoutErrorloggererrorkillOSError)r   rB   rC   rD   streamr!   r!   r"   r?      s$    
zService._terminate_processc                 C   s$   z|    W n ty   Y n0 d S r)   )r@   r=   r%   r!   r!   r"   __del__   s    zService.__del__)r*   r   c                 C   sZ  |g}| |   | jdt dk}zLtj|f| j|| j| jt	| j
d| j| _td| j d| jj  W n ty    Y n ty } zf|jtjkrtdtj| j d| j |jtjkrtdtj| j d| j  W Y d	}~nXd	}~0  tyT } z6td
tj| j d| j dt| W Y d	}~n
d	}~0 0 d	S )zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdsWindows)r   rP   rC   rD   rB   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   rH   Popenr   r   r   r   r4   rJ   debugr   pidr>   rM   errnoENOENTr
   r   r*   basenamer   EACCESr=   r,   )r   r*   cmdZclose_file_descriptorserrer!   r!   r"   r/      sB    
 $zService._start_process)__name__
__module____qualname____doc__r   r,   intr   typingOptionalMappingAnyr#   propertyr&   r   Listr(   r*   setterr3   r0   boolr1   r:   r@   r?   rO   r/   r!   r!   r!   r"   r   )   s:   r   )rY   loggingr   rH   re   abcr   r   platformr   r   r   timer   urllibr   urllib.errorr	   Zselenium.common.exceptionsr
   Zselenium.typesr   Zselenium.webdriver.commonr   	getLoggerr`   rJ   r   r   r!   r!   r!   r"   <module>   s$   
