o
    VhK                     @  s  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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
rd 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-e.Z/e0dZ1G dd de2Z3G dd dZ4ej5d"d d!Z6dS )#    )annotationsN)Path)TYPE_CHECKING)Any)Version)Factory)
Dependency)Package)PyProjectTOML)parse_requires)temporary_directory)InvalidMarkerError)InvalidRequirementError)
extractall)IsolatedBuildBackendError)isolated_builder)Iterator)Mapping)Sequence)RawMetadata)NormalizedName)ProjectPackagez2.2c                      s   e Zd Zd	 fddZ  ZS )
PackageInfoErrorpathr   reasonsBaseException | strreturnNonec                   s2   d|g|R }t  ddd |D  d S )Nz+Unable to determine package info for path: z

c                 s  s     | ]}|rt | V  qd S N)strstrip).0msg r#   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/inspection/info.py	<genexpr>0   s    z,PackageInfoError.__init__.<locals>.<genexpr>)super__init__join)selfr   r   	__class__r#   r$   r'   .   s    zPackageInfoError.__init__)r   r   r   r   r   r   )__name__
__module____qualname__r'   __classcell__r#   r#   r*   r$   r   -   s    r   c                	   @  s  e Zd ZddddddddddHddZedIddZdJddZdKddZedLddZ		dMdNd"d#Z
edOd&d'ZedPd(d)ZedQd,d-ZedRd/d0ZedSd3d4ZedTd6d7ZedUd9d:ZedVd<d=ZedQd>d?ZedQd@dAZedQdBdCZedQdDdEZedQdFdGZdS )WPackageInfoNF)nameversionsummaryrequires_distrequires_pythonfilesyankedcache_versionr1   
str | Noner2   r3   r4   list[str] | Noner5   r6   "Sequence[Mapping[str, str]] | Noner7   
str | boolr8   r   r   c          	      C  sJ   || _ || _|| _|| _|| _|pg | _|| _|| _d | _d | _	d | _
d S r   )r1   r2   r3   r4   r5   r6   r7   _cache_version_source_type_source_url_source_reference)	r)   r1   r2   r3   r4   r5   r6   r7   r8   r#   r#   r$   r'   4   s   

zPackageInfo.__init__c                 C  s   | j S r   )r=   r)   r#   r#   r$   r8   L   s   zPackageInfo.cache_versionotherc                 C  sf   |j p| j | _ |jp| j| _|jp| j| _|jp| j| _|jp!| j| _|jp(| j| _|jp/| j| _| S r   )r1   r2   r3   r4   r5   r6   r8   r=   )r)   rB   r#   r#   r$   updateP   s   zPackageInfo.updatedict[str, Any]c              	   C  s&   | j | j| j| j| j| j| j| jdS )z[
        Helper method to convert package info into a dictionary used for caching.
        r1   r2   r3   r4   r5   r6   r7   r=   rE   rA   r#   r#   r$   asdictZ   s   zPackageInfo.asdictdatac                 C  s   | dd}| dd|i|S )z
        Helper method to load data from a dictionary produced by `PackageInfo.asdict()`.

        :param data: Data to load. This is expected to be a `dict` object output by
            `asdict()`.
        r=   Nr8   r#   )pop)clsrG   r8   r#   r#   r$   loadi   s   zPackageInfo.loadroot_dirPath | Noner	   c              
   C  s  |p| j }|std| jstd| t|| j| j| j| j| jd}| jdur-| j|_	||_
| jp4d|_| j|_|durA|}n| jdkrQ| jdurQt| j}nd}|durp| j|d}|rp|j|_|jD ]}|| qf|S t }i }| jpyg D ]k}	z	tj|	|d}W n3 ty   td	|	|j |j |	d
d }	tj|	|d}Y n ty   td|	|j |j Y qzw |jr|jD ]}
|
|vrg ||
< ||
 | q|jdd}	|	|vr|| ||	 qz||_|S )a  
        Create a new `poetry.core.packages.package.Package` instance using metadata from
        this instance.

        :param name: Name to use for the package, if not specified name from this
            instance is used.
        :param extras: Extras to activate for this package.
        :param root_dir:  Optional root directory to use for the package. If set,
            dependency strings will be parsed relative to this directory.
        z%Unable to create package with no namez+Unable to retrieve the package version for )r1   r2   source_type
