o
    ٷi-                     @  s&  d dl mZ d dlZd dlZd dlZd dlm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 d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZmZ d dlmZ g dZdZ dd Z!d@ddZ"dAddZ#dBdd Z$ed!d" d#d$d$d#ded!dd%d%dfdCd<d=Z%dDd>d?Z&dS )E    )annotationsN)Path)	Annotated)HfApi)Version)print)Console)Panel)ConfirmPrompt)parse)ArgumentOption)custom_component_analytics)redyellowgreenblueindigopurplepinkgrayz"https://pypi.org/account/register/c                 C  s8   g }|D ]}d|v s| ds| dr|| q|S )N__pycache__distnode_modules)
startswithappend)_srcnamesignoredn r!   Z/home/ubuntu/.local/lib/python3.10/site-packages/gradio/cli/commands/components/publish.py_ignore   s   
r#   	dist_filer   return
str | Nonec                 C  s    t d| j}|r|dS d S )Nz-(\d+\.\d+\.\d+[a-zA-Z]*\d*)-   )researchnamegroup)r$   matchr!   r!   r"   _get_version_from_file"   s   
r-   version_stringstr/semantic_version.Version | PEP440Version | Nonec                 C  sB   zt | W S  ty    zt| W  Y S  ty   Y Y dS w w )zCParse a version string, trying semantic_version first, then PEP440.N)semantic_versionr   
ValueErrorPEP440Version	Exception)r.   r!   r!   r"   _parse_version)   s   r5   distribution_files
list[Path]c                 C  sL   g }| D ]}t |}|rt|}|r|| q d S q|r$tt|S d S )N)r-   r5   r   r/   max)r6   versionspversionparsedr!   r!   r"   _get_max_version6   s   r=   .r   T Fdist_dir_Annotated[Path, Argument(help=f"Path to the wheel directory. Default is {Path('.') / 'dist'}")]upload_pypi:Annotated[bool, Option(help='Whether to upload to PyPI.')]pypi_username5Annotated[str, Option(help='The username for PyPI.')]pypi_password5Annotated[str, Option(help='The password for PyPI.')]upload_demoFAnnotated[bool, Option(help='Whether to upload demo to HuggingFace.')]demo_dirBAnnotated[Path | None, Option(help='Path to the demo directory.')]
source_dirUAnnotated[Path, Option(help='Path to the source directory of the custom component.')]hf_tokenAnnotated[str | None, Option(help='HuggingFace token for uploading demo. Can be omitted if already logged in via huggingface cli.')]prefer_localvAnnotated[bool, Option(help='Install the package from the local wheel in the demo space, even if it exists on PyPi.')]upload_sourcewAnnotated[bool, Option(help='Whether to upload the source code of the custom component, to share with the community.')]repo_idAnnotated[str | None, Option(help='The repository id to upload the demo to. If not provided, a space will be created with the same name as the package in the HuggingFace account corresponding to the hf_token.')]c               
     s  t dd |||	d t }|  } |  st|  d|  s&t|  ddd t| dD }dDd
d}tdd |D |d}|sHtdd }|r|rP|st	dt
 d}t| td}|rxt d  rxtd tt d }n|rtd td}tjddd}|rzddlm} ddlm} ddlm} W n ttfy } ztd|d }~ww |r|r|||d}n|r||d }ntd!z2t|  fd"d|D }td#d$|  z||| W n |y } ztd%|d }~ww W n ty   |  Y nw |rH|sHt	d&}t| td'}|rHt	d(}t| td)td*d+  }|pAttd*d+ }t| }|ro|	sot	d,}t| td-}	|	rotd.| ph|}t| }|rE|d/ }zt| }|d0 d1 }W n ty   |j d2d d3 \}}Y nw |std4|j d2d d3 \}}t!" }t#j$t|t|dd5 t#%t|d6 tt|d6  |	rt#j$t|tt|d7 dt&d8 t#%t|d9 tt|d9  t'|d:}|j(|
p|d;dd<d=}|j)}
|j*|
|d;d> |r|j+|
t||jd;d? td@ |j,|
d|dA tdB|
 dC W d    d S 1 s>w   Y  d S d S )ENpublish)rH   rB   rR   zQ does not exist. Run `gradio cc build` to create a wheel and source distribution.z is not a directoryc                 S  s   g | ]}|j d v r| qS )>   .gz.whl)suffixresolve.0r:   r!   r!   r"   
