a
    $lJhP                     @  s"  d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZ erdd
l m!Z!m"Z" ddl#m$Z$ dddddZ%G dd deeeef Z&G dd deeeef Z'ddddddddZ(dS )    )annotationsN)TYPE_CHECKING
CollectionGenericIterableMapping   )	CTKTRTDirectedGraphIterableViewIteratorMappingRequirementInformationStatebuild_iter_view   )AbstractResolverResult)	Criterion)InconsistentCandidateRequirementsConflictedResolutionImpossibleResolutionTooDeepResolverException)AbstractProvider
Preference)BaseReporterState[RT, CT, KT]Result[RT, CT, KT])statereturnc              
     s   | j }dd | D }d |td < t }|d  d h | j D ]\}}t| j|| s^qD||vrp|| | D ]J}z|t| }W n ty   Y qxY n0 ||vr|| |	|| qxqDt
 fdd| D || jdS )Nc                 S  s   i | ]\}}t ||qS  )id.0kvr"   r"   ]/var/www/shaz/venv/lib/python3.9/site-packages/pip/_vendor/resolvelib/resolvers/resolution.py
<dictcomp>$       z!_build_result.<locals>.<dictcomp>c                   s   i | ]\}}| v r||qS r"   r"   r$   	connectedr"   r(   r)   :   r*   )mappinggraphcriteria)r-   itemsr#   r   addr/   _has_route_to_rootiter_parentKeyErrorconnectr   )r    r-   all_keysr.   key	criterionppkeyr"   r+   r(   _build_result"   s0    



r;   c                   @  s   e Zd ZdZddddddZedd	d
dZdd	ddZdddddddZddddddZ	dddddZ
ddddd d!Zd"dd#d$d%Zdd&dd'd(Zd)dd*d+d,Zd-dd.d/d0Zd&d-d1d2d3Zd4d5dd6d7d8Zd9S ):
ResolutionzStateful resolution object.

    This is designed as a one-off object that holds information to kick start
    the resolution process, and holds the results afterwards.
    zAbstractProvider[RT, CT, KT]zBaseReporter[RT, CT, KT]None)providerreporterr!   c                 C  s   || _ || _g | _d S N)_p_r_states)selfr>   r?   r"   r"   r(   __init__G   s    zResolution.__init__r   )r!   c              
   C  s@   z| j d W S  ty: } ztd|W Y d }~n
d }~0 0 d S )Nr    )rC   
IndexErrorAttributeError)rD   er"   r"   r(   r    P   s    zResolution.statec                 C  s>   | j d }t|j |j |jdd d}| j | dS )zPush a new state into history.

        This new state will be used to hold resolution results of the next
        coming round.
        rF   Nr-   r/   backtrack_causes)rC   r   r-   copyr/   rK   append)rD   baser    r"   r"   r(   _push_new_stateW   s    
zResolution._push_new_statezdict[KT, Criterion[RT, CT]]r   z	CT | None)r/   requirementparentr!   c           	      C  s   | j j||d | jj|d}||}|r8t|j}ng }| jj|t|t	
d||git|t	d||id}|rt|j}|t|| nt||g}tt|||d}|jst||||< d S )N)rP   rQ   )requirement_or_candidateiter_requirementincompatibilities
identifierrequirementsrT   
candidatesinformationrT   )rB   adding_requirementrA   identifygetlistrT   find_matchesr   operatormethodcaller
attrgetterrZ   rM   r   r   r   rY   r   )	rD   r/   rP   rQ   rV   r8   rT   matchesrZ   r"   r"   r(   _add_to_criteriae   s>    

zResolution._add_to_criteriazCollection[KT])r/   parentsr!   c                   sD    sdS |  D ].\}}t|j fdd|jD |j||< qdS )aW  Remove information from parents of criteria.

        Concretely, removes all values from each criterion's ``information``
        field that have one of ``parents`` as provider of the requirement.

        :param criteria: The criteria to update.
        :param parents: Identifiers for which to remove information from all criteria.
        Nc                   s,   g | ]$}|j d u s$j|j  vr|qS r@   )rQ   rA   r\   )r%   rZ   re   rD   r"   r(   
