U n‰^ºRã@sBddlmZddlmZddlZddlZddlZddlZddlm Z ddlm Z ddl m Z m Z ddlmZddlmZe e¡ZGd d „d eƒZGd d „d eƒZGd d„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZeddgƒZedƒZ ed ƒZ!dS)!é)Úfutures)Ú namedtupleN)ÚMAXINT)Úsix)ÚCancelledErrorÚTransferNotDoneError)ÚFunctionContainer)Ú TaskSemaphorec@s0eZdZedd„ƒZdd„Zdd„Zdd„Zd S) ÚBaseTransferFuturecCs tdƒ‚dS)z-The metadata associated to the TransferFutureÚmetaN©ÚNotImplementedError©Úself©rún/private/var/folders/sd/whlwsn6x1_qgglc0mjv25_695qk2gl/T/pip-install-4zq3fp6i/s3transfer/s3transfer/futures.pyr szBaseTransferFuture.metacCs tdƒ‚dS)zmDetermines if a TransferFuture has completed :returns: True if completed. False, otherwise. zdone()Nr rrrrÚdone$szBaseTransferFuture.donecCs tdƒ‚dS)úì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 rrrrÚresult+szBaseTransferFuture.resultcCs tdƒ‚dS)z6Cancels the request associated with the TransferFuturezcancel()Nr rrrrÚcancel4szBaseTransferFuture.cancelN)Ú__name__Ú __module__Ú __qualname__Úpropertyr rrrrrrrr s   r c@s0eZdZedd„ƒZedd„ƒZedd„ƒZdS)ÚBaseTransferMetacCs tdƒ‚dS)ú*The call args used in the transfer requestÚ call_argsNr rrrrr:szBaseTransferMeta.call_argscCs tdƒ‚dS)úThe unique id of the transferÚ transfer_idNr rrrrr?szBaseTransferMeta.transfer_idcCs tdƒ‚dS)ú.A dictionary that requesters can store data inÚ user_contextNr rrrrr DszBaseTransferMeta.user_contextN)rrrrrrr rrrrr9s   rc@sBeZdZddd„Zedd„ƒZdd„Zdd „Zd d „Zd d „Z dS)ÚTransferFutureNcCs0||_|dkrtƒ|_||_|dkr,tƒ|_dS)a‚The 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Ú _coordinatorÚTransferCoordinator)rr Z coordinatorrrrÚ__init__Ks zTransferFuture.__init__cCs|jS©N)r"rrrrr ^szTransferFuture.metacCs |j ¡Sr')r$rrrrrrbszTransferFuture.donec Cs@z |j ¡WStk r:}z| ¡|‚W5d}~XYnXdSr')r$rÚKeyboardInterruptr)rÚerrrres  zTransferFuture.resultcCs|j ¡dSr')r$rrrrrroszTransferFuture.cancelcCs$| ¡stdƒ‚|jj|dd�dS)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@sReZdZdZddd„Zedd„ƒZedd„ƒZed d „ƒZed d „ƒZ d d„Z 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-rrrrrƒszTransferMeta.call_argscCs|jS)r)r.rrrrrˆszTransferMeta.transfer_idcCs|jS)z)The size of the transfer request if known©r/rrrrÚsize�szTransferMeta.sizecCs|jS)r)r0rrrrr ’szTransferMeta.user_contextcCs ||_dS)zÚA 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)rr2rrrÚprovide_transfer_size—sz"TransferMeta.provide_transfer_size)NN) rrrÚ__doc__r&rrrr2r r3rrrrr#{s     r#c@sìeZdZdZd5dd„Zdd„Zedd„ƒZed d „ƒZed d „ƒZ ed d„ƒZ dd„Z d6dd„Z dd„Z defdd„Zdd„Zdd„Zdd„Zd7dd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„ZdS)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Ú _exceptionÚsetÚ_associated_futuresÚ_failure_cleanupsÚ_done_callbacksÚ threadingÚEventÚ _done_eventÚLockÚ_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__±sÿzTransferCoordinator.__repr__cCs|jSr')r8rrrrr,µszTransferCoordinator.exceptionc Cs*|j�t |j¡W5QR£SQRXdS)zÜThe 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)rBÚcopyr:rrrrÚassociated_futures¹sz&TransferCoordinator.associated_futurescCs|jS)z;The list of callbacks to call when the TransferFuture fails)r;rrrrÚfailure_cleanupsÆsz$TransferCoordinator.failure_cleanupscCs|jS)aßThe 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. )r6rrrrÚstatusËszTransferCoordinator.statusc Cs(|j�d|_||_d|_W5QRXdS)a¾Set 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. NÚsuccess)rAr8r7r6©rrrrrÚ set_resultÝs zTransferCoordinator.set_resultFc Cs.|j�| ¡r|r ||_d|_W5QRXdS)zåSet 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|j t¡|jr|j‚|jS)r)r?Úwaitrr8r7rrrrrùs zTransferCoordinator.resultÚc CsZ|j�J| ¡sLd}t d||¡||ƒ|_|jdkr:d}d|_|rL| ¡W5QRXdS)z­Cancels 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)rArÚloggerÚdebugr8r6Ú announce_done)rÚmsgÚexc_typeZshould_announce_donerrrr s  zTransferCoordinator.cancelcCs| d¡dS)z+Sets the TransferFutrue's status to runningZqueuedN©Ú_transition_to_non_done_staterrrrÚset_status_to_queuedsz(TransferCoordinator.set_status_to_queuedcCs| d¡dS)z+Sets the TransferFuture's status to runningZrunningNrWrrrrÚset_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%sÿÿz1TransferCoordinator._transition_to_non_done_statecCsDt d|||jf¡|j||d�}| |¡| t|j|ƒ¡|S)aßSubmits 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)rRrSrÚsubmitÚadd_associated_futureÚadd_done_callbackrÚremove_associated_future)rÚexecutorÚtaskr\Úfuturerrrr]-sÿÿ  ÿzTransferCoordinator.submitcCs |jdkS)z¥Determines 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:Úadd©rrcrrrr^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�|j t|f|ž|Ž¡W5QRXdS)z7Add a done callback to be invoked when transfer is doneN)rCr<Úappendr©rÚfunctionÚargsÚkwargsrrrr_Zsÿz%TransferCoordinator.add_done_callbackc Os.|j�|j t|f|ž|Ž¡W5QRXdS)z$Adds a callback to call upon failureN)rDr;rgrrhrrrÚadd_failure_cleanupasÿz'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�| |j¡g|_W5QRXdSr')rCÚ_run_callbacksr<rrrrrnts z'TransferCoordinator._run_done_callbacksc Cs(|j�| |j¡g|_W5QRXdSr')rDrorIr;rrrrrm|s z)TransferCoordinator._run_failure_cleanupscCs|D]}| |¡qdSr')Ú _run_callback)rÚ callbacksÚcallbackrrrro„sz"TransferCoordinator._run_callbackscCs6z |ƒWn&tk r0tjd|dd�YnXdS)NzException raised in %s.T)Úexc_info)Ú ExceptionrRrS)rrrrrrrpˆs z!TransferCoordinator._run_callback)N)F)N)rrrr4r&rFrr,rHrIrJrMr+rrrrYrZrXr]rr^r`r_rlrTrnrmrorprrrrr%¡s:        r%c@s0eZdZejZd dd„Zd dd„Zd dd„ZdS) Ú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)rÚmax_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 ) ryrzÚacquirerrÚreleaseÚExecutorFuturerxr]r_)rrbr\ÚblockZ semaphoreZ acquire_tokenZrelease_callbackrcrrrr]´s ÿ zBoundedExecutor.submitcCs|j |¡dSr')rxÚshutdown©rrOrrrr€ÙszBoundedExecutor.shutdown)NN)NT)T) rrrrZThreadPoolExecutorrwr&r]r€rrrrru‘s ÿ %ruc@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) r~cCs ||_dS)a›A 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 |j ¡Sr')r‚rrrrrrëszExecutorFuture.resultcs‡fdd„}|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. csˆƒSr'r)Zfuture_passed_to_callback©ÚfnrrÚ done_callbackøsz7ExecutorFuture.add_done_callback..done_callbackN)r‚r_)rr„r…rrƒrr_îs z ExecutorFuture.add_done_callbackcCs |j ¡Sr')r‚rrrrrrüszExecutorFuture.doneN)rrrr&rr_rrrrrr~Ýs r~c@s,eZdZdZd dd„Zdd„Zd dd „ZdS) Ú BaseExecutorzABase Executor class implementation needed to work with s3transferNcCsdSr'r)rrvrrrr&szBaseExecutor.__init__cOs tdƒ‚dS)Nzsubmit()r )rr„rjrkrrrr]szBaseExecutor.submitTcCs tdƒ‚dS)Nz shutdown()r r�rrrr€szBaseExecutor.shutdown)N)T)rrrr4r&r]r€rrrrr†s r†c@s"eZdZdZdd„Zddd„ZdS) ÚNonThreadedExecutorz@A drop-in replacement non-threaded version of ThreadPoolExecutorc Oshtƒ}z|||Ž}| |¡WnDtk rbt ¡dd…\}}t d|||¡| ||¡YnX|S)Néz0Setting exception for %s to %s with traceback %s)ÚNonThreadedExecutorFuturerMrtÚsysrsrRrSÚset_exception_info)rr„rjrkrcrr)Útbrrrr]s þzNonThreadedExecutor.submitTcCsdSr'rr�rrrr€szNonThreadedExecutor.shutdownN)T)rrrr4r]r€rrrrr‡ sr‡c@sReZdZdZdd„Zdd„Zdd„Zdd d „Zd d „Zd d„Z dd„Z dd„Z dS)r‰z«The 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')r8r�r�)rr,Ú tracebackrrrr‹1sz,NonThreadedExecutorFuture.set_exception_infoNcCs$|jrt t|jƒ|j|j¡|jSr')r8rÚreraiseÚtyper�r7)rÚtimeoutrrrr6sÿz NonThreadedExecutorFuture.resultcCs&d|_|jD]}| |¡q g|_dS)NT)rŽr<Ú_invoke_done_callback©rr…rrrr�<s  z#NonThreadedExecutorFuture._set_donecCs||ƒSr'rr•rrrr”Bsz/NonThreadedExecutorFuture._invoke_done_callbackcCs|jSr')rŽrrrrrEszNonThreadedExecutorFuture.donecCs"|jr| |¡n |j |¡dSr')rŽr”r<rg)rr„rrrr_Hs z+NonThreadedExecutorFuture.add_done_callback)N) rrrr4r&rMr‹rr�r”rr_rrrrr‰ s r‰ÚTaskTagÚnameZin_memory_uploadZin_memory_download)"Z concurrentrÚ collectionsrrGÚloggingrŠr=Zs3transfer.compatrrZs3transfer.exceptionsrrZs3transfer.utilsrr Ú getLoggerrrRÚobjectr rr!r#r%rur~r†r‡r‰r–ZIN_MEMORY_UPLOAD_TAGZIN_MEMORY_DOWNLOAD_TAGrrrrÚ s2       1&qL# /