
    =2g                        d Z 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mZ ddlZdZdZd	Zej&                  d
z
  dz
  ZddZ G d dej,                        Ze	d d dd	 	 	 	 	 dd       Zy)a  
Tools for creating ``.conda``-format archives.

Uses ``tempfile.SpooledTemporaryFile`` to buffer ``pkg-*.tar`` and
``info-*.tar``, then compress directly into an open `ZipFile` at the end.
`SpooledTemporaryFile` buffers the first 10MB of the package and its metadata in
memory, but writes out to disk for larger packages.

Uses more disk space than ``conda-package-handling`` (temporary uncompressed
tarballs of the package contents) but accepts streams instead of just
files-on-the-filesystem.
    )annotationsN)contextmanager)Path)CallableIterator         i   c                >    dx| _         | _        dx| _        | _        | S )z
    Pass to ``tarfile.add(..., filter=anonymize)`` to anonymize uid/gid.

    Does not anonymize mtime or any other field.
    r    )uidgidunamegname)tarinfos    >lib/python3.12/site-packages/conda_package_streaming/create.py	anonymizer   (   s%     !"!GK'+$&&GMGMN    c                  P     e Zd ZU dZded<   ded<   d d	 d
 fdZd fd		Z xZS )CondaTarFileaM  
    Subclass of :external+python:py:class:`tarfile.TarFile` that adds members to
    a second ``info`` tar if they match ``is_info(name)``.

    Create this with ``conda_builder(...)`` which sets up the component
    archives, then wraps them into a ``.conda`` on exit.

    Only useful for creating, not extracting ``.conda``.
    tarfile.TarFileinfo_tarr   is_infoc                $    | j                  d      S Nzinfo/
startswith)names    r   <lambda>zCondaTarFile.<lambda>E   s    T__W5r   )r   c               @    t        |   |i | || _        || _        y N)super__init__r   r   )selfr   r   argskwargs	__class__s        r   r#   zCondaTarFile.__init__A   s%     	$)&) r   c                    | j                  |j                        r| j                  j                  ||      S t        |   ||      S )a  
        Add the TarInfo object ``tarinfo`` to the archive. If ``fileobj`` is
        given, it should be a binary file, and tarinfo.size bytes are read from
        it and added to the archive. You can create TarInfo objects directly, or
        by using ``gettarinfo()``.

        If ``self.is_info(tarinfo.name)`` returns ``True``, add ``tarinfo`` to
        ``self.info_tar`` instead.
        )fileobj)r   r   r   addfiler"   )r$   r   r)   r'   s      r   r*   zCondaTarFile.addfileL   s@     <<%==(('(BB7?7G44r   )r   r   r!   )__name__
__module____qualname____doc____annotations__r#   r*   __classcell__)r'   s   @r   r   r   3   s4      6		 "	5 5r   r   c                 @    t        j                  t        t              S )N)levelthreads)	zstandardZstdCompressorZSTD_COMPRESS_LEVELZSTD_COMPRESS_THREADS r   r   r   r   c   s    	((!+@r   c                $    | j                  d      S r   r   )filenames    r   r   r   f   s    h6I6I'6Rr   zutf-8)
compressorr   encodingc          
   #    K   t        ||  d      }t        j                         5 }t        j                         5 }t        j                  |d|      5 }t        |d|||      5 }	|	 |j                          |	j                          |j                         }
|j                         }|j                  d       |j                  d       ddd       ddd       t        j                  |dt        j                        5 } |       }d	t        i}|j                  d
t        j                  |             |j!                  d|  ddt"        kD        5 }|j%                  ||d      5 }t'        j(                  |j*                  |       ddd       ddd       |j!                  d|  dd
t"        kD        5 }|j%                  ||
d      5 }t'        j(                  |j*                  |       ddd       ddd       ddd       ddd       ddd       y# 1 sw Y   PxY w# 1 sw Y   UxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   \xY w# 1 sw Y   `xY w# 1 sw Y   dxY w# 1 sw Y   hxY w# 1 sw Y   yxY ww)a  
    Produce a ``TarFile`` subclass used to build a ``.conda`` package. The
    subclass delegates ``addfile()`` to the ``info-`` component when ``is_info``
    returns True.

    When the context manager exits, ``{path}/{stem}.conda`` is written with
    the component tar archives.

    Args:
        stem: output filename without extension

        path: destination path for transmuted .conda package compressor: A
            function that creates instances of ``zstandard.ZstdCompressor()``.

        encoding: passed to TarFile constructor. Keep default "utf-8" for valid
            .conda.

    Yields:
        ``CondaTarFile``
    z.condaw)r)   moder<   )r)   r?   r   r   r<   r   Nx)compresslevelconda_pkg_format_versionzmetadata.jsonzpkg-z.tar.zst)force_zip64F)sizeclosefdzinfo-)r   tempfileSpooledTemporaryFiletarfileTarFiler   closetellseekzipfileZipFile
ZIP_STOREDCONDA_PACKAGE_FORMAT_VERSIONwritestrjsondumpsopenCONDA_ZIP64_LIMITstream_writershutilcopyfileobj_file)stempathr;   r   r<   output_path	info_filepkg_filer   pkg_tar	info_sizepkg_size
conda_filedata_compresspkg_metadatapkg_file_zip
pkg_streaminfo_file_zipinfo_streams                      r   conda_builderri   \   sM    B tvV_-K		&	&	(Ix7T7T7VZb__C(
|
  MNNMMO!(I}}HNN1MM!'

. __!,,
 &LM68TULL1IJtfH%%(99 !  }::8U  ;  ""8>>:> 	 vX&&):: !   ; ; !< ! ""9??K@!	)
1 8W	(	(
 

 
L   	 ! !	 )
 
1 8W7V	(	(s   $JI<I0H'%A'H	H'.I0AI$I 	+!H4I 	'I$;I	!I1I	9I$I0	I<	JH$H''H1,I04H=9I 	 I	I$II	I!I$$I-)I00I9	5I<<JJ)r   ztarfile.TarInfo)r;   z&Callable[[], zstandard.ZstdCompressor]r   zCallable[[str], bool]returnzIterator[CondaTarFile])r.   
__future__r   rR   rW   rH   rF   rM   
contextlibr   pathlibr   typingr   r   r4   r6   r7   rP   ZIP64_LIMITrU   r   rI   r   ri   r8   r   r   <module>rp      s    #      %  %       ''73a7 &57?? &5R 	 &SVA	VA #VA VA VAr   