U n^R@sBddlmZddlmZddlZddlZddlZddlZddlm Z ddlm Z ddl m Z m Z ddlmZddlmZeeZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeddgZedZ ed Z!dS)!)futures) namedtupleN)MAXINT)six)CancelledErrorTransferNotDoneError)FunctionContainer) TaskSemaphorec@s0eZdZeddZddZddZddZd S) BaseTransferFuturecCs tddS)z-The metadata associated to the TransferFuturemetaNNotImplementedErrorselfrn/private/var/folders/sd/whlwsn6x1_qgglc0mjv25_695qk2gl/T/pip-install-4zq3fp6i/s3transfer/s3transfer/futures.pyr szBaseTransferFuture.metacCs tddS)zmDetermines if a TransferFuture has completed :returns: True if completed. False, otherwise. zdone()Nr rrrrdone$szBaseTransferFuture.donecCs tddS)Waits until TransferFuture is done and returns the result If the TransferFuture succeeded, it will return the result. If the TransferFuture failed, it will raise the exception associated to the failure. zresult()Nr rrrrresult+szBaseTransferFuture.resultcCs tddS)z6Cancels the request associated with the TransferFuturezcancel()Nr rrrrcancel4szBaseTransferFuture.cancelN)__name__ __module__ __qualname__propertyr rrrrrrrr s   r c@s0eZdZeddZeddZeddZdS)BaseTransferMetacCs tddS)*The call args used in the transfer request call_argsNr rrrrr:szBaseTransferMeta.call_argscCs tddS)The unique id of the transfer transfer_idNr rrrrr?szBaseTransferMeta.transfer_idcCs tddS).A dictionary that requesters can store data in user_contextNr rrrrr DszBaseTransferMeta.user_contextN)rrrrrrr rrrrr9s   rc@sBeZdZdddZeddZddZdd Zd d Zd d Z dS)TransferFutureNcCs0||_|dkrt|_||_|dkr,t|_dS)aThe future associated to a submitted transfer request :type meta: TransferMeta :param meta: The metadata associated to the request. This object is visible to the requester. :type coordinator: TransferCoordinator :param coordinator: The coordinator associated to the request. This object is not visible to the requester. N)_meta TransferMeta _coordinatorTransferCoordinator)rr Z coordinatorrrr__init__Ks zTransferFuture.__init__cCs|jSN)r"rrrrr ^szTransferFuture.metacCs |jSr')r$rrrrrrbszTransferFuture.donec Cs@z |jWStk r:}z||W5d}~XYnXdSr')r$rKeyboardInterruptr)rerrrres  zTransferFuture.resultcCs|jdSr')r$rrrrrroszTransferFuture.cancelcCs$|std|jj|dddS)z!Sets the exception on the future.z?set_exception can only be called once the transfer is complete.T)overrideN)rrr$ set_exception)r exceptionrrrr+rs zTransferFuture.set_exception)NN) rrrr&rr rrrr+rrrrr!Js   r!c@sReZdZdZdddZeddZeddZed d Zed d Z d dZ dS)r#z'Holds metadata about the TransferFutureNcCs||_||_d|_i|_dSr') _call_args _transfer_id_size _user_context)rrrrrrr&}szTransferMeta.__init__cCs|jS)r)r-rrrrrszTransferMeta.call_argscCs|jS)r)r.rrrrrszTransferMeta.transfer_idcCs|jS)z)The size of the transfer request if knownr/rrrrsizeszTransferMeta.sizecCs|jS)r)r0rrrrr szTransferMeta.user_contextcCs ||_dS)zA method to provide the size of a transfer request By providing this value, the TransferManager will not try to call HeadObject or use the use OS to determine the size of the transfer. Nr1)rr2rrrprovide_transfer_sizesz"TransferMeta.provide_transfer_size)NN) rrr__doc__r&rrrr2r r3rrrrr#{s     r#c@seZdZdZd5ddZddZeddZed d Zed d Z ed dZ ddZ d6ddZ ddZ defddZddZddZddZd7dd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdS)8r%z*A helper class for managing TransferFutureNcCsb||_d|_d|_d|_t|_g|_g|_t |_ t |_ t |_ t |_t |_dS)N not-started)r_status_result _exceptionset_associated_futures_failure_cleanups_done_callbacks threadingEvent _done_eventLock_lock_associated_futures_lock_done_callbacks_lock_failure_cleanups_lock)rrrrrr&s    zTransferCoordinator.__init__cCsd|jj|jfS)Nz%s(transfer_id=%s)) __class__rrrrrr__repr__szTransferCoordinator.__repr__cCs|jSr')r8rrrrr,szTransferCoordinator.exceptionc Cs*|jt|jW5QRSQRXdS)zThe list of futures associated to the inprogress TransferFuture Once the transfer finishes this list becomes empty as the transfer is considered done and there should be no running futures left. N)rBcopyr:rrrrassociated_futuressz&TransferCoordinator.associated_futurescCs|jS)z;The list of callbacks to call when the TransferFuture fails)r;rrrrfailure_cleanupssz$TransferCoordinator.failure_cleanupscCs|jS)aThe status of the TransferFuture The currently supported states are: * not-started - Has yet to start. If in this state, a transfer can be canceled immediately and nothing will happen. * queued - SubmissionTask is about to submit tasks * running - Is inprogress. In-progress as of now means that the SubmissionTask that runs the transfer is being executed. So there is no guarantee any transfer requests had been made to S3 if this state is reached. * cancelled - Was cancelled * failed - An exception other than CancelledError was thrown * success - No exceptions were thrown and is done. )r6rrrrstatusszTransferCoordinator.statusc Cs(|jd|_||_d|_W5QRXdS)aSet a result for the TransferFuture Implies that the TransferFuture succeeded. This will always set a result because it is invoked on the final task where there is only ever one final task and it is ran at the very end of a transfer process. So if a result is being set for this final task, the transfer succeeded even if something came a long and canceled the transfer on the final task. Nsuccess)rAr8r7r6rrrrr set_results zTransferCoordinator.set_resultFc Cs.|j|r|r ||_d|_W5QRXdS)zSet an exception for the TransferFuture Implies the TransferFuture failed. :param exception: The exception that cause the transfer to fail. :param override: If True, override any existing state. failedN)rArr8r6)rr,r*rrrr+s z!TransferCoordinator.set_exceptioncCs|jt|jr|j|jS)r)r?waitrr8r7rrrrrs zTransferCoordinator.resultc CsZ|jJ|sLd}td|||||_|jdkr:d}d|_|rL|W5QRXdS)zCancels the TransferFuture :param msg: The message to attach to the cancellation :param exc_type: The type of exception to set for the cancellation Fz%s cancel(%s) calledr5T cancelledN)rArloggerdebugr8r6 announce_done)rmsgexc_typeZshould_announce_donerrrr s  zTransferCoordinator.cancelcCs|ddS)z+Sets the TransferFutrue's status to runningZqueuedN_transition_to_non_done_staterrrrset_status_to_queuedsz(TransferCoordinator.set_status_to_queuedcCs|ddS)z+Sets the TransferFuture's status to runningZrunningNrWrrrrset_status_to_running!sz)TransferCoordinator.set_status_to_runningc Cs6|j&|r"td|j|f||_W5QRXdS)Nz=Unable to transition from done state %s to non-done state %s.)rAr RuntimeErrorrJr6)rZ desired_staterrrrX%sz1TransferCoordinator._transition_to_non_done_statecCsDtd|||jf|j||d}|||t|j||S)aSubmits a task to a provided executor :type executor: s3transfer.futures.BoundedExecutor :param executor: The executor to submit the callable to :type task: s3transfer.tasks.Task :param task: The task to submit to the executor :type tag: s3transfer.futures.TaskTag :param tag: A tag to associate to the submitted task :rtype: concurrent.futures.Future :returns: A future representing the submitted task z;Submitting task %s to executor %s for transfer request: %s.)tag)rRrSrsubmitadd_associated_futureadd_done_callbackrremove_associated_future)rexecutortaskr\futurerrrr]-s  zTransferCoordinator.submitcCs |jdkS)zDetermines if a TransferFuture has completed :returns: False if status is equal to 'failed', 'cancelled', or 'success'. True, otherwise )rNrQrK)rJrrrrrHszTransferCoordinator.donec Cs"|j|j|W5QRXdS)z6Adds a future to be associated with the TransferFutureN)rBr:addrrcrrrr^Psz)TransferCoordinator.add_associated_futurec Cs"|j|j|W5QRXdS)z4Removes a future's association to the TransferFutureN)rBr:removererrrr`Usz,TransferCoordinator.remove_associated_futurec Os.|j|jt|f||W5QRXdS)z7Add a done callback to be invoked when transfer is doneN)rCr<appendrrfunctionargskwargsrrrr_Zsz%TransferCoordinator.add_done_callbackc Os.|j|jt|f||W5QRXdS)z$Adds a callback to call upon failureN)rDr;rgrrhrrradd_failure_cleanupasz'TransferCoordinator.add_failure_cleanupcCs(|jdkr||j|dS)aMAnnounce that future is done running and run associated callbacks This will run any failure cleanups if the transfer failed if not they have not been run, allows the result() to be unblocked, and will run any done callbacks associated to the TransferFuture if they have not already been ran. rKN)rJ_run_failure_cleanupsr?r9_run_done_callbacksrrrrrTgs  z!TransferCoordinator.announce_donec Cs(|j||jg|_W5QRXdSr')rC_run_callbacksr<rrrrrnts z'TransferCoordinator._run_done_callbacksc Cs(|j||jg|_W5QRXdSr')rDrorIr;rrrrrm|s z)TransferCoordinator._run_failure_cleanupscCs|D]}||qdSr') _run_callback)r callbackscallbackrrrrosz"TransferCoordinator._run_callbackscCs6z |Wn&tk r0tjd|ddYnXdS)NzException raised in %s.T)exc_info) ExceptionrRrS)rrrrrrrps z!TransferCoordinator._run_callback)N)F)N)rrrr4r&rFrr,rHrIrJrMr+rrrrYrZrXr]rr^r`r_rlrTrnrmrorprrrrr%s:        r%c@s0eZdZejZd ddZd ddZd ddZdS) BoundedExecutorNcCs6||_|dkr|j}||jd|_t||_||_dS)aDAn executor implentation that has a maximum queued up tasks The executor will block if the number of tasks that have been submitted and is currently working on is past its maximum. :params max_size: The maximum number of inflight futures. An inflight future means that the task is either queued up or is currently being executed. A size of None or 0 means that the executor will have no bound in terms of the number of inflight futures. :params max_num_threads: The maximum number of threads the executor uses. :type tag_semaphores: dict :params tag_semaphores: A dictionary where the key is the name of the tag and the value is the semaphore to use when limiting the number of tasks the executor is processing at a time. :type executor_cls: BaseExecutor :param underlying_executor_cls: The executor class that get bounded by this executor. If None is provided, the concurrent.futures.ThreadPoolExecutor class is used. N) max_workers)Z_max_num_threads EXECUTOR_CLS _executorr _semaphore_tag_semaphores)rmax_sizeZmax_num_threadsZtag_semaphoresZ executor_clsrrrr&s  zBoundedExecutor.__init__TcCsP|j}|r|j|}||j|}t|j|j|}t|j|}| ||S)a1Submit a task to complete :type task: s3transfer.tasks.Task :param task: The task to run __call__ on :type tag: s3transfer.futures.TaskTag :param tag: An optional tag to associate to the task. This is used to override which semaphore to use. :type block: boolean :param block: True if to wait till it is possible to submit a task. False, if not to wait and raise an error if not able to submit a task. :returns: The future assocaited to the submitted task ) ryrzacquirerrreleaseExecutorFuturerxr]r_)rrbr\blockZ semaphoreZ acquire_tokenZrelease_callbackrcrrrr]s  zBoundedExecutor.submitcCs|j|dSr')rxshutdownrrOrrrrszBoundedExecutor.shutdown)NN)NT)T) rrrrZThreadPoolExecutorrwr&r]rrrrrrus  %ruc@s,eZdZddZddZddZddZd S) r~cCs ||_dS)aA future returned from the executor Currently, it is just a wrapper around a concurrent.futures.Future. However, this can eventually grow to implement the needed functionality of concurrent.futures.Future if we move off of the library and not affect the rest of the codebase. :type future: concurrent.futures.Future :param future: The underlying future N)_futurererrrr&s zExecutorFuture.__init__cCs |jSr')rrrrrrrszExecutorFuture.resultcsfdd}|j|dS)aAdds a callback to be completed once future is done :parm fn: A callable that takes no arguments. Note that is different than concurrent.futures.Future.add_done_callback that requires a single argument for the future. csSr'r)Zfuture_passed_to_callbackfnrr done_callbacksz7ExecutorFuture.add_done_callback..done_callbackN)rr_)rrrrrrr_s z ExecutorFuture.add_done_callbackcCs |jSr')rrrrrrrszExecutorFuture.doneN)rrrr&rr_rrrrrr~s r~c@s,eZdZdZd ddZddZd dd ZdS) BaseExecutorzABase Executor class implementation needed to work with s3transferNcCsdSr'r)rrvrrrr&szBaseExecutor.__init__cOs tddS)Nzsubmit()r )rrrjrkrrrr]szBaseExecutor.submitTcCs tddS)Nz shutdown()r rrrrrszBaseExecutor.shutdown)N)T)rrrr4r&r]rrrrrrs rc@s"eZdZdZddZdddZdS) NonThreadedExecutorz@A drop-in replacement non-threaded version of ThreadPoolExecutorc Osht}z|||}||WnDtk rbtdd\}}td||||||YnX|S)Nz0Setting exception for %s to %s with traceback %s)NonThreadedExecutorFuturerMrtsysrsrRrSset_exception_info)rrrjrkrcrr)tbrrrr]s zNonThreadedExecutor.submitTcCsdSr'rrrrrrszNonThreadedExecutor.shutdownN)T)rrrr4r]rrrrrr src@sReZdZdZddZddZddZdd d Zd d Zd dZ ddZ ddZ dS)rzThe Future returned from NonThreadedExecutor Note that this future is **not** thread-safe as it is being used from the context of a non-threaded environment. cCs"d|_d|_d|_d|_g|_dS)NF)r7r8 _traceback_doner<rrrrr&&s z"NonThreadedExecutorFuture.__init__cCs||_|dSr')r7 _set_donerLrrrrM-sz$NonThreadedExecutorFuture.set_resultcCs||_||_|dSr')r8rr)rr, tracebackrrrr1sz,NonThreadedExecutorFuture.set_exception_infoNcCs$|jrtt|j|j|j|jSr')r8rreraisetyperr7)rtimeoutrrrr6sz NonThreadedExecutorFuture.resultcCs&d|_|jD]}||q g|_dS)NT)rr<_invoke_done_callbackrrrrrr<s  z#NonThreadedExecutorFuture._set_donecCs||Sr'rrrrrrBsz/NonThreadedExecutorFuture._invoke_done_callbackcCs|jSr')rrrrrrEszNonThreadedExecutorFuture.donecCs"|jr||n |j|dSr')rrr<rg)rrrrrr_Hs z+NonThreadedExecutorFuture.add_done_callback)N) rrrr4r&rMrrrrrr_rrrrr s rTaskTagnameZin_memory_uploadZin_memory_download)"Z concurrentr collectionsrrGloggingrr=Zs3transfer.compatrrZs3transfer.exceptionsrrZs3transfer.utilsrr getLoggerrrRobjectr rr!r#r%rur~rrrrZIN_MEMORY_UPLOAD_TAGZIN_MEMORY_DOWNLOAD_TAGrrrr s2       1&qL# /