o
    Vh̠                     @  sV  d dl mZ d dlZd dl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 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rd dl.m/Z/ d dl.m0Z0 d dl.m1Z1 d dl.m2Z2 d dl3m4Z4 d dl5m6Z6 d d l7m8Z8 d d!lm9Z9 d d"l:m;Z; d d#l<m=Z= d d$l>m?Z? d d%l@mAZA d d&lBmCZC d d'lDmEZE d d(lmFZF d d)lGmHZH d d*lImJZJ eKeLZMG d+d, d,eNZOG d-d. d.eZPG d/d0 d0ZQdS )1    )annotationsN)defaultdict)contextmanager)TYPE_CHECKING)Any)ClassVar)cast)ProgressIndicator)EmptyConstraint)Version)VersionRange)!get_python_constraint_from_marker)	AnyMarker)parse_marker)union)Incompatibility)DependencyCauseError)PythonCauseError)Term)DependencyPackage)DirectOrigin)PackageCollection)OverrideNeededError)get_file_hash)Callable)
Collection)Iterable)Iterator)Path)IO)NormalizedName)VersionConstraint)
Dependency)DirectoryDependency)FileDependency)Package)URLDependency)VCSDependency)
BaseMarker)RepositoryPool)Envc                      s(   e Zd ZdZddd fddZ  ZS )IncompatibleConstraintsErrorzX
    Exception when there are duplicate dependencies with incompatible constraints.
    Fwith_sourcespackager%   dependenciesr"   r-   boolreturnNonec                  sv   g }|D ]%}|  }| r|d|j d7 }|r$|jr$|d|j 7 }|| qt d| dd|  d S )Nz ()z
 ; source=z,Incompatible constraints in requirements of z:

)
to_pep_508is_direct_origin
constraintsource_nameappendsuper__init__join)selfr.   r-   r/   constraintsdepr7   	__class__ j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/puzzle/provider.pyr;   B   s   

z%IncompatibleConstraintsError.__init__)r.   r%   r/   r"   r-   r0   r1   r2   )__name__
__module____qualname____doc__r;   __classcell__rB   rB   r@   rC   r+   =   s    r+   c                   @  s@   e Zd ZU dZded< eedddZdd	d
ZdddZ	dS )	IndicatorN
str | NoneCONTEXTr1   &Iterator[Callable[[str | None], None]]c                  c  s    ddd} | V  | d  d S )NcontextrJ   r1   r2   c                 S  s
   | t _d S NrI   rK   )rM   rB   rB   rC   _set_context[      
z'Indicator.context.<locals>._set_context)rM   rJ   r1   r2   rB   )rP   rB   rB   rC   rM   X   s   
zIndicator.contextstrc                 C  s   t jd u rdS dt j dS )N z <c1>z</> rO   r=   rB   rB   rC   _formatter_contextb   s   
zIndicator._formatter_contextc                 C  s(   | j d usJ t | j  }|ddS )Nz.1fs)_start_timetime)r=   elapsedrB   rB   rC   _formatter_elapsedh   s   zIndicator._formatter_elapsed)r1   rL   )r1   rR   )
rD   rE   rF   rK   __annotations__staticmethodr   rM   rU   rZ   rB   rB   rB   rC   rI   U   s   
 
rI   c                   @  sf  e Zd ZU e Zded< ddddnddZedoddZedpddZ	e
jdqddZe
jdrddZdsdd Zdtd#d$Zdud&d'Zedvd+d,Zedwd/d0Zedxd2d3Zedyd6d7Zdzd8d9Zd{d;d<Zd|d>d?Zd}dAdBZd~dDdEZddGdHZddKdLZddPdQZddRdSZddUdVZ ddd\d]Z!dd`daZ"ddbdcZ#ddfdgZ$ddhdiZ%	dddldmZ&dS )ProviderzClassVar[set[str]]UNSAFE_PACKAGESN)lockedactive_root_extrasr.   r%   poolr)   ior   r_   list[Package] | Noner`   !Collection[NormalizedName] | Noner1   r2   c                C  s   || _ || _t| jj| _|| _d | _|j| _| j	 p | j
 | _i | _i | _d| _d | _i | _tt| _g | _|d urAt|nd | _i | _|pJg D ]}| j|j t| | qK| j D ]}|jdd dd qat| jj | _!d S )NTc                 S  s   | j jS rN   )r.   versionprB   rB   rC   <lambda>       z#Provider.__init__.<locals>.<lambda>keyreverse)"_package_poolr   artifact_cache_direct_origin_io_envpython_constraint_package_python_constraintis_debugis_very_verbose_is_debugging
