
    ^j0              
          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mZm	Z	m
Z
mZ d dlmZ dZdZdZdZdZd	Zd
ZdZ G d de      Z G d de      Z G d d      Zd,dedede
eg df      defdZdede
e   fdZdefdZdefdZ de
e   de
e   defdZ!de	e   dee   d edefd!Z"de	e   de#fd"Z$de	e   defd#Z%de	e   defd$Z&de	e   defd%Z'de	e   defd&Z(de	e   defd'Z)e*d(k(  r ed      J  ed      J  e       Z+e+jY                  d)dd* ee      g+       e+jZ                  sJ e+j\                  sJ e+j_                          e+jZ                  rJ e+j\                  rJ yy)-    N)AnyCallableListOptionalSet)urllib3z	127.0.0.1is  i`  i  zappium/build/lib/main.jsz/status/i  c                       e Zd Zy)AppiumServiceErrorN__name__
__module____qualname__     S/home/ubuntu/.local/lib/python3.12/site-packages/appium/webdriver/appium_service.pyr   r   "       r   r   c                       e Zd Zy)AppiumStartupErrorNr   r   r   r   r   r   &   r   r   r   c                   ~    e Zd ZddZdedej                  fdZddede	fdZ
ede	fd       Zede	fd	       Zdd
Zy)AppiumServicereturnNc                      d | _         d | _        y N)_process_cmdselfs    r   __init__zAppiumService.__init__+   s    ,0)-	r   kwargsc                    | j                          d|v r|d   nd}|j                  d      xs
 t               }|j                  d      xs
 t               }|j                  d      xs t	        ||      }t
        j                  dk(  rt        j                  nt        j                  }d|v r|d   n|}d|v r|d   n|}d	|v rt        |d	         nt        }	||g}
d
|v r|
j                  |d
          |
| _        t        j                  |
|||      | _        d}d| d| d| d}|	dkD  r1t!        |
      }	 t#        ||	dz  | j$                        s	d| d|	 d}n| j(                  s|}|{|t        j                  k(  rM| j                  j*                  7| j                  j*                  j-                         }|r|dt/        |       z  }| j                          t1        |      | j                  S # t&        $ r |}Y w xY w)a~  Starts Appium service with given arguments.

        If you use the service to start Appium 1.x
        then consider providing ['--base-path', '/wd/hub'] arguments. By default,
        the service assumes Appium server listens on '/' path, which is the default path
        for Appium 2.

        The service will be forcefully restarted if it is already running.

        Args:
            env (dict): Environment variables mapping. The default system environment,
                which is inherited from the parent process, is assigned by default.
            node (str): The full path to the main NodeJS executable. The service will try
                to retrieve it automatically if not provided.
            npm (str): The full path to the Node Package Manager (npm) script. The service will try
                to retrieve it automatically if not provided.
            stdout (int): Check the documentation for subprocess.Popen for more details.
                The default value is subprocess.DEVNULL on Windows and subprocess.PIPE on other platforms.
            stderr (int): Check the documentation for subprocess.Popen for more details.
                The default value is subprocess.DEVNULL on Windows and subprocess.PIPE on other platforms.
            timeout_ms (int): The maximum time to wait until Appium process starts listening
                for HTTP connections. If set to zero or a negative number then no wait will be applied.
                60000 ms by default.
            main_script (str): The full path to the main Appium executable
                (usually located at build/lib/main.js). If not set
                then the service tries to detect the path automatically.
            args (str): List of Appium arguments (all must be strings). Check
                https://appium.io/docs/en/writing-running-appium/server-args/ for more details
                about possible arguments and their values.

        Returns: You can use Popen.communicate interface or stderr/stdout properties
            of the instance (stdout/stderr must not be set to None in such case) in order to retrieve the actual process
            output.

        envNnodenpmmain_scriptwin32stdoutstderr
