o
    Uh,                     @   s   d 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m	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZ ddlmZmZ eeeeef eef Zdd	gZG d
d dZG dd deeZG dd deeZG dd	 d	eZ dS )z)Source of information about a wheel file.    N)contextmanager)BinaryIOClassVarIteratorListOptionalTupleTypecast)InstallerError)RecordEntryparse_record_file)canonicalize_nameparse_wheel_filenameWheelSource	WheelFilec                       s   e Zd ZU dZeZeee  e	d< de
de
ddf fddZed	d
 Zedd Zedee
 fddZde
de
fddZdddZdee fddZ  ZS )r   zyRepresents an installable wheel.

    This is an abstract class, whose methods have to be implemented by subclasses.
    validation_errordistributionversionreturnNc                    s   t    || _|| _dS )zInitialize a WheelSource object.

        :param distribution: distribution name (like ``urllib3``)
        :param version: version associated with the wheel
        N)super__init__r   r   )selfr   r   	__class__ e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/installer/sources.pyr      s   

zWheelSource.__init__c                 C      | j  d| j dS ) Name of the dist-info directory.-
.dist-infor   r   r   r   r   r   dist_info_dir&      zWheelSource.dist_info_dirc                 C   r   )zName of the data directory.r   z.datar!   r"   r   r   r   data_dir+   r$   zWheelSource.data_dirc                 C      t )zGet names of all files in the dist-info directory.

        Sample usage/behaviour::

            >>> wheel_source.dist_info_filenames
            ['METADATA', 'WHEEL']
        NotImplementedErrorr"   r   r   r   dist_info_filenames0   s   	zWheelSource.dist_info_filenamesfilenamec                 C   r&   )zGet contents, from ``filename`` in the dist-info directory.

        Sample usage/behaviour::

            >>> wheel_source.read_dist_info("METADATA")
            ...

        :param filename: name of the file
        r'   )r   r*   r   r   r   read_dist_info;   s   
zWheelSource.read_dist_infoc                 C   r&   )zValidate ``RECORD`` of the wheel.

        This method should be called before :py:func:`install <installer.install>`
        if validation is required.
        r'   r"   r   r   r   validate_recordG   s   zWheelSource.validate_recordc                 C   r&   )a  Sequential access to all contents of the wheel (including dist-info files).

        This method should return an iterable. Each value from the iterable must be a
        tuple containing 3 elements:

        - record: 3-value tuple, to pass to
          :py:meth:`RecordEntry.from_elements <installer.records.RecordEntry.from_elements>`.
        - stream: An :py:class:`io.BufferedReader` object, providing the contents of the
          file at the location provided by the first element (path).
        - is_executable: A boolean, representing whether the item has an executable bit.

        All paths must be relative to the root of the wheel.

        Sample usage/behaviour::

            >>> iterable = wheel_source.get_contents()
            >>> next(iterable)
            (('pkg/__init__.py', '', '0'), <...>, False)

        This method may be called multiple times. Each iterable returned must
        provide the same content upon reading from a specific file's stream.
        r'   r"   r   r   r   get_contentsO   s   zWheelSource.get_contents)r   N)__name__
__module____qualname____doc__
ValueErrorr   r   r	   	Exception__annotations__strr   propertyr#   r%   r   r)   r+   r,   r   WheelContentElementr-   __classcell__r   r   r   r   r      s   
 




c                       s<   e Zd ZdZdee ddf fddZdefddZ  ZS )	_WheelFileValidationErrorz*Raised when a wheel file fails validation.issuesr   Nc                    s   t  t| || _d S N)r   r   reprr:   )r   r:   r   r   r   r   l   s   
z"_WheelFileValidationError.__init__c                 C   s   d| j dS )Nz WheelFileValidationError(issues=))r:   r"   r   r   r   __repr__p   s   z"_WheelFileValidationError.__repr__)	r.   r/   r0   r1   r   r5   r   r>   r8   r   r   r   r   r9   i   s    r9   c                       sD   e Zd ZdZdedee deddf fddZdefd	d
Z  ZS )_WheelFileBadDistInfoz8Raised when a wheel file has issues around `.dist-info`.reasonr*   	dist_infor   Nc                   s"   t  | || _|| _|| _d S r;   )r   r   r@   r*   rA   )r   r@   r*   rA   r   r   r   r   w   s   
z_WheelFileBadDistInfo.__init__c                 C   s   | j  d| jd| jdS )Nz (filename=z, dist_info=r=   r@   r*   rA   r"   r   r   r   __str__}   s   z_WheelFileBadDistInfo.__str__)	r.   r/   r0   r1   r5   r   r   rC   r8   r   r   r   r   r?   t   s    "r?   c                       s   e Zd ZdZeZdejddf fddZe	e
ddded  fd	d
ZedefddZedee fddZdedefddZdddeddfddZdee fddZ  ZS )r   zImplements `WheelSource`, for an existing file from the filesystem.

    Example usage::

        >>> with WheelFile.open("sampleproject-2.0.0-py3-none-any.whl") as source:
        ...     installer.install(source, destination)
    fr   Nc                    sD   || _ |jsJ tj|j}t|}t j|j|j	d d| _
