o
    8)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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 ddlmZ ddlmZ ddlmZ er\ddl
mZ ddlmZ ddlmZ ddlmZ ee Z!d/ddZ"d0ddZ#G d d! d!eZ$d1d&d'Z%d2d+d,Z&d3d-d.Z'dS )4    )annotationsN)Path)TYPE_CHECKING)Any   )Configuration)Version)data_from_mime)Workdir)get_latest_file_mtime)
ScmVersion)metatag_to_version)_types)CompletedProcess)require_command)runreturnstrc                   C  s   t jddS )zDGet the hg command from environment, allowing runtime configuration.ZSETUPTOOLS_SCM_HG_COMMANDhg)osenvironget r   r   L/home/app/PyTorch/.pytorch/lib/python3.10/site-packages/setuptools_scm/hg.py_get_hg_command   s   r   args	list[str]cwd_t.PathTkwargsr   r   c                 K  s    t  g| }t|fd|i|S )z8Run mercurial command with the configured hg executable.r   )r   _run)r   r   r!   cmdr   r   r   run_hg#   s   r$   c                   @  s   e Zd Zed@ddZdAd
dZdBddZdCddZdDddZdEddZ	dFd d!Z
dGd$d%ZdHd(d)ZdId*d+ZdJd.d/ZdKd1d2ZdLdMd7d8ZdNd:d;ZdOd=d>Zd?S )P	HgWorkdirwdr    r   HgWorkdir | Nonec                 C  s$   t dg|}|jrd S | t|jS )Nroot)r$   
returncoder   stdout)clsr&   resr   r   r   from_potential_worktree*   s   z!HgWorkdir.from_potential_worktreeconfigr   ScmVersion | Nonec                 C  s   |   }|d u r
d S |\}}}|  }|\}}}	| |||	}
| |r,| ||||
S d| }| |}| ||}|rEt||||dS | |||||
S )Nh)dirtybranchr.   )	_get_node_info_get_branch_info_get_node_date_is_initial_node_create_initial_meta_parse_tags_get_version_from_tagsr   _get_distance_based_version)selfr.   Z	node_infonodetags_strnode_date_strZbranch_infor2   r1   
dirty_date	node_datetagsZtag_versionr   r   r   get_meta1   s   



zHgWorkdir.get_metatuple[str, str, str] | Nonec                 C  sD   z|  ddd\}}}|||fW S  ty!   td Y dS w )z8Get node, tags, and date information from mercurial log..z{node}
{tag}
{date|shortdate}
zFailed to get node infoN)hg_logsplit
ValueErrorlog	exception)r;   r<   r=   r>   r   r   r   r3   P   s   
zHgWorkdir._get_node_infotuple[str, bool, str]c                 C  s8   t g d| jddjd\}}}tt|}|||fS )z.Get branch name, dirty status, and dirty date.)id-Tz+{branch}
{if(dirty, 1, 0)}
{date|shortdate}Tr   checkrE   )r$   pathr*   rG   boolint)r;   r2   Z	dirty_strr?   r1   r   r   r   r4   [   s   
zHgWorkdir._get_branch_infor1   rQ   r>   r   r?   datetime.datec                 C  s0   |r|   }|dur|S tj|S tj|S )zRGet the appropriate node date, preferring file modification times for dirty repos.N)get_dirty_tag_datedatetimedatefromisoformat)r;   r1   r>   r?   Zfile_mod_dater   r   r   r5   e   s   zHgWorkdir._get_node_dater<   c                 C  s   |dt | kS )z2Check if this is an initial/empty repository node.0)len)r;   r<   r   r   r   r6   r   s   zHgWorkdir._is_initial_noder2   r@   r   c                 C  s$   t d| j ttd||||dS )z-Create metadata for initial/empty repository.zinitial node %s0.0)r.   r1   r2   r@   )rI   debugrP   r   r   )r;   r.   r1   r2   r@   r   r   r   r7   v   s   zHgWorkdir._create_initial_metar=   r   c                 C  s   |  }d|v r|d |S )z,Parse and filter tags from mercurial output.Ztip)rG   remove)r;   r=   rA   r   r   r   r8      s   
zHgWorkdir._parse_tagsrA   Version | Nonec                 C  s   |rt |d |}|S dS )z+Try to get a version from the current tags.r   Nr   )r;   rA   r.   tagr   r   r   r9      s   z HgWorkdir._get_version_from_tagsc           
   	   C  s   zT|   }|du r| d}n| |}|dks|du r%td}|d7 }nt||d}	|	du r8td}|d7 }n|	}| |sA|rMt|||||||dW S t|||dW S  tyc   td	 Y dS w )
