a
    `                     @   s   d Z ddlZddlZddlZddlZddl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dZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zejdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)ZdS )+znModule containing the main git hook interface and helpers.

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

    N)defaults)
exceptions)hookinstallFc                 C   s   ddl m} | }t R}tt|| }|dg t|jj	||j_	d|j_
|r^|| W d   n1 sr0    Y  |rt|j| |  |r|jS dS )a  Execute Flake8 on the files in git's index.

    Determine which files are about to be committed and run Flake8 over them
    to check for violations.

    :param bool lazy:
        Find files not added to the index prior to committing. This is useful
        if you frequently use ``git commit -a`` for example. This defaults to
        False since it will otherwise include files not in the index.
    :param bool strict:
        If True, return the total number of errors/violations found by Flake8.
        This will cause the hook to fail.
    :returns:
        Total number of errors found during the run.
    :rtype:
        int
    r   )application.TN)Zflake8.mainr   ZApplicationmake_temporary_directorylistcopy_indexed_files_toZ
initializeupdate_excludesoptionsZexcludeZ_running_from_vcsZ
run_checksupdate_pathsZfile_checker_managerZreport_errorsZresult_count)lazystrictr   ZappZtempdirZ	filepaths r   .lib/python3.9/site-packages/flake8/main/git.pyr      s    (r   c                  C   s   t  } | du stj| sdS tj| d}tj|sBt| tjtj|d}tj|rptj|dt	 }t
|d"}|tj|d W d   n1 s0    Y  tjtjB tjB }t|| td td	 d
S )a  Install the git hook script.

    This searches for the ``.git`` directory and will install an executable
    pre-commit python script in the hooks sub-directory if one does not
    already exist.

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

    :returns:
        True if successful, False if the git directory doesn't exist.
    :rtype:
        bool
    :raises:
        flake8.exceptions.GitHookAlreadyExists
    NFZhooksz
pre-commit)pathw)
executablez<git pre-commit hook installed, for configuration options seez7http://flake8.pycqa.org/en/latest/user/using-hooks.htmlT)find_git_directoryosr   existsjoinmkdirabspathr   ZGitHookAlreadyExistsget_executableopenwrite_HOOK_TEMPLATEformatstatS_IRWXUS_IRGRPS_IROTHchmodprint)Zgit_directoryZhooks_directoryZpre_commit_filer   fdZpre_commit_permissionsr   r   r   r   A   s&    
0r   c                   C   s   t jd urt jS dS )Nz/usr/bin/env python)sysr   r   r   r   r   r   s   s    
r   c                  C   s6   t g d} |  \}}t|}| jdkr2| S d S )N)gitz	rev-parsez	--git-dirr   )piped_processcommunicateto_text
returncodestrip)Z	rev_parsestdout_r   r   r   r   y   s    
r   c                 c   sR   t |}|D ]}t|}t| || qt|}|D ]}t|}t| ||V  q2d S N)find_setup_cfgsget_staged_contents_fromcopy_file_tofind_modified_files)temporary_directoryr   Z
setup_cfgsfilenamecontentsZmodified_filesr   r   r   r
      s    r
   c                 C   s~   t jt j|\}}t| |}t j|s8t | t j||}t|d}|	| W d    n1 sp0    Y  |S )Nwb)
r   r   splitr   make_temporary_directory_fromr   makedirsr   r   r   )Zdestination_directoryfilepathr7   	directoryr6   r5   Ztemporary_filepathr&   r   r   r   r3      s    
(r3   c                 C   s.   t j|| g}t jj||d}t j| |S )N)start)r   r   commonprefixrelpathr   )Zdestinationr=   prefixZcommon_directory_pathr   r   r   r:      s    r:   c                 C   s:   g d}| r| d t|}| \}}t|}| S )N)r(   z
diff-index--cachedz--name-onlyz--diff-filter=ACMRTUXBZHEADrB   remover)   r*   r+   
splitlines)r   Zdiff_index_cmdZ
diff_indexr.   r/   r   r   r   r4      s    
r4   c                 C   s:   g d}| r| d t|}| \}}t|}| S )N)r(   zls-filesrB   z
*setup.cfgrB   rC   )r   Zsetup_cfg_cmdZextra_filesr.   r/   r   r   r   r1      s    
r1   c                 C   s$   t ddd| g}| \}}|S )Nr(   Zshowz:{0})r)   r   r*   )r6   Zgit_showr.   r/   r   r   r   r2      s    r2   c                  c   s    t  } | V  tj| dd d S )NT)ignore_errors)tempfileZmkdtempshutilrmtree)r5   r   r   r   r      s    r   c                 C   s   t t| ddr| dS | S )zEnsure that the string is text.decodeNzutf-8)callablegetattrrJ   )stringr   r   r   r+      s    
r+   c                 C   s   t j| t jt jdS )N)r.   stderr)
subprocessPopenPIPE)Zcommandr   r   r   r)      s    
r)   c                 C   s*   t dddd| g}| \}}t| S )Nr(   configz--getz--bool)r)   r*   r+   r-   )	parameterrR   r.   r/   r   r   r   git_config_for   s    rT   c                 C   s8   d |  }d | }tj|t|}| tjv S )Nz
flake8_{0}z
flake8.{0})	r   upperr   environgetrT   lowerr   ZTRUTHY_VALUES)rS   Zenvironment_variableZgit_variablevaluer   r   r   
config_for   s    
rZ   c                    s    fdd| D S )Nc                    s$   g | ]}t j|r | n|qS r   )r   r   isabs).0patterntemporary_directory_pathr   r   
<listcomp>   s   

z#update_excludes.<locals>.<listcomp>r   )Zexclude_listr_   r   r^   r   r      s    
r   c                 C   s>   t |}| jD ]*}|j}||rtj||d  |_qd S r0   )lenZcheckersZdisplay_name
startswithr   r   r@   )Zchecker_managerZtemp_prefixZtemp_prefix_lengthZcheckerr6   r   r   r   r      s    


r   z#!{executable}
import sys

from flake8.main import git

if __name__ == '__main__':
    sys.exit(
        git.hook(
            strict=git.config_for('strict'),
            lazy=git.config_for('lazy'),
        )
    )
)FF) __doc__
contextlibr   Zos.pathrH   r    rO   r'   rG   Zflake8r   r   __all__r   r   r   r   r
   r3   r:   r4   r1   r2   contextmanagerr   r+   r)   rT   rZ   r   r   r   r   r   r   r   <module>   s<   
+2

	
