a
    bai^                     @  s8  U d dl mZ d dlZd dlZd dlZd dl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m Z  ddl!m"Z"m#Z# ddl$m%Z% dZ&de'd< 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Generic   )	CTKTRTDirectedGraphIterableViewIteratorMappingRequirementInformationStatebuild_iter_view   )AbstractResolverResult)	Criterion)InconsistentCandidateRequirementsConflictedResolutionImpossibleResolutionTooDeepResolverException)
CollectionIterableMapping)AbstractProvider
Preference)BaseReporterg?float_OPTIMISTIC_BACKJUMPING_RATIO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$   e/var/www/html/SiteWeb/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	   addr1   _has_route_to_rootiter_parentKeyErrorconnectr   )r"   r/   all_keysr0   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.Zdd	d/d0Zd1dd2d3d4Zd&d1d5d6d7Zd8d9dd:d;d<Zd=S )>
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 | _t| _d | _d | _d S N)_p_r_statesr   _optimistic_backjumping_ratio_save_statesZ_optimistic_start_round)selfr@   rA   r$   r$   r*   __init__K   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"   )rE   
IndexErrorAttributeError)rH   er$   r$   r*   r"   Y   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.
        rJ   Nr/   r1   backtrack_causes)rE   r   r/   copyr1   rO   append)rH   baser"   r$   r$   r*   _push_new_state`   s    
zResolution._push_new_statezdict[KT, Criterion[RT, CT]]r   z	CT | None)r1   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)rT   rU   )requirement_or_candidateiter_requirementincompatibilities
identifierrequirementsrX   
candidatesinformationrX   )rD   adding_requirementrC   identifygetlistrX   find_matchesr   operatormethodcaller
attrgetterr^   rQ   r   r   r   r]   r   )	rH   r1   rT   rU   rZ   r:   rX   matchesr^   r$   r$   r*   _add_to_criterian   s>    

