o
    !d                     @   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mZ d dlm	Z	 d dl
mZ G dd de	Zdd Zd	d
 ZG dd deZdS )    N)
S3Transfer)BasicCommand)human_readable_sizec                   @   sR   e Zd ZdZdZddddddddd	dd
dddddddddgZdd ZdS )UploadBuildCommandzupload-buildz#Upload a new build to AWS GameLift.nameTzThe name of the build)r   requiredZ	help_textzbuild-versionzThe version of the buildz
build-rootz8The path to the directory containing the build to uploadzserver-sdk-versionFzEThe version of the GameLift server SDK used to create the game serverzoperating-systemz&The operating system the build runs onc              	   C   sx  | j jd|j|j|jd}t|jstj	d|j  dS |j
|jd}|jr,|j|d< |jr4|j|d< |jdi |}|d d	 }|j|d
}|d }|d d }|d d }	|d }
|d }|d }| j jd|
|||j|jd}t|}z-td| \}}t||j |j|||	t|d|j d dd W t| t| nt| t| w tj	d|j|f  dS )NZgamelift)region_nameendpoint_urlverifyzHFail to upload %s. The build root directory is empty or does not exist.
   )NameVersionZOperatingSystemZServerSdkVersionBuildBuildId)r   ZUploadCredentialsZStorageLocationZBucketKeyZAccessKeyIdZSecretAccessKeyZSessionTokenZs3)Zaws_access_key_idZaws_secret_access_keyZaws_session_tokenr   r
   z%s.zipz
Uploading :)label)callbackz6Successfully uploaded %s to AWS GameLift
Build ID: %s
r    )Z_sessionZcreate_clientZregionr	   Z
verify_sslvalidate_directoryZ
build_rootsysstderrwriter   Zbuild_versionZoperating_systemZserver_sdk_versionZcreate_buildZrequest_upload_credentialsr   tempfilemkstempzip_directoryZupload_fileProgressPercentageoscloseremovestdout)selfargsZparsed_globalsZgamelift_clientZcreate_build_kwargsresponseZbuild_idZupload_credentialsbucketkeyZ
access_keyZ
secret_keyZsession_tokenZ	s3_clientZs3_transfer_mgrfdZtemporary_zipfiler   r   Nusr/lib/python3.10/site-packages/awscli/customizations/gamelift/uploadbuild.py	_run_main-   st   




zUploadBuildCommand._run_mainN)__name__
__module____qualname__NAMEZDESCRIPTIONZ	ARG_TABLEr(   r   r   r   r'   r      s(    r   c              	   C   s   t j|}t| dS}t|dtjd}t|,}t 	|D ]\}}}|D ]}t j
||}	t j|	|}
||	|
 q'q W d    n1 sIw   Y  W d    d S W d    d S 1 saw   Y  d S )NwbwT)r   pathabspathopenzipfileZipFileZIP_DEFLATED
contextlibclosingwalkjoinrelpathr   )Zzipfile_namesource_rootfZzip_filezfrootdirsfilesfilename	full_pathZrelative_pathr   r   r'   r   s   s"   "r   c                 C   s,   | sdS t | D ]
\}}}|r dS q	dS )NFT)r   r7   )r:   r/   r>   r?   r   r   r'   r      s   r   c                   @   s   e Zd ZdddZdd ZdS )r   Nc                 C   sD   || _ || _| jd u r| j | _ttj|| _d| _t	 | _
d S )Nr   )	_filename_labelfloatr   r/   getsize_size_seen_so_far	threadingLock_lock)r!   r@   r   r   r   r'   __init__   s   
zProgressPercentage.__init__c                 C   s   | j = |  j|7  _| jdkr8| j| j d }tjd| jt| jt| j|f  tj  W d    d S W d    d S 1 sCw   Y  d S )Nr   d   z%s  %s / %s  (%.2f%%))	rJ   rG   rF   r   r    r   rC   r   flush)r!   Zbytes_amountZ
percentager   r   r'   __call__   s   

"zProgressPercentage.__call__)N)r)   r*   r+   rK   rN   r   r   r   r'   r      s    
	r   )rH   r5   r   r   r   r2   Z
s3transferr   Zawscli.customizations.commandsr   Zawscli.customizations.s3.utilsr   r   r   r   objectr   r   r   r   r'   <module>   s   Y