U
    @vg48                     @   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
 ddl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lmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& e!rddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ dddgZ0e1e2Z3e
j45 Z6dd Z7dd Z8dd Z9dd Z:dd Z;G d d! d!e<Z=d"d# Z>d/d%dZ?d&d' Z@d(d) ZAd*d+ ZBd0d,dZCd1d-d.ZDdS )2a~  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)InstallationError)PyPITestPyPI)Link)Wheel)make_pyproject_path)InstallRequirement)ARCHIVE_EXTENSIONS)is_installable_dirsplitext)MYPY_CHECK_RUNNING)path_to_url)is_urlvcs)AnyDictOptionalSetTupleUnion)
WheelCacheinstall_req_from_editableinstall_req_from_lineparse_editablec                 C   s    t | d  }|tkrdS dS )z9Return True if `name` is a considered as an archive file.   TF)r   lowerr   )nameext r$   B/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/req/constructors.pyis_archive_file3   s    r&   c                 C   s6   t d| }d }|r*|d}|d}n| }||fS )Nz^(.+)(\[[^\]]+\])$r       )rematchgroup)pathmextraspath_no_extrasr$   r$   r%   _strip_extras<   s    
r/   c                 C   s   | s
t  S td|   jS )Nplaceholder)setr   r!   r-   )r-   r$   r$   r%   convert_extrasI   s    r2   c           
      C   s`  | }t |\}}tj|rptjtj|dshdtj|}t|}tj	|r`|d7 }t
|t|}| drt|j}|r||td|  jfS ||dfS tD ]&}| d| rd||f } qqd	|krt
d
| |d	dd  }t|s:d|  ddd tjD  d }	t
|	t|j}|sVt
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.pyzMFile "setup.py" not found. Directory cannot be installed in editable mode: {}zb
(A "pyproject.toml" file was found, but editable mode currently requires a setup.py based build.)zfile:r0   Nz%s:z%s+%s+z{} is not a valid editable requirement. It 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r"   ).0backendr$   r$   r%   
<listcomp>   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joinformatabspathr   isfiler   r   r!   
startswithr   egg_fragmentr   r-   r   splitget_backendbackends)
editable_requrlurl_no_extrasr-   msgpyproject_pathpackage_nameversion_controlvc_typeerror_messager$   r$   r%   r   P   sb    