timeout_msargs)r*   r'   r(   r"   z`Appium server process is unable to start. Make sure proper values have been provided to 'node' (z
), 'npm' (z) and 'main_script' (z) method arguments.r   i  timeoutcustom_validatorz2Appium server has started but is not listening on z within znms timeout. Make sure proper values have been provided to --base-path, --address and --port process arguments.z
Original error: )stopgetget_nodeget_npmget_main_scriptsysplatformspDEVNULLPIPEintSTARTUP_TIMEOUT_MSextendr   Popenr   _make_server_urlis_service_listening_assert_is_runningr   
is_runningr(   readstrr   )r   r    r"   r#   r$   r%   default_stdr'   r(   r)   r*   	error_msgstartup_failure_msg
server_url
err_outputs                  r   startzAppiumService.start/   s   H 			$fUmDJJv&4(*::e$1	!::m4Rc8R$'LLG$;bjj%-%7![%-%7![2>&2HS-.N`
-VKKv'	d6&cR#'	##'&
3%7L[M Z ! 	
 >)$/J0+&-%)%<%< MZL Y"", .RS  +I  T]]%9%9%E!]]11668
#5c*o5F!GGIIIK$Y//}} & 0/	0s   6$G2 2H ?H r,   c           	         d}| j                   rG| j                  sJ d}| j                  j                          	 | j                  j                  |       d| _        d| _        |S # t        j
                  $ rg t        j                  dk(  r7t	        j                  dddt        | j                  j                        g       n| j                  j                          Y w xY w)	a[  Stops Appium service if it is running.

        The call will be ignored if the service is not running
        or has been already stopped.

        Args:
            timeout: The maximum time in float seconds to wait for the server process to terminate

        Returns:
            `True` if the service was running before being stopped
        FTr,   r&   taskkillz/fz/pidN)r?   r   	terminatecommunicater5   SubprocessErrorr3   r4   callrA   pidkillr   )r   r,   was_runnings      r   r.   zAppiumService.stop   s     ??== =KMM##%)))'): 	 %% )<<7*GGZvs4==;L;L7MNOMM&&(	)s   A& &A7C C c                 z    | j                   duxr, | j                  duxr | j                   j                         du S )z\Check if the service is running.

        :return: `True` if the service is running
        N)r   r   pollr   s    r   r?   zAppiumService.is_running   s9     }}D(cTYYd-Bct}}GYGYG[_cGccr   c                     | j                   sy| j                  sJ 	 t        t        | j                        t        | j
                        S # t        $ r Y yw xY w)a  Check if the service is listening on the given/default host/port.

        The fact, that the service is running, does not always mean it is listening.
        The default host/port/base path values can be customized by providing
        --address/--port/--base-path command line arguments while starting the service.

        Returns:
            `True` if the service is running and listening on the given/default host/port
        Fr+   )r?   r   r=   r<   STATE_CHECK_INTERVAL_MSr>   r   r   s    r   is_listeningzAppiumService.is_listening   sX     yyy	' +/!%!8!8 
 " 		s   .A 	AAc                 0    | j                   s
t               y r   )r?   r   r   s    r   r>   z AppiumService._assert_is_running   s    $&& r   )r   N)g      @)r   r   r   r   r   r5   r;   rG   floatboolr.   propertyr?   rV   r>   r   r   r   r   r   *   ss    .Sc Sbhh SjE D 8 dD d d d  .'r   r   urlr,   r-   r   c                    t        j                         }t        j                  d      }t        j                         ||z   k  rh| |        	 |j	                  d|       }|j
                  t        k  ry	 t        j                  t        dz         t        j                         ||z   k  rhy# t        j                  j                  $ r Y Ww xY w)a:  
    Check if the service is running

    Args:
        url: Full server url
        timeout: Timeout in float seconds
        custom_validator: Custom callable method to be executed upon each validation loop before the timeout happens

    Returns:
        True if Appium server is running before the timeout
    g      ?rI   HEADTg     @@F)timeperf_counterr   PoolManagerrequeststatusHTTP_STATUS_ERROR
