
    3i                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSK	J
r
  SSKrSSKJrJrJr  SS	KJrJr  S
 S S.     SS jjrS S S.     SS jjr    SS jrg)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)Iterator)Path)Callable   )ZSTD_COMPRESS_LEVELZSTD_COMPRESS_THREADSconda_builder)CondaComponentstream_conda_componentc                 <    [         R                  " [        [        S9$ N)levelthreads	zstandardZstdCompressorr   r	        Alib/python3.13/site-packages/conda_package_streaming/transmute.py<lambda>r           	((!+@r   c                $    U R                  S5      $ Nzinfo/
startswithfilenames    r   r   r   #       h6I6I'6Rr   
compressoris_infoc                  U R                  S5      (       d   S5       e[        R                  R                  U5      (       d   e[        R                  R	                  U 5      S[        S5      *  n[        U 5      n[        UUUUUS9$ )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                 <    [         R                  " [        [        S9$ r   r   r   r   r   r   r   H   r   r   c                $    U R                  S5      $ r   r   r   s    r   r   r   K   r   r   c                  [        X S35      n[        XX#S9 nU HM  u  pxUR                  5       (       a"  UR                  XR	                  U5      5        M<  UR                  U5        MO     SSS5        U$ ! , (       d  f       U$ = f)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,   B   s}    X tvV_-K	tj	Ji)KC}}!!&//&*AB!!&)	 * 
K  
K	J s   AA77
Bc                2   U R                  S5      (       d   S5       e[        R                  R                  U5      (       d   eU R                  S5      (       a  SOSn[        R                  R	                  U 5      S[        U5      *  nUS:X  a!  [        R                  [        R                  /nO[        R                  /n[        X S35      n[        U S5       n[        R                  " US5       nU Hi  n[        [        XUS95      n	U	 HM  u  pUR                  5       (       a"  UR                  XR!                  U5      5        M<  UR                  U5        MO     Mk     SSS5        SSS5        U$ ! , (       d  f       N= f! , (       d  f       U$ = f)	z
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   y   sN     233H5HH377==")"2"28"<"<h*Oggw'(?3+?*?@G("$))>+=+=>
 %(()
ty12K	gt	k7)Kw#I0YWXF%==??OOFOOF,CDOOF+	  & $ *L	  *L)K		 s%   F4A0E6$F6
F	 F
F)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   collections.abcr   pathlibr   typingr   r   creater   r	   r
   package_streamingr   r   r/   r,   rI   r   r   r   <module>rU      s    # 	  $    M M F	 &S%	% #% 
%\	 &S4	4 #4 F4n)) 
)r   