
    =2g                        d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
Z
ddlmZmZmZ ddlmZmZ d	 d
 d	 	 	 	 	 ddZd d d	 	 	 	 	 ddZ	 	 	 	 ddZy)a/  
Convert .tar.bz2 to .conda

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.
    )annotationsN)Path)CallableIterator   )ZSTD_COMPRESS_LEVELZSTD_COMPRESS_THREADSconda_builder)CondaComponentstream_conda_componentc                 @    t        j                  t        t              S N)levelthreads	zstandardZstdCompressorr   r	        Alib/python3.12/site-packages/conda_package_streaming/transmute.py<lambda>r          	((!+@r   c                $    | j                  d      S Nzinfo/
startswithfilenames    r   r   r   "       h6I6I'6Rr   
compressoris_infoc                  | j                  d      sJ d       t        j                  j                  |      sJ t        j                  j	                  |       dt        d        }t        |       }t        |||||      S )a  
    Convert .tar.bz2 conda package to .conda-format under path.

    :param package: path to .tar.bz2 conda package
    :param path: destination path for transmuted .conda package
    :param compressor: A function that creates instances of
        ``zstandard.ZstdCompressor()`` to override defaults.
    :param is_info: A function that returns True if a file belongs in the
        ``info`` component of a `.conda` package.  ``conda-package-handling``
        (not this package ``conda-package-streaming``) uses a set of regular
        expressions to keep expected items in the info- component, while other
        items starting with ``info/`` wind up in the pkg- component.

    :return: Path to transmuted package.
    .tar.bz2z#can only convert .tar.bz2 to .condaN)r!   r"   package_stream)endswithospathisdirbasenamelenr   transmute_stream)packager(   r!   r"   stemr%   s         r   	transmuter/      s}    4 J'N)NN'77==77G$%7J'78D+G4N% r   c                 @    t        j                  t        t              S r   r   r   r   r   r   r   G   r   r   c                $    | j                  d      S r   r   r   s    r   r   r   J   r   r   c               
   t        ||  d      }t        | |||      5 }|D ]H  \  }}|j                         r"|j                  ||j	                  |             8|j                  |       J 	 ddd       |S # 1 sw Y   |S xY w)a?  
    Convert (TarFile, TarInfo) iterator like those produced by
    ``stream_conda_component`` to .conda-format under path. Allows for more
    creative data sources.

    e.g. recompress ``.conda``:

    .. code-block:: python

        transmute_stream(..., package_stream=itertools.chain(
            stream_conda_component("package.conda",
            component=CondaComponent.pkg),
            stream_conda_component("package.conda",
            component=CondaComponent.info),
        ))

    This example could move files between the ``pkg-`` and ``info-`` components
    depending on the ``is_info`` function.

    :param stem: output filename without extension
    :param path: destination path for transmuted .conda package
    :param compressor: A function that creates instances of
        ``zstandard.ZstdCompressor()`` to override defaults.
    :param is_info: A function that returns True if a file belongs in the
        ``info`` component of a `.conda` package.  ``conda-package-handling``
        (not this package ``conda-package-streaming``) uses a set of regular
        expressions to keep expected items in the info- component, while other
        items starting with ``info/`` wind up in the pkg- component.
    :param package_stream: Iterator of (Tarfile, TarInfo) tuples.

    :return: Path to transmuted package.
    .condar    N)r   r
   isfileaddfileextractfile)	r.   r(   r!   r"   r%   output_path	conda_tartarmembers	            r   r,   r,   A   s    X tvV_-K	tTj'	Ji)KC}}!!&#//&*AB!!&)	 * 
K  
K s   AA88Bc           
        | j                  d      sJ d       t        j                  j                  |      sJ | j                  d      rdnd}t        j                  j	                  |       dt        |        }|dk(  r!t        j                  t        j                  g}nt        j                  g}t        || d      }t        | d      5 }t        j                  |d      5 }|D ]f  }t        t        | ||            }	|	D ]H  \  }
}|j                         r"|j                  ||
j!                  |             8|j                  |       J h 	 ddd       ddd       |S # 1 sw Y   xY w# 1 sw Y   |S xY w)	a  
    Convert .conda package to .tar.bz2 format under path.

    Can recompress .tar.bz2 packages.

    Args:
        package: path to .conda or .tar.bz2 package.
        path: destination path for transmuted package.

    Returns:
        Path to transmuted package.
    )r$   r3   zUnknown extensionr3   r$   Nrbzx:bz2)	component)r&   r'   r(   r)   r*   r+   r   infopkgr   opentarfileiterr   r4   r5   r6   )r-   r(   incoming_formatfile_id
componentsr7   fileobjpkg_tarr=   streamr9   r:   s               r   transmute_tar_bz2rI   x   sH     23H5HH377==")"2"28"<h*Oggw'(?3+?*?@G("$))>+=+=>
 %(()
ty12K	gt	k7)Kw#I0'YWXF%V==?OOFCOOF,CDOOF+	  & $ *L	  *L)K	 s%   E/$A,E#E/#E,	(E//E9)r!   &Callable[[], zstandard.ZstdCompressor]r"   Callable[[str], bool]returnr   )r!   rJ   r"   rK   r%   z1Iterator[tuple[tarfile.TarFile, tarfile.TarInfo]])r-   strrL   r   )__doc__
__future__r   r'   rA   pathlibr   typingr   r   r   creater   r	   r
   package_streamingr   r   r/   r,   rI   r   r   r   <module>rT      s    # 	   %  M M F	 &S%	% #% 
%\	 &S4	4 #4 F4n)) 
)r   