
    ndV                        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Zddl	m
Z
 ddlmZ ddlZddlmZmZ dZdZd	Zd
 d dddZddZdS )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.

Conda packages created this way have `info-*` as the last element in the
`ZipFile`, instead of the first for `.conda` packages created with pre-2.0
`conda-package-handling`.
    )annotationsN)Path)Callable   )CondaComponentstream_conda_component      c                 B    t          j        t          t                    S )N)levelthreads)	zstandardZstdCompressorZSTD_COMPRESS_LEVELZSTD_COMPRESS_THREADS     Alib/python3.11/site-packages/conda_package_streaming/transmute.py<lambda>r   -   s    	(!+@   r   c                ,    |                      d          S )Nzinfo/)
startswith)filenames    r   r   r   0   s    h6I6I'6R6R r   )
compressoris_infor   &Callable[[], zstandard.ZstdCompressor]r   Callable[[str], bool]returnr   c               \   |                      d          s
J d            t          j                            |          sJ t          j                            |           dt          d                    }t          || d          }t          j                    5 }t          j                    5 }t          j
        |d          5 }t          j
        |d          5 }	t          t          |                     }
|
D ]l\  }} ||j                  r|n|	}|                                r*|                    ||                    |                     W|                    |           m|                                 |	                                 |                                }|                                }|                    d           |                    d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   t)          j        |dt(          j        	          5 } |            }d
t.          i}|                    dt3          j        |                     |                    d| dd          5 }|                    ||d          5 }t;          j        |j        |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |                    d| dd          5 }|                    ||d          5 }t;          j        |j        |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |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.condaw)fileobjmoder   x)compresslevelconda_pkg_format_versionzmetadata.jsonzpkg-z.tar.zstF)sizeclosefdzinfo-) endswithospathisdirbasenamelenr   tempfileSpooledTemporaryFiletarfileTarFileiterr   nameisfileaddfileextractfileclosetellseekzipfileZipFile
ZIP_STOREDCONDA_PACKAGE_FORMAT_VERSIONwritestrjsondumpsopenstream_writershutilcopyfileobj_file)packager+   r   r   file_idoutput_path	info_filepkg_fileinfo_tarpkg_tarstreamtarmembertar_get	info_sizepkg_size
conda_filedata_compresspkg_metadatapkg_file_zip
pkg_streaminfo_file_zipinfo_streams                          r   	transmuter[   '   s   4 J''NN)NNNN7==gw''(:3z??*:(:;Gt///00K		&	(	( /AIx7T7V7V /AZb_YS999 	Xw3H
 H
 H
 	 099::F% , ,V&-gfk&:&:G((==?? ,OOFCOOF,C,CDDDDOOF++++NNMMOOO!((I}}HNN1MM!+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	. _!,
 
 
 	A &JLLM68TULL1I1IJJJ(w(((#  ?}::8U  ;    ? "8>:>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ))))3  A ; ;Iu !< ! !A "9?K@@@A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A%	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A1/A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /A /Ab s4  N!0N
HDG0	$H0G44H7G48H;N
HN
H$N
3AM3	K 	"K	=K 		KK 	KK 	M3 K$$M3'K$(M3M	M9M	M		M	M	M	M3M  M3#M $M3'N
3M77N
:M7;N
>N!
N	N!N	N!!N%(N%rG   strc           
     J   |                      d          s
J d            t          j                            |          sJ |                      d          rdnd}t          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 ]|}t          t          | ||                    }	|	D ]X\  }
}|                                r*|                    ||
                    |                     C|                    |           Y}	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |S )	a  
    Convert .conda :package to .tar.bz2 format under path.

    Can recompress .tar.bz2 packages.

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

    :return: Path to transmuted package.
    )r   r    zUnknown extensionr    r   Nrbzx:bz2)	component)r)   r*   r+   r,   r-   r.   r   infopkgr   rB   r1   r3   r   r5   r6   r7   )rG   r+   incoming_formatrH   
componentsrI   r"   rM   r_   rN   rO   rP   s               r   transmute_tar_bz2rd   z   s)    233HH5HHHH7==")"2"28"<"<Lhh*Ogw''(?3+?+?*?(?@G(""$)>+=>

 %()
t11122K	gt		 ,k7)K)K ,w# 	, 	,I0'YWWWXXF% , ,V==?? ,OOFCOOF,C,CDDDDOOF++++	,	,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , s7   F4B F5FF	FF		FFF)r   r   r   r   r   r   )rG   r\   r   r   )__doc__
__future__r   r@   r*   rD   r1   r/   r;   pathlibr   typingr   r   package_streamingr   r   r   r   r>   r[   rd   r   r   r   <module>rj      s    # " " " " "  				                     F E E E E E E E     	 	 &S%RP P P P P Pf' ' ' ' ' 'r   