
     tho0                     l   d Z ddlZddlZddlZddlZddlZddlZddlZddlZ	ddl
Z	ddlmc mZ ddlZddlmZ ddlZddlmZ dZddlmZmZ d Zd Zdd	ZddZd Zd Zd Z d Z!d Z"ddZ#d Z$d Z%ddee         de&fdZ'e(dk    r* e) e                         e) e'd
                     dS dS )z3
Helper functions for filename and URL generation.
    N)BytesIOversionz'Yeongbin Jo <iam.yeongbin.jo@gmail.com>)AnyStrOptionalc                  H    t           j                            d          rdS dS )zc
    Returns the filename of the binary for the current platform.
    :return: Binary filename
    winzchromedriver.exechromedriversysplatform
startswith     t/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/chromedriver_autoinstaller/utils.pyget_chromedriver_filenamer      s'    
 |u%% "!!>r   c                  H    t           j                            d          rdS dS )zz
    Returns the environment variable separator for the current platform.
    :return: Environment variable separator
    r	   ;:r   r   r   r   get_variable_separatorr   $   s&    
 |u%% s3r   c                    t           j                            d          rt           j        dk    rd}d}nt           j        dk    rd}t	          j                    dk    rh| %t          |           dk    rt          d           d	}n| <t          j	        |           t          j	        d
          k    rt          d           d}nd}nt	          j                    dk    r*| %t          |           dk    rt          d           d}nEd}nBt          d          t           j                            d          rd}d}nt          d          ||fS )Nlinuxl        64darwinmacarm115z9CHROME >= 115, using mac-arm64 as architecture identifierz-arm64z106.0.5249.21zBCHROME <= 106.0.5249.21, using mac64_m1 as architecture identifier64_m1_arm64i386z7CHROME >= 115, using mac-x64 as architecture identifierz-x64z/Could not determine Mac processor architecture.r	   32z@Could not determine chromedriver download URL for this platform.)r   r   r   maxsizepf	processorget_major_versionprintr   parseRuntimeError)chrome_versionr   architectures      r   get_platform_architecturer+   .   sh   
|w'' $
CK%,?,?		!	! <>>U"").?.O.OSX.X.XQRRR'+n0M0MQXQ^_nQoQo0o0oZ[[[&'\^^v%%).?.O.OSX.X.XOPPP%#PQQQ		 	 	'	' 
N
 
 	
 \!!r   Fc                     t          |           \  }}t          |           dk    r |D ]}|d         ||z   k    r
|d         c S d	S d}|rd|z   nd|z   }|| z   dz   |z   |z   dz   S )
av  
    Generates the download URL for current platform , architecture and the given version.
    Supports Linux, MacOS and Windows.

    :param chromedriver_version: ChromeDriver version string
    :param no_ssl:               Whether to use the encryption protocol when downloading the chrome driver
    :return:                     String. Download URL for chromedriver
    r   r   urlz$chromedriver.storage.googleapis.com/http://https://z/chromedriver_z.zipN)r+   r%   )chromedriver_versiondownload_optionsno_sslr   r*   optionbase_urls          r   get_chromedriver_urlr5   W   s     77KLLHl-..%77& 	- 	-Fj!X%<<<%e},,, =	- 	- :+1L9x''zH7L..1AAHL|[^dddr   c                    dt           j        vrdS t           j        d                             t                                D ]}t           j                            t           j                            ||                     }t           j                            |          r#t          j        |t           j	                  r|c S dS )z
    Searches for a binary named `filename` in the current PATH. If an executable is found, its absolute path is returned
    else None.
    :param filename: Filename of the binary
    :return: Absolute path or None
    PATHN)
osenvironsplitr   pathabspathjoinisfileaccessX_OK)filename	directorybinarys      r   find_binary_in_pathrD   k   s     RZtZ'--.D.F.FGG  	i!B!BCC7>>&!! 	bi&@&@ 	MMM4r   c                     	 t          j        | dg          }t          j        d |j        d                    d         }||k    rdS n# t
          $ r Y dS w xY wdS )Nz-vz.*?([\d.]+).*?utf-8   TF)
