B
    Uvg
5                 @   s   d Z ddl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 ddlmZmZ dd	lmZ eeZG d
d deZd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)BestVersionAlreadyInstalledDistributionNotFound	HashError
HashErrorsUnsupportedPythonVersion)install_req_from_req)
indent_log)dist_in_usersite
ensure_dir)check_dist_requires_pythonc                   sb   e Zd ZdZdddhZ 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.
    eagerzonly-if-neededzto-satisfy-onlyc                sr   t t|   || jkst|| _|| _|| _|| _d | _	|| _
|	| _|
| _|| _|| _|| _|| _tt| _d S )N)superr   __init___allowed_strategiesAssertionErrorpreparerfindersessionwheel_cacherequire_hashesupgrade_strategyforce_reinstallisolatedignore_dependenciesignore_installedignore_requires_pythonuse_user_siter   list_discovered_dependencies)selfr   r   r   r   r   r   r   r   r   r   r   )	__class__ 6/tmp/pip-install-r_9ig3yj/pip/pip/_internal/resolve.pyr   $   s    zResolver.__init__c             C   s   | j jrt| j j |jt|j  }|jp>tdd |D | _| j	
 }|rZt| g }t }x\t||D ]N}y|| || W qp tk
r } z||_|| W dd}~X Y qpX qpW |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.
        c             s   s   | ]}|j V  qd S )N)has_hash_options).0reqr$   r$   r%   	<genexpr>U   s    z#Resolver.resolve.<locals>.<genexpr>N)r   wheel_download_dirr   unnamed_requirementsr    requirementsvaluesr   anyr   get_formatted_locationsloggerinfor   r   extend_resolve_oner   r(   append)r"   requirement_set	root_reqs	locationsdiscovered_reqshash_errorsr(   excr$   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 )Nzto-satisfy-onlyFr   Tzonly-if-needed)r   r   	is_direct)r"   r(   r$   r$   r%   _is_upgrade_allowedo   s    

zResolver._is_upgrade_allowedc             C   s"   | j rt|jr|j|_d|_dS )z4
        Set a requirement to be installed.
        N)r   r   satisfied_byconflicts_with)r"   r(   r$   r$   r%   _set_req_to_reinstallx   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sy| j	j
|dd W n& tk
r~   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.
        Nzonly-if-neededz#already satisfied, skipping upgradezalready satisfiedT)upgradezalready up-to-date)r   check_if_existsr   r>   r   r@   r=   r   linkr   find_requirementr   r   )r"   req_to_installr$   r$   r%   _check_skip_installed   s*    



zResolver._check_skip_installedc             C   s   | j dk	std|jr0| j|| j | j| jS |jdks>t| |}|jr`| j	|| 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.
        Nz9require_hashes should have been set in Resolver.resolve()zto-satisfy-onlyfilez<Requirement already satisfied (use --upgrade to upgrade): %s)r   r   editabler   prepare_editable_requirementr   r   r>   rF   prepare_installed_requirementr=   prepare_linked_requirementr   r   rB   r   r   rC   schemer@   r0   r1   )r"   r(   skip_reasonupgrade_allowedabstract_distshould_modifyr$   r$   r%   _get_abstract_dist_for   s6    


zResolver._get_abstract_dist_forc          
      s|  j sjrg S d_j }|j}yt| W n< tk
r } zj	rnt
|jd  n W dd}~X Y nX g   fdd}t  jsd_jdd jsRjrt
ddj ttjt|j }x|D ]}t
d	|| q W tt|jtj@ }	x ||	D ]}
||
|	d
 q<W jsnjsnj W dQ R X  S )zxPrepare a single requirements file.

        :return: A list of additional InstallRequirements to also install.
        Tr   Nc                sX   t t| jjd}j}j|||d\}}|rJ|rJj| |  | d S )N)r   r   )parent_req_nameextras_requested)	r	   strr   r   nameadd_requirementr!   r4   r2   )subreqrS   sub_install_reqrR   to_scan_againadd_to_parent)	more_reqsrE   r5   r"   r$   r%   add_req  s    

z&Resolver._resolve_one.<locals>.add_req)rR   z!Installing extra requirements: %r,z"%s does not provide the extra '%s')rS   )
constraintpreparedreqs_to_cleanupr4   rQ   distr   r   r   r   r0   warningargsr
   has_requirementrU   r<   rV   r   extrasdebugjoinsortedsetrequiresrH   r>   successfully_downloaded)r"   r5   rE   rO   ra   errr\   missing_requestedmissingavailable_requestedrW   r$   )r[   rE   r5   r"   r%   r3      sJ    


zResolver._resolve_onec                s<   g  t   fddx|j D ]}| q(W  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                sR   | j s| krd S | jrd S |  xj| j D ]}| q4W  |  d S )N)r>   r^   addr!   rU   r4   )r(   dep)orderordered_reqsscheduler"   r$   r%   rt   U  s    
z1Resolver.get_installation_order.<locals>.schedule)ri   r,   r-   )r"   req_setinstall_reqr$   )rr   rs   rt   r"   r%   get_installation_orderH  s    

zResolver.get_installation_order)__name__
__module____qualname____doc__r   r   r;   r=   r@   rF   rQ   r3   rw   __classcell__r$   r$   )r#   r%   r      s   
1	49Xr   )r{   loggingcollectionsr   	itertoolsr   pip._internal.exceptionsr   r   r   r   r   pip._internal.req.constructorsr	   pip._internal.utils.loggingr
   pip._internal.utils.miscr   r   pip._internal.utils.packagingr   	getLoggerrx   r0   objectr   r$   r$   r$   r%   <module>   s   
