o
    6‘ði”^  ã                   @  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$   új/root/parts/websockify/install/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Ú
Resolutionz­Stateful 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 )Néÿÿÿÿr    )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 )z‰Push 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           
   	     sP  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rMn3ˆ jr]ˆ jd	u r]||vr]ˆ  ¡  ‡ fd
d„ˆ j |¡D ƒ}| |¡spn|jstntˆ jƒdkrt
|ƒ‚q(dd„ |j ¡ D ƒ}| ||gf¡ ˆ  ¡  ˆ  |¡}	|	rŸd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    ÿr   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  sX   þ

ÿÿÿ


ÿ
Õ-ÿ
ÁEz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rÑtˆjj|ˆjjtˆjj	t  d
¡ƒtˆjj	t  d¡ƒˆjj!d�ƒ}	n|}	|	sÙtdƒ‚t|	ƒd	krçt"|	ˆ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$    



,




%
#


k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Ï   8  s    zResolver.resolveN)rÖ   )r_   r±   r²   r³   r"   r#   )rÐ   rÑ   rÒ   rÓ   r   Úbase_exceptionrÏ   r$   r$   r$   r*   rÕ   3  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   ]  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*    ,
   r*