o
    !7hP                     @  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rbdd
l m!Z!m"Z" ddl#m$Z$ 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
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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/epreuve.sigeris.cm/public_html/epreuve/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   addr.   _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d>dd	Zed?ddZd@ddZdAddZdBddZ	dCddZ
dDd#d$ZdEd'd(ZdFd*d+ZdGd.d/ZdHd2d3ZdId5d6ZdJd;d<Zd=S )K
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 | _d S N)_p_r_states)selfr<   r>   r"   r"   r(   __init__G   s   
zResolution.__init__r   c              
   C  s0   z| j d W S  ty } ztd|d }~ww )Nr   )rD   
IndexErrorAttributeError)rE   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.
        rG   Nr,   r.   backtrack_causes)rD   r   r,   copyr.   rL   append)rE   baser   r"   r"   r(   _push_new_stateW   s   
zResolution._push_new_stater.   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)rR   rS   )requirement_or_candidateiter_requirementincompatibilities
identifierrequirementsrW   
candidatesinformationrW   )rC   adding_requirementrB   identifygetlistrW   find_matchesr   operatormethodcaller
attrgetterr]   rN   r   r   r   r\   r   )	rE   r.   rR   rS   rY   r7   rW   matchesr]   r"   r"   r(   _add_to_criteriae   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 rA   )rS   rB   r_   )r%   r]   rh   rE   r"   r(   
<listcomp>   s    
z@Resolution._remove_information_from_criteria.<locals>.<listcomp>)r/   r   r\   r]   rW   )rE   r.   rh   r6   r7   r"   rj   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\   r]   )rY   resolutionsr\   r]   rL   )	rB   get_preferencer   r,   r   r.   rc   re   rL   )rE   rm   r"   r"   r(   _get_preference   s   zResolution._get_preferencer7   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 )rR   	candidateNrB   is_satisfied_byr%   rcurrent_pinrE   r"   r(   	<genexpr>   
    
z8Resolution._is_current_pin_satisfying.<locals>.<genexpr>)r   r,   r3   allrV   )rE   rm   r7   r"   rz   r(   _is_current_pin_satisfying   s   z%Resolution._is_current_pin_satisfyingru   r	   c                 C  s4   | j j }| jj|dD ]
}| j|||d q|S )Nru   rS   )r   r.   rM   rB   get_dependenciesrg   )rE   ru   r.   rR   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  rs   rt   rv   rx   ru   rE   r"   r(   r|      r}   z7Resolution._attempt_to_pin_criterion.<locals>.<genexpr>r   )r   r.   r\   r   r   rC   rejecting_candidater7   rN   r~   rV   r   pinningupdater,   pop)rE   rm   r7   causesr.   rJ   	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 )NrV   rW   rX   Fr[   T)r   r.   r3   rB   rb   r   rc   rd   re   r   extendrW   r   ra   r]   )rE   r   r&   rW   r7   rf   r\   r"   r"   r(   _patch_criteria   s<   zResolution._patch_criteriar   $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 ||vrJn fd
d j|D }||s]n|jsan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 rA   r   r%   cr"   r"   r(   r|   5  s    z'Resolution._backjump.<locals>.<genexpr>c                 s  s    | ]}|j V  qd S rA   )rR   r   r"   r"   r(   r|   6  s    c                      h | ]} j |qS r"   rB   r_   rx   rE   r"   r(   	<setcomp>8  s    z'Resolution._backjump.<locals>.<setcomp>   rG   TNc                   r   r"   r   )r%   dr   r"   r(   r   N  s    c                 S  s   g | ]\}}|t |jfqS r"   )ra   rW   r$   r"   r"   r(   rk   Z  s    z(Resolution._backjump.<locals>.<listcomp>F)	itertoolschainlenrD   r   r   r,   popitemrH   r3   r   rB   r   
isdisjointr.   r/   rN   rP   r   )
rE   r   incompatible_reqsincompatible_depsbroken_staterm   ru   current_dependenciesr   successr"   r   r(   	_backjump  sF   





3z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"   )r]   r#   )r%   r   ir"   r"   r(   r)   r  s   " z.Resolution._extract_causes.<locals>.<dictcomp>)ra   values)rE   r   r"   r"   r(   _extract_causesn  s   zResolution._extract_causesrZ   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   t|D ]}jj|d fddjj	 D }|sh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 #|
}|
jjd d < |stjjn fddjj	 D }$jj	|   jj%|jd qCt&|)Nzalready resolvedrK   r   )indexc                   s    g | ]\}}  ||s|qS r"   r   r%   r6   r7   r   r"   r(   rk     s    
z&Resolution.resolve.<locals>.<listcomp>)r   r   r\   r]   )identifiersrn   r\   r]   rL   z-narrow_requirement_selection returned 0 names)r6   r   )r   c                   s(   h | ]\}}| v r ||s|qS r"   r   r   satisfied_namesrE   r"   r(   r     s    
z%Resolution.resolve.<locals>.<setcomp>)r   r   )'rD   RuntimeErrorrC   startingr   collectionsOrderedDictrg   r   r.   r   r   r7   r]   rP   rangestarting_roundr/   endingsetkeysr   ra   rB   narrow_requirement_selectionr,   r   rc   re   rL   minrp   r   r   resolving_conflictsr   rl   ending_roundr   )rE   rZ   r   ry   rJ   round_indexunsatisfied_namesnarrowed_unstatisfied_namesrm   failure_criterionr   r   newly_unsatisfied_namesr"   r   r(   resolvet  s   







zResolution.resolveN)r<   r=   r>   r?   r    r@   )r    r   )r    r@   )r.   rQ   rR   r   rS   rT   r    r@   )r.   rQ   rh   ri   r    r@   )rm   r
   r    r   )rm   r
   r7   rq   r    rr   )ru   r	   r    rQ   )rm   r
   r    r   )r   r   r    rr   )r   r   r    rr   )r   r   r    r   )rZ   r   r   r   r    r   )__name__
__module____qualname____doc__rF   propertyr   rP   rg   rl   rp   r   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
ZdS )Resolverz3The thing that performs the actual resolution work.d   rZ   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:   )rE   rZ   r   
resolutionr   r"   r"   r(   r     s    zResolver.resolveN)r   )rZ   r   r   r   r    r!   )r   r   r   r   r   base_exceptionr   r"   r"   r"   r(   r     s
    r   r.   Mapping[KT, Criterion[RT, CT]]r6   	KT | Noner5   dict[int, KT | None]r+   set[KT | None]rr   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)r2   r#   r3   r0   r1   )r.   r6   r5   r+   r8   r9   r"   r"   r(   r1     s&   

r1   )r   r   r    r!   )
r.   r   r6   r   r5   r   r+   r   r    rr   ))
__future__r   r   r   rc   typingr   r   r   r   r   structsr	   r
   r   r   r   r   r   r   r   abstractr   r   r7   r   
exceptionsr   r   r   r   r   	providersr   r   	reportersr   r:   r;   r   r1   r"   r"   r"   r(   <module>   s&    ,
    *