<listcomp>   s   
z@Resolution._remove_information_from_criteria.<locals>.<listcomp>)r0   r   rY   rZ   rT   )rD   r/   re   r7   r8   r"   rf   r(   !_remove_information_from_criteria   s    z,Resolution._remove_information_from_criteriar
   r   )namer!   c              	   C  s>   | j j|| jjt| jjtdt| jjtd| jjdS )NrY   rZ   )rV   resolutionsrY   rZ   rK   )	rA   get_preferencer    r-   r   r/   r`   rb   rK   )rD   ri   r"   r"   r(   _get_preference   s    zResolution._get_preferencezCriterion[RT, CT]bool)ri   r8   r!   c                   sB   zj j|  W n ty$   Y dS 0 t fdd| D S )NFc                 3  s   | ]}j j| d V  qdS )rP   	candidateNrA   is_satisfied_byr%   rcurrent_pinrD   r"   r(   	<genexpr>   s   z8Resolution._is_current_pin_satisfying.<locals>.<genexpr>)r    r-   r4   allrS   )rD   ri   r8   r"   rt   r(   _is_current_pin_satisfying   s    z%Resolution._is_current_pin_satisfyingr	   )ro   r!   c                 C  s4   | j j }| jj|dD ]}| j|||d q|S )Nro   rQ   )r    r/   rL   rA   get_dependenciesrd   )rD   ro   r/   rP   r"   r"   r(   _get_updated_criteria   s    z Resolution._get_updated_criteriazlist[Criterion[RT, CT]]c                   s   j j| }g }|jD ]ȉ z }W nL tyt } z4j|j  ||j W Y d }~qW Y d }~n
d }~0 0 t	 fdd|
 D }|st |jj d j j| j j|d   j j|< g   S |S )Nc                 3  s   | ]}j j| d V  qdS rn   rp   rr   ro   rD   r"   r(   rv      s   z7Resolution._attempt_to_pin_criterion.<locals>.<genexpr>ry   )r    r/   rY   r|   r   rB   Zrejecting_candidater8   rM   rw   rS   r   pinningupdater-   pop)rD   ri   r8   causesr/   rI   	satisfiedr"   r}   r(   _attempt_to_pin_criterion   s(    
"
z$Resolution._attempt_to_pin_criterionzlist[tuple[KT, list[CT]]])incompatibilities_from_brokenr!   c              	   C  s   |D ]\}}|sqz| j j| }W n ty8   Y qY n0 | jj|t| j jtdt| j jtd||id}t	|}|s dS |
|j t|t|j|d| j j|< qdS )NrS   rT   rU   FrX   T)r    r/   r4   rA   r_   r   r`   ra   rb   r   extendrT   r   r^   rZ   )rD   r   r&   rT   r8   rc   rY   r"   r"   r(   _patch_criteria   s:    
zResolution._patch_criteriaz$list[RequirementInformation[RT, CT]])r   r!   c           
   	     s  t dd |D dd |D } fdd|D }t jdkr jd=  j}z j }|j \}}W n  tt	fy   t
|dY n0 ||vrqȇ fd	d j|D }||sq|jsPqqPd
d |j D }|||gf     |}	|	r2dS q2dS )a2  Perform backjumping.

        When we enter here, the stack is like this::

            [ state Z ]
            [ state Y ]
            [ state X ]
            .... earlier states are irrelevant.

        1. No pins worked for Z, so it does not have a pin.
        2. We want to reset state Y to unpinned, and pin another candidate.
        3. State X holds what state Y was before the pin, but does not
           have the incompatibility information gathered in state Y.

        Each iteration of the loop will:

        1.  Identify Z. The incompatibility is not always caused by the latest
            state. For example, given three requirements A, B and C, with
            dependencies A1, B1 and C1, where A1 and B1 are incompatible: the
            last state might be related to C, so we want to discard the
            previous state.
        2.  Discard Z.
        3.  Discard Y but remember its incompatibility information gathered
            previously, and the failure we're dealing with right now.
        4.  Push a new state Y' based on X, and apply the incompatibility
            information from Y to Y'.
        5a. If this causes Y' to conflict, we need to backtrack again. Make Y'
            the new Z and go back to step 2.
        5b. If the incompatibilities apply cleanly, end backtracking.
        c                 s  s   | ]}|j d ur|j V  qd S r@   rz   r%   cr"   r"   r(   rv   5  r*   z'Resolution._backjump.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S r@   )rP   r   r"   r"   r(   rv   6  r*   c                   s   h | ]} j |qS r"   rA   r\   rr   rD   r"   r(   	<setcomp>8  r*   z'Resolution._backjump.<locals>.<setcomp>   rF   Nc                   s   h | ]} j |qS r"   r   )r%   dr   r"   r(   r   N  s   c                 S  s   g | ]\}}|t |jfqS r"   )r^   rT   r$   r"   r"   r(   rg   Z  s   z(Resolution._backjump.<locals>.<listcomp>TF)	itertoolschainlenrC   r    r   r-   popitemrG   r4   r   rA   r{   
isdisjointr/   r0   rM   rO   r   )
rD   r   Zincompatible_reqsZincompatible_depsbroken_stateri   ro   Zcurrent_dependenciesr   successr"   r   r(   	_backjump  s>    




