a
    ›`°  ã                   @   sf   d Z ddlZddlZddlZddlmZ ddlmZ dZ	dd„ Z
dd	„ Zd
d„ Zddd„Zdd„ ZdS )zsModule containing the main mecurial hook interface and helpers.

.. autofunction:: hook
.. autofunction:: install

é    N)ÚSet)Ú
exceptions)ÚhookÚinstallc           	      K   sŒ   ddl m} tdd}|du r.tdƒ tdƒ‚t|ƒ}|jdd	dd
}tt||ƒƒ}| 	¡ }| 
|¡ d|j_| ¡  | ¡  |rˆ|jS dS )au  Execute Flake8 on the repository provided by Mercurial.

    To understand the parameters read more of the Mercurial documentation
    around Hooks: https://www.mercurial-scm.org/wiki/Hook.

    We avoid using the ``ui`` attribute because it can cause issues with
    the GPL license that Mercurial is under. We don't import it, but we
    avoid using it all the same.
    r   )ÚapplicationF©Úcreate_if_missingNz-Cannot locate your root mercurial repository.TÚflake8Ústrict)Úfallback)Zflake8.mainr   Ú	find_hgrcÚprintÚ
SystemExitÚconfigparser_forÚgetÚlistÚget_filenames_fromZApplicationZ
initializeÚoptionsZ_running_from_vcsZ
run_checksÚreportZresult_count)	ZuiZrepoÚkwargsr   ÚhgrcÚhgconfigr
   Ú	filenamesZapp© r   ú4lib/python3.9/site-packages/flake8/main/mercurial.pyr      s     


r   c                  C   s  t dd} | du rdS t| ƒ}| d¡s2| d¡ | dd¡rTtj| | dd¡d‚| dd¡rvtj| | dd¡d‚| 	ddd	¡ | 	ddd	¡ | d
¡s¦| d
¡ | d
d¡sÀ| 	d
dd¡ t
| dƒ}| |¡ W d  ƒ n1 sê0    Y  tdƒ tdƒ dS )a  Ensure that the mercurial hooks are installed.

    This searches for the ``.hg/hgrc`` configuration file and will add commit
    and qrefresh hooks to it, if they do not already exist.

    It will also print a message to stdout about how to configure the hook.

    :returns:
        True if successful, False if the ``.hg/hgrc`` file doesn't exist.
    :rtype:
        bool
    :raises:
        flake8.exceptions.MercurialCommitHookAlreadyExists
    :raises:
        flake8.exceptions.MercurialQRefreshHookAlreadyExists
    Tr   NFZhooksZcommit)ÚpathÚvalueZqrefreshz!python:flake8.main.mercurial.hookr	   r
   Úwz8mercurial hooks installed, for configuration options seez7http://flake8.pycqa.org/en/latest/user/using-hooks.html)r   r   Zhas_sectionZadd_sectionZ
has_optionÚexcZ MercurialCommitHookAlreadyExistsr   Z"MercurialQRefreshHookAlreadyExistsÚsetÚopenÚwriter   )r   r   Úfdr   r   r   r   2   s2    


ÿÿ

(r   c           	      c   s†   t ƒ }|d }t| | t| ƒƒD ]`}| |  ¡ D ]N}tj | j|¡}||v }tj |¡ }|s0|rdq0| 	|¡ | 
d¡r0|V  q0q d S )NÚnodez.py)r   ÚrangeÚlenÚfilesÚosr   ÚjoinÚrootÚexistsÚaddÚendswith)	Z
repositoryr   Zseen_filenamesr#   ZrevisionÚfilenameZfull_filenameZhave_seen_filenameZfilename_does_not_existr   r   r   r   h   s    

r   Fc                 C   sŽ   t jddgt jt jd}| ¡ \}}tt|dd ƒƒr>| d¡}tj 	|¡sNd S tj 
tj |dd¡¡}tj |¡sŠ| r†t|dƒ ¡  nd S |S )	NZhgr)   )ÚstdoutÚstderrÚdecodezutf-8z.hgr   r   )Ú
subprocessÚPopenÚPIPEZcommunicateÚcallableÚgetattrr0   r'   r   ÚisdirÚabspathr(   r*   r    Úclose)r   r)   Zhg_directoryÚ_r   r   r   r   r   x   s    ÿ
r   c                 C   s   t jd d}| | ¡ |S )N)Zinterpolation)ÚconfigparserZConfigParserÚread)r   Úparserr   r   r   r   Ž   s    
r   )F)Ú__doc__r:   r'   r1   Útypingr   r	   r   r   Ú__all__r   r   r   r   r   r   r   r   r   Ú<module>   s   !6
