o
    h]                     @  s&  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rgd 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Z(G dd deee	e
f Z)G dd deee	e
f Z*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State[RT, CT, KT]returnResult[RT, CT, KT]c              
     s   | j }dd | D }d |td < t }|d  d h | j D ]>\}}t| j|| s/q"||vr8|| | D ]#}z|t| }W n	 tyO   Y q<w ||vrY|| |	|| q<q"t
 fdd| D || jdS )Nc                 S  s   i | ]	\}}t ||qS  )id.0kvr$   r$   ^/var/www/html/venv/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers/resolution.py
<dictcomp>(   s    z!_build_result.<locals>.<dictcomp>c                   s   i | ]\}}| v r||qS r$   r$   r&   	connectedr$   r*   r+   >   s    )mappinggraphcriteria)r.   itemsr%   r	   addr0   _has_route_to_rootiter_parentKeyErrorconnectr   )r    r.   all_keysr/   key	criterionppkeyr$   r,   r*   _build_result&   s4   


	r<   c                   @  s   e Zd ZdZdBdd	ZedCddZdDddZdEddZdFddZ	dGddZ
dHd#d$ZdId'd(ZdJd*d+ZdKd.d/ZdDd0d1ZdDd2d3ZdLd6d7ZdMd9d:ZdNd?d@ZdAS )O
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.
    providerAbstractProvider[RT, CT, KT]reporterBaseReporter[RT, CT, KT]r"   Nonec                 C  s(   || _ || _g | _t| _d | _d | _d S N)_p_r_statesr   _optimistic_backjumping_ratio_save_states_optimistic_start_round)selfr>   r@   r$   r$   r*   __init__K   s   
zResolution.__init__r!   c              
   C  s0   z| j d W S  ty } ztd|d }~ww )Nr    )rF   
IndexErrorAttributeError)rJ   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.
        rL   Nr.   r0   backtrack_causes)rF   r   r.   copyr0   rQ   append)rJ   baser    r$   r$   r*   _push_new_state`   s   
zResolution._push_new_stater0   dict[KT, Criterion[RT, CT]]requirementr   parent	CT | Nonec           	      C  s   | j j||d | jj|d}||}|rt|j}ng }| jj|t|t	
d||git|t	d||id}|rJt|j}|t|| nt||g}tt|||d}|js`t||||< d S )N)rW   rX   )requirement_or_candidateiter_requirementincompatibilities
identifierrequirementsr\   
candidatesinformationr\   )rE   adding_requirementrD   identifygetlistr\   find_matchesr   operatormethodcaller
attrgetterrb   rS   r   r   r   ra   r   )	rJ   r0   rW   rX   r^   r9   r\   matchesrb   r$   r$   r*   _add_to_criterian   s>   

zResolution._add_to_criteriaparentsCollection[KT]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 rC   )rX   rD   rd   )r'   rb   rm   rJ   r$   r*   
<listcomp>   s    
z@Resolution._remove_information_from_criteria.<locals>.<listcomp>)r1   r   ra   rb   r\   )rJ   r0   rm   r8   r9   r$   ro   r*   !_remove_information_from_criteria   s   
z,Resolution._remove_information_from_criterianamer   r   c              	   C  s>   | j j|| jjt| jjtdt| jjtd| jjdS )Nra   rb   )r^   resolutionsra   rb   rQ   )	rD   get_preferencer    r.   r   r0   rh   rj   rQ   )rJ   rr   r$   r$   r*   _get_preference   s   zResolution._get_preferencer9   Criterion[RT, CT]boolc                   sB   zj j|  W n
 ty   Y dS w t fdd| D S )NFc                 3       | ]}j j| d V  qdS )rW   	candidateNrD   is_satisfied_byr'   rcurrent_pinrJ   r$   r*   	<genexpr>   
    
z8Resolution._is_current_pin_satisfying.<locals>.<genexpr>)r    r.   r5   allr[   )rJ   rr   r9   r$   r   r*   _is_current_pin_satisfying   s   z%Resolution._is_current_pin_satisfyingrz   r   c                 C  s4   | j j }| jj|dD ]
}| j|||d q|S )Nrz   rX   )r    r0   rR   rD   get_dependenciesrl   )rJ   rz   r0   rW   r$   r$   r*   _get_updated_criteria   s   z Resolution._get_updated_criterialist[Criterion[RT, CT]]c                   s   j j| }g }|jD ]^ z }W n  ty4 } zj|j  ||j W Y d }~qd }~ww t	 fdd|
 D }|sJt |jj d j j| j j|d   j j|< g   S |S )Nc                 3  rx   ry   r{   r}   rz   rJ   r$   r*   r      r   z7Resolution._attempt_to_pin_criterion.<locals>.<genexpr>r   )r    r0   ra   r   r   rE   rejecting_candidater9   rS   r   r[   r   pinningupdater.   pop)rJ   rr   r9   causesr0   rO   	satisfiedr$   r   r*   _attempt_to_pin_criterion   s,   
	
