o
    .&ßa³)  ã                   @   s�   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	 d dl
mZ d dlmZ d dlmZ dZd	e ZG d
d„ deƒZdS )é    N)Údatetime)ÚClientError)Úsix)Úvalidate_s3_location)ÚBasicCommand)ÚZIP_COMPRESSION_MODEi   é   c                	   @   s¨   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dœdddddœddddœgZdd„ Zdd„ Zdd „ Ze	j
d,d!d"„ƒZd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+S )-ÚPushÚpusha¶  Bundles and uploads to Amazon Simple Storage Service (Amazon S3) an application revision, which is a zip archive file that contains deployable content and an accompanying Application Specification file (AppSpec file). If the upload is successful, a message is returned that describes how to call the create-deployment command to deploy the application revision from Amazon S3 to target Amazon Elastic Compute Cloud (Amazon EC2) instances.zapplication-namez--application-name <app-name>TzdRequired. The name of the AWS CodeDeploy application to be associated with the application revision.)ÚnameÚsynopsisÚrequiredÚ	help_textzs3-locationz!--s3-location s3://<bucket>/<key>a  Required. Information about the location of the application revision to be uploaded to Amazon S3. You must specify both a bucket and a key that represent the Amazon S3 bucket name and the object key name. Content will be zipped before uploading. Use the format s3://\<bucket\>/\<key\>zignore-hidden-filesÚ
store_trueFzÏOptional. Set the --ignore-hidden-files flag to not bundle and upload hidden files to Amazon S3; otherwise, set the --no-ignore-hidden-files flag (the default) to bundle and upload hidden files to Amazon S3.)r   ÚactionÚdefaultÚ
group_namer   zno-ignore-hidden-files)r   r   r   r   Úsourcez--source <path>Ú.zÆOptional. The location of the deployable content and the accompanying AppSpec file on the development machine to be zipped and uploaded to Amazon S3. If not specified, the current directory is used.)r   r   r   r   Údescriptionz--description <description>zÙOptional. A comment that summarizes the application revision. If not specified, the default string "Uploaded by AWS CLI 'time' UTC" is used, where 'time' is the current system time in Coordinated Universal Time (UTC).)r   r   r   c                 C   sH   |   |¡ | jjd|j|j|jd�| _| jjd|jd�| _|  |¡ d S )NÚ
codedeploy)Úregion_nameÚendpoint_urlÚverifyÚs3)r   )	Ú_validate_argsÚ_sessionÚcreate_clientÚregionr   Ú
verify_sslr   r   Ú_push)ÚselfÚparsed_argsÚparsed_globals© r$   úG/usr/lib/python3/dist-packages/awscli/customizations/codedeploy/push.pyÚ	_run_mainm   s   
üþzPush._run_mainc                 C   s@   t |dƒ |jr|jrtdƒ‚|jsd t ¡  ¡ ¡|_d S d S )NÚs3_locationzKYou cannot specify both --ignore-hidden-files and --no-ignore-hidden-files.zUploaded by AWS CLI {0} UTC)	r   Úignore_hidden_filesÚno_ignore_hidden_filesÚRuntimeErrorr   Úformatr   ÚutcnowÚ	isoformat)r!   r"   r$   r$   r%   r   {   s   
ÿÿ
ÿÿÿzPush._validate_argsc                 C   sì   |   |j|j¡�<}z|  ||¡}|d  dd¡|_d|v r"|d |_W n ty< } ztd|j|j	t
|ƒf ƒ‚d }~ww W d   ƒ n1 sGw   Y  |  |¡ d|v r\d |j¡}nd}d |j|j|j|¡}tj d	 |j|¡¡ d S )
NÚETagú"Ú Ú	VersionIdz!Failed to upload '%s' to '%s': %sÚversionz,version={0}z;--s3-location bucket={0},key={1},bundleType=zip,eTag={2}{3}zÙTo deploy with this revision, run:
aws deploy create-deployment --application-name {0} {1} --deployment-group-name <deployment-group-name> --deployment-config-name <deployment-config-name> --description <description>
)Ú	_compressr   r(   Ú_upload_to_s3ÚreplaceÚeTagr2   Ú	Exceptionr*   r'   ÚstrÚ_register_revisionr+   ÚbucketÚkeyÚsysÚstdoutÚwriteÚapplication_name)r!   ÚparamsÚbundleÚupload_responseÚeÚversion_stringÚs3location_stringr$   r$   r%   r    Š   sV   þ
€þÿÿ€ÿ€÷
üþ	þúz
Push._pushc              	   c   s(  � t j |¡}t jj |dg¡}t d¡�u}tj|ddd�}z[d}t j	|dd�D ]B\}}	}
|rAdd	„ |
D ƒ}
d
d	„ |	D ƒ|	d d …< |
D ]&}t j ||¡}t j |¡}|t
|ƒd d … }||krbd}| ||t¡ qCq(|sttd |¡ƒ‚W | ¡  n| ¡  w |V  W d   ƒ d S 1 s�w   Y  d S )Nzappspec.ymlzw+bÚwT)Ú
allowZip64F)Útopdownc                 S   ó   g | ]	}|  d ¡s|‘qS ©r   ©Ú
startswith)Ú.0Úfnr$   r$   r%   Ú
<listcomp>Â   ó    z"Push._compress.<locals>.<listcomp>c                 S   rI   rJ   rK   )rM   Údnr$   r$   r%   rO   Ã   rP   é   z{0} was not found)ÚosÚpathÚabspathÚsepÚjoinÚtempfileÚTemporaryFileÚzipfileÚZipFileÚwalkÚlenr>   r   r*   r+   Úclose)r!   r   r(   Úsource_pathÚappspec_pathÚtfÚzfÚcontains_appspecÚrootÚdirsÚfilesrN   ÚfilenameÚarcnamer$   r$   r%   r3   ¶   s6   €úÿÿ"ézPush._compressc                 C   s6   |   |¡}|tk r| jj|j|j|d�S |  |||¡S )N)ÚBucketÚKeyÚBody)Ú_bundle_sizeÚMULTIPART_LIMITr   Ú
put_objectr:   r;   Ú_multipart_upload_to_s3)r!   r@   rA   Úsize_remainingr$   r$   r%   r4   Ó   s   
ýýzPush._upload_to_s3c                 C   s"   |  dd¡ | ¡ }|  d¡ |S )Nr   é   )ÚseekÚtell)r!   rA   Úsizer$   r$   r%   rl   â   s   
zPush._bundle_sizec              
   C   sâ   | j j|j|jd�}|d }zId}g }| d¡ |dkrI| t¡}| j j|j|j||t 	|¡d�}	| 
