o
    0j                     @   s   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 eeZd	eeejd
df ddfddZdefddZdedB fddZd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/.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pathz/Local path to repository you want to configure.)helpreturnc                 C   sZ   t j| }t j|stdtjd d|d tjdt  d|d t	d dS )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 largefilesN)
osr
   abspathisdirr   
subprocessrunsplitr   print)r
   Z
local_path r   X/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/huggingface_hub/cli/lfs.pylfs_enable_largefiles%   s   r   msgc                 C   s(   t | d }tj| tj  dS )z-Write out the message in Line delimited JSON.
N)jsondumpssysstdoutwriteflush)r   Zmsg_strr   r   r   	write_msgC   s   r!   c                  C   sV   t tj  } d| d| dfv rdS | ddvr)td t	d | S )z$Read Line delimited JSON from stdin.	terminatetypeeventN)downloaduploadzReceived unexpected message   )
r   loadsr   stdinreadlinestripgetloggercriticalexit)r   r   r   r   read_msgJ   s   

r0   c               
   C   s  t tj  } | ddkr| ddks&tddddi td	 ti  	 t	 }|du r7td |d }|d }|d d }|d d }t
|d}t| }td|d	dd g }t|dQ}	t|D ]D\}
}t|	|
| |d/}t j||d}t| ||jd|
d	 d td||
d	 | |d W d   n1 sw   Y  qmW d   n1 sw   Y  t j|||dd}t| td|d q+)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$   initZ	operationr&   error    zWrong lfs init operation)codemessager'   TNr   oidr
   actionhrefheader
chunk_sizeprogress)r$   r6   Z
bytesSoFarZbytesSinceLastrb)Z	seek_fromZ
read_limit)dataetag)r>   Z
partNumber)r6   parts)r   Zcomplete)r$   r6   )r   r(   r   r)   r*   r+   r,   r!   r/   r0   intpoplistvaluesopen	enumerater	   r   putr   appendheaderspost)Zinit_msgr   r6   filepathZcompletion_urlr9   r:   Zpresigned_urlsr?   fileiZpresigned_urlr=   rr   r   r   lfs_multipart_uploadY   st   	

	

rN   )r   N)__doc__r   r   r   r   typingr   ZtyperZhuggingface_hub.errorsr   Zhuggingface_hub.lfsr   utilsr   r   r   Z
utils._lfsr	   Z
get_logger__name__r-   strZArgumentr   dictr!   r0   rN   r   r   r   r   <module>   s4    