_overrides_deferred_cache_load_deferred_source_root_direct_origin_packagesr   list_locked_use_latest	frozenset_active_root_extras_explicit_sourcesnamer9   r   to_dependencyvaluessort	functoolscacher.   get_package_from_pool)r=   r.   ra   rb   r_   r`   dependency_packagesrB   rB   rC   r;   r   s6   	
zProvider.__init__c                 C     | j S rN   )rn   rT   rB   rB   rC   ra         zProvider.poolCollection[NormalizedName]c                 C  r   rN   r   rT   rB   rB   rC   
use_latest   r   zProvider.use_latestr(   c                 C  s4   t  }| j D ]}| D ]}||j}qq|S rN   )r   rx   r   	intersectmarker)r=   overrides_marker_intersectiondep_overridesr?   rB   rB   rC   _overrides_marker_intersection   s   z'Provider._overrides_marker_intersectionr!   c                 C  s   | j t| jS rN   )rt   r   r   r   rT   rB   rB   rC   _python_constraint   s   zProvider._python_constraintr0   c                 C  r   rN   )rw   rT   rB   rB   rC   is_debugging   s   zProvider.is_debugging	overrides$dict[Package, dict[str, Dependency]]c                 C  s&   || _ | jdd  | jdd  d S )Nr   r   )rx   __dict__pop)r=   r   rB   rB   rC   set_overrides   s   zProvider.set_overridesload_deferredc                 C  s
   || _ d S rN   )rz   )r=   r   rB   rB   rC   r      rQ   zProvider.load_deferredsource_rootr   Iterator[Provider]c                 c  s*    | j }|| _ z	| V  W || _ d S || _ w rN   )r{   )r=   r   original_source_rootrB   rB   rC   use_source_root   s   zProvider.use_source_rootenvr*   c                 c  sH    | j }|| _t|jd | _ z| V  W d | _|| _ d S d | _|| _ w )Npython_full_version)rt   rr   r   parse
marker_env)r=   r   original_python_constraintrB   rB   rC   use_environment   s   
zProvider.use_environmentnamesc                 c  s$    || _ z	| V  W g | _ d S g | _ w rN   r   )r=   r   rB   rB   rC   use_latest_for   s
   zProvider.use_latest_for
dependencyr"   c                 C  s(   | j |j krtd| j  d|j  d S )NzThe dependency name for z+ does not match the actual package's name: )r   RuntimeErrorr   r.   rB   rB   rC   validate_package_for_dependency   s   
z(Provider.validate_package_for_dependencyc                 C  s   | j |}|d urnF| rtd|}| |}n7| r)td|}| |}n(| r8td|}| |}n|	 rGtd|}| 
|}n
t| d|j | ra|j|_|j|_|j|_|j|_|jj|_|| j |< |S )Nr'   r$   r#   r&   z!: unknown direct dependency type )ry   getis_vcsr   _search_for_vcsis_file_search_for_fileis_directory_search_for_directoryis_url_search_for_urlr   source_typesource_reference_source_referencesource_resolved_reference_source_resolved_referencesource_subdirectory_source_subdirectoryre   _constrainttext_pretty_constraintr=   r   r.   rB   rB   rC   #search_for_direct_origin_dependency   s4   





z,Provider.search_for_direct_origin_dependencylist[DependencyPackage]c                   s    j r
t | jgS   r|  }|| j j< t |gS | j j}|r5| r5|g}t |S | j	
 }|j fdddd t |S )z
        Search for the specifications that match the given dependency.

        The specifications in the returned list will be considered in reverse
        order, so the latest version ought to be last.
        c                   s    | j  |   o   | jfS rN   )yankedis_prereleaseallows_prereleasesre   rf   r   rB   rC   rh   +  s   z%Provider.search_for.<locals>.<lambda>Trj   )is_rootr   rm   r6   r   r|   r   r   	satisfiesrn   find_packagesr   )r=   r   r.   direct_origin_packagepackagesrB   r   rC   
search_for  s    



	zProvider.search_forr'   c              
   C  sZ   | j j|j|j|j|j|j|j| jp| j	r| j	j
dndd}| j||d |j|_|S )z
        Search for the specifications that match the given VCS dependency.

        Basically, we clone the repository in a temporary directory
        and get the information we need by checking out the specified reference.
        srcN)branchtagrevsubdirectoryr   r   )rp   get_package_from_vcsvcssourcer   r   r   r   r{   rr   pathjoinpathr   developr   rB   rB   rC   r   5  s   zProvider._search_for_vcsr$   c                 C  sZ   |j dd | j|j}| j||d |jd ur|j|_|jjdt	|j dg|_
|S )NTraise_errorr   zsha256:)filehash)validaterp   get_package_from_file	full_pathr   baseroot_dirr   r   r   filesr   rB   rB   rC   r   M  s   
zProvider._search_for_filer#   c                 C  sF   |j dd | j|j}| j||d |j|_|jd ur!|j|_|S )NTr   r   )r   rp   get_package_from_directoryr   r   r   r   r   r   rB   rB   rC   r   _  s   
zProvider._search_for_directoryr&   c                 C  sl   | j |j}| j||d t|jD ] }||jv r3|j| D ]}|  q|j| D ]}|| q+q|S )Nr   )rp   get_package_from_urlurlr   sortedextrasactivateadd_dependency)r=   r   r.   extrar?   	extra_deprB   rB   rC   r   l  s   

zProvider._search_for_urlr/   list[Dependency]c                 C  sr   | j |i }g }g }|D ])}|j|v r1|j|v rq||j j s*|||j  ||j q|| q|S rN   )rx   r   r   r7   is_emptyr9   )r=   r/   r.   r   _dependencies
overriddenr?   rB   rB   rC    _get_dependencies_with_overrides{  s   

z)Provider._get_dependencies_with_overridesdependency_packager   list[Incompatibility]c                   s   |j    r j}nJ j} j| jsUt|jj	} j
|}||}|
| j}| sB| j
|jj sB| sB| sUtt  dgt jt| jgS  fdd| | D S )a  
        Returns incompatibilities that encapsulate a given package's dependencies,
        or that it can't be safely selected.

        If multiple subsequent versions of this package have the same
        dependencies, this will return incompatibilities that reflect that. It
        won't return incompatibilities that have already been returned by a
        previous call to _incompatibilities_for().
        Tc                   s,   g | ]}t t  d t|dgt qS )TF)r   r   r   r   .0r?   r.   rB   rC   
<listcomp>  s    z2Provider.incompatibilities_for.<locals>.<listcomp>)r.   r   all_requiresrequiresrs   
allows_allr   r   r   transitive_markerr   
differenceis_anyr   r   r   r   r   python_versionsrR   r   )r=   r   r/   transitive_python_constraintintersectionr   rB   r   rC   incompatibilities_for  sD   
	
	
zProvider.incompatibilities_forc           !        s  |j }|j}| r| }|j }|j}|j}n| r |j}nt|j|j	|j
|jd}|j }|j}|j}t }t }g }|jrt|j}|ry| }	|	|v rRqG||	 |j|	g }
|
D ]}|j|jkrp|t|j7 }q`||j q`|sI||j}|j }|j}|  }|j|_|js|jr|j|_|| |D ]l}j|jsq|jjv rqjr| rjnjj}|j |sq| s|! r|j|vs|j"rt|j"#|sqjd u r| r|j"r| }|j$t%d&dd |j"D |_|| qj'r4|D ] }| r2(|}|d ur-|j )|r-q*| q+||}t,t-}|D ]}||j | q@g }|. D ]C\}}t/|dkrg||d  qR0d| d t,t-}|D ]}||j1 | qvt/|dkr| rjn|j}2|||}nO|. D ]\}}t/|dkr2||d ||< qt3d	d |4 D rt3d
d t56|4 dD rt-t5j7|4  }n|4 D ]}|8| qqRt/|dkr0d| d ||d  qRfdd|D }t/|dk rA|r&t/|dkr-|d j9: r-d| d}nd| d}||d  0| qRddd d& fdd|d d D }|d |d  7 }0d| d g }|D ]$}j;< }||i < }|=|j|i |=||i || qnt>| g }|D ]9}|j?@ A s|j?@ $|j@ }|: rq||_?|jA s|j$|j} | : rΐq|| q|jBg dd}t||}|D ]}|C| qjDr| rjEF  |D ]}|jr	|jjE|j< q|S ) N)repository_namez or c                 s  s    | ]	}d | dV  qdS )z
extra == ""NrB   )r   r   rB   rB   rC   	<genexpr>-      z,Provider.complete_package.<locals>.<genexpr>   r   z"<debug>Duplicate dependencies for z</debug>c                 s  s    | ]	}t |d kV  qdS )r  N)lenr   drB   rB   rC   r  t  r  c                 s  s.    | ]\}}|d  j |d  j  V  qdS )r   N)r   r   r   )r   d1d2rB   rB   rC   r  t  s
    
   z <debug>Merging requirements for c                   s"   g | ]} j |j s|qS rB   )r   r   r   r   r   rT   rB   rC   r     s    z-Provider.complete_package.<locals>.<listcomp>z$<debug>No relevant requirements for z)<debug>Only one relevant requirement for r  r"   r1   rR   c                 S  s0   | j  s| j nd}d| j d| j d| dS )N*z<c1>z</c1> <fg=default>(<c2>z</c2>)</> with markers <b>z</b>)r   r   r   pretty_constraint)r  dependency_markerrB   rB   rC   fmt_warning  s
   z.Provider.complete_package.<locals>.fmt_warningz, c                 3  s    | ]} |V  qd S rN   rB   r  )r  rB   rC   r    s    z and z*<warning>Different requirements found for z.</warning>T)only)r  r"   r1   rR   )Gr.   r   r   cloner   r6   r   r   r   pretty_namere   r8   setr   r   r   addr   r   with_featureswithout_featuresr   r   r9   r   
allows_anyrs   r^   rr   _marker_valuesr   r   r   is_optional	in_extrasr   r   r   r<   rz   
get_lockedis_same_package_asr   r   r   r}   itemsr  debugcomplete_name_resolve_overlapping_markersallr   	itertoolscombinationschainextendr7   r   rx   copyupdater   r   without_extrasr   with_dependency_groupsr   r~   r   clear)!r=   r   r.   r   r   found_extrasoptional_dependenciesr   stackr   extra_dependenciesextra_dependencynew_dependencyr?   marker_valuesr_   r/   
duplicatesdep_namedepsduplicates_by_extrasactive_extrascomplete_dep_namedeps_by_extrar  msgwarningsr   current_overridespackage_overridesclean_dependenciestransitive_marker_intersectionpython_constraint_intersectionrB   )r  r=   rC   complete_package  sT  	



	


 

$

 






zProvider.complete_packageDependencyPackage | Nonec                 C  sd   |j | jv rd S | j|j g }|D ]}|j}||r/| j|j  }r(||_t||  S qd S rN   )	r   r   r~   r   r.   r   r   r8   r   )r=   r   r_   r   r.   explicit_sourcerB   rB   rC   r    s   
zProvider.get_lockedr   messagerR   depthintc              
     sR  | j  s| j  sd S |dr{d|v r]td|}|d u r&td| td|d}|r@|d}d|d d	}n|d}d
}d| d| d|d d|d d		}nd|v ritdd|}ntdd|}d|	dd  }n|drtdd|}nw|drtd|}|rd|d d|d d	}nZd|	dd  d}nN|drtd|}|rtd|d}|r|d}d|d d	}n|d}d
}d | d| d|d d|d d		}n
d!|	d"d  }|
d#d$}|  r't|}d% fd&d'|	d%D d% }| j | d S d S )(Nzfact:z
depends onz&fact: (.+?) depends on (.+?) \((.+?)\)zUnable to parse fact: z(.+?) \((.+?)\)r  z (<c2>r
  z</c2>) z<fg=blue>fact</>: <c1>z</c1>z depends on <c1>z</c1> (<c2>   z is zfact: (.+) is (.+)z,<fg=blue>fact</>: <c1>\1</c1> is <c2>\2</c2>z(?<=: )(.+?) \((.+?)\)z<c1>\1</c1> (<c2>\2</c2>)z<fg=blue>fact</>: zfact: z
selecting zselecting (.+?) \((.+?)\)z/<fg=blue>selecting</> <c1>\1</c1> (<c2>\2</c2>)zderived:zderived: (.+?) \((.+?)\)$z<fg=blue>derived</>: <c1>z	derived: z	conflict:z*conflict: (.+?) depends on (.+?) \((.+?)\)z&<fg=red;options=bold>conflict</>: <c1>z"<fg=red;options=bold>conflict</>: z
conflict: z! z<error>!</error> r4   c                   s&   g | ]}d t  d d| qS )z<debug>   z
:</debug> )rR   rjust)r   rV   rD  rB   rC   r   C  s    z"Provider.debug.<locals>.<listcomp>)rq   rv   ru   
startswithrematch
ValueErrorgroupsubsplitreplacer   rR   r<   write)r=   rC  rD  mm2r   re   
debug_inforB   rJ  rC   r    s   










zProvider.debugIterable[Dependency]list[list[Dependency]]c                 C  sT   g }|D ]#}|D ]}| |d r |j|d jkr ||  nq||g q|S )z
        Takes a list of dependencies and returns a list of groups of dependencies,
        each group containing all dependencies from the same source.
        r   )is_same_source_asr8   r9   )r=   r/   groupsr?   rO  rB   rB   rC   _group_by_sourceM  s   
zProvider._group_by_sourcec           	      C  s   |  |}g }|D ]5}tt}|D ]
}||j | q| D ]}|d }t|dkr8dd |D }t| |_|| q q	|S )ay  
        Merge dependencies with the same constraint
        by building a union of their markers.

        For instance, if we have:
           - foo (>=2.0) ; python_version >= "3.6" and python_version < "3.7"
           - foo (>=2.0) ; python_version >= "3.7"
        we can avoid two overrides by merging them to:
           - foo (>=2.0) ; python_version >= "3.6"
        r   r  c                 s  s    | ]}|j V  qd S rN   )r   r   rB   rB   rC   r  w  s    z=Provider._merge_dependencies_by_constraint.<locals>.<genexpr>)	r[  r   r}   r7   r9   r   r  marker_unionr   )	r=   r/   
dep_groupsmerged_dependenciesrO  by_constraintr?   r4  new_markersrB   rB   rC   !_merge_dependencies_by_constrainta  s   

z*Provider._merge_dependencies_by_constraintr   r6  c                 C  sF   |   o"| jt|o"|du p|d|io"| j p"|| jjS )a  
        A marker is relevant if
        - it is not empty
        - allowed by the project's python constraint
        - allowed by active extras of the dependency (not relevant for root package)
        - allowed by the environment (only during installation)
        Nr   )r   r   r  r   r   rr   r   )r=   r   r6  rB   rB   rC   _is_relevant_marker}  s   
zProvider._is_relevant_markerc                 C  s:  |  |}g }tjddgt|dD ]}dd tt||dd ddD }t }|D ]}||}q*| ||s9qt	 }	d	}
t
t||}|D ]&}| sQ|jrh|
rf||
r^|j|
jkrft|||
dd
|}
|	|j}	qH|	 r{t|g|R  t|st }	|}|
r|
n|d |	}||_|| q|  |S )ai  
        Convert duplicate dependencies with potentially overlapping markers
        into duplicate dependencies with mutually exclusive markers.

        Therefore, the intersections of all combinations of markers and inverted markers
        have to be calculated. If such an intersection is relevant (not empty, etc.),
        the intersection of all constraints, whose markers were not inverted is built
        and a new dependency with the calculated version constraint and marker is added.
        (The marker of such a dependency does not overlap with the marker
        of any other new dependency.)
        TF)repeatc                 s  s(    | ]\}}|r|j n|j  V  qd S rN   )r   invert)r   user?   rB   rB   rC   r    s
    
z8Provider._resolve_overlapping_markers.<locals>.<genexpr>c                 S  s   | d S )Nr   rB   )udrB   rB   rC   rh     ri   z7Provider._resolve_overlapping_markers.<locals>.<lambda>rj   Nr,   r   )ra  r"  productr  r   zipr   r   rb  r   r}   compressr6   r8   rY  r+   r7   r   anyr
   with_constraintr   r9   )r=   r.   r/   r6  new_dependenciesusesmarkersused_marker_intersectionrT  r7   specific_source_dependencyused_dependenciesr?   new_deprB   rB   rC   r     sP   

z%Provider._resolve_overlapping_markersr   dict[str, Any]c                 C  sB   | j dur| j j ni }|durd|vsJ dt||d< |S )z
        Marker values, from `self._env` if present plus the supplied extras

        :param extras: the values to add to the 'extra' marker value
        Nr   z4'extra' marker key is already present in environment)rr   r   r&  r  )r=   r   resultrB   rB   rC   r    s   
zProvider._marker_values)r.   r%   ra   r)   rb   r   r_   rc   r`   rd   r1   r2   )r1   r)   )r1   r   )r1   r(   )r1   r!   )r1   r0   )r   r   r1   r2   )r   r0   r1   r2   )r   r   r1   r   )r   r*   r1   r   )r   r   r1   r   )r   r"   r.   r%   r1   r2   )r   r"   r1   r%   )r   r"   r1   r   )r   r'   r1   r%   )r   r$   r1   r%   )r   r#   r1   r%   )r   r&   r1   r%   )r/   r   r.   r%   r1   r   )r   r   r1   r   )r   r   r1   r   )r   r"   r1   rA  )r   )rC  rR   rD  rE  r1   r2   )r/   rW  r1   rX  )r/   rW  r1   r   )r   r(   r6  rd   r1   r0   )r.   r%   r/   r   r6  rd   r1   r   rN   )r   rd   r1   rs  )'rD   rE   rF   r  r^   r[   r;   propertyra   r   r   cached_propertyr   r   r   r   r   r   r   r   r   r\   r   r   r   r   r   r   r   r   r   r@  r  r  r[  ra  rb  r   r  rB   rB   rB   rC   r]   o   sV   
 (	


	


&
%





7  
$
V


\r]   )R
__future__r   r   r"  loggingrL  rX   collectionsr   
contextlibr   typingr   r   r   r   cleo.ui.progress_indicatorr	   poetry.core.constraints.versionr
   r   r    poetry.core.packages.utils.utilsr   poetry.core.version.markersr   r   r   r\  poetry.mixology.incompatibilityr   %poetry.mixology.incompatibility_causer   r   poetry.mixology.termr   poetry.packagesr   poetry.packages.direct_originr   "poetry.packages.package_collectionr   poetry.puzzle.exceptionsr   poetry.utils.helpersr   collections.abcr   r   r   r   pathlibr   
cleo.io.ior   packaging.utilsr    r!   poetry.core.packages.dependencyr"   )poetry.core.packages.directory_dependencyr#   $poetry.core.packages.file_dependencyr$   poetry.core.packages.packager%   #poetry.core.packages.url_dependencyr&   #poetry.core.packages.vcs_dependencyr'   r(   poetry.repositoriesr)   poetry.utils.envr*   	getLoggerrD   logger	Exceptionr+   rI   r]   rB   rB   rB   rC   <module>   sf    
