
0c@_Û9  ã               @   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 m Z d d l m	 Z	 d d l
 m Z d g Z e j e ƒ Z e j d e j ƒ Z e j d	 e j e j Bƒ Z e j d d
 … Z Gd d „  d e ƒ Z d S)zu
Class representing the list of files in a distribution.

Equivalent to distutils.filelist, but fixes some problems.
é    Né   )ÚDistlibException)Úfsdecode)Úconvert_pathÚManifestz\\w*
z#.*?(?=
)|
(?=$)é   c               @   sÇ   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d 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 d d d d „ Z d d „  Z d S)r   z~A list of files built by on exploring the filesystem and filtered by
    applying various patterns to what we find there.
    Nc             C   sY   t  j j t  j j | p! t  j ƒ  ƒ ƒ |  _ |  j t  j |  _ d |  _ t	 ƒ  |  _
 d S)zd
        Initialise an instance.

        :param base: The base directory to explore under.
        N)ÚosÚpathÚabspathÚnormpathÚgetcwdÚbaseÚsepÚprefixÚallfilesÚsetÚfiles)Úselfr   © r   ú;/tmp/pip-build-jynh7p1z/pip/pip/_vendor/distlib/manifest.pyÚ__init__*   s    -	zManifest.__init__c             C   sù   d d l  m } m } m } g  |  _ } |  j } | g } | j } | j } x¥ | rô | ƒ  } t j	 | ƒ }	 x€ |	 D]x }
 t j
 j | |
 ƒ } t j  | ƒ } | j } | | ƒ rÊ | j t | ƒ ƒ qu | | ƒ ru | | ƒ ru | | ƒ qu WqP Wd S)zmFind all files under the base and set ``allfiles`` to the absolute
        pathnames of files found.
        r   )ÚS_ISREGÚS_ISDIRÚS_ISLNKN)Ústatr   r   r   r   r   ÚpopÚappendr   Úlistdirr	   ÚjoinÚst_moder   )r   r   r   r   r   ÚrootÚstackr   ÚpushÚnamesÚnameÚfullnamer   Úmoder   r   r   Úfindall9   s"    							zManifest.findallc             C   sJ   | j  |  j ƒ s* t j j |  j | ƒ } |  j j t j j | ƒ ƒ d S)zz
        Add a file to the manifest.

        :param item: The pathname to add. This can be relative to the base.
        N)	Ú
startswithr   r   r	   r   r   r   Úaddr   )r   Úitemr   r   r   r)   T   s    zManifest.addc             C   s"   x | D] } |  j  | ƒ q Wd S)z†
        Add a list of files to the manifest.

        :param items: The pathnames to add. These can be relative to the base.
        N)r)   )r   Úitemsr*   r   r   r   Úadd_many^   s    zManifest.add_manyFc                s‹   ‡  ‡ f d d †  ‰  t  ˆ j ƒ } | rg t  ƒ  } x' | D] } ˆ  | t j j | ƒ ƒ q: W| | O} d d „  t d d „  | Dƒ ƒ Dƒ S)z8
        Return sorted files in directory order
        c                sg   |  j  | ƒ t j d | ƒ | ˆ j k rc t j j | ƒ \ } } | d k sV t ‚ ˆ  |  | ƒ d  S)Nzadd_dir added %sÚ ú/)r-   r.   )r)   ÚloggerÚdebugr   r   r	   ÚsplitÚAssertionError)ÚdirsÚdÚparentÚ_)Úadd_dirr   r   r   r7   l   s    z Manifest.sorted.<locals>.add_dirc             S   s"   g  |  ] } t  j j | Œ  ‘ q Sr   )r   r	   r   )Ú.0Z
path_tupler   r   r   ú
<listcomp>z   s   	 z#Manifest.sorted.<locals>.<listcomp>c             s   s!   |  ] } t  j j | ƒ Vq d  S)N)r   r	   r1   )r8   r	   r   r   r   ú	<genexpr>{   s    z"Manifest.sorted.<locals>.<genexpr>)r   r   r   r	   ÚdirnameÚsorted)r   ZwantdirsÚresultr3   Úfr   )r7   r   r   r<   g   s    	
	zManifest.sortedc             C   s   t  ƒ  |  _ g  |  _ d S)zClear all collected files.N)r   r   r   )r   r   r   r   Úclear}   s    zManifest.clearc             C   s  |  j  | ƒ \ } } } } | d k r` xÚ| D]+ } |  j | d d ƒs. t j d | ƒ q. Wn¤| d k r• x•| D] } |  j | d d ƒ} qs Wno| d k rÚ x`| D]+ } |  j | d d ƒs¨ t j d | ƒ q¨ Wn*| d	 k rx| D] } |  j | d d ƒ} qí Wnõ | d
 k rWxæ | D]. } |  j | d | ƒs"t j d | | ƒ q"Wn­ | d k rŒxž | D] } |  j | d | ƒ} qjWnx | d k rÀ|  j d d | ƒst j d | ƒ nD | d k rô|  j d d | ƒst j d | ƒ n t d | ƒ ‚ d S)av  
        Process a directive which either adds some files from ``allfiles`` to
        ``files``, or removes some files from ``files``.

        :param directive: The directive to process. This should be in a format
                     compatible with distutils ``MANIFEST.in`` files:

                     http://docs.python.org/distutils/sourcedist.html#commands
        ÚincludeÚanchorTzno files found matching %rÚexcludezglobal-includeFz3no files found matching %r anywhere in distributionzglobal-excludezrecursive-includer   z-no files found matching %r under directory %rzrecursive-excludeÚgraftNz no directories found matching %rÚprunez4no previously-included directories found matching %rzinvalid action %r)Ú_parse_directiveÚ_include_patternr/   ÚwarningÚ_exclude_patternr   )r   Ú	directiveÚactionÚpatternsÚthedirZ