exceptions	HTTPErrorsleeprU   )r[   r,   r-   time_started_secconnresps         r   r=   r=      s     ((*s+D



 07 :
:'	<<,D{{.. / 	

*V34 


 07 :
:  !!++ 		s   %B. .CC
executablec                    t         j                  d   }|j                  t         j                        }t         j                  j                  |       \  }}t        j                  dk(  r|s| dz   } t         j                  j                  |       r| S |D ]E  }t         j                  j                  ||       }t         j                  j                  |      sC|c S  y )NPATHr&   z.exe)
osenvironsplitpathseppathsplitextr3   r4   isfilejoin)rj   rq   paths_extp	full_paths          r   find_executablerz      s    ::fDJJrzz"EWWj)FAs
||ws&(
	ww~~j! GGLLJ/	77>>)$
 r   c                  6    t        d      } | t        d      | S )Nr#   zUNodeJS main executable cannot be found. Make sure it is installed and present in PATH)rz   r   results    r   r0   r0      s"    V$F~ !xyyMr   c                  `    t        t        j                  dk(  rdnd      } | t        d      | S )Nr&   znpm.cmdr$   z^Node Package Manager executable cannot be found. Make sure it is installed and present in PATH)rz   r3   r4   r   r|   s    r   r1   r1      s5    #,,'*AYuMF~ l
 	
 Mr   r#   r$   c                    d }|xs
 t               }ddgdgfD ]  }	 t        j                  |g|z         j                         j	                  d      }t        j                  j                  |gt        j                  d       }t
        j                  j                  |      r|} n |N| xs
 t               }	 t        j                  |ddt         dg      j	                  d      j                         }|S |S # t        j                  $ r Y w xY w# t        j                  $ r}t        |j                        |d }~ww xY w)Nrootz-gzutf-8r	   z-ezconsole.log(require.resolve("z")))r1   r5   check_outputstripdecoderm   rq   rt   MAIN_SCRIPT_PATHro   existsCalledProcessErrorr0   r   output)	r#   r$   r}   npm_pathr*   modules_rootry   	node_pathes	            r   r2   r2     s5    FgiH$&* 	??H:+<=CCELLWUL\P4D4J4J34OPIww~~i(" )	 ~&HJ		6D4QRbQccf2g hi  M6M $$ 		 $$ 	6$QXX.A5	6s*   B
C<;=D <DDE(D>>Er*   	arg_namesdefaultc                 l    t        |       D ]%  \  }}||v s|t        |       dz
  k  s| |dz      c S  |S )N   )	enumeratelen)r*   r   r   idxargs        r   _parse_arg_valuer     sE    dO !S)c$i!m 3a= ! Nr   c           	      L    t        t        | ddht        t                          S )Nz--port-p)r8   r   rA   DEFAULT_PORTr*   s    r   _parse_portr   #  s!    x&6L8IJKKr   c                 (    t        | ddht              S )Nz--base-pathz-pa)r   DEFAULT_BASE_PATHr   s    r   _parse_base_pathr   '  s    D=%"8:KLLr   c                 (    t        | ddht              S )N	--addressz-a)r   DEFAULT_HOSTr   s    r   _parse_hostr   +  s    D;"5|DDr   c                 B    t        | dhd      rt        | dhd      rdS dS )Nz--ssl-cert-path z--ssl-key-pathhttpshttp)r   r   s    r   _parse_protocolr   /  s:     D#4"5r:?OPTWgVhjl?m 	 r   c                 t    t        |       }|t        k(  rt        S t        j                  dd|       t         S )Nz/+$r   )r   r   
STATUS_URLresub)r*   	base_paths     r   _make_status_pathr   7  s9     &I"&77:kvrS\@]?^_i^j=kkr   c                 b    t        |        dt        |        dt        |        t        |        S )Nz://:)r   r   r   r   r   s    r   r<   r<   <  s7    d#$CD(9':!K<M;NO`aeOfNghhr   __main__r   r   r   )   N)0rm   r   
subprocessr5   r3   r^   typingr   r   r   r   r   +selenium.webdriver.remote.remote_connectionr   r   r   r9   rU   r   r   r   rc   RuntimeErrorr   r   r   rA   rX   rY   r=   rz   r0   r1   r2   r   r8   r   r   r   r   r   r<   r   servicerG   r?   rV   r.   r   r   r   <module>r      sT   
 	  
  5 5 ?  - 
  	 		 	X' X'vc E RZ[]_c[cRdIe qu 8  $#  (3- hsm  249 S C C Ld3i LC LM49 M MEd3i EC E$s)  lDI l# l
i49 i i z6"...5!---oGMM[$L8IJMKLLN!!!!##### r   