o
    é’OjbY  ã                   @   s¨   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	m
Z
mZmZ d dlmZmZmZmZ G dd„ deƒZG d	d
„ d
e	ƒZG dd„ deƒZG dd„ deƒZdS )é    N)Ú	parse_qsl)ÚClientError)ÚS3CopyFailedError)ÚCompleteMultipartUploadTaskÚCreateMultipartUploadTaskÚSubmissionTaskÚTask)ÚChunksizeAdjusterÚcalculate_range_parameterÚget_callbacksÚget_filtered_dictc                
   @   s¾   e Zd ZdZdddddddd	d
dœ	Zg d¢Zg d¢Zg d¢Zg d¢Zd	d
gZ	g d¢Z
d	d
gZd	d
gZg d¢Zdd„ Zdd„ Z		d$dd„Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ ZdS )%ÚCopySubmissionTaskz+Task for submitting tasks to execute a copyÚIfMatchÚIfModifiedSinceÚIfNoneMatchÚIfUnmodifiedSinceÚSSECustomerKeyÚSSECustomerAlgorithmÚSSECustomerKeyMD5ÚRequestPayerÚExpectedBucketOwner)	ÚCopySourceIfMatchÚCopySourceIfModifiedSinceÚCopySourceIfNoneMatchÚCopySourceIfUnmodifiedSinceÚCopySourceSSECustomerKeyÚCopySourceSSECustomerAlgorithmÚCopySourceSSECustomerKeyMD5r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   ÚMetadataDirectiveÚTaggingDirectiveÚAnnotationDirectiveÚTagging)ÚCacheControlÚContentDispositionÚContentEncodingÚContentLanguageÚContentTypeÚExpiresÚMetadata)r   r   r   r   r   )r   r   ÚChecksumAlgorithmc              	   C   sì   i }d}|j j}|j jdu s|j jdu rV|  |j¡}	|j}
|
 ¡ D ]\}}|| jv r1||	| j| < q!|j	j
di |	¤Ž}|j  |d ¡ |j  | d¡¡ |  |¡}| d¡}|j j|jk rh|  |||||¡ dS | j|||||||d� dS )aÒ  
        :param client: The client associated with the transfer manager

        :type config: s3transfer.manager.TransferConfig
        :param config: The transfer config associated with the transfer
            manager

        :type osutil: s3transfer.utils.OSUtil
        :param osutil: The os utility associated to the transfer manager

        :type request_executor: s3transfer.futures.BoundedExecutor
        :param request_executor: The request executor associated with the
            transfer manager

        :type transfer_future: s3transfer.futures.TransferFuture
        :param transfer_future: The transfer future associated with the
            transfer request that tasks are being submitted for
        NÚContentLengthÚETagÚ	VersionId)Úsource_version_id© )ÚmetaÚ	call_argsÚsizeÚetagÚ)_get_head_object_request_from_copy_sourceÚcopy_sourceÚ
extra_argsÚitemsÚEXTRA_ARGS_TO_HEAD_ARGS_MAPPINGÚsource_clientÚhead_objectÚprovide_transfer_sizeÚprovide_object_etagÚgetÚ_extract_preserved_metadataÚmultipart_thresholdÚ_submit_copy_requestÚ_submit_multipart_request)ÚselfÚclientÚconfigÚosutilÚrequest_executorÚtransfer_futureÚpreserved_metadatar-   r0   Úhead_object_requestr5   ÚparamÚvalueÚresponser.   r.   úW/var/www/html/pharmsmart-cdr-gen/venv/lib/python3.10/site-packages/s3transfer/copies.pyÚ_submito   sR   
ÿ
þ
ÿÿ
þÿ€
ÿÿ