zResolution._backjump)criteronr!   c                 C  s   t dd |D  S )z5Extract causes from list of criterion and deduplicatec                 S  s"   i | ]}|j D ]}t||qqS r"   )rZ   r#   )r%   r   ir"   r"   r(   r)   r  r*   z.Resolution._extract_causes.<locals>.<dictcomp>)r^   values)rD   r   r"   r"   r(   _extract_causesn  s    zResolution._extract_causesIterable[RT]intrW   
max_roundsr!   c                   s,  j rtdj  tt i g dg_ |D ]P}zjjj	|d d W q2 t
y } zt|jj|W Y d }~q2d }~0 0 q2  t|D ]}jj|d fddjj	 D }|sjjjd j  S tjj	 t|  t|dkrJtjj|jjtjj	td	tjj	td
jjd}n|}|s\tdt|dkrzt|jd}n|d } |}	|	r؈!|	}
jj"|
d #|
}|
jjd d < |stjjn4 fddjj	 D }$jj	|   jj%|jd qt&|d S )Nzalready resolvedrJ   rz   )indexc                   s    g | ]\}}  ||s|qS r"   rx   r%   r7   r8   r   r"   r(   rg     s   z&Resolution.resolve.<locals>.<listcomp>)r    r   rY   rZ   )Zidentifiersrj   rY   rZ   rK   z-narrow_requirement_selection returned 0 names)r7   r   )r   c                   s(   h | ] \}}| v r ||s|qS r"   r   r   Zsatisfied_namesrD   r"   r(   r     s   z%Resolution.resolve.<locals>.<setcomp>)r   r    )'rC   RuntimeErrorrB   startingr   collectionsOrderedDictrd   r    r/   r   r   r8   rZ   rO   rangestarting_roundr0   endingsetkeysr   r^   rA   Znarrow_requirement_selectionr-   r   r`   rb   rK   minrl   r   r   Zresolving_conflictsr   rh   ending_roundr   )rD   rW   r   rs   rI   round_indexZunsatisfied_namesZnarrowed_unstatisfied_namesri   Zfailure_criterionr   r   Znewly_unsatisfied_namesr"   r   r(   resolvet  s~    
&






zResolution.resolveN)__name__
__module____qualname____doc__rE   propertyr    rO   rd   rh   rl   rx   r|   r   r   r   r   r   r"   r"   r"   r(   r<   @   s   	,%#Yr<   c                   @  s(   e Zd ZdZeZd
ddddddZd	S )Resolverz3The thing that performs the actual resolution work.d   r   r   r   r   c                 C  s$   t | j| j}|j||d}t|S )a  Take a collection of constraints, spit out the resolution result.

        The return value is a representation to the final resolution result. It
        is a tuple subclass with three public members:

        * `mapping`: A dict of resolved candidates. Each key is an identifier
            of a requirement (as returned by the provider's `identify` method),
            and the value is the resolved candidate.
        * `graph`: A `DirectedGraph` instance representing the dependency tree.
            The vertices are keys of `mapping`, and each edge represents *why*
            a particular package is included. A special vertex `None` is
            included to represent parents of user-supplied requirements.
        * `criteria`: A dict of "criteria" that hold detailed information on
            how edges in the graph are derived. Each key is an identifier of a
            requirement, and the value is a `Criterion` instance.

        The following exceptions may be raised if a resolution cannot be found:

        * `ResolutionImpossible`: A resolution cannot be found for the given
            combination of requirements. The `causes` attribute of the
            exception is a list of (requirement, parent), giving the
            requirements that could not be satisfied.
        * `ResolutionTooDeep`: The dependency tree is too deeply nested and
            the resolver gave up. This is usually caused by a circular
            dependency, but you can try to resolve this by increasing the
            `max_rounds` argument.
        )r   )r<   r>   r?   r   r;   )rD   rW   r   
resolutionr    r"   r"   r(   r     s     zResolver.resolveN)r   )r   r   r   r   r   base_exceptionr   r"   r"   r"   r(   r     s    r   zMapping[KT, Criterion[RT, CT]]z	KT | Nonezdict[int, KT | None]zset[KT | None]rm   )r/   r7   r6   r,   r!   c              	   C  s   ||v rdS || vrdS |d us$J | |   D ]b}z|t| }W n tyZ   Y q0Y n0 ||v rt||  dS t| |||r0||  dS q0dS )NTF)r3   r#   r4   r1   r2   )r/   r7   r6   r,   r9   r:   r"   r"   r(   r2     s"    


r2   ))
__future__r   r   r   r`   typingr   r   r   r   r   structsr	   r
   r   r   r   r   r   r   r   Zabstractr   r   r8   r   
exceptionsr   r   r   r   r   	providersr   r   	reportersr   r;   r<   r   r2   r"   r"   r"   r(   <module>   s$   ,    *