z$Resolution._attempt_to_pin_criterionincompatibilities_from_brokenlist[tuple[KT, list[CT]]]c              	   C  s   |D ]R\}}|s	qz| j j| }W n	 ty   Y qw | 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 )Nr[   r\   r]   Fr`   T)r    r0   r5   rD   rg   r   rh   ri   rj   r   extendr\   r   rf   rb   )rJ   r   r(   r\   r9   rk   ra   r$   r$   r*   _patch_criteria   s<   zResolution._patch_criteriac                 C  s$   | j du rdd | jD | _ dS dS )zCSave states for potential rollback if optimistic backjumping fails.Nc                 S  s0   g | ]}t |j |j |jd d  dqS )NrP   )r   r.   rR   r0   rQ   )r'   sr$   r$   r*   rp   !  s    z*Resolution._save_state.<locals>.<listcomp>)rH   rF   rJ   r$   r$   r*   _save_state  s
   
zResolution._save_statec                 C  s"   d| _ | jr| j| _d| _dS dS )z3Rollback states and disable optimistic backjumping.g        N)rG   rH   rF   r   r$   r$   r*   _rollback_states*  s
   
zResolution._rollback_statesr   $list[RequirementInformation[RT, CT]]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yD   t
|d	w  jsM||vrMn( jr] jd	u r]||vr]    fd
d j|D }||spn|jstnq(dd |j D }|||gf     |}	|	rdS t jdks d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 rC   r   r'   cr$   r$   r*   r   Q  s    z'Resolution._backjump.<locals>.<genexpr>c                 s  s    | ]}|j V  qd S rC   )rW   r   r$   r$   r*   r   R  s    c                      h | ]} j |qS r$   rD   rd   r}   r   r$   r*   	<setcomp>T  s    z'Resolution._backjump.<locals>.<setcomp>   rL   TNc                   r   r$   r   )r'   dr   r$   r*   r   w  s    c                 S  s   g | ]\}}|t |jfqS r$   )rf   r\   r&   r$   r$   r*   rp     s    z(Resolution._backjump.<locals>.<listcomp>F)	itertoolschainlenrF   r    r   r.   popitemrM   r5   r   rG   rH   r   rD   r   
isdisjointr0   r1   rS   rU   r   )
rJ   r   incompatible_reqsincompatible_depsbroken_staterr   rz   current_dependenciesr   successr$   r   r*   	_backjump1  sT   





(
@zResolution._backjumpcriteronc                 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$   )rb   r%   )r'   r   ir$   r$   r*   r+     s   " z.Resolution._extract_causes.<locals>.<dictcomp>)rf   values)rJ   r   r$   r$   r*   _extract_causes  s   zResolution._extract_causesr_   Iterable[RT]
max_roundsintc                   s  j rtdj  tt i g dg_ |D ]!}zjjj	|d d W q t
y: } zt|jj|d }~ww   d }d }t|D ]#}jj|d jrjd ur|d u rr|}t|| j }|dkrq  qGn|d ur|| |kr  qGfddjj	 D }|s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 }
$|
}|rH%|}jj&|d z'|}W n ty   jrjrd}n Y nw t(| o(jo(j}|r6jr6  n,|jj!d d < |sGtjj!n fddjj	 D })jj	|   jj*|jd qGt+|)Nzalready resolvedrP   r   )indexr   c                   s    g | ]\}}  ||s|qS r$   r   r'   r8   r9   r   r$   r*   rp     s    
z&Resolution.resolve.<locals>.<listcomp>)r    r   ra   rb   )identifiersrs   ra   rb   rQ   z-narrow_requirement_selection returned 0 names)r8   )r   Tc                   s(   h | ]\}}| v r ||s|qS r$   r   r   satisfied_namesrJ   r$   r*   r     s    
z%Resolution.resolve.<locals>.<setcomp>)r   r    ),rF   RuntimeErrorrE   startingr   collectionsOrderedDictrl   r    r0   r   r   r9   rb   rU   rangestarting_roundrG   rH   r   r   r1   endingsetkeysr   rf   rD   narrow_requirement_selectionr.   r   rh   rj   rQ   minru   r   r   resolving_conflictsr   rw   rq   ending_roundr   )rJ   r_   r   r~   rO   optimistic_rounds_cutoff"optimistic_backjumping_start_roundround_indexunsatisfied_namesnarrowed_unstatisfied_namesrr   failure_criterionr   r   failed_optimistic_backjumpingnewly_unsatisfied_namesr$   r   r*   resolve  s   







zResolution.resolveN)r>   r?   r@   rA   r"   rB   )r"   r!   )r"   rB   )r0   rV   rW   r   rX   rY   r"   rB   )r0   rV   rm   rn   r"   rB   )rr   r   r"   r   )rr   r   r9   rv   r"   rw   )rz   r   r"   rV   )rr   r   r"   r   )r   r   r"   rw   )r   r   r"   rw   )r   r   r"   r   )r_   r   r   r   r"   r!   )__name__
__module____qualname____doc__rK   propertyr    rU   rl   rq   ru   r   r   r   r   r   r   r   r   r   r$   r$   r$   r*   r=   D   s$    



,




%
#


fr=   c                   @  s"   e Zd ZdZeZ	ddd	d
ZdS )Resolverz3The thing that performs the actual resolution work.d   r_   r   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<   )rJ   r_   r   
resolutionr    r$   r$   r*   r   3  s    zResolver.resolveN)r   )r_   r   r   r   r"   r#   )r   r   r   r   r   base_exceptionr   r$   r$   r$   r*   r   .  s
    r   r0   Mapping[KT, Criterion[RT, CT]]r8   	KT | Noner7   dict[int, KT | None]r-   set[KT | None]rw   c              	   C  s   ||v rdS || vrdS |d usJ | |   D ]/}z|t| }W n	 ty+   Y qw ||v r8||  dS t| |||rG||  dS qdS )NTF)r4   r%   r5   r2   r3   )r0   r8   r7   r-   r:   r;   r$   r$   r*   r3   X  s&   

r3   )r    r!   r"   r#   )
r0   r   r8   r   r7   r   r-   r   r"   rw   ),
__future__r   r   r   rh   typingr   r   structsr   r   r   r	   r
   r   r   r   r   abstractr   r   r9   r   
exceptionsr   r   r   r   r   collections.abcr   r   r   	providersr   r   	reportersr   r   __annotations__r<   r=   r   r3   r$   r$   r$   r*   <module>   s*    ,
   m*