dirpatternÚpatternÚfoundr   r   r   Úprocess_directive‚   sD    			
	
zManifest.process_directivec          	   C   sc  | j  ƒ  } t | ƒ d k r> | d d k r> | j d d ƒ | d } d } } } | d k r¤ t | ƒ d k  r„ t d | ƒ ‚ d d „  | d d … Dƒ } n¯ | d k rt | ƒ d k  rÒ t d | ƒ ‚ t | d ƒ } d d „  | d d … Dƒ } nQ | d k rCt | ƒ d k r0t d | ƒ ‚ t | d ƒ } n t d | ƒ ‚ | | | | f S)zŸ
        Validate a directive.
        :param directive: The directive to validate.
        :return: A tuple of action, patterns, thedir, dir_patterns
        r   r   r@   rB   úglobal-includeúglobal-excludeúrecursive-includeúrecursive-excluderC   rD   Nr   z$%r expects <pattern1> <pattern2> ...c             S   s   g  |  ] } t  | ƒ ‘ q Sr   )r   )r8   Úwordr   r   r   r9   ê   s   	 z-Manifest._parse_directive.<locals>.<listcomp>é   z*%r expects <dir> <pattern1> <pattern2> ...c             S   s   g  |  ] } t  | ƒ ‘ q Sr   )r   )r8   rT   r   r   r   r9   ò   s   	 z!%r expects a single <dir_pattern>zunknown action %r)úincludeúexcluderP   rQ   rR   rS   úgraftúprune)rV   rW   rP   rQ   )rR   rS   )rX   rY   )r1   ÚlenÚinsertr   r   )r   rI   ÚwordsrJ   rK   rL   Zdir_patternr   r   r   rE   Ñ   s:        	
	  zManifest._parse_directiveTc             C   st   d } |  j  | | | | ƒ } |  j d k r7 |  j ƒ  x6 |  j D]+ } | j | ƒ rA |  j j | ƒ d } qA 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.
        FNT)Ú_translate_patternr   r'   Úsearchr   r)   )r   rM   rA   r   Úis_regexrN   Ú
pattern_rer$   r   r   r   rF      s    

zManifest._include_patternc             C   sa   d } |  j  | | | | ƒ } x< t |  j ƒ D]+ } | j | ƒ r. |  j j | ƒ d } q. W| S)at  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.

        This API is public to allow e.g. exclusion of SCM subdirs, e.g. when
        packaging source distributions
        FT)r]   Úlistr   r^   Úremove)r   rM   rA   r   r_   rN   r`   r>   r   r   r   rH   )  s    
zManifest._exclude_patternc             C   sK  | r& t  | t ƒ r" t j | ƒ S| St d k rS |  j d ƒ j d ƒ \ } } } | r› |  j | ƒ } t d k r¡ | j | ƒ r’ | j | ƒ s¡ t	 ‚ n d } t j
 t j j |  j d ƒ ƒ }	 | d k	 røt d k r|  j d ƒ }
 |  j | ƒ d t |
 ƒ … } nY |  j | ƒ } | j | ƒ r9| j | ƒ s?t	 ‚ | t | ƒ t | ƒ t | ƒ … } t j } t j d k rƒd } t d k r³d |	 | j | d	 | f ƒ } q>| t | ƒ t | ƒ t | ƒ … } d
 | |	 | | | | f } nF | r>t d k rd |	 | } n# d | |	 | t | ƒ d … f } 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).
        rU   r   r6   r-   Nú\z\\ú^z.*z%s%s%s%s.*%s%sz%s%s%s)rU   r   )rU   r   )rU   r   )rU   r   )rU   r   )Ú
isinstanceÚstrÚreÚcompileÚ_PYTHON_VERSIONÚ_glob_to_reÚ	partitionr(   Úendswithr2   Úescaper   r	   r   r   rZ   r   )r   rM   rA   r   r_   Ústartr6   Úendr`   r   Zempty_patternZ	prefix_rer   r   r   r   r]   =  sB    	!'!#$&	&#zManifest._translate_patternc             C   sP   t  j | ƒ } t j } t j d k r- d } 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).
        rc   z\\\\z\1[^%s]z((?<!\\)(\\\\)*)\.)ÚfnmatchÚ	translater   r   rg   Úsub)r   rM   r`   r   Úescapedr   r   r   rj   t  s    	
zManifest._glob_to_re)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r)   r,   r<   r?   rO   rE   rF   rH   r]   rj   r   r   r   r   r   %   s    
	O/(6)rw   rp   Úloggingr   rg   Úsysr-   r   Úcompatr   Úutilr   Ú__all__Ú	getLoggerrt   r/   rh   ÚMZ_COLLAPSE_PATTERNÚSZ_COMMENTED_LINEÚversion_infori   Úobjectr   r   r   r   r   Ú<module>
   s   	