B
    ãUvge%  ã               @   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 ddlmZmZ ddlmZmZmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" dddgZ#e $e%¡Z&ej' (¡ Z)dd„ Z*dd„ Z+dd„ Z,ddd„Z-ddd„Z.ddd„Z/dS )a~  Backing implementation for InstallRequirement's various constructors

The idea here is that these formed a major chunk of InstallRequirement's size
so, moving them and support code dedicated to them outside of that class
helps creates for better understandability for the rest of the code.

These are meant to be used elsewhere within pip to create instances of
InstallRequirement.
é    N)ÚMarker)ÚInvalidRequirementÚRequirement)Ú	Specifier)ÚRequirementParseErrorÚparse_requirements)Úis_archive_fileÚis_urlÚpath_to_urlÚurl_to_path)ÚInstallationError)ÚPyPIÚTestPyPI)ÚLink)ÚInstallRequirement)Úis_installable_dir)Úvcs)ÚWheelÚinstall_req_from_editableÚinstall_req_from_lineÚparse_editablec             C   s6   t  d| ¡}d }|r*| d¡}| d¡}n| }||fS )Nz^(.+)(\[[^\]]+\])$é   é   )ÚreÚmatchÚgroup)ÚpathÚmÚextrasÚpath_no_extras© r    ú?/tmp/pip-install-r_9ig3yj/pip/pip/_internal/req/constructors.pyÚ_strip_extras)   s    
r"   c             C   s6  | }t |ƒ\}}tj |¡rFtj tj |d¡¡s>td| ƒ‚t|ƒ}| ¡  	d¡r„t
|ƒj}|rz||td| ¡  ƒjfS ||dfS x,tD ]$}| ¡  	d| ¡rŠd||f }P qŠW d|krÆtd	|  ƒ‚| dd
¡d  ¡ }t |¡�sd|  d dd„ tjD ƒ¡ d }t|ƒ‚t
|ƒj}|�s,td|  ƒ‚||dfS )a   Parses an editable requirement into:
        - a requirement name
        - an URL
        - extras
        - editable options
    Accepted requirements:
        svn+http://blahblah@rev#egg=Foobar[baz]&subdirectory=version_subdir
        .[some_extra]
    zsetup.pyz;Directory %r is not installable. File 'setup.py' not found.zfile:ÚplaceholderNz%s:z%s+%sú+zb%s should either be a path to a local project or a VCS url beginning with svn+, git+, hg+, or bzr+r   r   zFor --editable=%s only z, c             S   s   g | ]}|j d  ‘qS )z+URL)Úname)Ú.0Úbackendr    r    r!   ú
<listcomp>i   s    z"parse_editable.<locals>.<listcomp>z is currently supportedzZCould not detect requirement name for '%s', please specify one with #egg=your_package_name)r"   Úosr   ÚisdirÚexistsÚjoinr   r
   ÚlowerÚ
startswithr   Úegg_fragmentr   r   r   ÚsplitÚget_backendÚbackends)Úeditable_reqÚurlÚurl_no_extrasr   Úpackage_nameÚversion_controlÚvc_typeÚerror_messager    r    r!   r   5   sD    



c          	   C   s”   d}t j | ¡r„d}yFt| dƒ�2}tt| ¡ ƒƒ |dd|   d d d 7 }W d	Q R X W q� tk
r€   tj	d
|  dd� Y q�X n|d|  7 }|S )z„Returns helpful msg in case requirements file does not exist,
    or cannot be parsed.

    :params req: Requirements file path
    Ú z It does exist.Úrz The argument you provided z(%s) appears to be az" requirements file. If that is thez# case, use the '-r' flag to installz" the packages specified within it.Nz2Cannot parse '%s' as requirements             filer   )Úexc_infoz File '%s' does not exist.)
r)   r   r+   ÚopenÚnextr   Úreadr   ÚloggerÚdebug)ÚreqÚmsgÚfpr    r    r!   Údeduce_helpful_msgv   s    rE   Fc             C   sŽ   t | ƒ\}}}| d¡r"t|ƒ}	nd }	|d k	r^yt|ƒ}
W qb tk
rZ   td| ƒ‚Y qbX nd }
t|
||	dt|ƒ|||r~|ni ||pˆdd�
S )Nzfile:zInvalid requirement: '%s'Tr    )Ú
source_dirÚeditableÚlinkÚ
constraintÚisolatedÚoptionsÚwheel_cacher   )r   r.   r   r   r   r   r   r   )r3   Ú
comes_fromrJ   rK   rL   rI   r%   r4   Úextras_overriderF   rB   r    r    r!   r   ”   s&    


