U
    ”Ê¹j  ã                   @   sÀ   d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
 ejdejejB ejB d	�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eee dœdd„ZdS )z-Contains utilities to manage Git credentials.é    N)ÚListÚOptionalé   )ÚENDPOINTé   )Úrun_interactive_subprocessÚrun_subprocesszß
        ^\s* # start of line
        credential\.helper # credential.helper value
        \s*=\s* # separator
        ([\w\-\/]+) # the helper name or absolute path (group 1)
        (\s|$) # whitespace or end of line
    )Úflags)ÚfolderÚreturnc              
   C   sP   zt d| d�j}t|ƒ}|W S  tjk
rJ } 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
   N)r   ÚstdoutÚ_parse_credential_outputÚ
subprocessÚCalledProcessErrorÚEnvironmentErrorÚstderr)r
   ÚoutputÚparsedÚexc© r   úd/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/utils/_git_credential.pyÚ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_credential9   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   Ú
Nz	username=)r   r   r   r   r   )r   r
   r    r!   Zstandard_inputr   r   r   Úunset_git_credentialP   s    
r$   )r   r   c                 C   s   t tdd„ t | ¡D ƒƒƒS )z›Parse the output of `git credential fill` to extract the password.

    Args:
        output (`str`):
            The output of `git credential fill`.
    c                 s   s   | ]}|d  V  qdS )r   Nr   )Ú.0Úmatchr   r   r   Ú	<genexpr>v   s    z+_parse_credential_output.<locals>.<genexpr>)ÚsortedÚsetÚGIT_CREDENTIAL_REGEXÚfindall)r   r   r   r   r   k   s
    
ÿÿr   )N)r   N)r   N)Ú__doc__Úrer   Útypingr   r   Ú	constantsr   Z_subprocessr   r   ÚcompileÚ	MULTILINEÚ
IGNORECASEÚVERBOSEr*   Ústrr   r"   r$   r   r   r   r   r   Ú<module>   s   ø