a
    hbòf_4  ã                   @   s–   d 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m	Z	 ddl
mZ G dd„ dƒZdd	„ ZG d
d„ deƒZejfdd„Zdd„ Zddd„ZdS )zsdistutils.filelist

Provides the FileList class, used for poking about the filesystem
and building lists of files.
é    N©Úconvert_path)ÚDistutilsTemplateErrorÚDistutilsInternalError)Úlogc                   @   s|   e Zd ZdZddd„Zdd„ Zejf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d„Zddd„ZdS ) ÚFileListaÑ  A list of files built by on exploring the filesystem and filtered by
    applying various patterns to what we find there.

    Instance attributes:
      dir
        directory from which files will be taken -- only used if
        'allfiles' not supplied to constructor
      files
        list of filenames currently being built/filtered/manipulated
      allfiles
        complete list of files under consideration (ie. without any
        filtering applied)
    Nc                 C   s   d | _ g | _d S ©N)ÚallfilesÚfiles)ÚselfÚwarnÚdebug_print© r   ú[/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/setuptools/_distutils/filelist.pyÚ__init__    s    zFileList.__init__c                 C   s
   || _ d S r   )r	   )r   r	   r   r   r   Úset_allfiles&   s    zFileList.set_allfilesc                 C   s   t |ƒ| _d S r   )Úfindallr	   )r   Údirr   r   r   r   )   s    zFileList.findallc                 C   s   ddl m} |rt|ƒ dS )z~Print 'msg' to stdout if the global DEBUG (taken from the
        DISTUTILS_DEBUG environment variable) flag is true.
        r   )ÚDEBUGN)Údistutils.debugr   Úprint)r   Úmsgr   r   r   r   r   ,   s    zFileList.debug_printc                 C   s   | j  |¡ d S r   )r
   Úappend)r   Úitemr   r   r   r   6   s    zFileList.appendc                 C   s   | j  |¡ d S r   )r
   Úextend)r   Úitemsr   r   r   r   9   s    zFileList.extendc                 C   s<   t ttjj| jƒƒ}g | _|D ]}| j tjj|Ž ¡ qd S r   )ÚsortedÚmapÚosÚpathÚsplitr
   r   Újoin)r   Zsortable_filesZ
sort_tupler   r   r   Úsort<   s    zFileList.sortc                 C   s@   t t| jƒd ddƒD ]$}| j| | j|d  kr| j|= qd S )Né   r   éÿÿÿÿ)ÚrangeÚlenr
   )r   Úir   r   r   Úremove_duplicatesE   s    zFileList.remove_duplicatesc                 C   sÞ   |  ¡ }|d }d  } }}|dv rTt|ƒdk r<td| ƒ‚dd„ |dd … D ƒ}n~|dv r˜t|ƒd	k rttd