subprocesscheck_outputrematchdecode	Exception)rC   required_versionr   s      r   check_versionrO   {   s{    )64.99(,ngnW.E.EFFqI&&&4 '   uu5s   AA
 

AAc                  V   t                      \  } }| dk    rt                      }t          j        |dgt          j                  5 }|j                                                            d                              dd                              dd          	                                }ddd           n# 1 swxY w Y   na| d	k    rvt          j        d
dgt          j                  }|
                                d                             d                              dd          	                                }n| dk    rt          j                            d          pt          j                            d           d}t          j                            d           d}t          j                            |          r|n"t          j                            |          r|nd}|rd t          j        |          D             nd}|rt#          |          nd}ndS |S )z<
    :return: the version of chrome installed on client
    r   z	--version)stdoutrF   Chromium zGoogle ChromeNr   z</Applications/Google Chrome.app/Contents/MacOS/Google Chromer   zUTF-8r	   PROGRAMW6432PROGRAMFILESz\Google\Chrome\ApplicationzPROGRAMFILES(X86)c                 x    g | ]7}|                                 t          j        d |j                  0|j        8S )z	^[0-9.]+$)is_dirrJ   rK   name).0fs     r   
<listcomp>z&get_chrome_version.<locals>.<listcomp>   s<    aaa1AHHJJa28KYZY_C`C`aaaar   )r+   get_linux_executable_pathrH   PopenPIPErQ   readrL   replacestripcommunicater8   r9   getr;   existsscandirmax)	r   _r;   procr   processrU   PROGRAMFILESX86dirss	            r   get_chrome_versionrl      s2    ,--KHa7(**t[1*/JJJ 	d  ""R(("-- 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
U		"N ?
 
 
 !!!$VG__W_b))UWW	 	 
U		*..88ZBJNN><Z<ZyyyZ^^,?@@___!w~~l;;}||TVT[TbTbcrTsTsA}y}eisaa
4 0 0aaaaos#-#d)))Ns   A'B;;B?B?c                  ^    dD ]} t          j        |           }||c S t          d          )z
    Look through a list of candidates for Google Chrome executables that might
    exist, and return the full path to first one that does. Raise a ValueError
    if none do.

    :return: the full path to a Chrome executable on the system
    )zgoogle-chromezgoogle-chrome-stablezgoogle-chrome-betazgoogle-chrome-devzchromium-browserchromiumNz"No chrome executable found on PATH)shutilwhich
ValueError)
executabler;   s     r   r\   r\      sG     
 

 |J''KKK 
9
:
::r   c                 .     | j         d          d         S )zX
    :param version: the version of chrome
    :return: the major version of chrome
    .r   )r:   r   s    r   r%   r%      s    
 7=a  r   c                    t          |           dk    rt          |           }d}|rd| nd| }t          j        t          j                            |                    }|d                             |          }|r*	 |d         d         }|d         |fS # t          $ r Y d	S w xY wnd
}|rd|z   nd|z   }t          j                            |                                          }t          j
        |          }|                    d          D ]T}	|	j                            t          |           dz             dk    r$|	j                            d          d         dfc S Ud	S )a  
    Try to find a specific version of ChromeDriver to match a version of cCrome

    :param chrome_version: the version of Chrome to match against
    :param no_ssl:         get version list using unsecured HTTP get
    :return:               String. The version of chromedriver that matches the Chrome version
                           None.   if no matching version of chromedriver was discovered
    r   z_googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.jsonr.   r/   
milestones	downloadsr
   r   )NNz#chromedriver.storage.googleapis.comz+{http://doc.s3.amazonaws.com/2006-03-01}Keyrt   r   /N)r%   jsonloadurllibrequesturlopenrc   KeyErrorr_   elemTree
