
    -:j#                        d Z ddlZddlZddlZddlZddlmZ ddlZddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ d	d
lmZ  ej(                  e      Zdee ej0                  d      f   ddfdZdefdZdedz  fdZddZy)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/.venv/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)CLIError)LFS_MULTIPART_UPLOAD_COMMAND   )get_sessionhf_raise_for_statuslogging)SliceFileObj   )outpathz/Local path to repository you want to configure.)helpreturnc                 j   t         j                  j                  |       }t         j                  j                  |      st	        d      t        j                  dj                         d|       t        j                  dt         j                         d|       t        j                  d|       y)	z
    Configure your repository to enable upload of files > 5GB.

    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.z/git config lfs.customtransfer.multipart.path hfT)checkcwdz-git config lfs.customtransfer.multipart.args z Local repo set up for largefiles)r   N)osr   abspathisdirr   
subprocessrunsplitr   r   result)r   
local_paths     Y/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/cli/lfs.pylfs_enable_largefilesr   &   s     &J77==$BCCNN9??A
 NN
78T7UV\\^
 JJ1
C    msgc                     t        j                  |       dz   }t        j                  j	                  |       t        j                  j                          y)z-Write out the message in Line delimited JSON.
N)jsondumpssysstdoutwriteflush)r   msg_strs     r   	write_msgr(   D   s6    jjo$GJJWJJr   c                  H   t        j                  t        j                  j	                         j                               } d| j                  d      | j                  d      fv ry| j                  d      dvr*t        j                  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_msgr7   K   sv    
**SYY'')//1
2Cswwv(899
www5556Jr   c            
      2   t        j                  t        j                  j	                         j                               } | j                  d      dk(  r| j                  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        |j                  d            }t        |j                               }t        d|d	dd       g }t        |d      5 }	t        |      D ]  \  }
}t!        |	|
|z  |      5 }t#               j%                  ||      }t'        |       |j)                  |j*                  j                  d      |
d	z   d       t        d||
d	z   |z  |d       d
d
d
        	 d
d
d
       t#               j-                  |||d      }t'        |       t        d|d       h# 1 sw Y   xY w# 1 sw Y   NxY w)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	operationr.   error    zWrong lfs init operation)codemessager   Nr   oidr   actionhrefheader
chunk_sizeprogress)r,   r?   
bytesSoFarbytesSinceLastrb)	seek_from
read_limit)dataetag)rK   
partNumber)r?   parts)r!   complete)r,   r?   )r!   r/   r#   r0   r1   r2   r3   r(   r6   r7   intpoplistvaluesopen	enumerater
   r   putr   appendheaderspost)init_msgr   r?   filepathcompletion_urlrB   rC   presigned_urlsrM   fileipresigned_urlrJ   rs                 r   lfs_multipart_uploadra   Z   s    zz#)),,.4467HLL!V+[0IX0U7R4NOPQ bM j;
 HHQK%jv;Xv.Xx(L12
$($9
 	#"#		
 (D! 	T$-n$=  =!*n)  #))-d)CA'*LL$%IIMM&$9*+a% %/#&+,q5J*>.8	 	4 M  
 	AJs34A > 	 	s%   %#HA-H5
HH
HH)r   N)__doc__r!   r   r   r#   typingr   typerhuggingface_hub.errorsr   huggingface_hub.lfsr   utilsr   r   r	   
utils._lfsr
   _outputr   
get_logger__name__r4   strArgumentr   dictr(   r7   ra    r   r   <module>rp      s   $  	  
   + < = = %  
		H	%D
B	
	D 
D<4 $+ V5r   