||	d dœ¡ |d7 }|t|ƒ8 }|dks| j j|j|j|d|id	�W S  typ }
 z| j j|j|j|d
� |
‚d }
~
ww )N)ri   rj   ÚUploadIdrR   r   )ri   rj   ru   Ú
PartNumberrk   r.   )rv   r.   ÚParts)ri   rj   ru   ÚMultipartUpload)ri   rj   ru   )r   Úcreate_multipart_uploadr:   r;   rr   Úreadrm   Úupload_partr   ÚBytesIOÚappendr]   Úcomplete_multipart_uploadr   Úabort_multipart_upload)r!   r@   rA   rp   Úcreate_responseÚ	upload_idÚpart_numÚmultipart_listÚdatarB   rC   r$   r$   r%   ro   è   sP   þ

ûþòüý€úzPush._multipart_upload_to_s3c                 C   sJ   d|j |jd|jdœdœ}d|v r|j|d d< | jj|j||jd� d S )NÚS3Úzip)r:   r;   Ú
bundleTyper6   )ÚrevisionTypeÚ
s3Locationr2   r‰   )ÚapplicationNameÚrevisionr   )r:   r;   r6   r2   r   Úregister_application_revisionr?   r   )r!   r@   r‹   r$   r$   r%   r9     s   üþ	
ýzPush._register_revisionN)F)Ú__name__Ú
__module__Ú__qualname__ÚNAMEÚDESCRIPTIONÚ	ARG_TABLEr&   r   r    Ú
contextlibÚcontextmanagerr3   r4   rl   ro   r9   r$   r$   r$   r%   r	   !   sX    ÿû
ûúüûüÍ?,'r	   )rS   r<   rZ   rX   r“   r   Úbotocore.exceptionsr   Úawscli.compatr   Ú&awscli.customizations.codedeploy.utilsr   Úawscli.customizations.commandsr   r   ÚONE_MBrm   r	   r$   r$   r$   r%   Ú<module>   s   