fromstringitertextfindr:   )
r)   r2   browser_major_versionversion_urllatest_version_per_milestone	milestoner1   docrootks
             r    get_matched_chromedriver_versionr      s    ((E11 1. A Aw17U----=U=U=U'+y1G1G1T1T'U'U$ 1>BBCXYY	 	""#,[#9.#I  +-=== " " "!zz"		" <17Ui+--Z+=Un$$[116688"3''HII 	2 	2Av{{,^<<sBCCqHHv||C((+T1111 I :s    B 
B&%B&c                      t           j                            t           j                            t                              S )z2
    :return: path of the chromedriver binary
    )r8   r;   r<   dirname__file__r   r   r   get_chromedriver_pathr      s&     7??27??844555r   c                  <    t          t                                 dS )z4
    Print the path of the chromedriver binary.
    N)r&   r   r   r   r   print_chromedriver_pathr      s     


!
!"""""r   r;   r2   c                    t                      }|st          j        d           dS t          ||          \  }}t	          |          }|r|dk    r|st          j        d           dS | rot          j                            |           st          d|            t          j        
                    t          j                            |           |          }n_t          j        
                    t          j                            t          j                            t                              |          }t                      }t          j        
                    ||          }t          j                            |          rt!          ||          st          j        d| d           t          j                            |          st          j        |           t'          |||          }		 t(          j                            |	          }
|
                                d	k    rt(          j                            d
          n*# t(          j        j        $ r t5          d|	           w xY wt7          |
                                          }t;          j        |          5 }|                                D ]I}t          j                             |j!                  |k    r||_!        |"                    ||            nJddd           n# 1 swxY w Y   nt          j        d           t          j#        |t          j$                  st          j%        |d           |S )a  
    Downloads, unzips and installs chromedriver.
    If a chromedriver binary is found in PATH it will be copied, otherwise downloaded.

    :param str path: Path of the directory where to save the downloaded chromedriver to.
    :param bool no_ssl: Determines whether or not to use the encryption protocol when downloading the chrome driver.
    :return: The file path of chromedriver
    zChrome is not installed.Nr   zACan not find chromedriver for currently installed chrome version.zInvalid path: zDownloading chromedriver (z)...)r0   r1   r2      z	Not Foundz)Failed to download chromedriver archive: z"Chromedriver is already installed.i  )&rl   loggingdebugr   r%   warningr8   r;   isdirrq   r=   r<   r   r   r   r>   rO   infomakedirsr5   r{   r|   r}   getcodeerrorURLErrorr(   r   r_   zipfileZipFileinfolistbasenamerA   extractr?   r@   chmod)r;   r2   r)   r0   r1   major_versionchromedriver_dirchromedriver_filenamechromedriver_filepathr-   responsearchivezip_filezip_infos                 r   download_chromedriverr     sv    ())N 0111-Mn^d-e-e**%&:;;M MU$:$:CS$:O	
 	
 	
 	  
w}}T"" 	64d445557<<(=(=}MM7<<GOOBGOOH5566
 
 677GLL)9;PQQ7>>/00 ;39 9 ; 	L2FLLLMMMw}}-.. 	*K()))"8L_ox~	R~--c22H!!S((l++K888 )|$ 	R 	R 	RP3PPQQQ	R(--//**_W%% 	$--//  7##H$566:OOO(=H%$$X/?@@@E P	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	9:::9*BG44 /
&...  s    5AI 'I3+ALLL__main__)r2   )N)F)NF)*__doc__ry   r   r8   rJ   ro   rH   r   urllib.errorr{   urllib.requestxml.etree.ElementTreeetreeElementTreer   r   ior   r   r#   	packagingr   
__author__typingr   r   r   r   r+   r5   rD   rO   rl   r\   r%   r   r   r   boolr   __name__r&   r   r   r   <module>r      s'      				 				      



         ( ( ( ( ( ( ( ( (                 6
 # # # # # # # #    &" &" &" &"Re e e e(     ( ( (V; ; ;,! ! !$ $ $ $N6 6 6# # #<! <! 0 <! <! <! <! <!~ z	E



	E

u
-
-
-..... r   