a
    7ñ»jµ,  ã                   @   s–   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
Z
 d dlmZ d dlmZ e e¡Zej d¡ZG dd„ deƒZdS )é    N)Úsuppress)Ú
sb_install)Úshared_utils)ÚdarwinÚcygwinÚlinuxc                   @   sR  e Zd ZddlmZ dZdZdZej	Z
e
 d¡r@ed; Zed; Ze
 d¡rZed	; Zed
; Ze
 d¡rted; Zed
; Ze ¡ Zej e¡s®zejedd� W n ey¬   Y n0 ej ej e¡¡Zd/dd„Zd0dd„Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZedd „ ƒZd1d!d"„Zd#d$„ Z ed%d&„ ƒZ!d2d'd(„Z"d)d*„ Z#d+d,„ Z$d-d.„ Z%dS )3ÚPatcherr   )Údownload_helperz+https://chromedriver.storage.googleapis.comzchromedriver_%s.zipzchromedriver%sÚwin32ú.exer   Zlinux64Ú r   Zmac64T©Úexist_okNFc                 C   sî   || _ d| _d}tj | j¡sVttƒ�  tj| jdd� W d  ƒ n1 sL0    Y  |sxtj 	| jd 	|| j
g¡¡| _ts˜|r˜|dd… dks˜|d7 }tj 	| j|¡| _|sÈtj tj 	d| j¡¡| _d	| _|rÞd| _|| _|| _d| _dS )
a  Args:
        executable_path: None = automatic
            A full file path to the chromedriver executable
        force: False
            Terminate processes which are holding lock
        version_main: 0 = auto
            Specify main chrome version (rounded, ex: 82) NZ
undetectedTr   Ú_éüÿÿÿr   Ú.F)ÚforceÚexecutable_pathÚosÚpathÚexistsÚ	data_pathr   Ú	ExceptionÚmakedirsÚjoinÚexe_nameÚIS_POSIXÚzip_pathÚabspathÚ_custom_exe_pathÚversion_mainÚversion_full)Úselfr   r   r    Úprefix© r$   úEE:\soccer\ml_env\Lib\site-packages\seleniumbase/undetected/patcher.pyÚ__init__.   s2    
.ÿÿzPatcher.__init__c                 C   s0  |r|| _ d| _| jr2|  | j ¡}|s.|  ¡ S d S |r<|| _|du rJ|| _zt | j ¡ W nt ty¾   | jr’|  	| j ¡ | j }| j
|d� Y S z|  ¡ r¤W Y dS W n ty¸   Y n0 Y n tyÎ   Y n0 |  ¡ }| d¡d | _|| _t| jƒdk �r|  |  ¡ ¡ ntjd| j t ¡ dd� |  ¡ S )NT)r   r   r   és   zchromedriver %s)ÚoverrideZintel_for_ucZforce_uc)r   r   Úis_binary_patchedÚ	patch_exer    r   r   ÚunlinkÚPermissionErrorÚforce_kill_instancesÚautoÚFileNotFoundErrorÚfetch_release_numberÚsplitr!   ÚintÚunzip_packageÚfetch_packager   Úmainr   Z
is_arm_macÚpatch)r"   r   r   r    Z	ispatchedZ	not_forceÚreleaser$   r$   r%   r.   P   sJ    
ýzPatcher.autoc                 C   s   |   ¡  |  ¡ S )N)r*   r)   ©r"   r$   r$   r%   r6   y   s    zPatcher.patchc                 C   st   ddl m} d}| jr$|d| j 7 }| ¡ }t d| ¡ | jr^t| jƒdkr^t t	| jƒ¡S || j
| ƒ ¡  ¡ S )Nr   )Úurlopenz/latest_releasez_%szGetting release number from %sér   )Úurllib.requestr9   r    ÚupperÚloggerÚdebugr2   r   Z%get_cft_latest_version_from_milestoneÚstrÚurl_repoÚreadÚdecode)r"   r9   r   r$   r$   r%   r0   }   s    ÿÿzPatcher.fetch_release_numberc                 C   s:   ddl m} d| j| j| jf }t d| ¡ ||ƒd S )zMDownloads chromedriver from source.
        :return: path to downloaded file r   )Úurlretrievez%s/%s/%szDownloading from %s)r;   rC   r@   r!   Úzip_namer=   r>   )r"   rC   Úur$   r$   r%   r4   �   s    ÿzPatcher.fetch_packagec              	   C   s  t  d| ¡ zt | j¡ W n ttfy4   Y n0 ztj| jddd� W n ty^   Y n0 t	j
|dd�� }| | j| j¡ W d  ƒ n1 s”0    Y  z*t tj | j| j¡| j¡ t |¡ W n tyÚ   Y n0 zt | j¡ t | jd¡ W n t�y   Y n0 | jS )z& :return: path to unpacked executable zUnzipping %sií  T)Úmoder   Úr)rF   N)r=   r>   r   r+   r   r/   ÚOSErrorr   r   ÚzipfileÚZipFileÚextractr   Úrenamer   r   r   Úremover,   ÚrmdirÚchmod)r"   ÚfpÚzfr$   r$   r%   r3   ˜   s2    .þzPatcher.unzip_packagec                 C   s4   t j | ¡} tr t  d|  ¡}nt  d|  ¡}| S )z€Terminate instances of UC.
        :param: Executable name to kill. (Can be a path)
        :return: True on success else False.zkill -f -9 $(pidof %s)ztaskkill /f /im %s)r   r   Úbasenamer   Úsystem)r   rG   r$   r$   r%   r-   ´   s
    zPatcher.force_kill_instancesc                  C   sN   t jtjdd�} ttj| dd… ƒ| dd…< | d | d< d| d< d	 | ¡ ¡ S )
