o
    Ì^½^ž  ã                   @   s|   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 e  	e
¡ZdZdd„ Zdd„ Zd	d
„ Zdd„ Zddd„ZdS )é    N)Úparse_version)Ú
ghp_importz-Deployed {sha} with MkDocs version: {version}c                  C   sR   zt jg d¢t jt jd�} W n ty   t d¡ tdƒ‚w |  ¡  |  ¡ dkS )N)Úgitú	rev-parsez--is-inside-work-tree©ÚstdoutÚstderrz6Could not find git - is it installed and on your path?é   r   )	Ú
subprocessÚPopenÚPIPEÚFileNotFoundErrorÚlogÚerrorÚ
SystemExitÚcommunicateÚwait)Úproc© r   ú;/usr/lib/python3/dist-packages/mkdocs/commands/gh_deploy.pyÚ_is_cwd_git_repo   s   
ý
þr   c                 C   s8   t jg d¢| t jt jd�}| ¡ \}}| d¡ ¡ }|S )N)r   r   z--shortÚHEAD)Úcwdr   r   úutf-8)r
   r   r   r   ÚdecodeÚstrip)Ú	repo_pathr   r   Ú_Úshar   r   r   Ú_get_current_sha   s   ÿr   c                 C   s†   d|  }t jddd|gt jt jd�}| ¡ \}}| d¡ ¡ }d }d }d|v r3| dd¡\}}||fS d	|v r?| d	d¡\}}||fS )
Nzremote.%s.urlr   Úconfigz--getr   r   zgithub.com/r	   zgithub.com:)r
   r   r   r   r   r   Úsplit)Úremote_nameÚremoter   r   r   ÚurlÚhostÚpathr   r   r   Ú_get_remote_url'   s   ÿýr'   c                 C   sÈ   t jddddd | ¡gt jt jd�}| ¡ \}}| d¡ ¡ }t d|tj	tj
B ¡}|r2t| ¡ ƒnd }ttjƒ}|sBt d	¡ d S ||krQt d
 ||¡¡ d S ||k rbt d ||¡¡ tdƒ‚d S )Nr   Úshowz-sz--format=%szrefs/heads/{}r   r   z0\d+(\.\d+)+((a|b|rc)\d+)?(\.post\d+)?(\.dev\d+)?zCVersion check skipped: No version specified in previous deployment.z`Previous deployment was done with MkDocs version {}; you are deploying with a newer version ({})z«Deployment terminated: Previous deployment was made with MkDocs version {}; you are attempting to deploy with an older version ({}). Use --ignore-version to deploy anyway.r	   )r
   r   Úformatr   r   r   r   ÚreÚsearchÚXÚIr   ÚgroupÚmkdocsÚ__version__r   ÚwarningÚinfor   r   )Úbranchr   r   r   ÚmsgÚmÚ	previousvÚcurrentvr   r   r   Ú_check_version<   s,   ÿ
þýúr8   Fc                 C   s|  t ƒ st d¡ | d }| d }|st|ƒ |d u rt}ttj | j	¡ƒ}|j
tj|d�}t d| d | d ¡ t | d ||||¡\}}|sQt d|¡ tdƒ‚tj | d d	¡}	tj |	¡rˆt|	d
ƒ�}
|
 ¡  ¡ }W d   ƒ n1 svw   Y  t d|¡ t d¡ d S t|ƒ\}}|d u r™t d¡ d S | dd¡\}}| d¡r¯|d tdƒ … }d 
||¡}t d| ¡ d S )NzECannot deploy - this directory does not appear to be a git repositoryÚremote_branchr"   )Úversionr   z2Copying '%s' to '%s' branch and pushing to GitHub.Úsite_dirz*Failed to deploy to GitHub with error: 
%sr	   ÚCNAMEÚrzVBased on your CNAME file, your documentation should be available shortly at: http://%szSNOTE: Your DNS records must be configured appropriately for your CNAME URL to work.z/Your documentation should be available shortly.ú/z.gitzhttps://{}.github.io/{}/z3Your documentation should shortly be available at: )r   r   r   r8   Údefault_messager   Úosr&   ÚdirnameÚconfig_file_pathr)   r/   r0   r2   r   r   ÚjoinÚisfileÚopenÚreadr   r'   r!   ÚendswithÚlen)r    ÚmessageÚforceÚignore_versionr9   r"   r   Úresultr   Ú
cname_fileÚfÚ
cname_hostr%   r&   ÚusernameÚrepor$   r   r   r   Ú	gh_deployV   sJ   
ÿÿÿÿ

rR   )NFF)Úloggingr
   r@   r*   Úpkg_resourcesr   r/   Úmkdocs.utilsr   Ú	getLoggerÚ__name__r   r?   r   r   r'   r8   rR   r   r   r   r   Ú<module>   s    

