B dm@spddlZddlZddlZddlmZddlmZddlmZddl m Z m Z ddl m Z mZmZmZmZmZmZmZmZeeZGdddZGd d d eZGd d d eZGd ddeZGdddeZGddde ZGddde ZGdddeZ Gddde Z!Gddde Z"Gddde Z#Gddde Z$Gdd d e Z%Gd!d"d"Z&Gd#d$d$Z'dS)%N)seekable)RetriesExceededError)IN_MEMORY_DOWNLOAD_TAG)SubmissionTaskTask) S3_RETRYABLE_DOWNLOAD_ERRORSCountCallbackInvokerDeferredOpenFileFunctionContainerStreamReaderProgresscalculate_num_partscalculate_range_parameter get_callbacksinvoke_progress_callbacksc@sTeZdZdZddZeddZddZdd Zd d Z d d Z ddZ ddZ dS)DownloadOutputManageraBase manager class for handling various types of files for downloads This class is typically used for the DownloadSubmissionTask class to help determine the following: * Provides the fileobj to write to downloads to * Get a task to complete once everything downloaded has been written The answers/implementations differ for the various types of file outputs that may be accepted. All implementations must subclass and override public methods from this class. cCs||_||_||_dS)N)_osutil_transfer_coordinator _io_executor)selfosutiltransfer_coordinator io_executorr:/tmp/pip-target-jj3kjtpb/lib/python/s3transfer/download.py__init__2szDownloadOutputManager.__init__cCs tddS)aeDetermines if the target for the download is compatible with manager :param download_target: The target for which the upload will write data to. :param osutil: The os utility to be used for the transfer :returns: True if the manager can handle the type of target specified otherwise returns False. zmust implement is_compatible()N)NotImplementedError)clsdownload_targetrrrr is_compatible7s z#DownloadOutputManager.is_compatiblecCsdS)zGet the tag (if any) to associate all GetObjectTasks :rtype: s3transfer.futures.TaskTag :returns: The tag to associate all GetObjectTasks with Nr)rrrrget_download_task_tagEsz+DownloadOutputManager.get_download_task_tagcCs tddS)aGet file-like object to use for io writes in the io executor :type transfer_future: s3transfer.futures.TransferFuture :param transfer_future: The future associated with upload request returns: A file-like object to write to z*must implement get_fileobj_for_io_writes()N)r)rtransfer_futurerrrget_fileobj_for_io_writesMsz/DownloadOutputManager.get_fileobj_for_io_writescCs|j|j||||dS)aQueue IO write for submission to the IO executor. This method accepts an IO executor and information about the downloaded data, and handles submitting this to the IO executor. This method may defer submission to the IO executor if necessary. N)rsubmitrget_io_write_task)rfileobjdataoffsetrrrqueue_file_io_taskWs z(DownloadOutputManager.queue_file_io_taskcCst|j|||ddS)aGet an IO write task for the requested set of data This task can be ran immediately or be submitted to the IO executor for it to run. :type fileobj: file-like object :param fileobj: The file-like object to write to :type data: bytes :param data: The data to write out :type offset: integer :param offset: The offset to write the data to in the file-like object :returns: An IO task to be used to write data to a file-like object )r$r%r&) main_kwargs) IOWriteTaskr)rr$r%r&rrrr#ds z'DownloadOutputManager.get_io_write_taskcCs tddS)aGet the final io task to complete the download This is needed because based on the architecture of the TransferManager the final tasks will be sent to the IO executor, but the executor needs a final task for it to signal that the transfer is done and all done callbacks can be run. :rtype: s3transfer.tasks.Task :returns: A final task to completed in the io executor z"must implement get_final_io_task()N)r)rrrrget_final_io_task~s z'DownloadOutputManager.get_final_io_taskcCs$t|d|jjd}|j|j|S)Nwb)modeZ open_function)r ropenradd_failure_cleanupclose)rfilenamefrrr_get_fileobj_from_filenamesz0DownloadOutputManager._get_fileobj_from_filenameN) __name__ __module__ __qualname____doc__r classmethodrrr!r'r#r*r2rrrrr$s     rcs@eZdZfddZeddZddZddZd d ZZ S) DownloadFilenameOutputManagercs&t|||d|_d|_d|_dS)N)superr_final_filename_temp_filename _temp_fileobj)rrrr) __class__rrrsz&DownloadFilenameOutputManager.__init__cCs t|tS)N) isinstancestr)rrrrrrrsz+DownloadFilenameOutputManager.is_compatiblecCs.|jjj}||_|j||_||_|jS)N) meta call_argsr$r:rZget_temp_filenamer;_get_temp_fileobjr<)rr r$rrrr!s   z7DownloadFilenameOutputManager.get_fileobj_for_io_writescCst|j|j|j|jdddS)N)r$final_filenamerT)rr(is_final)IORenameFileTaskrr<r:r)rrrrr*s z/DownloadFilenameOutputManager.get_final_io_taskcCs$||j}|j|jj|j|S)N)r2r;rr.rZ remove_file)rr1rrrrBs z/DownloadFilenameOutputManager._get_temp_fileobj) r3r4r5rr7rr!r*rB __classcell__rr)r=rr8s   r8c@s(eZdZeddZddZddZdS)DownloadSeekableOutputManagercCst|S)N)r)rrrrrrrsz+DownloadSeekableOutputManager.is_compatiblecCs |jjjS)N)r@rAr$)rr rrrr!sz7DownloadSeekableOutputManager.get_fileobj_for_io_writescCs t|jdS)N)r)CompleteDownloadNOOPTaskr)rrrrr*sz/DownloadSeekableOutputManager.get_final_io_taskN)r3r4r5r7rr!r*rrrrrGs rGcsVeZdZdfdd ZeddZddZdd Zd d Zfd d Z ddZ Z S) DownloadNonSeekableOutputManagerNcs2t||||dkrt}||_t|_dS)N)r9r DeferQueue _defer_queue threadingLock_io_submit_lock)rrrr defer_queue)r=rrrs z)DownloadNonSeekableOutputManager.__init__cCs t|dS)Nwrite)hasattr)rrrrrrrsz.DownloadNonSeekableOutputManager.is_compatiblecCstS)N)r)rrrrrsz6DownloadNonSeekableOutputManager.get_download_task_tagcCs |jjjS)N)r@rAr$)rr rrrr!sz:DownloadNonSeekableOutputManager.get_fileobj_for_io_writescCs t|jdS)N)r)rHr)rrrrr*sz2DownloadNonSeekableOutputManager.get_final_io_taskc s\|jL|j||}x6|D].}|d}td|d|t|||qWWdQRXdS)Nr%z%Queueing IO offset %s for fileobj: %sr&)rNrKrequest_writesloggerdebugr9r')rr$r%r&writesrP)r=rrr's z3DownloadNonSeekableOutputManager.queue_file_io_taskcCst|j||ddS)N)r$r%)r()IOStreamingWriteTaskr)rr$r%r&rrrr#sz2DownloadNonSeekableOutputManager.get_io_write_task)N) r3r4r5rr7rrr!r*r'r#rFrr)r=rrIs  rIcs:eZdZd fdd ZeddZddZdd ZZS) $DownloadSpecialFilenameOutputManagerNcst||||d|_dS)N)r9r_fileobj)rrrrrO)r=rrrs z-DownloadSpecialFilenameOutputManager.__init__cCst|to||S)N)r>r?Zis_special_file)rrrrrrrsz2DownloadSpecialFilenameOutputManager.is_compatiblecCs|jjj}|||_|jS)N)r@rAr$r2rX)rr r0rrrr! s  z>DownloadSpecialFilenameOutputManager.get_fileobj_for_io_writescCst|jdd|jidS)NTr$)rrDr() IOCloseTaskrrX)rrrrr*sz6DownloadSpecialFilenameOutputManager.get_final_io_task)N) r3r4r5rr7rr!r*rFrr)r=rrWs rWc@sBeZdZdZddZdddZddZd d Zd d Zd dZ dS)DownloadSubmissionTaskz/Task for submitting tasks to execute a downloadcCsLttttg}|jjj}x|D]}|||r|SqWtd |t |dS)aRetrieves a class for managing output for a download :type transfer_future: s3transfer.futures.TransferFuture :param transfer_future: The transfer future for the request :type osutil: s3transfer.utils.OSUtils :param osutil: The os utility associated to the transfer :rtype: class of DownloadOutputManager :returns: The appropriate class to use for managing a specific type of input for downloads. z'Output {} of type: {} is not supported.N) rWr8rGrIr@rAr$r RuntimeErrorformattype)rr rZdownload_manager_resolver_chainr$Zdownload_manager_clsrrr _get_download_output_manager_clss   z7DownloadSubmissionTask._get_download_output_manager_clsNc Cs|jjdkrD|jf|jjj|jjjd|jjj}|j|d|||||j |} |jj|j kr| |||||| ||n| |||||| ||dS)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 io_executor: s3transfer.futures.BoundedExecutor :param io_executor: The io 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 :type bandwidth_limiter: s3transfer.bandwidth.BandwidthLimiter :param bandwidth_limiter: The bandwidth limiter to use when downloading streams N)BucketKeyZ ContentLength) r@sizeZ head_objectrAbucketkey extra_argsZprovide_transfer_sizer^rZmultipart_threshold_submit_download_request_submit_ranged_download_request) rclientconfigrrequest_executorrr bandwidth_limiterresponsedownload_output_managerrrr_submit;s:$    zDownloadSubmissionTask._submitc Csp|jj} ||} t|d} |} |} |jj|t|j|| j | j | | j | |j ||j |d | gd| ddS)Nprogress) rgrbrcr$rd callbacks max_attemptsrl io_chunksizerj)rr(done_callbacks)tag)r@rAr!rrr*rr"ImmediatelyWriteIOGetObjectTaskrbrcrdnum_download_attemptsrq)rrgrhrrirrlr rjrAr$progress_callbacksget_object_tag final_taskrrrres,  z/DownloadSubmissionTask._submit_download_requestc Cs|jj} t|d} ||} |j} t|jj| } |}t| ||}x~t | D]r}t | || }d|i}| | j ||jj|t|j|| j| j| || |j|| ||j|d |jgd|dqRW|dS)NrnRange) rgrbrcr$rdrorp start_indexrlrqrj)rr(rr)rs)r@rArr!Zmultipart_chunksizer rarr&_get_final_io_task_submission_callbackranger updaterd incrementrr" GetObjectTaskrbrcrurqZ decrementfinalize)rrgrhrrirrlr rjrArvr$ part_size num_partsrwZfinalize_download_invokeriZrange_parameterrdrrrrfsD       z6DownloadSubmissionTask._submit_ranged_download_requestcCs|}t|jj||S)N)r*r rr")rZdownload_managerrrxrrrr{sz=DownloadSubmissionTask._get_final_io_task_submission_callbackcCs:||}||dkrd}n ||d}d|d|}|S)Nzbytes=-r)rrZ part_indexrZ start_rangeZ end_rangeZ range_paramrrr_calculate_range_param s   z-DownloadSubmissionTask._calculate_range_param)N) r3r4r5r6r^rmrerfr{rrrrrrZs& E2GrZc@seZdZdddZddZdS)rrNc  Csd} xt|D]} y| }|jf||d|}t|d|}| rN| ||j}t|| }x8|D]0}|js||||||t|7}q^dSq^WdSt k r}z,t j d|| |dd|} t || |wWdd}~XYqXqWt | dS)aDownloads an object and places content into io queue :param client: The client to use when calling GetObject :param bucket: The bucket to download from :param key: The key to download from :param fileobj: The file handle to write content to :param exta_args: Any extra arguments to include in GetObject request :param callbacks: List of progress callbacks to invoke on download :param max_attempts: The number of retries to do when downloading :param download_output_manager: The download output manager associated with the current download. :param io_chunksize: The size of each io chunk to read from the download stream and queue in the io queue. :param start_index: The location in the file to start writing the content of the key to. :param bandwidth_limiter: The bandwidth limiter to use when throttling the downloading of data in streams. N)r_r`ZBodyzCRetrying exception caught (%s), retrying request, (attempt %s / %s)T)exc_info)r|Z get_objectr Zget_bandwith_limited_streamrDownloadChunkIteratordone _handle_iolenrrSrTrr)rrgrbrcr$rdrorprlrqrzrjZlast_exceptionrZ current_indexrkZstreaming_bodychunkschunkerrr_mainsD       zGetObjectTask._maincCs||||dS)N)r')rrlr$rindexrrrriszGetObjectTask._handle_io)rN)r3r4r5rrrrrrrs  Hrc@seZdZdZddZdS)rtaGetObjectTask that immediately writes to the provided file object This is useful for downloads where it is known only one thread is downloading the object so there is no reason to go through the overhead of using an IO queue and executor. cCs||||}|dS)N)r#)rrlr$rrtaskrrrrusz*ImmediatelyWriteIOGetObjectTask._handle_ioN)r3r4r5r6rrrrrrtmsrtc@seZdZddZdS)r)cCs||||dS)zPulls off an io queue to write contents to a file :param fileobj: The file handle to write content to :param data: The data to write :param offset: The offset to write the data to. N)seekrP)rr$r%r&rrrr{s zIOWriteTask._mainN)r3r4r5rrrrrr)zsr)c@seZdZdZddZdS)rVz/Task for writing data to a non-seekable stream.cCs||dS)zWrite data to a fileobj. Data will be written directly to the fileobj without any prior seeking. :param fileobj: The fileobj to write content to :param data: The data to write N)rP)rr$r%rrrrs zIOStreamingWriteTask._mainN)r3r4r5r6rrrrrrVsrVc@seZdZdZddZdS)rEaA task to rename a temporary file to its final filename :param fileobj: The file handle that content was written to. :param final_filename: The final name of the file to rename to upon completion of writing the contents. :param osutil: OS utility cCs|||j|dS)N)r/Z rename_filename)rr$rCrrrrrszIORenameFileTask._mainN)r3r4r5r6rrrrrrEsrEc@seZdZdZddZdS)rYziA task to close out a file once the download is complete. :param fileobj: The fileobj to close. cCs |dS)N)r/)rr$rrrrszIOCloseTask._mainN)r3r4r5r6rrrrrrYsrYcs*eZdZdZdfdd ZddZZS) rHzA NOOP task to serve as an indicator that the download is complete Note that the default for is_final is set to True because this should always be the last task. NTcstj|||||ddS)N)rr(pending_main_kwargsrrrD)r9r)rrr(rrrrD)r=rrrs z!CompleteDownloadNOOPTask.__init__cCsdS)Nr)rrrrrszCompleteDownloadNOOPTask._main)NNNT)r3r4r5r6rrrFrr)r=rrHs  rHc@s(eZdZddZddZddZeZdS)rcCs||_||_d|_dS)zIterator to chunk out a downloaded S3 stream :param body: A readable file-like object :param chunksize: The amount to read each time rN)_body _chunksize _num_reads)rbody chunksizerrrrszDownloadChunkIterator.__init__cCs|S)Nr)rrrr__iter__szDownloadChunkIterator.__iter__cCs<|j|j}|jd7_|r$|S|jdkr2|StdS)Nr)rreadrr StopIteration)rrrrr__next__s zDownloadChunkIterator.__next__N)r3r4r5rrrnextrrrrrs  rc@s eZdZdZddZddZdS)rJa"IO queue that defers write requests until they are queued sequentially. This class is used to track IO data for a *single* fileobj. You can send data to this queue, and it will defer any IO write requests until it has the next contiguous block available (starting at 0). cCsg|_t|_d|_dS)Nr)_writesset_pending_offsets _next_offset)rrrrrszDeferQueue.__init__cCs||jkrgSg}||jkr gSt|j||f|j|xh|jr|jdd|jkrt|j}||d|dd|j|d|jt |d7_q@W|S)aRequest any available writes given new incoming data. You call this method by providing new data along with the offset associated with the data. If that new data unlocks any contiguous writes that can now be submitted, this method will return all applicable writes. This is done with 1 method call so you don't have to make two method calls (put(), get()) which acquires a lock each method call. rr)r&r%) rrheapqheappushraddheappopappendremover)rr&r%rUZ next_writerrrrRs    zDeferQueue.request_writesN)r3r4r5r6rrRrrrrrJsrJ)(rloggingrLZs3transfer.compatrZs3transfer.exceptionsrZs3transfer.futuresrZs3transfer.tasksrrZs3transfer.utilsrrr r r r r rr getLoggerr3rSrr8rGrIrWrZrrtr)rVrErYrHrrJrrrr s0   , q(/{Y