U
    @vg?                     @   sB  d 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dl	m
Z
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mZ dd
l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l"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ee,e)ge)f Z-ee,ee) f Z.e/e0Z1dddZ2G dd de3Z4dS )ay  Dependency Resolution

The dependency resolution in pip is performed as follows:

for top-level requirements:
    a. only one spec allowed per project, regardless of conflicts or not.
       otherwise a "double requirement" exception is raised
    b. they override sub-dependency requirements.
for sub-dependencies
    a. "first found, wins" (where the order is breadth first)
    N)defaultdict)chain)
specifiers)BestVersionAlreadyInstalledDistributionNotFound	HashError
HashErrorsUnsupportedPythonVersion)
indent_log)dist_in_usersitenormalize_version_info)check_requires_pythonget_requires_python)MYPY_CHECK_RUNNING)CallableDefaultDictListOptionalSetTuple)pkg_resources)AbstractDistribution)PackageFinder)RequirementPreparer)InstallRequirement)RequirementSetFc              
   C   s   t | }zt||d}W n: tjk
rR } ztd| j| W Y dS d}~X Y nX |r\dS dtt	|}|rt
d| j|| dS td| j||dS )a  
    Check whether the given Python version is compatible with a distribution's
    "Requires-Python" value.

    :param version_info: A 3-tuple of ints representing the Python
        major-minor-micro version to check.
    :param ignore_requires_python: Whether to ignore the "Requires-Python"
        value if the given Python version isn't compatible.

    :raises UnsupportedPythonVersion: When the given Python version isn't
        compatible.
    )version_infoz-Package %r has an invalid Requires-Python: %sN.zBIgnoring failed Requires-Python check for package %r: %s not in %rz8Package {!r} requires a different Python: {} not in {!r})r   r   r   InvalidSpecifierloggerwarningproject_namejoinmapstrdebugr	   format)distr   ignore_requires_pythonrequires_pythonis_compatibleexcversion r-   @/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/legacy_resolve.py_check_dist_requires_python9   s>     
     r/   c                       sd   e Zd ZdZdddhZd 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  ZS )ResolverzResolves which packages need to be installed/uninstalled to perform     the requested operation without breaking the requirements of any package.
    eageronly-if-neededto-satisfy-onlyNc                    s   t t|   |	| jkst|
d kr4tjd d }
nt|
}
|
| _|| _	|| _
|	| _|| _|| _|| _|| _|| _|| _tt| _d S )N   )superr0   __init___allowed_strategiesAssertionErrorsysr   r   _py_version_infopreparerfinderupgrade_strategyforce_reinstallignore_dependenciesignore_installedr(   use_user_site_make_install_reqr   list_discovered_dependencies)selfr;   r<   make_install_reqrA   r?   r@   r(   r>   r=   py_version_info	__class__r-   r.   r6   p   s"    zResolver.__init__c                 C   s   |j t|j  }g }t }t||D ]N}z|| || W q( tk
rt } z||_	|
| W 5 d}~X Y q(X q(|r|dS )a  Resolve what operations need to be done

        As a side-effect of this method, the packages (and their dependencies)
        are downloaded, unpacked and prepared for installation. This
        preparation is done by ``pip.operations.prepare``.

        Once PyPI has static dependency metadata available, it would be
        possible to move the preparation to become a step separated from
        dependency resolution.
        N)unnamed_requirementsrC   requirementsvaluesr   r   extend_resolve_oner   reqappend)rE   requirement_set	root_reqsdiscovered_reqshash_errorsrO   r+   r-   r-   r.   resolve   s    	zResolver.resolvec                 C   s4   | j dkrdS | j dkrdS | j dks*t|jS d S )Nr3   Fr1   Tr2   )r=   r8   	is_directrE   rO   r-   r-   r.   _is_upgrade_allowed   s    

zResolver._is_upgrade_allowedc                 C   s    | j rt|jrd|_d|_dS )z4
        Set a requirement to be installed.
        TN)rA   r   satisfied_byshould_reinstallrW   r-   r-   r.   _set_req_to_reinstall   s    zResolver._set_req_to_reinstallc                 C   s   | j r
