o
    VhBh                     @  s\  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ erzd dlmZ d dlm Z  d dl!m"Z" e# Z$G dd deZ%e&e%e'e(e'f Z)e&e*e	e* e	e* e	e* e	e* f Z+G dd dZ,G dd dZ-dS )    )annotationsN)IntEnum)TYPE_CHECKING)Optional)
Dependency)SolveFailureError)Incompatibility)ConflictCauseError)NoVersionsCauseError)RootCauseError)PartialSolution)SolverResult)SetRelation)Term)PackageCollection)ProjectPackage)DependencyPackage)Providerc                   @  s$   e Zd ZdZdZdZdZdZdZdS )
Preferencez
    Preference is one of the criteria for choosing which dependency to solve
    first. A higher value means that there are "more options" to satisfy
    a dependency. A lower value takes precedence.
    r               N)	__name__
__module____qualname____doc__DIRECT_ORIGIN	NO_CHOICE
USE_LATESTLOCKEDDEFAULT r"   r"   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/mixology/version_solver.pyr   #   s    r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )DependencyCachez
    A cache of the valid dependencies.

    The key observation here is that during the search - except at backtracking
    - once we have decided that a dependency is invalid, we never need check it
    again.
    providerr   returnNonec                 C  s6   || _ tt| _tt| _tjdd| j| _	d S )N   )maxsize)
	_providercollectionsdefaultdictlist_cache_cached_dependencies_by_level	functools	lru_cache_search_for_search_for_cached)selfr%   r"   r"   r#   __init__A   s   zDependencyCache.__init__
dependencyr   keyDependencyCacheKeylist[DependencyPackage]c                   s>   | j | }|r fdd|d D }nd }|s| j }|S )Nc                   s    g | ]} j |jjr|qS r"   )
constraintallowspackageversion.0pr6   r"   r#   
<listcomp>]   s    z/DependencyCache._search_for.<locals>.<listcomp>)r.   r*   
search_for)r4   r6   r7   cache_entriespackagesr"   rA   r#   r2   V   s   

	zDependencyCache._search_fordecision_levelintc                 C  s   |j |j|j|j|jf}| |jr| n||}| j| r'| j| d |ur7| j| 	| | j
| 	| |jrH|rHt|d j|j|S |S )NrC   r   )namesource_type
source_urlsource_referencesource_subdirectoryr3   featureswithout_featuresr.   appendr/   r   r6   with_features)r4   r6   rG   r7   rF   r"   r"   r#   rD   q   s"   

zDependencyCache.search_forlevelc                 C  s<   || j v r| j  | j |D ]}| j|   qd S d S N)r/   r3   cache_clearpopr.   )r4   rR   r7   r"   r"   r#   clear_level   s   

zDependencyCache.clear_levelN)r%   r   r&   r'   )r6   r   r7   r8   r&   r9   )r6   r   rG   rH   r&   r9   )rR   rH   r&   r'   )r   r   r   r   r5   r2   rD   rV   r"   r"   r"   r#   r$   8   s    


r$   c                   @  s   e Zd ZdZd/dd	Zed0ddZd1ddZd2ddZd3ddZ	d4ddZ
d5ddZd6d"d#Zd7d%d&Zd1d'd(Zd8d)d*Zd9d,d-Zd.S ):VersionSolverz
    The version solver that finds a set of package versions that satisfy the
    root package's dependencies.

    See https://github.com/dart-lang/pub/tree/master/doc/solver.md for details
    on how this solver works.
    rootr   r%   r   r&   r'   c                 C  sJ   || _ || _t|| _i | _t | _tt| _	t
 | _t| j| _d S rS   )_rootr*   r$   _dependency_cache_incompatibilitiesset_contradicted_incompatibilitiesr+   r,   (_contradicted_incompatibilities_by_levelr   	_solutionr0   cache_get_comp_key_get_comp_key_cached)r4   rX   r%   r"   r"   r#   r5      s   
zVersionSolver.__init__r   c                 C  s   | j S rS   )r_   )r4   r"   r"   r#   solution   s   zVersionSolver.solutionr   c                 C  s   t   }t| jj| jj}d|_| tt|dgt	  z7z.| jj}|dur4| 
| |  }|dus'|  W W | dt   | dd| jj d S  tyT    w | dt   | dd| jj d w )z
        Finds a set of dependencies that match the root package's constraints,
        or raises an error if no such set is available.
        TFNzVersion solving took z.3fz seconds.