ÿ
ùzCopySubmissionTask._submitc                 C   sL   |j j}t|dƒ}| j |t| j||j|j|j|j	||j j
dœdd�¡ d S )NÚprogress)rB   r4   ÚbucketÚkeyr5   Ú	callbacksr1   T)Útransfer_coordinatorÚmain_kwargsÚis_final)r/   r0   r   Ú_transfer_coordinatorÚsubmitÚCopyObjectTaskr4   rO   rP   r5   r1   )rA   rB   rC   rD   rE   rF   r0   Úprogress_callbacksr.   r.   rL   r?   ¾   s"   
ù	õþz'CopySubmissionTask._submit_copy_requestNc                 C   s¤  |j j}|  |j|pi ¡}	i }
|	 ¡ D ]\}}|| jvr ||
|< q| j |t| j||j	|j
|
dœd�¡}|j}tƒ }| ||j j¡}tt |j jt|ƒ ¡ƒ}g }t|dƒ}td|d ƒD ]R}|  |j¡}t||d ||j jƒ|d< |j jd ur~|j j|d< |  ||d ||j j¡}|j d¡}| | j |t| j||j|j	|j
|||||dœ	d	|id
�¡¡ q]|  |j¡}| j |t| j||j	|j
|||dœ||dœdd�¡ d S )N)rB   rO   rP   r5   )rR   rS   rN   é   ÚCopySourceRanger   r)   )	rB   r4   rO   rP   Úpart_numberr5   rQ   r1   Úchecksum_algorithmÚ	upload_id)rR   rS   Úpending_main_kwargs)rB   rO   rP   r5   r0   r-   )r]   ÚpartsT)rR   rS   r^   rT   )r/   r0   Ú_apply_preserved_metadatar5   r6   ÚCREATE_MULTIPART_ARGS_BLACKLISTrU   rV   r   rO   rP   Úmultipart_chunksizer	   Úadjust_chunksizer1   ÚintÚmathÚceilÚfloatr   ÚrangeÚ_extra_upload_part_argsr
   r2   Ú_get_transfer_sizer<   ÚappendÚCopyPartTaskr4   Ú_extra_complete_multipart_argsÚCopyCompleteMultipartUploadTask)rA   rB   rC   rD   rE   rF   rG   r-   r0   Úmerged_extra_argsÚcreate_multipart_extra_argsrI   ÚvalÚcreate_multipart_futureÚ	part_sizeÚadjusterÚ	num_partsÚpart_futuresrX   r[   Úextra_part_argsr1   r\   Úcomplete_multipart_extra_argsr.   r.   rL   r@   Ø   s´   

ÿ
€üþþÿÿ
ÿüÿü÷ÿóþÿÿú	þòþz,CopySubmissionTask._submit_multipart_requestc                 C   s(   i }| j D ]}||v r|| ||< q|S ©N©ÚPRESERVED_METADATA_FIELDS)rA   Úhead_object_responseÚ	preservedÚfieldr.   r.   rL   r=   U  s   
€z.CopySubmissionTask._extract_preserved_metadatac                    s6   |  d¡dkr	|S ‡ fdd„| ¡ D ƒ}| |¡ |S )Nr   ÚREPLACEc                    s    i | ]\}}|ˆ j vr||“qS r.   rz   ©Ú.0ÚkÚv©rA   r.   rL   Ú
<dictcomp>b  s    þÿz@CopySubmissionTask._apply_preserved_metadata.<locals>.<dictcomp>)r<   r6   Úupdate)rA   r5   rG   Úresultr.   r„   rL   r`   \  s   
ÿ
z,CopySubmissionTask._apply_preserved_metadatac                 C   s.   t |tƒr
t |¡S td|› dt|ƒ› d�ƒ‚)NzLExpecting dictionary formatted: {"Bucket": bucket_name, "Key": key} but got z	 or type Ú.)Ú
isinstanceÚdictÚcopyÚ	TypeErrorÚtype)rA   r4   r.   r.   rL   r3   i  s   

þþÿz<CopySubmissionTask._get_head_object_request_from_copy_sourcec                 C   ó   t || jƒS ry   )r   ÚUPLOAD_PART_COPY_ARGS©rA   r5   r.   r.   rL   ri   s  s   z*CopySubmissionTask._extra_upload_part_argsc                 C   rŽ   ry   )r   ÚCOMPLETE_MULTIPART_ARGSr�   r.   r.   rL   rm   x  s   z1CopySubmissionTask._extra_complete_multipart_argsc                 C   s   ||d kr|||  S |S )NrY   r.   )rA   rs   Ú
part_indexru   Útotal_transfer_sizer.   r.   rL   rj   {  s   z%CopySubmissionTask._get_transfer_size)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r�   ra   r{   r‘   ÚGET_OBJECT_TAGGING_ARGSÚPUT_OBJECT_TAGGING_ARGSÚLIST_OBJECT_ANNOTATIONS_ARGSÚGET_OBJECT_ANNOTATION_ARGSÚPUT_OBJECT_ANNOTATION_ARGSrM   r?   r@   r=   r`   r3   ri   rm   rj   r.   r.   r.   rL   r   "   s@    ÷
O!
ø}
r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	rn   zÜCompleteMultipartUpload variant that also applies tags and annotations.

    After the destination object is finalized, copies/applies tags and
    annotations inline. Errors during apply propagate as task failures.
    c	                 C   sX   |j d|||d|idœ|¤Ž}	|	 d¡}