c              	   C   s   d}t j| rd}zFt| d2}tt|  |dd|   d d d 7 }W 5 Q R X W q tk
r   tj	d	|  d
d Y qX n|d|  7 }|S )zReturns 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.z2Cannot parse '%s' as requirements             fileT)exc_infoz File '%s' does not exist.)
r8   r+   r:   opennextr   readr   loggerdebug)reqrG   fpr$   r$   r%   deduce_helpful_msg   s.    rW   c                   @   s   e Zd Zdd ZdS )RequirementPartsc                 C   s   || _ || _|| _|| _d S N)requirementlinkmarkersr-   )selfrZ   r[   r\   r-   r$   r$   r%   __init__   s    zRequirementParts.__init__N)__name__
__module____qualname__r^   r$   r$   r$   r%   rX      s   rX   c                 C   s`   t | \}}}|d k	rFzt|}W qJ tk
rB   td| Y qJX nd }t|}t||d |S )NInvalid requirement: '%s')r   r   r   r   r   rX   )rD   r"   rE   extras_overriderU   r[   r$   r$   r%   parse_req_from_editable   s    rd   Fc           	      C   sL   t | }|jjdkr|jjnd }t|j||d|j||||r>|ni ||jdS )NfileT)	
source_direditabler[   
constraint
use_pep517isolatedoptionswheel_cacher-   )rd   r[   scheme	file_pathr   rZ   r-   )	rD   
comes_fromri   rj   rk   rl   rh   partsrf   r$   r$   r%   r      s      
c                 C   s>   t jj| krdS t jjdk	r,t jj| kr,dS | dr:dS dS )ak  Checks whether the string "looks like" a path on the filesystem.

    This does not check whether the target actually exists, only judge from the
    appearance.

    Returns true if any of the following conditions is true:
    * a path separator is found (either os.path.sep or os.path.altsep);
    * a dot is found (which represents the current directory).
    TN.F)r8   r+   sepaltsepr?   r4   r$   r$   r%   _looks_like_path   s    
rt   c                 C   s   t |r0tj| r0t| r$t| S td| t| s<dS tj| rPt| S |	dd}t
|dkrxt |d sxdS td| t| S )ad  
    First, it checks whether a provided path is an installable directory
    (e.g. it has a setup.py). If it is, returns the path.

    If false, check if the path is an archive file (such as a .whl).
    The function checks if the path is a file. If false, if the path has
    an @, it will treat it as a PEP 440 URL requirement and return the path.
    zODirectory %r is not installable. Neither 'setup.py' nor 'pyproject.toml' found.N@r    r'   r   zARequirement %r looks like a filename, but the file does not exist)rt   r8   r+   r9   r   r   r   r&   r>   rA   lenrS   warning)r+   r"   urlreq_partsr$   r$   r%   _get_url_from_path  s(    
ry   c                    s  t | rd}nd}|| krF| |d\} }| }|s<d }qJt|}nd }|  } d tjtj| }d }d }t | rt| }n&t	|\}}t
|| }	|	d k	rt|	}|r|jdkrtd|jrtttjtj|j}|jr
t|j}
d|
j|
jf n|jn| t|} fdd}d k	rzt}W n tk
r   tjjkrrd	}|t7 }n,d
krtfddtD sd}nd}|d}|r|d|7 }t|Y nX nd }t||||S )Nz; ;r    re   z\.\./z%s==%sc                    s    s| S d |  S )Nz{} (from {}))r<   )text)line_sourcer$   r%   with_source\  s    z(parse_req_from_line.<locals>.with_sourcezIt looks like a path.=c                 3   s   | ]}| kV  qd S rY   r$   )r5   op)req_as_stringr$   r%   	<genexpr>j  s     z&parse_req_from_line.<locals>.<genexpr>z,= is not a valid operator. Did you mean == ?rM   zInvalid requirement: {!r}z	
Hint: {}) r   rA   stripr   r8   r+   normpathr=   r   r/   ry   rm   r(   searchrE   r   is_wheelr   filenamer"   versionr@   r2   r   r   rr   rW   any	operatorsr<   r   rX   )r"   r|   
marker_sepmarkers_as_stringr\   r+   r[   extras_as_stringprE   wheelr-   r}   rU   add_msgrG   r$   )r|   r   r%   parse_req_from_line*  sj    





r   c           	      C   s6   t | |}t|j||j|j|||r&|ni |||jd
S )a  Creates an InstallRequirement from a name, which might be a
    requirement, directory containing 'setup.py', filename, or URL.

    :param line_source: An optional string describing where the line is from,
        for logging purposes in case of an error.
    )r[   r\   ri   rj   rk   rl   rh   r-   )r   r   rZ   r[   r\   r-   )	r"   ro   ri   rj   rk   rl   rh   r|   rp   r$   r$   r%   r   z  s    
    
c                 C   sz   zt | }W n  tk
r,   td|  Y nX tjtjg}|jrh|rh|jrh|jj|krhtd|j	|f t
|||||dS )Nrb   zkPackages installed from PyPI cannot depend on packages which are not also hosted on PyPI.
%s depends on %s )rj   rl   ri   )r   r   r   r	   file_storage_domainr
   rE   r[   netlocr"   r   )
req_stringro   rj   rl   ri   rU   domains_not_allowedr$   r$   r%   install_req_from_req_string  s,    
   r   )NNFNNF)NNFNNFN)NFNN)E__doc__loggingr8   r(   pip._vendor.packaging.markersr   "pip._vendor.packaging.requirementsr   r   Z pip._vendor.packaging.specifiersr   Zpip._vendor.pkg_resourcesr   r   pip._internal.exceptionsr   pip._internal.models.indexr	   r
   pip._internal.models.linkr   pip._internal.models.wheelr   pip._internal.pyprojectr   pip._internal.req.req_installr   pip._internal.utils.filetypesr   pip._internal.utils.miscr   r   pip._internal.utils.typingr   pip._internal.utils.urlsr   pip._internal.vcsr   r   typingr   r   r   r   r   r   pip._internal.cacher   __all__	getLoggerr_   rS   
_operatorskeysr   r&   r/   r2   r   rW   objectrX   rd   r   rt   ry   r   r   r   r$   r$   r$   r%   <module>   sr     

	J      
"R       
    