source_urlsource_referencer7   N*	directoryr   )relative_toz9Stripping invalid marker (%s) found in %s-%s dependencies;r   z>Invalid requirement (%s) found in %s-%s dependencies, skippingT)with_extras) r1   RuntimeErrorr2   r	   r>   r?   r@   r7   r3   descriptionrK   r5   python_versionsr6   r   _get_poetry_packageextrasrequiresadd_dependencysetr4   r   create_from_pep_508r   loggerwarningsplitr   	in_extrasappend
to_pep_508add)r)   r1   rK   packager   poetry_package
dependencyseen_requirementspackage_extrasreqextrar#   r#   r$   
to_packaget   s   






zPackageInfo.to_packagedist-pkginfo.BDist | pkginfo.SDist | pkginfo.Wheelc                 C  sn   |j rt|j S |jdurt|j}|tkrd|jvrg S t|jd }|	 r5|j
dd}t|}|S dS )z
        Helper method to extract package requirements from a `pkginfo.Distribution`
        instance.

        :param dist: The distribution instance to extract requirements from.
        NzRequires-Distzrequires.txtzutf-8)encoding)r4   listmetadata_versionr   parseDYNAMIC_METADATA_VERSIONdynamicr   filenameexists	read_textr   )rI   rn   rr   r[   textrequirementsr#   r#   r$   _requirements_from_distribution   s   


z+PackageInfo._requirements_from_distributionc                 C  sz   |j r|j dd dd tjjD vrtd|j  | |}| |j|j|j	||j
d}d|_t|j  |_|S )z
        Helper method to parse package information from a `pkginfo.Distribution`
        instance.

        :param dist: The distribution instance to parse information from.
        .r   c                 S  s   h | ]	}| d d qS )r|   r   )ra   )r!   vr#   r#   r$   	<setcomp>  s    z1PackageInfo._from_distribution.<locals>.<setcomp>zUnknown metadata version: r1   r2   r3   r4   r5   file)rr   ra   pkginfodistributionHEADER_ATTRS
ValueErrorr{   r1   r2   r3   r5   r>   r   rv   resolveas_posixr?   )rI   rn   rz   infor#   r#   r$   _from_distribution   s   
zPackageInfo._from_distributionr   r   c                 C  sX  d}t t tt|}| |}W d   n1 sw   Y  |dur.|jdur.|S |j}|dk}|dkrK|j	}t
|dkrJ|d dkrJd}n|sOd}t I}t|}t|||d	 t|d
}	t
|	dkrv|	d  rv|	d }
n||j| }
|
 s|}
| j|
d}d|_|  |_W d   n1 sw   Y  |s|S ||S )aG  
        Helper method to parse package information from an sdist file. We attempt to
        first inspect the file using `pkginfo.SDist`. If this does not provide us with
        package requirements, we extract the source and handle it as a directory.

        :param path: The sdist file to parse information from.
        Nz.zipz.bz2   z.tarz.tar.bz2z.tar.gz)sourcedestziprP   r   rR   r   )
contextlibsuppressr   r   SDistr   r   r4   suffixsuffixeslenr   r   r   rq   globis_dirr1   rstripfrom_directoryr>   r   r   r?   rC   )rI   r   r   sdistr   r   r   tmp_strtmpelements	sdist_dirnew_infor#   r#   r$   _from_sdist_file!  s@   	