|	 d¡}|  ||||¡ |  |||||
¡ d S )NÚParts)ÚBucketÚKeyÚUploadIdÚMultipartUploadr+   r,   r.   )Úcomplete_multipart_uploadr<   Ú_apply_tagsÚ_apply_annotations)rA   rB   rO   rP   r]   r_   r5   r0   r-   rK   Ú	dest_etagÚdest_version_idr.   r.   rL   Ú_mainŒ  s   üû


ÿz%CopyCompleteMultipartUploadTask._mainc           
      C   sÔ   |j }| d¡}|dvrd S |dkr7|jd |jd dœt|tjƒ¥}|r)||d< |jjdi |¤Ž dg ¡}nd	d
„ t| dd¡dd�D ƒ}|sJd S |j	|j
d|idœt|tjƒ¥}	|r`||	d< |jdi |	¤Ž d S )Nr   )ÚCOPYr   r¨   rž   rŸ   ©rž   rŸ   r,   ÚTagSetc                 S   s   g | ]	\}}||d œ‘qS ))rŸ   ÚValuer.   r€   r.   r.   rL   Ú
<listcomp>¸  s    ÿÿz?CopyCompleteMultipartUploadTask._apply_tags.<locals>.<listcomp>r!   Ú T)Úkeep_blank_values)rž   rŸ   r!   r.   )r5   r<   r4   r   r   r˜   r8   Úget_object_taggingr   rO   rP   r™   Úput_object_tagging)
rA   rB   r0   r-   r¦   r5   Ú	directiveÚ
src_kwargsÚtag_setÚ
put_kwargsr.   r.   rL   r£   ¥  sL   
þÿý
ÿþ
þþýÿüz+CopyCompleteMultipartUploadTask._apply_tagsc                 C   sv  |j }| d¡dkrd S |jd |jd dœ}|r||d< i |¥t|tjƒ¥}i |¥t|tjƒ¥}	t|tjƒ}
|jj	di |¤Ž}g }i }| dg ¡D ]S}|d }|jj
di |	¤d|i¤Ž}|j|j||d	  ¡ d
œ|
¥}|rr||d< |rx||d< z|jdi |¤Ž | |¡ W qJ ty� } z
|||< W Y d }~qJd }~ww |r¹td|j› d|j› d|› dt| ¡ ƒ› d|› �
ƒ‚d S )Nr    r¨   rž   rŸ   r©   r,   ÚAnnotationsÚAnnotationNameÚAnnotationPayload)rž   rŸ   r¶   r·   ÚObjectIfMatchz#Failed to copy annotations to s3://ú/z. Succeeded: z
. Failed: z
. Errors: r.   )r5   r<   r4   r   r   rš   r›   rœ   r8   Úlist_object_annotationsÚget_object_annotationrO   rP   ÚreadÚput_object_annotationrk   Ú	Exceptionr   ÚlistÚkeys)rA   rB   r0   r-   r¦   r¥   r5   Úsrc_baseÚlist_kwargsÚget_kwargs_baseÚput_passthroughÚlist_responseÚ	succeededÚfailedÚ
annotationÚnameÚpayload_responser´   Úer.   r.   rL   r¤   Í  sŒ   	þÿÿþÿÿþÿ
ÿ
ÿþ
üû€ÿÿÿþ
ýüÿÿz2CopyCompleteMultipartUploadTask._apply_annotationsN)r”   r•   r–   r—   r§   r£   r¤   r.   r.   r.   rL   rn   …  s
    (rn   c                   @   s   e Zd ZdZdd„ ZdS )rW   zTask to do a nonmultipart copyc           	      C   s0   |j d|||dœ|¤Ž |D ]}||d� qdS )aÿ  
        :param client: The client to use when calling PutObject
        :param copy_source: The CopySource parameter to use
        :param bucket: The name of the bucket to copy to
        :param key: The name of the key to copy to
        :param extra_args: A dictionary of any extra arguments that may be
            used in the upload.
        :param callbacks: List of callbacks to call after copy
        :param size: The size of the transfer. This value is passed into
            the callbacks

        )Ú
CopySourcerž   rŸ   ©Úbytes_transferredNr.   )Úcopy_object)	rA   rB   r4   rO   rP   r5   rQ   r1   Úcallbackr.   r.   rL   r§     s   ÿÿÿzCopyObjectTask._mainN©r”   r•   r–   r—   r§   r.   r.   r.   rL   rW     s    rW   c                   @   s   e Zd ZdZ	ddd„ZdS )rl   z)Task to upload a part in a multipart copyNc              
   C   sÚ   z|j d|||||dœ|¤Ž}W n. ty> } z"|j di ¡ d¡}|d }|d }|dkr9td|› d|› d	�ƒ‚‚ d
}~ww |D ]}||	d� qA|d d }||dœ}|
rkd|
 ¡ › �}||d v rk|d | ||< |S )a<  
        :param client: The client to use when calling PutObject
        :param copy_source: The CopySource parameter to use
        :param bucket: The name of the bucket to upload to
        :param key: The name of the key to upload to
        :param upload_id: The id of the upload
        :param part_number: The number representing the part of the multipart
            upload
        :param extra_args: A dictionary of any extra arguments that may be
            used in the upload.
        :param callbacks: List of callbacks to call after copy part
        :param size: The size of the transfer. This value is passed into
            the callbacks
        :param checksum_algorithm: The algorithm that was used to create the multipart
            upload

        :rtype: dict
        :returns: A dictionary representing a part::

            {'Etag': etag_value, 'PartNumber': part_number}

            This value can be appended to a list to be used to complete
            the multipart upload. If a checksum is in the response,
            it will also be included.
        )rÌ   rž   rŸ   r    Ú
PartNumberÚErrorÚCoderŸ   rž   ÚPreconditionFailedzContents of stored object "z" in bucket "z" did not match expected ETag.NrÍ   ÚCopyPartResultr+   )r+   rÒ   ÚChecksumr.   )Úupload_part_copyr   rK   r<   r   Úupper)rA   rB   r4   rO   rP   r]   r[   r5   rQ   r1   r\   rK   rË   Ú
error_codeÚsrc_keyÚ
src_bucketrÐ   r2   Úpart_metadataÚchecksum_memberr.   r.   rL   r§   /  sD   &û
úÿÿ€õ
ÿzCopyPartTask._mainry   rÑ   r.   r.   r.   rL   rl   ,  s    õrl   )r‹   re   Úurllib.parser   Úbotocore.exceptionsr   Ús3transfer.exceptionsr   Ús3transfer.tasksr   r   r   r   Ús3transfer.utilsr	   r
   r   r   r   rn   rW   rl   r.   r.   r.   rL   Ú<module>   s     e 