dS )zInitialize a WheelFile object.

        :param f: An open zipfile, which will stay open as long as this object is used.
        )r   r   N)_zipfiler*   ospathbasenamer   r   r   r   r   _dist_info_dir)r   rD   rH   parsed_namer   r   r   r      s   

zWheelFile.__init__rG   zos.PathLike[str]c                 c   s<    t |}| |V  W d   dS 1 sw   Y  dS )z%Create a wheelfile from a given path.N)zipfileZipFile)clsrG   rD   r   r   r   open   s   "zWheelFile.openc              	   C   s   | j dur| j S dd | j D }dd |D }z|\}W n ty3   td| jjtt|ddw |dd	d
 }t	|}t	| j
}||krRtd| jj|d|| _ |S )r   Nc                 S   s   h | ]
}| d dd qS )/   r   )split).0rG   r   r   r   	<setcomp>   s    z*WheelFile.dist_info_dir.<locals>.<setcomp>c                 S   s   g | ]	}| d r|qS )r    )endswithrR   namer   r   r   
<listcomp>   s
    
z+WheelFile.dist_info_dir.<locals>.<listcomp>z6Wheel doesn't contain exactly one .dist-info directoryrB   r      r   z7Wheel .dist-info directory doesn't match wheel filename)rI   rE   namelistr2   r?   r*   r5   sortedrsplitr   r   )r   top_level_directories
dist_infosr#   di_dnamenorm_di_dnamenorm_file_dnamer   r   r   r#      s<   



zWheelFile.dist_info_dirc                    s   | j   fdd| j D S )z2Get names of all files in the dist-info directory.c                    sB   g | ]}|d d dkr t | gkr|t d d qS )NrO   rP   )	posixpathcommonprefixlenrU   baser   r   rW      s    z1WheelFile.dist_info_filenames.<locals>.<listcomp>)r#   rE   rY   r"   r   re   r   r)      s   
zWheelFile.dist_info_filenamesr*   c                 C   s    t | j|}| j|dS )z;Get contents, from ``filename`` in the dist-info directory.zutf-8)rb   joinr#   rE   readdecode)r   r*   rG   r   r   r   r+      s   zWheelFile.read_dist_infoT)validate_contentsrj   c          
   
   C   s  z|  d }dd t|D }W n ty, } ztd| jj d|g|d}~ww g }| j D ]}|jdd dkr@q4||jd}| j	t
| j	|jgkrq|jdd d	v rq|durp|d
| jj d|j d q4|du r|d
| jj d|j d q4tj| }|j| j	 dkr|jdus|jdur|d
| jj d q4|jdu s|jdu r|d
| jj d|j d |r| j|}	||	s|d
| jj d|j d q4|rt|dS )a  Validate ``RECORD`` of the wheel.

        This method should be called before :py:func:`install <installer.install>`
        if validation is required.

        File names will always be validated against ``RECORD``.

        If ``validate_contents`` is true, sizes and hashes of files
        will also be validated against ``RECORD``.

        :param validate_contents: Whether to validate content integrity.
        RECORDc                 S      i | ]}|d  |qS r   r   rR   recordr   r   r   
<dictcomp>   s    
z-WheelFile.validate_record.<locals>.<dictcomp>z!Unable to retrieve `RECORD` from z: Nra   rO   )z
RECORD.p7sz
RECORD.jwszIn z, digital signature file z$ is incorrectly contained in RECORD.z, z is not mentioned in RECORDz/RECORDz/, RECORD file incorrectly contains hash / size.z, hash / size of z is not included in RECORDz didn't match RECORD)r+   
splitlinesr   r3   r9   rE   r*   infolistpopr#   rb   rc   rQ   appendr   from_elementshash_sizerh   validate)
r   rj   record_linesrecord_mappingexcr:   itemrecord_argsro   datar   r   r   r,      sj   



zWheelFile.validate_recordc           
   	   c   s    |  d }t|}dd |D }| j D ]J}|jdd dkr$q||j|jddf}|jd? }t|o?t	
|o?|d	@ }| j|}td
|}	||	|fV  W d   n1 s]w   Y  qdS )a  Sequential access to all contents of the wheel (including dist-info files).

        This implementation requires that every file that is a part of the wheel
        archive has a corresponding entry in RECORD. If they are not, an
        :any:`AssertionError` will be raised.
        rk   c                 S   rl   rm   r   rn   r   r   r   rp   -  s    z*WheelFile.get_contents.<locals>.<dictcomp>ra   NrO       I   r   )r+   rq   r   rE   rr   r*   rs   external_attrboolstatS_ISREGrN   r
   )
r   ry   recordsrz   r|   ro   modeis_executablestreamstream_castedr   r   r   r-   #  s    

zWheelFile.get_contents)r.   r/   r0   r1   r9   r   rK   rL   r   classmethodr   r   rN   r6   r5   r#   r   r)   r+   r   r,   r7   r-   r8   r   r   r   r   r      s    $
I)!r1   rF   rb   r   rK   
contextlibr   typingr   r   r   r   r   r   r	   r
   installer.exceptionsr   installer.recordsr   r   installer.utilsr   r   r5   r   r7   __all__r   r2   r9   r?   r   r   r   r   r   <module>   s     (U