c                s`  t | ƒrd}nd}|| krF|  |d¡\} }| ¡ }|s<d}qJt|ƒ}nd}|  ¡ } d‰ tj tj | ¡¡}d}	d}
t | ƒr„t| ƒ}	nœt	|ƒ\}}
tj 
|¡oÈtjj| kpÈtjjdk	rÀtjj| kpÈ|  d¡}|rðt|ƒsâtd|  ƒ‚tt|ƒƒ}	n0t|ƒ�r tj |¡�st d| ¡ tt|ƒƒ}	|	�rŒ|	jdk�r`t d	|	j¡�r`tttj tj |	j¡¡ƒƒ}	|	j�r„t|	jƒ}d
|j|jf ‰ n|	j‰ n| ‰ |
�rªtd|
  ¡  ƒj!}
nd}
ˆ dk	�r<ytˆ ƒ‰ W nv t"k
�r:   tjjˆ k�röd}|t#ˆ ƒ7 }n0dˆ k�rt$‡ fdd„t%D ƒƒ�sd}nt& '¡ }tdˆ |f ƒ‚Y nX t(ˆ ||	|||�rR|ni |||
d�	S )z„Creates an InstallRequirement from a name, which might be a
    requirement, directory containing 'setup.py', filename, or URL.
    z; ú;r   NÚ.zODirectory %r is not installable. Neither 'setup.py' nor 'pyproject.toml' found.zARequirement %r looks like a filename, but the file does not existÚfilez\.\./z%s==%sr#   r    zIt looks like a path.ú=c             3   s   | ]}|ˆ kV  qd S )Nr    )r&   Úop)rB   r    r!   ú	<genexpr>  s    z(install_req_from_line.<locals>.<genexpr>z,= is not a valid operator. Did you mean == ?zInvalid requirement: '%s'
%s)rH   ÚmarkersrJ   rK   rL   rI   r   ))r	   r0   Ústripr   r)   r   ÚnormpathÚabspathr   r"   r*   ÚsepÚaltsepr.   r   r   r
   r   Úisfiler@   ÚwarningÚschemer   Úsearchr4   Úis_wheelr   Úfilenamer%   Úversionr/   r   r-   r   r   rE   ÚanyÚ	operatorsÚ	tracebackÚ
format_excr   )r%   rM   rJ   rK   rL   rI   Ú
marker_seprU   r   rH   r   ÚpÚlooks_like_dirÚwheelÚadd_msgr    )rB   r!   r   ±   s‚    





"c             C   sn   yt | ƒ} W n  tk
r,   td|  ƒ‚Y nX tjtjg}| jr^|jj|kr^td|j	| f ƒ‚t
| |||d�S )NzInvalid requirement: '%s'zkPackages installed from PyPI cannot depend on packages which are not also hosted on PyPI.
%s depends on %s )rJ   rL   )r   r   r   r   Úfile_storage_domainr   r4   rH   Únetlocr%   r   )rB   rM   rJ   rL   Údomains_not_allowedr    r    r!   Úinstall_req_from_req  s    rn   )NFNNF)NFNNF)NFN)0Ú__doc__Úloggingr)   r   rd   Zpip._vendor.packaging.markersr   Ú"pip._vendor.packaging.requirementsr   r   Z pip._vendor.packaging.specifiersr   Zpip._vendor.pkg_resourcesr   r   Úpip._internal.downloadr   r	   r
   r   Úpip._internal.exceptionsr   Úpip._internal.models.indexr   r   Úpip._internal.models.linkr   Zpip._internal.req.req_installr   Úpip._internal.utils.miscr   Úpip._internal.vcsr   Úpip._internal.wheelr   Ú__all__Ú	getLoggerÚ__name__r@   Ú
_operatorsÚkeysrc   r"   r   rE   r   r   rn   r    r    r    r!   Ú<module>	   s8   

A 
 
b