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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_trueFzOptional. 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>.zOptional. 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>zOptional. 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_urlZverifys3)r   )	_validate_argsZ_sessionZcreate_clientZregionr   Z
verify_sslr   r   _push)selfparsed_argsZparsed_globals r   Iusr/lib/python3.10/site-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_filesZ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" Z	VersionIdz!Failed to upload '%s' to '%s': %sversionz,version={0}z;--s3-location bucket={0},key={1},bundleType=zip,eTag={2}{3}zTo 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eTagr*   	Exceptionr#   r!   str_register_revisionr$   bucketkeysysstdoutwriteapplication_name)r   paramsbundleupload_responseeversion_stringZ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 sw   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   r@   rA   rB   )rD   dnr   r   r   rF      rG      z{0} was not found)ospathabspathsepjointempfileTemporaryFilezipfileZipFilewalklenr6   r   r#   r$   close)r   r   r"   source_pathZappspec_pathtfzfZcontains_appspecrootdirsfilesrE   filenamearcnamer   r   r   r+      s6   "zPush._compressc                 C   s6   |  |}|tk r| jj|j|j|dS | |||S )N)BucketKeyBody)_bundle_sizeMULTIPART_LIMITr   Z
put_objectr2   r3   _multipart_upload_to_s3)r   r8   r9   size_remainingr   r   r   r,      s   
zPush._upload_to_s3c                 C   s"   | dd | }| d |S )Nr      )seektell)r   r9   sizer   r   r   ra      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)r^   r_   UploadIdrI   r   )r^   r_   ri   
PartNumberr`   r'   )rj   r'   ZParts)r^   r_   ri   ZMultipartUpload)r^   r_   ri   )r   Zcreate_multipart_uploadr2   r3   rf   readrb   Zupload_partr   BytesIOappendrT   Zcomplete_multipart_uploadr   Zabort_multipart_upload)r   r8   r9   rd   Zcreate_responseZ	upload_idZpart_numZmultipart_listdatar:   r;   r   r   r   rc      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 )NZS3zip)r2   r3   Z
bundleTyper.   )ZrevisionType
s3Locationr*   rp   )ZapplicationNamerevisionr   )r2   r3   r.   r*   r   Zregister_application_revisionr7   r   )r   r8   rq   r   r   r   r1     s   	
zPush._register_revisionN)F)__name__
__module____qualname__NAMEZDESCRIPTIONZ	ARG_TABLEr    r   r   
contextlibcontextmanagerr+   r,   ra   rc   r1   r   r   r   r   r	   !   sX    
?,'r	   )rJ   r4   rQ   rO   rv   r   Zbotocore.exceptionsr   Zawscli.compatr   Z&awscli.customizations.codedeploy.utilsr   Zawscli.customizations.commandsr   r   ZONE_MBrb   r	   r   r   r   r   <module>   s   