î
]á[\°1  ã               @   sª   d  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
 Gd d „  d ƒ Z e j d d	 „ Z d
 d „  Z d d 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 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 d „  Z d d d d d „ Z d d 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   ú(/usr/lib/python3.4/distutils/filelist.pyÚ__init__   s    	zFileList.__init__c             C   s   | |  _  d  S)N)r   )r	   r   r   r   r   Úset_allfiles"   s    zFileList.set_allfilesc             C   s   t  | ƒ |  _ d  S)N)Úfindallr   )r	   Údirr   r   r   r   %   s    zFileList.findallc             C   s'   d d l  m } | r# t | ƒ n  d S)z~Print 'msg' to stdout if the global DEBUG (taken from the
        DISTUTILS_DEBUG environment variable) flag is true.
        r   )ÚDEBUGN)Zdistutils.debugr   Úprint)r	   Úmsgr   r   r   r   r   (   s    zFileList.debug_printc             C   s   |  j  j | ƒ d  S)N)r   Úappend)r	   Úitemr   r   r   r   2   s    zFileList.appendc             C   s   |  j  j | ƒ d  S)N)r   Úextend)r	   Úitemsr   r   r   r   5   s    zFileList.extendc             C   sX   t  t t j j |  j ƒ ƒ } g  |  _ x* | D]" } |  j j t j j | Œ  ƒ q. Wd  S)N)ÚsortedÚmapÚosÚpathÚsplitr   r   Újoin)r	   Zsortable_filesZ
sort_tupler   r   r   Úsort8   s    	zFileList.sortc             C   sY   xR t  t |  j ƒ d d d ƒ D]1 } |  j | |  j | d k r  |  j | =q  q  Wd  S)Né   r   éÿÿÿÿ)ÚrangeÚlenr   )r	   Úir   r   r   Úremove_duplicatesB   s    &zFileList.remove_duplicatesc             C   s:  | j  ƒ  } | d } d  } } } | d k ru t | ƒ d k  rU t d | ƒ ‚ n  d d	 „  | d
 d  … Dƒ } nµ | d k rÖ t | ƒ d k  r¦ t d | ƒ ‚ n  t | d
 ƒ } d d	 „  | d d  … Dƒ } nT | d k rt | ƒ d k rt d | ƒ ‚ n  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 Sr   )r   )Ú.0Úwr   r   r   ú
<listcomp>V   s   	 z1FileList._parse_template_line.<locals>.<listcomp>r    úrecursive-includeúrecursive-excludeé   z,'%s' expects <dir> <pattern1> <pattern2> ...c             S   s   g  |  ] } t  | ƒ ‘ q Sr   )r   )r+   r,   r   r   r   r-   \   s   	 ÚgraftÚprunez#'%s' expects a single <dir_pattern>zunknown action '%s')r&   r'   r(   r)   )r.   r/   )r1   r2   )r   r#   r   r   )r	   ÚlineZwordsÚactionÚpatternsr   Údir_patternr   r   r   Ú_parse_template_lineK   s,    
	  zFileList._parse_template_linec             C   s  |  j  | ƒ \ } } } } | d k r} |  j d d j | ƒ ƒ xÕ| D]. } |  j | d d ƒsH t j d | ƒ qH qH Wnœ| d k rß |  j d d j | ƒ ƒ xs| D]. } |  j | d d ƒsª t j d	 | ƒ qª qª Wn:| d
 k rA|  j d d j | ƒ ƒ x| D]. } |  j | d d ƒst j d | ƒ qqWnØ| d k r£|  j d d j | ƒ ƒ x¯| D]. } |  j | d d ƒsnt j d | ƒ qnqnWnv| d k r|  j d | d j | ƒ f ƒ xG| D]1 } |  j | d | ƒsÖt j d | | ƒ qÖqÖWn| d k ry|  j d | d j | ƒ f ƒ xÜ | D]1 } |  j | d | ƒsAt j d | | ƒ qAqAWn  | d k rÁ|  j d | ƒ |  j d  d | ƒst j d | ƒ qnX | d k r	|  j d | ƒ |  j d  d | ƒst j d | ƒ qn t d | ƒ ‚ d  S)Nr&   zinclude ú Úanchorr    z%warning: no files found matching '%s'r'   zexclude z9warning: no previously-included files found matching '%s'zglobal-includezglobal-include r   z>warning: no files found matching '%s' anywhere in distributionzglobal-excludezglobal-exclude zRwarning: no previously-included files matching '%s' found anywhere in distributionzrecursive-includezrecursive-include %s %sÚprefixz:warning: no files found matching '%s' under directory '%s'zrecursive-excludezrecursive-exclude %s %szNwarning: no previously-included files matching '%s' found under directory '%s'r1   zgraft z+warning: no directories found matching '%s'r2   zprune z6no previously-included directories found matching '%s'z'this cannot happen: invalid action '%s')r7   r   r   Úinclude_patternr   r
   Úexclude_patternr   )r	   r3   r4   r5   r   r6   Úpatternr   r   r   Úprocess_template_lineg   sf    										zFileList.process_template_liner    r   c             C   sœ   d } t  | | | | ƒ } |  j d | j ƒ |  j d k rK |  j ƒ  n  xJ |  j D]? } | j | ƒ rU |  j d | ƒ |  j j | ƒ d } qU qU W| 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   r=   r   r   Úsearchr   r   )r	   r=   r9   r:   Úis_regexÚfiles_foundÚ