| ƒ‚t|d ƒ}dd„ |dd … D ƒ}n:|dv rÆt|ƒdkr¸td| ƒ‚t|d ƒ}ntd| ƒ‚||||fS )Nr   )ÚincludeÚexcludeúglobal-includeúglobal-excludeé   z&'%s' expects <pattern1> <pattern2> ...c                 S   s   g | ]}t |ƒ‘qS r   r   ©Ú.0Úwr   r   r   Ú
<listcomp>X   ó    z1FileList._parse_template_line.<locals>.<listcomp>r#   )úrecursive-includeúrecursive-excludeé   z,'%s' expects <dir> <pattern1> <pattern2> ...c                 S   s   g | ]}t |ƒ‘qS r   r   r.   r   r   r   r1   ^   r2   )ÚgraftÚprunez#'%s' expects a single <dir_pattern>zunknown action '%s')r    r&   r   r   )r   ÚlineÚwordsÚactionÚpatternsr   Údir_patternr   r   r   Ú_parse_template_lineM   s0    ÿÿÿzFileList._parse_template_linec                 C   sD  |   |¡\}}}}|dkrV|  dd |¡ ¡ |D ]}| j|dd�s2t d|¡ q2�nê|dkrš|  dd |¡ ¡ |D ]}| j|dd�svt d	|¡ qv�n¦|d
krÞ|  dd |¡ ¡ |D ]}| j|dd�sºt d|¡ qº�nb|dk�r(|  dd |¡ ¡ |D ]"}| j|dd��s t d|¡ �q �n|dk�rz|  d|d |¡f ¡ |D ](}| j||d��sNd}t |||¡ �qNnÆ|dk�rÈ|  d|d |¡f ¡ |D ]$}| j||d��s t d||¡ �q nx|dk�rþ|  d| ¡ | jd |d��s@t d|¡ nB|dk�r4|  d| ¡ | jd |d��s@t d|¡ ntd| ƒ‚d S )Nr)   zinclude ú r#   )Úanchorz%warning: no files found matching '%s'r*   zexclude z9warning: no previously-included files found matching '%s'r+   zglobal-include r   z>warning: no files found matching '%s' anywhere in distributionr,   zglobal-exclude zRwarning: no previously-included files matching '%s' found anywhere in distributionr3   zrecursive-include %s %s)Úprefixz:warning: no files found matching '%s' under directory '%s'r4   zrecursive-exclude %s %szNwarning: no previously-included files matching '%s' found under directory '%s'r6   zgraft z+warning: no directories found matching '%s'r7   zprune z6no previously-included directories found matching '%s'z'this cannot happen: invalid action '%s')r=   r   r!   Úinclude_patternr   r   Úexclude_patternr   )r   r8   r:   r;   r   r<   Úpatternr   r   r   r   Úprocess_template_linei   s|    ÿ
ÿ
ÿ

þ
ÿÿ
ÿþ

ÿ
ÿÿzFileList.process_template_liner#   r   c                 C   sl   d}t ||||ƒ}|  d|j ¡ | jdu r4|  ¡  | jD ],}| |¡r:|  d| ¡ | j |¡ d}q:|S )a—  Select strings (presumably filenames) from 'self.files' that
        match 'pattern', a Unix-style wildcard (glob) pattern.  Patterns
        are not quite the same as implemented by the 'fnmatch' module: '*'
        and '?'  match non-special characters, where "special" is platform-
        dependent: slash on Unix; colon, slash, and backslash on
        DOS/Windows; and colon on Mac OS.

        If 'anchor' is true (the default), then the pattern match is more
        stringent: "*.py" will match "foo.py" but not "foo/bar.py".  If
        'anchor' is false, both of these will match.

        If 'prefix' is supplied, then only filenames starting with 'prefix'
        (itself a pattern) and ending with 'pattern', with anything in between
        them, will match.  'anchor' is ignored in this case.

        If 'is_regex' is true, 'anchor' and 'prefix' are ignored, and
        'pattern' is assumed to be either a string containing a regex or a
        regex object -- no translation is done, the regex is just compiled
        and used as-is.

        Selected strings will be added to self.files.

        Return True if files are found, False otherwise.
        Fz%include_pattern: applying regex r'%s'Nz adding T)Útranslate_patternr   rC   r	   r   Úsearchr
   r   )r   rC   r?   r@   Úis_regexÚfiles_foundÚ
pattern_reÚnamer   r   r   rA   ¶   s    ÿ


zFileList.include_patternc                 C   sr   d}t ||||ƒ}|  d|j ¡ tt| jƒd ddƒD ]4}| | j| ¡r8|  d| j|  ¡ | j|= d}q8|S )a  Remove strings (presumably filenames) from 'files' that match
        'pattern'.  Other parameters are the same as for
        'include_pattern()', above.
        The list 'self.files' is modified in place.
        Return True if files are found, False otherwise.
        Fz%exclude_pattern: applying regex r'%s'r#   r$   z
 removing T)rE   r   rC   r%   r&   r
   rF   )r   rC   r?   r@   rG   rH   rI   r'   r   r   r   rB   à   s    ÿzFileList.exclude_pattern)NN)r#   Nr   )r#   Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úcurdirr   r   r   r   r"   r(   r=   rD   rA   rB   r   r   r   r   r      s   

	M
