U
    9%e                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 dee
 ee
 dd	d
Zde
e
ee
 ddddZde
ee
 ddddZdS )z-Contains utilities to manage Git credentials.    N)ListOptional   )ENDPOINT   )run_interactive_subprocessrun_subprocess)folderreturnc              
   C   s\   z(t d| dj}tdd |dD W S  tjk
rV } zt|jW 5 d}~X Y nX dS )a  Return the list of git credential helpers configured.

    See https://git-scm.com/docs/gitcredentials.

    Credentials are saved in all configured helpers (store, cache, macOS keychain,...).
    Calls "`git credential approve`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit config --listr	   c                 S   s*   h | ]"}d |kr| dd   d qS )zcredential.helper=r   )split).0line r   d/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/huggingface_hub/utils/_git_credential.py	<setcomp>)   s     z*list_credential_helpers.<locals>.<setcomp>
N)r   stdoutsortedr   
subprocessCalledProcessErrorEnvironmentErrorstderr)r	   outputexcr   r   r   list_credential_helpers   s    r   hf_user)tokenusernamer	   r
   c              
   C   sJ   t d|d4\}}|dt d|  d|  d |  W 5 Q R X dS )a  Save a username/token pair in git credential for HF Hub registry.

    Credentials are saved in all configured helpers (store, cache, macOS keychain,...).
    Calls "`git credential approve`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        username (`str`, defaults to `"hf_user"`):
            A git username. Defaults to `"hf_user"`, the default user used in the Hub.
        token (`str`, defaults to `"hf_user"`):
            A git password. In practice, the User Access Token for the Hub.
            See https://huggingface.co/settings/tokens.
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit credential approver   url=z

username=z

password=z

N)r   writer   lowerflush)r   r    r	   stdin_r   r   r   set_git_credential1   s
    "r'   )r    r	   r
   c              	   C   sb   t d|dL\}}dt d}| dk	r:|d|   d7 }|d7 }|| |  W 5 Q R X dS )aY  Erase credentials from git credential for HF Hub registry.

    Credentials are erased from the configured helpers (store, cache, macOS
    keychain,...), if any. If `username` is not provided, any credential configured for
    HF Hub endpoint is erased.
    Calls "`git credential erase`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        username (`str`, defaults to `"hf_user"`):
            A git username. Defaults to `"hf_user"`, the default user used in the Hub.
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit credential rejectr   r!   r   Nz	username=)r   r   r#   r"   r$   )r    r	   r%   r&   Zstandard_inputr   r   r   unset_git_credentialH   s    
r(   )N)r   N)r   N)__doc__r   typingr   r   	constantsr   Z_subprocessr   r   strr   r'   r(   r   r   r   r   <module>   s   