
    o i                         d Z ddlZddlZddlZddlZddlmZmZ ddlZddl	m
Z
 ddlmZmZmZ ddlmZ  ej        e          Zdee ej        d	
          f         ddfdZdefdZdee         fdZddZdS )a  
Implementation of a custom transfer agent for the transfer type "multipart" for
git-lfs.

Inspired by:
github.com/cbartz/git-lfs-swift-transfer-agent/blob/master/git_lfs_swift_transfer.py

Spec is: github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md


To launch debugger while developing:

``` [lfs "customtransfer.multipart"]
path = /path/to/huggingface_hub/.env/bin/python args = -m debugpy --listen 5678
--wait-for-client
/path/to/huggingface_hub/src/huggingface_hub/commands/huggingface_cli.py
lfs-multipart-upload ```    N)	AnnotatedOptional)LFS_MULTIPART_UPLOAD_COMMAND   )get_sessionhf_raise_for_statuslogging)SliceFileObjpathz/Local path to repository you want to configure.)helpreturnc                    t           j                            |           }t           j                            |          s$t	          d           t          j        d          t          j        d	                                d|           t          j        dt           	                                d|           t	          d           d	S )
z
    Configure a local git repository to use the multipart transfer agent for large files.

    This command sets up git-lfs to use the custom multipart transfer agent
    which enables efficient uploading of large files in chunks.
    z)This does not look like a valid git repo.   )codez/git config lfs.customtransfer.multipart.path hfT)checkcwdz-git config lfs.customtransfer.multipart.args z Local repo set up for largefilesN)osr   abspathisdirprinttyperExit
subprocessrunsplitr   )r   
local_paths     W/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/huggingface_hub/cli/lfs.pylfs_enable_largefilesr   $   s     &&J7==$$ !9:::ja    N9??AA   
 NV8TVV\\^^   
 

,-----    msgc                     t          j        |           dz   }t          j                            |           t          j                                         dS )z-Write out the message in Line delimited JSON.
N)jsondumpssysstdoutwriteflush)r    msg_strs     r   	write_msgr*   C   sD    joo$GJWJr   c                  p   t          j        t          j                                                                                  } d|                     d          |                     d          fv rdS |                     d          dvr.t                              d           t          j	        d           | S )z$Read Line delimited JSON from stdin.	terminatetypeeventN)downloaduploadzReceived unexpected messager   )
r#   loadsr%   stdinreadlinestripgetloggercriticalexit)r    s    r   read_msgr9   J   s    
*SY''))//11
2
2Cswwv(8(8999t
www5555666Jr   c            
         t          j        t          j                                                                                  } |                     d          dk    r|                     d          dk    s(t          ddddi           t          j        d	           t          i            	 t                      }|t          j        d           |d         }|d         }|d         d         }|d         d         }t          |                    d                    }t          |                                          }t          d|d	dd           g }t          |d          5 }	t          |          D ]\  }
}t!          |	|
|z  |          5 }t#                                          ||          }t'          |           |                    |j                            d          |
d	z   d           t          d||
d	z   |z  |d           ddd           n# 1 swxY w Y   	 ddd           n# 1 swxY w Y   t#                                          |||d          }t'          |           t          d|d           )zInternal git-lfs custom transfer agent for multipart uploads.

    This function implements the custom transfer protocol for git-lfs multipart uploads.
    Handles chunked uploads of large files to Hugging Face Hub.
    r.   init	operationr0   error    zWrong lfs init operation)r   messager   TNr   oidr   actionhrefheader
chunk_sizeprogress)r.   r@   
bytesSoFarbytesSinceLastrb)	seek_from
read_limit)dataetag)rL   
partNumber)r@   parts)r#   complete)r.   r@   )r#   r1   r%   r2   r3   r4   r5   r*   r8   r9   intpoplistvaluesopen	enumerater
   r   putr   appendheaderspost)init_msgr    r@   filepathcompletion_urlrC   rD   presigned_urlsrN   fileipresigned_urlrK   rs                 r   lfs_multipart_uploadrb   Y   s0    z#),,..446677HLL!!V++[0I0IX0U0U7R4NOOPQQQ bMMM@5jj;
 HQKKK%jv;Xv.Xx(L1122
$($9$9
 	#"#	 	
 	
 	
 (D!! 	T$-n$=$=   =!*n)    #))-d)CCA'***LL$%IMM&$9$9*+a%    %/#&+,q5J*>.8	                 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	4 MM   
 
 	AJs33444A@5s7   &*H5B HH5H  H5#H $H55H9<H9)r   N)__doc__r#   r   r   r%   typingr   r   r   huggingface_hub.lfsr   utilsr   r   r	   
utils._lfsr
   
get_logger__name__r6   strArgumentr   dictr*   r9   rb    r   r   <module>rn      sY   $  				     



 & & & & & & & &  < < < < < < = = = = = = = = = = % % % % % % 
	H	%	%.
B	
 	
 	
	. 
. . . .>4    (4.    V5 V5 V5 V5 V5 V5r   