pattern_reÚnamer   r   r   r;   ³   s    	zFileList.include_patternc             C   sž   d } t  | | | | ƒ } |  j d | j ƒ xh t t |  j ƒ d d d ƒ D]G } | j |  j | ƒ rO |  j d |  j | ƒ |  j | =d } qO qO W| 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    z
 removing Tr!   r!   )r?   r   r=   r"   r#   r   r@   )r	   r=   r9   r:   rA   rB   rC   r$   r   r   r   r<   Þ   s    	&
zFileList.exclude_pattern)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úcurdirr   r   r   r   r   r%   r7   r>   r;   r<   r   r   r   r   r      s   

	L,r   c             C   s  d d l  m } m } m } m } g  } |  g } | j } | j } x» | r | ƒ  }  t j |  ƒ }	 x– |	 D]Ž }
 |  t j	 k r˜ t j
 j |  |
 ƒ } n |
 } t j  | ƒ } | | } | | ƒ rÓ | j | ƒ qk | | ƒ rk | | ƒ rk | | ƒ qk qk WqF W| S)z^Find all files under 'dir' and return the list of full filenames
    (relative to 'dir').
    r   )ÚST_MODEÚS_ISREGÚS_ISDIRÚS_ISLNK)ÚstatrJ   rK   rL   rM   Úpopr   r   ÚlistdirrI   r   r   )r   rJ   rK   rL   rM   ÚlistÚstackrO   ÚpushÚnamesrD   ÚfullnamerN   Úmoder   r   r   r   õ   s&    "					
r   c             C   sS   t  j |  ƒ } t j } t j d k r0 d } n  d | } t j 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)r=   rC   rZ   Zescapedr   r   r   Ú
glob_to_re  s    		
r]   r    c             C   sÚ   | r) t  |  t ƒ r" t j |  ƒ S|  Sn  |  r> t |  ƒ } n d } | d k	 rº t d ƒ } t | ƒ d t | ƒ … } t j } t j d k rš d } n  d | j | d | f ƒ } n | rÍ d | } n  t j | ƒ 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).
    Ú NrW   z\\ú^z.*)	Ú
isinstanceÚstrr[   Úcompiler]   r#   r   rZ   r   )r=   r9   r:   rA   rC   Zempty_patternZ	prefix_rerZ   r   r   r   r?   +  s"    		 r?   )rH   r   r[   rX   Zdistutils.utilr   Zdistutils.errorsr   r   Z	distutilsr   r   rI   r   r]   r?   r   r   r   r   Ú<module>   s   è