Tried z solutions.)timer   rY   rI   r=   is_root_add_incompatibilityr   r   r   
_propagate_choose_package_version_result_logr_   attempted_solutions	Exception)r4   startroot_dependencynextr"   r"   r#   solve   s4   

zVersionSolver.solver<   strc                 C  s   |h}|r^|  }t| j| D ]I}|| jv rq| |}|tu rI| |}|  | |}|dus5J |tks;J t|t	sBJ |
|  n|durYt|t	sTJ |
| q|sdS dS )z
        Performs unit propagation on incompatibilities transitively
        related to package to derive new assignments for _solution.
        N)rU   reversedr[   r]   _propagate_incompatibility	_conflict_resolve_conflictclear
isinstancerq   add)r4   r<   changedincompatibilityresult
root_causer"   r"   r#   rg      s*   





zVersionSolver._propagaterz   r   str | object | Nonec                 C  s   d}|j D ].}| j|}|tjkr%| j| | j| jj |  dS |tj	kr3|dur1 dS |}q|du r:t
S | j| | j| jj | | rPdnd}| d| |j  | j|j|  | |jj}|S )ad  
        If incompatibility is almost satisfied by _solution, adds the
        negation of the unsatisfied term to _solution.

        If incompatibility is satisfied by _solution, returns _conflict. If
        incompatibility is almost satisfied by _solution, returns the
        unsatisfied term's package name.

        Otherwise, returns None.
        Nznot  z	derived: )termsr_   relationr   DISJOINTr]   rx   r^   rG   OVERLAPPINGrt   is_positiverj   r6   derivecomplete_name)r4   rz   unsatisfiedtermr   adverbr   r"   r"   r#   rs      s:   


z(VersionSolver._propagate_incompatibilityc                   s
  |  d|  d}| sd d}d}d}|jD ]B}| j|}|du r+| |}n|j|jk r>t||j}| |}d}nt||j} |kr\| }|dur\t|| j|j	j}q|duscJ ||jk sm|j
du rt| jj|dD ]}|| jv r| j| j| | j| qu| j| |r| | |S  fdd|jD }	|j
jD ]}|j|jkr|	| q|dur|j	}
|
j|jkr|	|
 t|	t||j
}d}|du rd	nd
}|  d  d| d|  |  d|j
 d |  d|  | rt|)a  
        Given an incompatibility that's satisfied by _solution,
        The `conflict resolution`_ constructs a new incompatibility that encapsulates
        the root cause of the conflict and backtracks _solution until the new
        incompatibility will allow _propagate() to deduce new assignments.

        Adds the new incompatibility to _incompatibilities and returns it.

        .. _conflict resolution:
        https://github.com/dart-lang/pub/tree/master/doc/solver.md#conflict-resolution
        z
conflict: FNr   rC   c                   s   g | ]}| kr|qS r"   r"   r?   r   most_recent_termr"   r#   rB     s    z3VersionSolver._resolve_conflict.<locals>.<listcomp>Tr~   z
 partiallyz! z isz satisfied by z! which is caused by ""z! thus: )rj   
is_failurer   r_   	satisfierindexmaxrG   
differenceinversecauseranger^   r]   difference_updaterU   rZ   rV   	backtrackrf   r6   rP   r   r	   r   )r4   rz   new_incompatibilitymost_recent_satisfierr   previous_satisfier_levelr   r   rR   	new_termsr   	partiallyr"   r   r#   ru   4  s   












 zVersionSolver._resolve_conflictr6   r   CompKeyc              	     sL  t j}  r
t j}g } j| jjv }|s(| j }|r(|t jkr%t j}|g}|s3| j	
 | jj}t|}|r|d j}| rF|j}n3|t jkro| so j|jfD ]}	z| jj|j|j|	d}W n	 tym   Y qUw   fdd|jD }t|}