+ ÿr   c                 C   s0   t  tj| dd�¡}dd„ |D ƒ}ttjj|ƒS )z%
    Find all files under 'path'
    T)Úfollowlinksc                 s   s,   | ]$\}}}|D ]}t j ||¡V  qqd S r   )r   r   r!   )r/   ÚbaseÚdirsr
   Úfiler   r   r   Ú	<genexpr>û   s   þz#_find_all_simple.<locals>.<genexpr>)Ú_UniqueDirsÚfilterr   Úwalkr   Úisfile)r   Z
all_uniqueÚresultsr   r   r   Ú_find_all_simpleö   s
    þrZ   c                   @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )rU   z†
    Exclude previously-seen dirs from walk results,
    avoiding infinite recursion.
    Ref https://bugs.python.org/issue44497.
    c                 C   sF   |\}}}t  |¡}|j|jf}|| v }|r6|dd…= |  |¡ | S )z®
        Given an item from an os.walk result, determine
        if the item represents a unique dir for this instance
        and if not, prevent further traversal.
        N)r   ÚstatÚst_devÚst_inoÚadd)r   Z	walk_itemrQ   rR   r
   r[   Ú	candidateÚfoundr   r   r   Ú__call__	  s    



z_UniqueDirs.__call__c                 C   s   t | ƒ |ƒS r   )rV   )Úclsr   r   r   r   rV     s    z_UniqueDirs.filterN)rK   rL   rM   rN   ra   ÚclassmethodrV   r   r   r   r   rU     s   rU   c                 C   s6   t | ƒ}| tjkr.tjtjj| d�}t||ƒ}t|ƒS )zŒ
    Find all files under 'dir' and return the list of full filenames.
    Unless dir is '.', return full filenames with dir prepended.
    )Ústart)	rZ   r   rO   Ú	functoolsÚpartialr   Úrelpathr   Úlist)r   r
   Zmake_relr   r   r   r     s
    

r   c                 C   s8   t  | ¡}tj}tjdkrd}d| }t d||¡}|S )zæTranslate a shell-like glob pattern to a regular expression; return
    a string containing the regex.  Differs from 'fnmatch.translate()' in
    that '*' does not match "special characters" (which are
    platform-specific).
    ú\z\\\\z\1[^%s]z((?<!\\)(\\\\)*)\.)ÚfnmatchÚ	translater   ÚsepÚreÚsub)rC   rI   rl   Úescapedr   r   r   Ú
glob_to_re)  s    

rp   r#   c           
      C   s
  |rt | tƒrt | ¡S | S tdƒ d¡\}}}| rVt| ƒ}| |¡rP| |¡sZJ ‚nd}|durât|ƒ}| |¡r~| |¡s‚J ‚|t|ƒt|ƒt|ƒ … }t	j
}	t	j
dkr²d}	|t|ƒt|ƒt|ƒ … }d|||	||f }n|�r d||t|ƒd… f }t |¡S )a  Translate a shell-like wildcard pattern to a compiled regular
    expression.  Return the compiled regex.  If 'is_regex' true,
    then 'pattern' is directly compiled to a regex (if it's a string)
    or just returned as-is (assumes it's a regex object).
    Ú_Ú Nri   z\\z%s\A%s%s.*%s%sz%s\A%s)Ú
isinstanceÚstrrm   Úcompilerp   Ú	partitionÚ
startswithÚendswithr&   r   rl   )
rC   r?   r@   rG   rd   rq   ÚendrI   Z	prefix_rerl   r   r   r   rE   @  s.    



ÿrE   )r#   Nr   )rN   r   rm   rj   re   Údistutils.utilr   Údistutils.errorsr   r   Ú	distutilsr   r   rZ   ÚsetrU   rO   r   rp   rE   r   r   r   r   Ú<module>   s    f