dS || j |js dS | jr4| | dS | |sP| jdkrLdS dS |jsz| j	j
|dd W n( tk
r   Y dS  tk
r   Y nX | | dS )a  Check if req_to_install should be skipped.

        This will check if the req is installed, and whether we should upgrade
        or reinstall it, taking into account all the relevant user options.

        After calling this req_to_install will only have satisfied_by set to
        None if the req_to_install is to be upgraded/reinstalled etc. Any
        other value will be a dist recording the current thing installed that
        satisfies the requirement.

        Note that for vcs urls and the like we can't assess skipping in this
        routine - we simply identify that we need to pull the thing down,
        then later on it is pulled down and introspected to assess upgrade/
        reinstalls etc.

        :return: A text reason for why it was skipped, or None.
        Nr2   z#already satisfied, skipping upgradezalready satisfiedT)upgradezalready up-to-date)r@   check_if_existsrA   rY   r>   r[   rX   r=   linkr<   find_requirementr   r   )rE   req_to_installr-   r-   r.   _check_skip_installed   s*    



zResolver._check_skip_installedc                 C   s   |j r| j|S |jdks t| |}|jr>| j||S | |}| jj}|	| j
|| | j|}| js~|| j |jr| jdkp| jp| jp|jjdk}|r| | ntd| |S )zzTakes a InstallRequirement and returns a single AbstractDist         representing a prepared variant of the same.
        Nr3   filez<Requirement already satisfied (use --upgrade to upgrade): %s)editabler;   prepare_editable_requirementrY   r8   ra   prepare_installed_requirementrX   require_hashespopulate_linkr<   prepare_linked_requirementr@   r]   rA   r=   r>   r^   schemer[   r   info)rE   rO   skip_reasonupgrade_allowedrf   abstract_distshould_modifyr-   r-   r.   _get_abstract_dist_for  s<    
 



zResolver._get_abstract_dist_forc           
   	      s8  j sjrg S d_j }| }t|jjd g   fdd}t	  
jsjsztjdd jsjrtddj ttjt|j }|D ]}td	|| qtt|jtj@ }||D ]}	||	|d
 qjs*js*j W 5 Q R X  S )zxPrepare a single requirements file.

        :return: A list of additional InstallRequirements to also install.
        T)r   r(   c                    sP    t| }j}j|||d\}}|rB|rBj| |  | d S )N)parent_req_nameextras_requested)rB   r$   nameadd_requirementrD   rP   rM   )subreqrq   sub_install_reqrp   to_scan_againadd_to_parent	more_reqsr`   rQ   rE   r-   r.   add_reqZ  s    

z&Resolver._resolve_one.<locals>.add_reqN)rp   z!Installing extra requirements: %r,z"%s does not provide the extra '%s')rq   )
constraintpreparedreqs_to_cleanuprP   ro   get_pkg_resources_distributionr/   r:   r(   r
   has_requirementrr   rV   r8   rs   r?   extrasr   r%   r"   sortedsetr    requiresrc   rY   successfully_downloaded)
rE   rQ   r`   rm   r'   rz   missing_requestedmissingavailable_requestedrt   r-   rx   r.   rN   8  sV    
 
 
 zResolver._resolve_onec                    s8   g  t   fdd|j D ]}| q& S )zCreate the installation order.

        The installation order is topological - requirements are installed
        before the requiring thing. We break cycles at an arbitrary point,
        and make no other guarantees.
        c                    sN   | j s| krd S | jrd S |  j| j D ]}| q2 |  d S )N)rY   r|   addrD   rr   rP   )rO   deporderordered_reqsschedulerE   r-   r.   r     s    

z1Resolver.get_installation_order.<locals>.schedule)r   rK   rL   )rE   req_setinstall_reqr-   r   r.   get_installation_order  s    

zResolver.get_installation_order)N)__name__
__module____qualname____doc__r7   r6   rU   rX   r[   ra   ro   rN   r   __classcell__r-   r-   rH   r.   r0   i   s   
 &#
55\r0   )F)5r   loggingr9   collectionsr   	itertoolsr   Zpip._vendor.packagingr   pip._internal.exceptionsr   r   r   r   r	   pip._internal.utils.loggingr
   pip._internal.utils.miscr   r   pip._internal.utils.packagingr   r   pip._internal.utils.typingr   typingr   r   r   r   r   r   pip._vendorr   pip._internal.distributionsr   "pip._internal.index.package_finderr    pip._internal.operations.preparer   pip._internal.req.req_installr   Zpip._internal.req.req_setr   r$   ZInstallRequirementProviderZDiscoveredDependencies	getLoggerr   r   r/   objectr0   r-   r-   r-   r.   <module>   s4    

 
0