U C^g@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddl mZddlmZddlmZddlmZddlmZd Zd Zd ZeeZejeeefZd3d dZddZddZddZ d4ddZ!ddZ"ddZ#ddZ$Gddde%Z&Gdd d e%Z'Gd!d"d"e%Z(Gd#d$d$e%Z)Gd%d&d&e%Z*Gd'd(d(e%Z+Gd)d*d*e%Z,Gd+d,d,e-Z.Gd-d.d.e%Z/Gd/d0d0e/Z0Gd1d2d2e%Z1dS)5N) defaultdict)IncompleteReadError)ReadTimeoutError) SOCKET_ERROR rename_file)seekable) fallocatei'liPcCsdddt|DS)Ncss|]}ttjVqdSN)randomchoicestring hexdigits).0_r8/tmp/pip-install-6_kvzl1k/s3transfer/s3transfer/utils.py 2sz(random_file_extension..)joinrange)Z num_digitsrrrrandom_file_extension1srcKs"|dkrt|jdr|jdS)NZ PutObjectZ UploadPartsignal_not_transferring)hasattrbodyrrequestZoperation_namekwargsrrrr5s rcKs"|dkrt|jdr|jdS)Nrsignal_transferring)rrr rrrrr ;s r cCstt|t|Sr )intmathceilfloat)size part_sizerrrcalculate_num_partsAsr'cCsJ||}||dkr.d}|dk r:t|d}n ||d}d||f}|S)aCalculate the range parameter for multipart downloads/copies :type part_size: int :param part_size: The size of the part :type part_index: int :param part_index: The index for which this parts starts. This index starts at zero :type num_parts: int :param num_parts: The total number of parts in the transfer :returns: The value to use for Range parameter on downloads or the CopySourceRange parameter for copies r Nz bytes=%s-%s)str)r&Z part_index num_partsZ total_sizeZ start_rangeZ end_rangeZ range_paramrrrcalculate_range_parameterEs   r+cCsDg}|jjjD]0}d|}t||r|tjt|||dq|S)a Retrieves callbacks from a subscriber :type transfer_future: s3transfer.futures.TransferFuture :param transfer_future: The transfer future the subscriber is associated to. :type callback_type: str :param callback_type: The type of callback to retrieve from the subscriber. Valid types include: * 'queued' * 'progress' * 'done' :returns: A list of callbacks for the type specified. All callbacks are preinjected with the transfer future. Zon_)future)metaZ call_argsZ subscribersrappend functoolspartialgetattr)Ztransfer_futureZ callback_type callbacks subscriberZ callback_namerrr get_callbacksbs r4cCs|r|D]}||dqdS)aCalls all progress callbacks :param callbacks: A list of progress callbacks to invoke :param bytes_transferred: The number of bytes transferred. This is passed to the callbacks. If no bytes were transferred the callbacks will not be invoked because no progress was achieved. It is also possible to receive a negative amount which comes from retrying a transfer request. bytes_transferredNr)r2r6callbackrrrinvoke_progress_callbackss r8cCs*i}|D]\}}||kr |||<q |S)aoGets a dictionary filtered by whitelisted keys :param original_dict: The original dictionary of arguments to source keys and values. :param whitelisted_key: A list of keys to include in the filtered dictionary. :returns: A dictionary containing key/values from the original dictionary whose key was included in the whitelist )items)Z original_dictZwhitelisted_keysZ filtered_dictkeyvaluerrrget_filtered_dicts  r<c@seZdZddZdS)CallArgscKs"|D]\}}t|||qdS)zA class that records call arguments The call arguments must be passed as keyword arguments. It will set each keyword argument as an attribute of the object along with its associated value. N)r9setattr)selfrargr;rrr__init__szCallArgs.__init__N)__name__ __module__ __qualname__rArrrrr=sr=c@s(eZdZdZddZddZddZdS) FunctionContainerzAn object that contains a function and any args or kwargs to call it When called the provided function will be called with provided args and kwargs. cOs||_||_||_dSr Z_func_args_kwargs)r?funcargsrrrrrAszFunctionContainer.__init__cCsd|j|j|jfS)Nz'Function: %s with args %s and kwargs %srFr?rrr__repr__s zFunctionContainer.__repr__cCs|j|j|jSr rFrKrrr__call__szFunctionContainer.__call__N)rBrCrD__doc__rArLrMrrrrrEsrEc@s<eZdZdZddZeddZddZdd Zd d Z d S) CountCallbackInvokerzAn abstraction to invoke a callback when a shared count reaches zero :param callback: Callback invoke when finalized count reaches zero cCs t|_||_d|_d|_dS)NrF) threadingLock_lock _callback_count _is_finalizedr?r7rrrrAs zCountCallbackInvoker.__init__c Cs$|j|jW5QRSQRXdSr rRrTrKrrr current_countsz"CountCallbackInvoker.current_countc Cs2|j"|jrtd|jd7_W5QRXdS)zIncrement the count by onez;Counter has been finalized it can no longer be incremented.r(N)rRrU RuntimeErrorrTrKrrr increments zCountCallbackInvoker.incrementc CsN|j>|jdkrtd|jd8_|jr@|jdkr@|W5QRXdS)zDecrement the count by onerz,Counter is at zero. It cannot dip below zeror(N)rRrTrYrUrSrKrrr decrements zCountCallbackInvoker.decrementc Cs.|jd|_|jdkr |W5QRXdS)zFinalize the counter Once finalized, the counter never be incremented and the callback can be invoked once the count reaches zero TrN)rRrUrTrSrKrrrfinalizes zCountCallbackInvoker.finalizeN) rBrCrDrNrApropertyrXrZr[r\rrrrrOs   rOc@sZeZdZdZddZddZdddZd d Zd d Zd dZ ddZ ddZ ddZ dS)OSUtilscCs tj|Sr )ospathgetsizer?filenamerrr get_file_sizeszOSUtils.get_file_sizecCstj||||ddS)NF)enable_callbacks) ReadFileChunk from_filename)r?rd start_byter%r2rrropen_file_chunk_readers zOSUtils.open_file_chunk_readerNcCst||||d|dS)NF)r2rfclose_callbacks)rg)r?fileobj chunk_sizefull_file_sizer2rkrrr#open_file_chunk_reader_from_fileobjsz+OSUtils.open_file_chunk_reader_from_fileobjcCs t||Sr )open)r?rdmoderrrrpsz OSUtils.opencCs(zt|Wntk r"YnXdS)z+Remove a file, noop if file does not exist.N)r`removeOSErrorrcrrr remove_fileszOSUtils.remove_filecCst||dSr r)r?Zcurrent_filenameZ new_filenamerrrrszOSUtils.rename_filecCsXtj|sdSt|j}t|r*dSt|r8dSt|rFdSt|rTdSdS)aChecks to see if a file is a special UNIX file. It checks if the file is a character special device, block special device, FIFO, or socket. :param filename: Name of the file :returns: True if the file is a special file. False, if is not. FT) r`raexistsstatst_modeS_ISCHRS_ISBLKS_ISFIFOS_ISSOCK)clsrdrqrrris_special_files      zOSUtils.is_special_filecCsLtjt}tj|}tj|}|d|jt||}tj||Sr ) r`extseprradirnamebasename_MAX_FILENAME_LENlenr)r?rdsuffixranameZ temp_filenamerrrget_temp_filename0s    zOSUtils.get_temp_filenamec CsPz&||d}t||W5QRXWn$ttfk rJ||YnXdS)Nwb)rpr rsIOErrorrt)r?rdr%frrrallocate7s  zOSUtils.allocate)N) rBrCrDrrerjrorprtrr}rrrrrrr^s  r^c@sjeZdZddefddZddZeddZdd d Zd d Z ddZ ddZ ddZ ddZ ddZd S)DeferredOpenFilerrbcCs"||_d|_||_||_||_dS)aA class that defers the opening of a file till needed This is useful for deferring opening of a file till it is needed in a separate thread, as there is a limit of how many open files there can be in a single thread for most operating systems. The file gets opened in the following methods: ``read()``, ``seek()``, and ``__enter__()`` :type filename: str :param filename: The name of the file to open :type start_byte: int :param start_byte: The byte to seek to when the file is opened. :type mode: str :param mode: The mode to use to open the file :type open_function: function :param open_function: The function to use to open the file N) _filename_fileobj _start_byte_mode_open_function)r?rdrirqZ open_functionrrrrAAs zDeferredOpenFile.__init__cCs8|jdkr4||j|j|_|jdkr4|j|jdS)Nr)rrrrrseekrKrrr_open_if_needed\s  z DeferredOpenFile._open_if_neededcCs|jSr )rrKrrrrbszDeferredOpenFile.nameNcCs||j|Sr )rrread)r?amountrrrrfszDeferredOpenFile.readcCs||j|dSr )rrwrite)r?datarrrrjszDeferredOpenFile.writecCs||j|dSr )rrrr?whererrrrnszDeferredOpenFile.seekcCs|jdkr|jS|jSr )rrtellrKrrrrrs zDeferredOpenFile.tellcCs|jr|jdSr )rcloserKrrrrwszDeferredOpenFile.closecCs ||Sr )rrKrrr __enter__{szDeferredOpenFile.__enter__cOs |dSr rr?rJrrrr__exit__szDeferredOpenFile.__exit__)N)rBrCrDrprArr]rrrrrrrrrrrrr@s  rc@seZdZd!ddZed"ddZddZd#d d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd ZdS)$rgNTcCsb||_|j|_|j|j||j|d|_d|_||_|dkrDg|_||_||_|dkr^||_dS)a Given a file object shown below:: |___________________________________________________| 0 | | full_file_size |----chunk_size---| f.tell() :type fileobj: file :param fileobj: File like object :type chunk_size: int :param chunk_size: The max chunk size to read. Trying to read pass the end of the chunk size will behave like you've reached the end of the file. :type full_file_size: int :param full_file_size: The entire content length associated with ``fileobj``. :type callbacks: A list of function(amount_read) :param callbacks: Called whenever data is read from this object in the order provided. :type enable_callbacks: boolean :param enable_callbacks: True if to run callbacks. Otherwise, do not run callbacks :type close_callbacks: A list of function() :param close_callbacks: Called when close is called. The function should take no arguments. )requested_sizeriactual_file_sizerN) rrr_calculate_file_size_size _amount_read _callbacks_callbacks_enabled_close_callbacks)r?rlrmrnr2rfrkrrrrAs # zReadFileChunk.__init__cCs4t|d}||t|j}||||||S)a[Convenience factory function to create from a filename. :type start_byte: int :param start_byte: The first byte from which to start reading. :type chunk_size: int :param chunk_size: The max chunk size to read. Trying to read pass the end of the chunk size will behave like you've reached the end of the file. :type full_file_size: int :param full_file_size: The entire content length associated with ``fileobj``. :type callbacks: function(amount_read) :param callbacks: Called whenever data is read from this object. :type enable_callbacks: bool :param enable_callbacks: Indicate whether to invoke callback during read() calls. :rtype: ``ReadFileChunk`` :return: A new instance of ``ReadFileChunk`` r)rprr`fstatfilenost_size)r|rdrirmr2rfr file_sizerrrrhs  zReadFileChunk.from_filenamecCs||}t||Sr )min)r?rlrrirZmax_chunk_sizerrrrsz"ReadFileChunk._calculate_file_sizecCsj|dkr|j|j}nt|j|j|}|j|}|jt|7_|jdk rf|jrft|jt||Sr ) rrrrrrrrr8)r?rZamount_to_readrrrrrs zReadFileChunk.readcCs"|t|jdr|jdS)Nr )enable_callbackrrr rKrrrr s z!ReadFileChunk.signal_transferringcCs"|t|jdr|jdS)Nr)disable_callbackrrrrKrrrrs z%ReadFileChunk.signal_not_transferringcCs d|_dS)NTrrKrrrrszReadFileChunk.enable_callbackcCs d|_dS)NFrrKrrrrszReadFileChunk.disable_callbackcCs@|j|j||jdk r6|jr6t|j||jd||_dS)Nr5)rrrrrr8rrrrrrszReadFileChunk.seekcCs0|jdk r"|jr"|jD] }|q|jdSr )rrrrrVrrrrs zReadFileChunk.closecCs|jSr )rrKrrrrszReadFileChunk.tellcCs|jSr )rrKrrr__len__szReadFileChunk.__len__cCs|Sr rrKrrrrszReadFileChunk.__enter__cOs |dSr rrrrrrszReadFileChunk.__exit__cCstgSr )iterrKrrr__iter__szReadFileChunk.__iter__)NTN)NT)N)rBrCrDrA classmethodrhrrr rrrrrrrrrrrrrrrgs* 1  rgc@s"eZdZdZdddZddZdS)StreamReaderProgressz s^           0OC%_