<listcomp>~   s    z_publish.<locals>.<listcomp>*pathr   c                 S  s>   t | jdd }t|}|d u rtd| d| j |S )N-r'   zCould not parse version z from )r/   r*   splitr5   r2   )r_   version_strr<   r!   r!   r"   _get_wheel_version   s
   z$_publish.<locals>._get_wheel_versionc                 s  s    | ]
}|j d kr|V  qdS )rX   N)rY   r[   r!   r!   r"   	<genexpr>   s    z_publish.<locals>.<genexpr>)keyzgA wheel file was not found in the distribution directory. Run `gradio cc build` to create a wheel file.zIt is recommended to upload your component to pypi so that [bold][magenta]anyone[/][/] can install it with [bold][magenta]pip install[/][/].

A PyPi account is needed. If you do not have an account, register account here: [blue]z[/]z:snake: Upload to pypi?z.pypircz<:closed_lock_with_key: Found .pypirc file in home directory.zx:light_bulb: If you have Two Factor Authentication enabled, the username is __token__ and your password is your API key.z*:laptop_computer: Enter your pypi usernamez/:closed_lock_with_key: Enter your pypi passwordT)passwordr   )upload)InvalidDistribution)Settingsz^The twine library must be installed to publish to pypi.Install it with pip, pip install twine.)usernamerf   )config_filezBNo pypi username or password provided and no ~/.pypirc file found.c                   s"   g | ]} r |j v rt|qS r!   )r*   r/   r[   max_versionr!   r"   r]      s    
zUploading files: ,zqInvalid distribution when uploading to pypi. Try upgrading 'pkginfo' with python -m pip install pkginfo --upgradezIt is recommended you upload a demo of your component to [blue]https://huggingface.co/spaces[/] so that anyone can try it from their browser.z:hugging_face: Upload demo?aX  Please provide the path to the [magenta]demo directory[/] for your custom component.

This directory should contain [magenta]all the files[/] it needs to run successfully.

Please make sure the gradio app is in an [magenta]space.py[/] file.

If you need additional python requirements, add a [magenta]requirements.txt[/] file to this directory.zR:roller_coaster: Please enter the path to the demo directory. Leave blank to use: r>   demozwIt is recommended that you share your [magenta]source code[/] so that others can learn from and improve your component.z1:books: Would you like to share your source code?zQ:file_folder: Please enter the path to the source directory. Leave blank to use: zpyproject.tomlprojectr*   r`      zdemo_dir must be set)dirs_exist_okz
.gitignoresrc)rr   ignorez	README.md)tokenspacegradio)rT   	repo_typeexist_ok	space_sdk)rT   folder_pathrx   )rT   path_or_fileobjpath_in_reporx   
)rT   factory_rebootru   z/Demo uploaded to https://huggingface.co/spaces/z !)r_   r   )-r   r   rZ   existsr2   is_dirr   globr8   r	   PYPI_REGISTER_URLr   r
   askhomer/   r   twine.commands.uploadrg   twine.exceptionsrh   twine.settingsri   ImportErrorModuleNotFoundErrorr=   joinr4   print_exceptionr   	read_textr*   ra   tempfileTemporaryDirectoryshutilcopytreecopyfiler#   r   create_reporT   upload_folderupload_filerestart_space) r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   consoler6   rc   
wheel_filerk   paneltwine_uploadrh   ri   etwine_settingstwine_files	demo_dir_source_dir_pyproject_toml_pathpyproject_tomlpackage_name_tempdirapirepo_urlr!   rl   r"   _publishE   sX  )








$r   c                 C  s@   |   }|d  r|d S |d  r|d S td|  d)Nzspace.pyzapp.pyz*Could not find "space.py" or "app.py" in "z".)rZ   r   FileNotFoundError)rJ   	_demo_dirr!   r!   r"   resolve_demo+  s   
r   )r$   r   r%   r&   )r.   r/   r%   r0   )r6   r7   r%   r&   )r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   )rJ   r   r%   r   )'
__future__r   r(   r   r   pathlibr   typingr   r1   huggingface_hubr   packaging.versionr   r3   richr   rich.consoler   
rich.panelr	   rich.promptr
   r   tomlkitr   typerr   r   gradio.analyticsr   colorsr   r#   r-   r5   r=   r   r   r!   r!   r!   r"   <module>   sH    



 g