zPackageInfo._from_sdist_fileIterator[Path]c                 c  s6    d}t j| | dd}|D ]}t|V  qdS )zl
        Discover all `*.*-info` directories in a given path.

        :param path: Path to search.
        z**/*.*-infoT)	recursiveN)r   iglobjoinpathr   r   )r   patterndirectoriesdr#   r#   r$   _find_dist_infoX  s   zPackageInfo._find_dist_infometadatar   c                 C  s0   | | d| d| d| d| ddS )zg
        Create package information from core metadata.

        :param metadata: raw metadata
        r1   r2   r3   r4   r5   r   )get)rI   r   r#   r#   r$   from_metadatah  s   zPackageInfo.from_metadataPackageInfo | Nonec              	   C  s   |j dv r	|g}nt| j|d}|D ]*}z|j dkr#t| }n|j dkr0t| }nW qW  n ty=   Y qw z	t| }W n
 tyQ   Y dS w | j|dS )z
        Helper method to parse package information from an unpacked metadata directory.

        :param path: The metadata directory to parse information from.
        >   	.egg-info
.dist-inforR   r   r   N)rn   )	r   rq   r   r   UnpackedSDistr   Wheelr   r   )rI   r   r   rQ   rn   r#   r#   r$   from_metadata_directoryw  s(   


z#PackageInfo.from_metadata_directoryrf   c              	   C  sp   dd |j D }|j D ]}|D ]	}||  qq| |jt|j|jt	||j
|j|jr4|jdS ddS )z
        Helper method to inspect a `Package` object, in order to generate package info.

        :param package: This must be a poetry package instance.
        c                 S  s   h | ]}|  qS r#   )rd   )r!   rh   r#   r#   r$   r~     s    z+PackageInfo.from_package.<locals>.<setcomp>F)r1   r2   r3   r4   r5   r6   r7   )r[   rZ   valuesre   rd   r1   r   r2   rW   rq   rX   r6   r7   yanked_reason)rI   rf   r[   extra_requiresrh   r#   r#   r$   from_package  s    
zPackageInfo.from_packageProjectPackage | Nonec                 C  sN   t | d r%tt t | jW  d    S 1 s w   Y  d S )Nzpyproject.toml)	r
   r   is_poetry_projectr   r   rV   r   create_poetryrf   rR   r#   r#   r$   rY     s
    zPackageInfo._get_poetry_packagec                 C  sl   |  |}|r| |}n| |}|r|jdu r,zt|}W n ty+   |s) Y nw d|_| |_|S )a7  
        Generate package information from a package source directory. If introspection
        of all available metadata fails, the package is attempted to be built in an
        isolated environment so as to generate required metadata.

        :param path: Path to generate package information from.
        NrQ   )	rY   r   r   r4   get_pep517_metadatar   r>   r   r?   )rI   r   project_packager   r#   r#   r$   r     s   
	

zPackageInfo.from_directoryc                 C  s    |  r
| j|dS | j|dS )z
        Gather package information from an sdist file, packed or unpacked.

        :param path: Path to an sdist file or unpacked directory.
        rR   )is_filer   r   rI   r   r#   r#   r$   
from_sdist  s   zPackageInfo.from_sdistc              
   C  s>   zt t|}| |W S  ty } zt||d}~ww )z_
        Gather package information from a wheel.

        :param path: Path to wheel.
        N)r   r   r   r   r   r   )rI   r   wheeler#   r#   r$   
from_wheel  s   
zPackageInfo.from_wheelc              
   C  sT   |j dkr| j|dS ztt|}| |W S  ty) } zt||d}~ww )zl
        Gather package information from a bdist (wheel etc.).

        :param path: Path to bdist.
        z.whlrR   N)r   r   r   BDistr   r   r   r   )rI   r   bdistr   r#   r#   r$   
from_bdist  s   

zPackageInfo.from_bdistc                 C  s.   z| j |dW S  ty   | j|d Y S w )z
        Gather package information from a given path (bdist, sdist, directory).

        :param path: Path to inspect.
        rR   )r   r   r   r   r#   r#   r$   	from_path  s
   zPackageInfo.from_path)r1   r9   r2   r9   r3   r9   r4   r:   r5   r9   r6   r;   r7   r<   r8   r9   r   r   )r   r9   )rB   r0   r   r0   )r   rD   )rG   rD   r   r0   )NN)r1   r9   rK   rL   r   r	   )rn   ro   r   r:   )rn   ro   r   r0   r   r   r   r0   )r   r   r   r   )r   r   r   r0   )r   r   r   r   )rf   r	   r   r0   )r   r   r   r   )r,   r-   r.   r'   propertyr8   rC   rF   classmethodrJ   rm   r{   r   r   staticmethodr   r   r   r   rY   r   r   r   r   r   r#   r#   r#   r$   r0   3   sX    


f$!6 	r0   r   r   r   c                 C  s   d}t  B}z%t|}t| d}|| W d   n1 s!w   Y  t|}W n ty@ } zt| t	|dd}~ww W d   n1 sKw   Y  |rT|S t| d)z
    Helper method to use PEP-517 library to build and read package metadata.

    :param path: Path to package source to build and read metadata for.
    Nr   z$Exhausted all core metadata sources.)
tempfileTemporaryDirectoryr   r   metadata_pathr0   r   r   r   r   )r   r   rn   r   builderr   r#   r#   r$   r     s"   

r   r   )7
__future__r   r   	functoolsr   loggingr   pathlibr   typingr   r   r   poetry.core.constraints.versionr   poetry.core.factoryr   poetry.core.packages.dependencyr   poetry.core.packages.packager	   poetry.core.pyproject.tomlr
   poetry.core.utils.helpersr   r   poetry.core.version.markersr    poetry.core.version.requirementsr   poetry.utils.helpersr   poetry.utils.isolated_buildr   r   collections.abcr   r   r   packaging.metadatar   packaging.utilsr   $poetry.core.packages.project_packager   	getLoggerr,   r_   rs   rt   r   r   r0   cacher   r#   r#   r#   r$   <module>   sL    

   _