o
    Zh]                     @  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$   Z/var/www/auris/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_statesZ_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)rI   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.
        rK   Nr.   r0   backtrack_causes)rF   r   r.   copyr0   rP   append)rI   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)rV   rW   )requirement_or_candidateiter_requirementincompatibilities
identifierrequirementsr[   
candidatesinformationr[   )rE   adding_requirementrD   identifygetlistr[   find_matchesr   operatormethodcaller
attrgetterra   rR   r   r   r   r`   r   )	rI   r0   rV   rW   r]   r9   r[   matchesra   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   )rW   rD   rc   )r'   ra   rl   rI   r$   r*   
<listcomp>   s    
z@Resolution._remove_information_from_criteria.<locals>.<listcomp>)r1   r   r`   ra   r[   )rI   r0   rl   r8   r9   r$   rn   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 )Nr`   ra   )r]   resolutionsr`   ra   rP   )	rD   get_preferencer    r.   r   r0   rg   ri   rP   )rI   rq   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 )rV   	candidateNrD   is_satisfied_byr'   rcurrent_pinrI   r$   r*   	<genexpr>   
    
z8Resolution._is_current_pin_satisfying.<locals>.<genexpr>)r    r.   r5   allrZ   )rI   rq   r9   r$   r~   r*   _is_current_pin_satisfying   s   z%Resolution._is_current_pin_satisfyingry   r   c                 C  s4   | j j }| jj|dD ]
}| j|||d q|S )Nry   rW   )r    r0   rQ   rD   get_dependenciesrk   )rI   ry   r0   rV   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  rw   rx   rz   r|   ry   rI   r$   r*   r      r   z7Resolution._attempt_to_pin_criterion.<locals>.<genexpr>r   )r    r0   r`   r   r   rE   Zrejecting_candidater9   rR   r   rZ   r   pinningupdater.   pop)rI   rq   r9   causesr0   rN   	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 )NrZ   r[   r\   Fr_   T)r    r0   r5   rD   rf   r   rg   rh   ri   r   extendr[   r   re   ra   )rI   r   r(   r[   r9   rj   r`   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 )NrO   )r   r.   rQ   r0   rP   )r'   sr$   r$   r*   ro   !  s    z*Resolution._save_state.<locals>.<listcomp>)rH   rF   rI   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   )rV   r   r$   r$   r*   r   R  s    c                      h | ]} j |qS r$   rD   rc   r|   r   r$   r*   	<setcomp>T  s    z'Resolution._backjump.<locals>.<setcomp>   rK   TNc                   r   r$   r   )r'   dr   r$   r*   r   w  s    c                 S  s   g | ]\}}|t |jfqS r$   )re   r[   r&   r$   r$   r*   ro     s    z(Resolution._backjump.<locals>.<listcomp>F)	itertoolschainlenrF   r    r   r.   popitemrL   r5   r   rG   rH   r   rD   r   
isdisjointr0   r1   rR   rT   r   )
rI   r   Zincompatible_reqsZincompatible_depsbroken_staterq   ry   Z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$   )ra   r%   )r'   r   ir$   r$   r*   r+     s   " z.Resolution._extract_causes.<locals>.<dictcomp>)re   values)rI   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 resolvedrO   r   )indexr   c                   s    g | ]\}}  ||s|qS r$   r   r'   r8   r9   r   r$   r*   ro     s    
z&Resolution.resolve.<locals>.<listcomp>)r    r   r`   ra   )Zidentifiersrr   r`   ra   rP   z-narrow_requirement_selection returned 0 names)r8   )r   Tc                   s(   h | ]\}}| v r ||s|qS r$   r   r   Zsatisfied_namesrI   r$   r*   r     s    
z%Resolution.resolve.<locals>.<setcomp>)r   r    ),rF   RuntimeErrorrE   startingr   collectionsOrderedDictrk   r    r0   r   r   r9   ra   rT   rangestarting_roundrG   rH   r   r   r1   endingsetkeysr   re   rD   Znarrow_requirement_selectionr.   r   rg   ri   rP   minrt   r   r   resolving_conflictsr   rv   rp   ending_roundr   )rI   r^   r   r}   rN   Zoptimistic_rounds_cutoffZ"optimistic_backjumping_start_roundround_indexunsatisfied_namesZnarrowed_unstatisfied_namesrq   Zfailure_criterionr   r   Zfailed_optimistic_backjumpingZnewly_unsatisfied_namesr$   r   r*   resolve  s   







zResolution.resolveN)r>   r?   r@   rA   r"   rB   )r"   r!   )r"   rB   )r0   rU   rV   r   rW   rX   r"   rB   )r0   rU   rl   rm   r"   rB   )rq   r   r"   r   )rq   r   r9   ru   r"   rv   )ry   r   r"   rU   )rq   r   r"   r   )r   r   r"   rv   )r   r   r"   rv   )r   r   r"   r   )r^   r   r   r   r"   r!   )__name__
__module____qualname____doc__rJ   propertyr    rT   rk   rp   rt   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<   )rI   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]rv   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"   rv   ),
__future__r   r   r   rg   typingr   r   structsr   r   r   r	   r
   r   r   r   r   Z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*