Né   ©Úkiúÿÿÿr   r   é   r   é   r   )	ÚrandomÚchoicesÚstringÚascii_lowercaseÚmapr?   r<   r   Úencode©Zcdcr$   r$   r%   Úgen_random_cdcÀ   s
    zPatcher.gen_random_cdcc                 C   sZ   |p| j }t |d¡�0}t d| ¡ ¡r8W d   ƒ dS W d   ƒ n1 sL0    Y  dS )NÚrbsQ   window.cdc_adoQpoasnfa76pfcZLmcfl_(Array|Promise|Symbol|Object|Proxy|JSON|Window)FT)r   ÚioÚopenÚreÚsearchrA   ©r"   r   Úfhr$   r$   r%   r)   È   s    
ý.zPatcher.is_binary_patchedc                 C   sˆ   dd„ }dd„ }t  | jd¡�V}| ¡ }t d||¡}t d||¡}t d||¡}| d	¡ | |¡ W d
  ƒ n1 sz0    Y  dS )zPatches the ChromeDriver binaryc                 S   s   dt |  ¡ ƒ S )Nó   
)ÚlenÚgroup)Úmatchr$   r$   r%   Úgen_js_whitespacesÕ   s    z-Patcher.patch_exe.<locals>.gen_js_whitespacesc                 S   sR   t |  ¡ ƒd }t d|¡}dttƒ  tjtj	|d�¡dƒ d d||   }|S )NrX   é   ó   ')Ú
populationrV   Úasciis   ';rh   )
ri   rj   rY   ÚrandintÚbytesr?   r   rZ   r[   Úascii_letters)rk   Zrep_lenZran_lenZbbr$   r$   r%   Úgen_call_function_js_cache_nameØ   s    ÿþþ
þz:Patcher.patch_exe.<locals>.gen_call_function_js_cache_nameúr+bs†   window\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol|Object|Proxy|JSON|Window) = window\.(Array|Promise|Symbol|Object|Proxy|JSON|Window);sP   window\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol|Object|Proxy|JSON|Window) \|\|s   '\$cdc_[a-zA-Z0-9]{22}_';r   NT)rb   rc   r   rA   rd   ÚsubÚseekÚwrite)r"   rl   rt   rg   Zfile_binr$   r$   r%   r*   Ó   s,    ûüý
(zPatcher.patch_exec                  C   s   t jtjdd�} d | ¡ ¡ S )Né   rU   r   )rY   rZ   r[   rs   r   r^   r_   r$   r$   r%   Úgen_random_cdc_betaø   s    zPatcher.gen_random_cdc_betac                 C   sh   |p| j }zDt |d¡�$}| ¡  d¡dkW  d   ƒ W S 1 sB0    Y  W n tyb   Y dS 0 d S )Nra   s   undetected chromedriveréÿÿÿÿF)r   rb   rc   rA   Úfindr/   rf   r$   r$   r%   Úis_binary_patched_betaý   s    
6zPatcher.is_binary_patched_betac                 C   s”   t  | jd¡�r}| ¡ }t d|¡}d }|rD|d }d t|ƒd¡}|rr| ||¡}||kr^n| 	d¡ | 
|¡ W d   ƒ n1 s†0    Y  d S )Nru   s   \{window\.cdc.*?;\}r   s.   {console.log("chromedriver is undetectable!")}ó    )rb   rc   r   rA   rd   re   Úljustri   Úreplacerw   rx   )r"   rg   ÚcontentZmatch_injected_codeblockZtarget_bytesZnew_target_bytesZnew_contentr$   r$   r%   Úpatch_exe_beta  s$    ÿÿÿ
zPatcher.patch_exe_betac                 C   s   d  | jj| j¡S )Nz{0:s}({1:s}))ÚformatÚ	__class__Ú__name__r   r8   r$   r$   r%   Ú__repr__  s    þzPatcher.__repr__c              
   C   s¦   | j r
d S d}t ¡ }t ¡ }|| |kr@t d| j|f ¡ q¢z$t | j¡ t d| j ¡ W q¢W q tt	t
fyŠ   t d¡ Y qY q tyž   Y q¢Y q0 qd S )NrX   z(Could not unlink %s in time (%d seconds)zSuccessfully unlinked %sgš™™™™™¹?)r   ÚtimeÚ	monotonicr=   r>   r   r   r+   rH   ÚRuntimeErrorr,   Úsleepr/   )r"   ÚtimeoutÚtÚnowr$   r$   r%   Ú__del__!  s2    ÿÿÿÿ
zPatcher.__del__)NFr   )NFN)N)N)&r…   Ú
__module__Ú__qualname__Zseleniumbase.corer	   r@   rD   r   ÚsysÚplatformZsys_platÚendswithZget_downloads_folderZdownloads_folderr   r   r   r   r   r   Ú
expanduserr   r&   r.   r6   r0   r4   r3   Ústaticmethodr-   r`   r)   r*   rz   r}   r‚   r†   rŽ   r$   r$   r$   r%   r      sN   



"
)


%

r   )rb   Úloggingr   rY   rd   r[   r‘   r‡   rI   Ú
contextlibr   Zseleniumbase.console_scriptsr   Zseleniumbase.fixturesr   Ú	getLoggerr…   r=   r’   Ú
startswithr   Úobjectr   r$   r$   r$   r%   Ú<module>   s   
