B
    è?F[¥  ã               @   s8   d dl Z d dlmZ d dlZd dlZG dd„ deƒZdS )é    N)Ú
ScriptBasec                   sD   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡  Z	S )ÚCopyBotc                s  t t| ƒ ¡  tj dd¡| _d| j | _t	j
 | j| j¡| _t | j| j¡ tj | jd¡| _tj | jd¡| _tj | jdd¡| _t ¡ }| | j¡| _| js´tj d| j ¡ tj | jd	d ¡}|rètj | jd
d ¡}t ||¡}| | j¡| _| js| | j¡| _d S )NZPyamiZworking_dirz%s.logZ
src_bucketZ
dst_bucketZreplace_dstTz Source bucket does not exist: %sZdest_aws_access_key_idZdest_aws_secret_access_key)Úsuperr   Ú__init__ÚbotoÚconfigÚgetÚwdirÚinstance_idÚlog_fileÚosÚpathÚjoinÚlog_pathZset_file_loggerÚnameZsrc_nameZdst_nameÚgetboolÚreplaceZ
connect_s3ÚlookupÚsrcÚlogÚerrorZconnectÚdstZcreate_bucket)ÚselfZs3Zdest_access_keyZdest_secret_key)Ú	__class__© ú1lib/python3.7/site-packages/boto/pyami/copybot.pyr      s&    zCopyBot.__init__c             C   s,   t j | jdd¡r(| j ¡ }| j |¡ d S )NÚ	copy_aclsT)r   r   r   r   r   Úget_xml_aclr   Úset_xml_acl)r   Úaclr   r   r   Úcopy_bucket_acl1   s    
zCopyBot.copy_bucket_aclc             C   s(   t j | jdd¡r$| ¡ }| |¡ d S )Nr   T)r   r   r   r   r   r   )r   r   r   r   r   r   r   Úcopy_key_acl6   s    zCopyBot.copy_key_aclc             C   s  t j d| jj ¡ t j d| jj ¡ yºx´| jD ]ª}| jsj| j |j¡}|rjt j d|j| jjf ¡ q2t j d|j|jf ¡ t	j
 |j¡\}}t	j
 | j|¡}| |¡ | j |j¡}| |¡ |  ||¡ t	 |¡ q2W W n   t j d|j ¡ Y nX d S )Nzsrc=%szdst=%sz%key=%s already exists in %s, skippingzcopying %d bytes from key=%szError copying key: %s)r   r   Úinfor   r   r   r   r   Úsizer   r   Úsplitr   r	   Zget_contents_to_filenameÚnew_keyÚset_contents_from_filenamer!   ÚunlinkZ	exception)r   ÚkeyÚexistsÚprefixÚbaser   r%   r   r   r   Ú	copy_keys;   s&    

zCopyBot.copy_keysc             C   s   | j  | j¡}| | j¡ d S )N)r   r%   r   r&   r   )r   r(   r   r   r   Úcopy_logP   s    zCopyBot.copy_logc             C   s–   t   ¡ }tj |¡ |  d| j| jf | ¡ ¡ | jrD| j	rD|  
¡  | j	rR|  ¡  |  d| j| jf d¡ tj | jdd¡r’t ¡ }| | jg¡ d S )Nz%s (%s) Startingz%s (%s) StoppingzCopy Operation CompleteZexit_on_completionT)ÚStringIOr   r   Z	dump_safeZnotifyr   r
   Úgetvaluer   r   r,   r-   r   Zconnect_ec2Zterminate_instances)r   ÚfpZec2r   r   r   ÚmainT   s    zCopyBot.main)
Ú__name__Ú
__module__Ú__qualname__r   r    r!   r,   r-   r1   Ú__classcell__r   r   )r   r   r      s   r   )r   Zboto.pyami.scriptbaser   r   r.   r   r   r   r   r   Ú<module>   s   