tdd |D }nd}
d}|t jkr|dk rt j}n|rt j}|| |
 | fS )	a  
        Returns a tuple of
        - preference
        - num_deps_upper_bound
        - has_deps
        - num_packages
        that serves as priority for choosing the next package to resolve.
        (A lower value takes precedence.)

        In order to provide results that are as deterministic as possible
        and consistent between `poetry lock` and `poetry update`, the return value
        of two different dependencies should not be equal if possible.

        ## preference

        See Preference class.

        ## num_deps_upper_bound

        A dependency with an upper bound is more likely to cause conflicts. Therefore,
        a package with more dependencies with upper bounds should be chosen first.

        ## has_deps

        A package with dependencies should be chosen first
        because a package without dependencies is less likely to cause conflicts.

        ## num_packages

        The original algorithm proposes to prefer packages with as few remaining
        versions as possible, so that if a conflict is necessary it's forced quickly.
        https://github.com/dart-lang/pub/blob/master/doc/solver.md#decision-making
        However, this leads to the famous boto3 vs. urllib3 issue, so we prefer
        packages with more remaining versions (see
        https://github.com/python-poetry/poetry/pull/8255#issuecomment-1657198242
        for more details).
        r   )repository_namec                   s&   g | ]}|j r|j d   jv r|qS )r   )	in_extrasextras)r?   rrA   r"   r#   rB     s    z/VersionSolver._get_comp_key.<locals>.<listcomp>c                 s  s    | ]
}|j  rd V  qdS )r   N)r:   has_upper_bound)r?   dr"   r"   r#   	<genexpr>   s    

z.VersionSolver._get_comp_key.<locals>.<genexpr>Fr   )r   r!   is_direct_originr   rI   r*   
use_latest
get_lockedr    rZ   rD   r_   rG   lenr<   re   all_requiressource_namerL   get_package_from_poolpretty_namer=   rl   requiresboolsumr   r   )r4   r6   
preferencerF   r   lockednum_packagesr<   relevant_dependenciesrepohas_depsnum_deps_upper_boundr"   rA   r#   ra     s^   &





zVersionSolver._get_comp_keyr   list[Dependency]c                 C  s   t || jdS )z6
        Chooses the next package to resolve.
        )r7   )minrb   )r4   r   r"   r"   r#   _choose_next.  s   zVersionSolver._choose_next
str | Nonec                   s  j j}|sdS | j }|du rCj j j}tt	|d}|du r=
tt dgt   j}|S  j|j_n|}j|}d}j|D ]}
| |pht fdd|jD }qS|sj |j d|jj d|jj d  j}|S )	a  
        Tries to select a version of a required package.

        Returns the name of the package whose incompatibilities should be
        propagated by _propagate(), or None indicating that version solving is
        complete and a solution has been found.
        NTFc                 3  s*    | ]}|j j jkpj|V  qd S rS   )r6   r   r_   	satisfiesr   r6   r4   r"   r#   r   b  s    

z8VersionSolver._choose_package_version.<locals>.<genexpr>z
selecting z ())r_   r   r   r*   r   rZ   rD   rG   ro   iterrf   r   r   r
   r   transitive_markerr6   complete_packageincompatibilities_forallr   decider<   rj   full_pretty_version)r4   r   r   rF   r<   r   conflictrz   r"   r   r#   rh   4  sD   


z%VersionSolver._choose_package_versionc                 C  s$   | j j}t| jdd |D | j jS )zI
        Creates a #SolverResult from the decisions in _solution
        c                 S  s   g | ]}|  s|qS r"   )re   r>   r"   r"   r#   rB   z  s    z)VersionSolver._result.<locals>.<listcomp>)r_   	decisionsr   rY   rk   )r4   r   r"   r"   r#   ri   r  s   zVersionSolver._resultc                 C  sd   |  d|  |jD ]$}|jj| jvrg | j|jj< || j|jj v r%q| j|jj | qd S )Nzfact: )rj   r   r6   r   r[   rP   )r4   rz   r   r"   r"   r#   rf   ~  s   
z"VersionSolver._add_incompatibilitytextc                 C  s   | j || jj d S rS   )r*   debugr_   rk   )r4   r   r"   r"   r#   rj     s   zVersionSolver._logN)rX   r   r%   r   r&   r'   )r&   r   )r&   r   )r<   rq   r&   r'   )rz   r   r&   r}   )rz   r   r&   r   )r6   r   r&   r   )r   r   r&   r   )r&   r   )rz   r   r&   r'   )r   rq   r&   r'   )r   r   r   r   r5   propertyrc   rp   rg   rs   ru   ra   r   rh   ri   rf   rj   r"   r"   r"   r#   rW      s     



,
= 

i

>
rW   ).
__future__r   r+   r0   rd   enumr   typingr   r   poetry.core.packages.dependencyr   poetry.mixology.failurer   poetry.mixology.incompatibilityr   %poetry.mixology.incompatibility_causer	   r
   r    poetry.mixology.partial_solutionr   poetry.mixology.resultr   poetry.mixology.set_relationr   poetry.mixology.termr   poetry.packagesr   $poetry.core.packages.project_packager   r   poetry.puzzle.providerr   objectrt   r   tuplerH   r   r   rq   r8   r$   rW   r"   r"   r"   r#   <module>   s<    ^