U
    ^»{jë  ã                   @  s$  d dl m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Z d dlmZ d dlZd dlmZ e e¡ZdZddd	d
dgZddœdd„Zddœdd„ZG dd„ dejƒZddefddœddddddœdd„Zddefddœddddddœdd „Zd!dœd"d#„ZdS )$é    )ÚannotationsN)Úpartial)ÚPath)Údefault_download_pathz]https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.jsonZlinux64Úwin32Zwin64zmac-x64z	mac-arm64ztuple[str, str, str, str])Úreturnc                  C  s’   t jdkrdnd} t ¡ dkr"dnd}d }t ¡ dkr@d|  }n.t ¡ dkrVd	|  }nt ¡ d
krnd| |  }d}|tkr~|}|| t ¡ t ¡ fS )Nl        Z64Z32ÚarmÚxÚWindowsÚwinÚLinuxÚlinuxÚDarwinzmac-Ú )ÚsysÚmaxsizeÚplatformÚ	processorÚsystemÚsupported_platform_strings)Zarch_size_detectedZarch_detectedZchrome_platform_detectedZplatform_string© r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/choreographer/cli/_cli_utils.pyÚ$get_google_supported_platform_string   s    

r   zPath | Nonec                  C  s�   t ƒ \} }}}| sd S tƒ }t ¡  d¡r>td| › � d }nNt ¡  d¡rltd| › � d d d d }n t ¡  d	¡rŒtd| › � d
 }|S )Nr   úchrome-Úchromer   úGoogle Chrome for Testing.appÚContentsÚMacOSúGoogle Chrome for TestingZWinú
chrome.exe)r   r   r   r   Ú
startswithr   )Z_chrome_platform_detectedÚ_Z_default_exe_pathr   r   r   Úget_chrome_download_path-   s0    ÿÿþýüûÿÿr"   c                      s   e Zd Z‡ fdd„Z‡  ZS )Ú_ZipFilePermissionsc                   sJ   t |tjƒs|  |¡}tƒ  |||¡}|jd? }|dkrFt|ƒ |¡ |S )Né   r   )	Ú
isinstanceÚzipfileÚZipInfoÚgetinfoÚsuperÚ_extract_memberÚexternal_attrr   Úchmod)ÚselfÚmemberÚ
targetpathÚpwdÚpathÚattr©Ú	__class__r   r   r*   L   s    

z#_ZipFilePermissions._extract_member)Ú__name__Ú
__module__Ú__qualname__r*   Ú__classcell__r   r   r3   r   r#   K   s   r#   F©Úverbosez
str | Nonez
int | Nonez
str | PathÚboolz
Path | str)ÚarchÚir1   r:   r   c             
   C  s  | st ƒ \} }}}| s2tdd t¡› d| › d�ƒ‚t|tƒrDt|ƒ}|rvt d¡ t	 
tj t¡ ¡ ¡}|d | }n<t d¡ ttƒ ¡ jjd d	  ¡ �}t	 |¡}W 5 Q R X |rÎt|d
 ƒ t|d ƒ |d d }d}	|D ]}
|
d | krâ|
d }	 �qqâtdd t¡› d| › d�ƒ‚| ¡ �s4|jdd� |d }tj |	¡�(}| d¡�}t ||¡ W 5 Q R X W 5 Q R X t|dƒ�}| |¡ W 5 Q R X | ¡  |  d¡�r¾|d| › � d }nT|  d¡�rê|d| › � d d d d }n(|  d¡�r
|d| › � d }ntd ƒ‚|S )!z2Download chrome synchronously: see `get_chrome()`.z"You must specify an arch, one of: z, z. Detected z is not supported.zLoading chrome from listÚversionsz'Using last known good version of chromeÚ	resourceszlast_known_good_chrome.jsonÚversionÚrevisionZ	downloadsr   r   r   Úurlz. T)Úparentsz
chrome.zipÚwbÚrr   r   Úmacr   r   r   r   r   r   z6Couldn't calculate exe_name, unsupported architecture.)r   ÚRuntimeErrorÚjoinr   r%   Ústrr   Ú_loggerÚinfoÚjsonÚloadsÚurllibÚrequestÚurlopenÚ_chrome_for_testing_urlÚreadÚ__file__ÚresolveÚparentÚopenÚloadÚprintÚexistsÚmkdirÚshutilÚcopyfileobjr#   Ú
extractallÚunlinkr    )r<   r=   r1   r:   r!   Zbrowser_listZversion_objÚfZchromium_sourcesrB   ÚsrcÚfilenameÚresponseÚout_fileZzip_refÚexe_namer   r   r   Úget_chrome_syncX   s€    ÿ

ÿÿ
ÿþÿÿ
 ÿþýüûÿre   c                Ã  s.   t  ¡ }tt| |||d�}|jd|d�I dH S )a{  
    Download google chrome from google-chrome-for-testing server.

    Args:
        arch: the target platform/os, as understood by google's json directory.
        i: the chrome version: -1 being the latest version, 0 being the oldest
           still in the testing directory.
        path: where to download it too (the folder).
        verbose: print out version found

    ©r<   r=   r1   r:   N)ÚexecutorÚfunc)ÚasyncioZget_running_loopr   re   Zrun_in_executor)r<   r=   r1   r:   ÚloopÚfnr   r   r   Ú
get_chrome§   s    þrl   ÚNonec                  C  sÖ   dt  ¡  ¡ krt dt¡ tjdtj	gd�} | j
ddtddd	� | j
d
ddd� | j
dddd� | j
dddddd� | jtd� | jd d� | jdd� |  ¡ }|j}|j}t|jƒ}|j}tt||||d�ƒ d S )NZubuntuzàYou are using `get_browser()` on Ubuntu. Ubuntu is **very strict** about where binaries come from. While sandbox is already off by default, do not set enable_sandbox to True OR you must install from Ubuntu's package manager.z9Will download Chrome for testing. All arguments optional.)ÚdescriptionrC   z--iz-ir=   zrGoogle offers thousands of chrome versions for download. '-i 0' is the oldest, '-i -1' is the newest: array syntax)ÚtypeÚdestÚhelpz--archr<   z%linux64|win32|win64|mac-x64|mac-arm64)rp   rq   z--pathr1   zWhere to store the download.z-vz	--verboser:   Ú
store_truez4Display found version number if using -i (to stdout))rp   Úactionrq   )r1   )r<   Fr9   rf   )r   r@   ÚlowerÚwarningsÚwarnÚUserWarningÚargparseÚArgumentParserÚlogistroÚparserÚadd_argumentÚintÚset_defaultsr   Ú
parse_argsr=   r<   r   r1   r:   rX   re   )r{   Úparsedr=   r<   r1   r:   r   r   r   Úget_chrome_cliÁ   sT    úþú
ýýû
r�   )Ú
__future__r   rx   ri   rL   r   r[   r   Úurllib.requestrN   ru   r&   Ú	functoolsr   Úpathlibr   rz   Zchoreographer.cli.defaultsr   Ú	getLoggerr5   rJ   rQ   r   r   r"   ÚZipFiler#   re   rl   r�   r   r   r   r   Ú<module>   s@   
ýûPýû