o
    (g                     @   s   d Z ddl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
 g dZG dd deZdd	 Zedfd
dZefddZefddZdd Zdd ZefddZeeefZdS )z/Utilities for extracting common archive formats    N)DistutilsError)ensure_directory)unpack_archiveunpack_zipfileunpack_tarfiledefault_filterUnrecognizedFormatextraction_driversunpack_directoryc                   @   s   e Zd ZdZdS )r   z#Couldn't recognize the archive typeN)__name__
__module____qualname____doc__ r   r   _/var/www/python.lazyprojects.co.uk/venv/lib/python3.10/site-packages/setuptools/archive_util.pyr      s    r   c                 C   s   |S )z@The default progress/filter callback; returns True for all filesr   )srcdstr   r   r   r      s   r   c              	   C   s@   |pt D ]}z
|| || W  dS  ty   Y qw td|  )a  Unpack `filename` to `extract_dir`, or raise ``UnrecognizedFormat``

    `progress_filter` is a function taking two arguments: a source path
    internal to the archive ('/'-separated), and a filesystem path where it
    will be extracted.  The callback must return the desired extract path
    (which may be the same as the one passed in), or else ``None`` to skip
    that file or directory.  The callback can thus be used to report on the
    progress of the extraction, as well as to filter the items extracted or
    alter their extraction paths.

    `drivers`, if supplied, must be a non-empty sequence of functions with the
    same signature as this function (minus the `drivers` argument), that raise
    ``UnrecognizedFormat`` if they do not support extracting the designated
    archive type.  The `drivers` are tried in sequence until one is found that
    does not raise an error, or until all are exhausted (in which case
    ``UnrecognizedFormat`` is raised).  If you do not supply a sequence of
    drivers, the module's ``extraction_drivers`` constant will be used, which
    means that ``unpack_zipfile`` and ``unpack_tarfile`` will be tried, in that
    order.
    Nz!Not a recognized archive type: %s)r	   r   )filenameextract_dirprogress_filterdriversdriverr   r   r   r      s   r   c                 C   s   t j| std|  | d|fi}t | D ]Q\}}}|| \}}|D ]}	||	 d t j||	f|t j||	< q$|D ]*}
t j||
}|||
 |}|sPq=t| t j||
}
t|
| t	|
| q=qdS )z"Unpack" a directory, using the same interface as for archives

    Raises ``UnrecognizedFormat`` if `filename` is not a directory
    z%s is not a directory /N)
ospathisdirr   walkjoinr   shutilcopyfilecopystat)r   r   r   pathsbasedirsfilesr   r   dftargetr   r   r   r
   @   s&   *r
   c           
   
   C   s  t | std| f t | p}| D ]b}|j}|ds'd|dv r(qtj	j
|g|dR  }|||}|s=q|drGt| n$t| ||j}t|d}|| W d   n1 sfw   Y  |jd? }	|	rxt||	 qW d   dS 1 sw   Y  dS )zUnpack zip `filename` to `extract_dir`

    Raises ``UnrecognizedFormat`` if `filename` is not a zipfile (as determined
    by ``zipfile.is_zipfile()``).  See ``unpack_archive()`` for an explanation
    of the `progress_filter` argument.
    z%s is not a zip filer   ..wbN   )zipfile
is_zipfiler   ZipFileinfolistr   
startswithsplitr   r   r   endswithr   readopenwriteexternal_attrchmod)
r   r   r   zinfonamer(   datar'   unix_attributesr   r   r   r   [   s0   




"r   c                 C   s   |dur5|  s| r5|j}| r$t|j}t||}t|}| |}|dur5|  s| s|duo@|	 p@|
 }|rE|S td)z;Resolve any links and extract link targets as normal files.NzGot unknown file type)islnkissymlinkname	posixpathdirnamer:   r   normpath
_getmemberisfiler   LookupError)tar_objtar_member_objlinkpathr#   is_file_or_dirr   r   r   _resolve_tar_file_or_dir   s,   


rJ   c              
   c   s    dd | _ t| U | D ]I}|j}|dsd|dv r qtjj|g|dR  }zt	| |}W n	 t
y=   Y qw |||}|sFq|tjrR|dd }||fV  qW d   dS 1 scw   Y  dS )z1Emit member-destination pairs from a tar archive.c                  W   s   d S )Nr   )argsr   r   r   <lambda>   s    z _iter_open_tar.<locals>.<lambda>r   r)   N)chown
contextlibclosingr:   r0   r1   r   r   r   rJ   rE   r2   sep)rF   r   r   memberr:   
prelim_dst	final_dstr   r   r   _iter_open_tar   s*   

"rU   c              
   C   sx   zt | }W n t jy } ztd| f |d}~ww t|||D ]\}}z||| W q# t jy9   Y q#w dS )zUnpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`

    Raises ``UnrecognizedFormat`` if `filename` is not a tarfile (as determined
    by ``tarfile.open()``).  See ``unpack_archive()`` for an explanation
    of the `progress_filter` argument.
    z/%s is not a compressed or uncompressed tar fileNT)tarfiler4   TarErrorr   rU   _extract_memberExtractError)r   r   r   tarobjerR   rT   r   r   r   r      s&   r   )r   r,   rV   r   r   r@   rO   distutils.errorsr   pkg_resourcesr   __all__r   r   r   r
   r   rJ   rU   r   r	   r   r   r   r   <module>   s*    
$%