zResolution._add_to_criteriazCollection[KT])r1   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 rB   )rU   rC   r`   )r'   r^   ri   rH   r$   r*   
<listcomp>   s   
z@Resolution._remove_information_from_criteria.<locals>.<listcomp>)r2   r   r]   r^   rX   )rH   r1   ri   r9   r:   r$   rj   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 )Nr]   r^   )rZ   resolutionsr]   r^   rO   )	rC   get_preferencer"   r/   r   r1   rd   rf   rO   )rH   rm   r$   r$   r*   _get_preference   s    zResolution._get_preferencezCriterion[RT, CT]bool)rm   r:   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 )rT   	candidateNrC   is_satisfied_byr'   rcurrent_pinrH   r$   r*   	<genexpr>   s   z8Resolution._is_current_pin_satisfying.<locals>.<genexpr>)r"   r/   r6   allrW   )rH   rm   r:   r$   rx   r*   _is_current_pin_satisfying   s    z%Resolution._is_current_pin_satisfyingr   )rs   r#   c                 C  s4   | j j }| jj|dD ]}| j|||d q|S )Nrs   rU   )r"   r1   rP   rC   get_dependenciesrh   )rH   rs   r1   rT   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 rr   rt   rv   rs   rH   r$   r*   rz      s   z7Resolution._attempt_to_pin_criterion.<locals>.<genexpr>r}   )r"   r1   r]   r   r   rD   Zrejecting_candidater:   rQ   r{   rW   r   pinningupdater/   pop)rH   rm   r:   causesr1   rM   	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 )NrW   rX   rY   Fr\   T)r"   r1   r6   rC   rc   r   rd   re   rf   r   extendrX   r   rb   r^   )rH   r   r(   rX   r:   rg   r]   r$   r$   r*   _patch_criteria   s:    
zResolution._patch_criteriac                 C  s    | j du rdd | jD | _ dS )zCSave states for potential rollback if optimistic backjumping fails.Nc                 S  s0   g | ](}t |j |j |jd d  dqS )NrN   )r   r/   rP   r1   rO   )r'   sr$   r$   r*   rk   !  s   z*Resolution._save_state.<locals>.<listcomp>)rG   rE   rH   r$   r$   r*   _save_state  s    
zResolution._save_statec                 C  s   d| _ | jr| j| _d| _dS )z3Rollback states and disable optimistic backjumping.g        N)rF   rG   rE   r   r$   r$   r*   _rollback_states*  s    zResolution._rollback_statesz$list[RequirementInformation[RT, CT]])r   r#   c           
   	     sN  t dd |D dd |D } fdd|D }t jdkrJ jd=  j}z j }|j \}}W n  tt	fy   t
|dY n0  js||vrq
 jr jdu r||vr    fd	d j|D }||sq
|jsq
t jd
krPt
|qPd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 rB   r~   r'   cr$   r$   r*   rz   Q  r,   z'Resolution._backjump.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S rB   )rT   r   r$   r$   r*   rz   R  r,   c                   s   h | ]} j |qS r$   rC   r`   rv   r   r$   r*   	<setcomp>T  r,   z'Resolution._backjump.<locals>.<setcomp>   rJ   Nc                   s   h | ]} j |qS r$   r   )r'   dr   r$   r*   r   w  s   r   c                 S  s   g | ]\}}|t |jfqS r$   )rb   rX   r&   r$   r$   r*   rk     s   z(Resolution._backjump.<locals>.<listcomp>TF)	itertoolschainlenrE   r"   r   r/   popitemrK   r6   r   rF   rG   r   rC   r   
isdisjointr1   r2   rQ   rS   r   )
rH   r   Zincompatible_reqsZincompatible_depsbroken_staterm   rs   Zcurrent_dependenciesr   successr$   r   r*   	_backjump1  sV    





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$   )r^   r%   )r'   r   ir$   r$   r*   r+     r,   z.Resolution._extract_causes.<locals>.<dictcomp>)rb   values)rH   r   r$   r$   r*   _extract_causes  s    zResolution._extract_causesIterable[RT]intr[   
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  d }d }t|D ]V}jj|d jrjd ur|d u r|}t|| j }|dkr  qn"|d ur|| |kr  qfddjj	 D }|sRjjjd j  S tjj	 t|  t|d	krtjj|jjtjj	t d
tjj	t djj!d}	n|}	|	stdt|	d	krt"|	j#d}
n|	d }
$|
}|r%|}jj&|d z'|}W n, tyV   jrPjrPd}n Y n0 t(| onjonj}|rjr  n"|jj!d d < |stjj!n4 fddjj	 D })jj	|   jj*|jd qt+|d S )Nzalready resolvedrN   r~   )indexr   c                   s    g | ]\}}  ||s|qS r$   r|   r'   r9   r:   r   r$   r*   rk     s   z&Resolution.resolve.<locals>.<listcomp>)r"   r   r]   r^   )Zidentifiersrn   r]   r^   rO   z-narrow_requirement_selection returned 0 names)r9   )r   Tc                   s(   h | ] \}}| v r ||s|qS r$   r   r   Zsatisfied_namesrH   r$   r*   r   "  s   z%Resolution.resolve.<locals>.<setcomp>)r   r"   ),rE   RuntimeErrorrD   startingr   collectionsOrderedDictrh   r"   r1   r   r   r:   r^   rS   rangestarting_roundrF   rG   r   r   r2   endingsetkeysr   rb   rC   Znarrow_requirement_selectionr/   r   rd   rf   rO   minrp   r   r   Zresolving_conflictsr   rq   rl   ending_roundr   )rH   r[   r   rw   rM   Zoptimistic_rounds_cutoffZ"optimistic_backjumping_start_roundround_indexZunsatisfied_namesZnarrowed_unstatisfied_namesrm   Zfailure_criterionr   r   Zfailed_optimistic_backjumpingZnewly_unsatisfied_namesr$   r   r*   resolve  s    
&







zResolution.resolveN)__name__
__module____qualname____doc__rI   propertyr"   rS   rh   rl   rp   r|   r   r   r   r   r   r   r   r   r$   r$   r$   r*   r>   D   s"   ,%#k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@   rA   r   r=   )rH   r[   r   
resolutionr"   r$   r$   r*   r   8  s     zResolver.resolveN)r   )r   r   r   r   r   base_exceptionr   r$   r$   r$   r*   r   3  s    r   zMapping[KT, Criterion[RT, CT]]z	KT | Nonezdict[int, KT | None]zset[KT | None]rq   )r1   r9   r8   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)r5   r%   r6   r3   r4   )r1   r9   r8   r.   r;   r<   r$   r$   r*   r4   ]  s"    


r4   ),
__future__r   r   r   rd   typingr   r   structsr   r   r   r	   r
   r   r   r   r   Zabstractr   r   r:   r   
exceptionsr   r   r   r   r   collections.abcr   r   r   	providersr   r   	reportersr   r   __annotations__r=   r>   r   r4   r$   r$   r$   r*   <module>   s(   ,   r*