z.Get version based on distance from latest tag.N nullrZ   r   r.   )distancer<   r1   r2   r.   r@   )r.   r@   error)	get_latest_normalizable_tagget_distance_revsr   r   check_changes_since_tagr   rH   rI   rJ   )
r;   r.   r1   r2   r<   r@   tag_strdistr^   Z	maybe_tagr   r   r   r:      s8   	




z%HgWorkdir._get_distance_based_versionrevsettemplatec                 C  s   t dd|d|g| jddjS )NrI   -rrM   TrN   )r$   rP   r*   )r;   ri   rj   r   r   r   rF      s
   zHgWorkdir.hg_log
str | Nonec                 C  s.   | j ddd }|sd S |d  d }|S )Nzancestors(.) and tag('re:\.')z{tags}{if(tags, '
', '')})ri   rj   )rF   rG   )r;   Zoutlinesr^   r   r   r   rd      s   z%HgWorkdir.get_latest_normalizable_tagrD   rev1rev2rR   c                 C  s*   d| d| d}|  |d}t|d S )N(z::)rD   r   )rF   rY   )r;   rn   ro   ri   outr   r   r   re      s   zHgWorkdir.get_distance_revsr^   c                 C  s6   |dks|d u r
dS d|d|d}t | |dS )NrZ   Tz(branch(.) and tag(z>)::. and (merge() or file('re:^(?!\.hgtags).*$')) and not tag(z))rD   )rQ   rF   )r;   r^   ri   r   r   r   rf      s   z!HgWorkdir.check_changes_since_tagdatetime.date | Nonec              
   C  s   zNt g d| jd}|jdkst|jsW dS t g d| jd}|jdkr(W dS g }|j dD ]}|rGt|dkrG|dd }|| q2t	|| jW S  t
yg } ztd| W Y d}~dS d}~ww )	zGet the latest modification time of changed files in the working directory.

        Returns the date of the most recently modified file that has changes,
        or None if no files are changed or if an error occurs.
        )rL   rM   z{dirty})r   r   N)statusz-mz-ark   rE      z Failed to get dirty tag date: %s)r$   rP   r)   rQ   r*   striprG   rY   appendr   	ExceptionrI   r[   )r;   r,   Z
status_resZchanged_fileslinefilepather   r   r   rT      s&   

zHgWorkdir.get_dirty_tag_dateN)r&   r    r   r'   )r.   r   r   r/   )r   rC   )r   rK   )r1   rQ   r>   r   r?   r   r   rS   )r<   r   r   rQ   )
r.   r   r1   rQ   r2   r   r@   rS   r   r   )r=   r   r   r   )rA   r   r.   r   r   r]   )r.   r   r1   rQ   r2   r   r<   r   r@   rS   r   r/   )ri   r   rj   r   r   r   )r   rl   )rD   )rn   r   ro   r   r   rR   )r^   rl   r   rQ   )r   rs   )__name__
__module____qualname__classmethodr-   rB   r3   r4   r5   r6   r7   r8   r9   r:   rF   rd   re   rf   rT   r   r   r   r   r%   )   s"    









	
.

r%   r(   r.   r   r/   c           
      C  s   t  }t| tjtj| drWtdg| }|jsW|j	dD ]5}|
drVt|	 d }|jds<|d  rVddlm} dd	lm} || }|rV|||  S q!t|j}	|	d u rcd S |	|S )
Nz.hg/gitrP   rE   z	default =ru   z.gitr   )_git_parse_inner)GitWorkdirHgClient)r   _require_commandr   rP   existsjoinr$   r)   r*   rG   
startswithr   nameendswithgitr   Zhg_gitr   r-   r%   Zabsolute_rootrB   )
r(   r.   Zhg_cmdr,   ry   rP   r   r   Zwd_hggitr&   r   r   r   parse  s&   


r   datadict[str, str]r   c                 C  s|   t d|  | dd}|rd| }d| v rt| d |dS d| v r4t| d t| d || d	|d
S t|d||dS )Nzdata %sr<   r_   r0   r^   ra   Z	latesttagZlatesttagdistancer2   )rb   r<   r2   r.   rZ   )r<   r.   )rI   r[   r   r   rR   Zversion_cls)r   r.   r<   r   r   r   archival_to_version  s   
r   c                 C  s"   t j| d}t|}t||dS )Nz.hg_archival.txtra   )r   rP   r   r	   r   )r(   r.   Zarchivalr   r   r   r   parse_archival1  s   r   )r   r   )r   r   r   r    r!   r   r   r   )r(   r    r.   r   r   r/   )r   r   r.   r   r   r   )r(   r    r.   r   r   r   )(
__future__r   rU   loggingr   pathlibr   typingr   r   r_   r   Z_version_clsr   Zintegrationr	   Zscm_workdirr
   r   versionr   r   r   r   Z_tZ_run_cmdr   r   r   r   r"   	getLoggerr|   rI   r   r$   r%   r   r   r   r   r   r   r   <module>   s8    


 
]
