| 1 |
""" |
| 2 |
Convert .tar.bz2 to .conda |
| 3 |
|
| 4 |
Uses ``tempfile.SpooledTemporaryFile`` to buffer ``pkg-*.tar`` and |
| 5 |
``info-*.tar``, then compress directly into an open `ZipFile` at the end. |
| 6 |
`SpooledTemporaryFile` buffers the first 10MB of the package and its metadata in |
| 7 |
memory, but writes out to disk for larger packages. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import os |
| 13 |
import tarfile |
| 14 |
from collections.abc import Iterator |
| 15 |
from pathlib import Path |
| 16 |
from typing import TYPE_CHECKING |
| 17 |
|
| 18 |
from .create import conda_builder |
| 19 |
|
| 20 |
# streams everything in .tar.bz2 mode |
| 21 |
from .package_streaming import CondaComponent, stream_conda_component |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from collections.abc import Callable |
| 25 |
|
| 26 |
from .create import LegacyCompressorOrFactory |
| 27 |
|
| 28 |
|
| 29 |
def transmute( |
| 30 |
package, |
| 31 |
path, |
| 32 |
*, |
| 33 |
compressor: LegacyCompressorOrFactory | None = None, |
| 34 |
compression_level: int | None = None, |
| 35 |
compression_threads: int | None = None, |
| 36 |
is_info: Callable[[str], bool] = lambda filename: filename.startswith("info/"), |
| 37 |
) -> Path: |
| 38 |
""" |
| 39 |
Convert .tar.bz2 conda package to .conda-format under path. |
| 40 |
|
| 41 |
:param package: path to .tar.bz2 conda package |
| 42 |
:param path: destination path for transmuted .conda package |
| 43 |
:param compressor: Legacy ``zstandard`` compressor object (or factory |
| 44 |
returning one) with ``stream_writer(...)``. Mutually exclusive with |
| 45 |
``compression_level`` and ``compression_threads``. |
| 46 |
:param compression_level: zstd compression level for ``compression.zstd`` or |
| 47 |
``backports.zstd`` code path. Defaults to ``ZSTD_COMPRESS_LEVEL`` if not |
| 48 |
specified and ``compressor`` is None. |
| 49 |
:param compression_threads: Number of zstd worker threads for |
| 50 |
``compression.zstd`` or ``backports.zstd`` code path. Defaults to |
| 51 |
``ZSTD_COMPRESS_THREADS`` if not specified and ``compressor`` is None. |
| 52 |
:param is_info: A function that returns True if a file belongs in the |
| 53 |
``info`` component of a `.conda` package. ``conda-package-handling`` |
| 54 |
(not this package ``conda-package-streaming``) uses a set of regular |
| 55 |
expressions to keep expected items in the info- component, while other |
| 56 |
items starting with ``info/`` wind up in the pkg- component. |
| 57 |
|
| 58 |
:return: Path to transmuted package. |
| 59 |
""" |
| 60 |
assert package.endswith(".tar.bz2"), "can only convert .tar.bz2 to .conda" |
| 61 |
assert os.path.isdir(path) |
| 62 |
stem = os.path.basename(package)[: -len(".tar.bz2")] |
| 63 |
package_stream = stream_conda_component(package) |
| 64 |
|
| 65 |
return transmute_stream( |
| 66 |
stem, |
| 67 |
path, |
| 68 |
compressor=compressor, |
| 69 |
compression_level=compression_level, |
| 70 |
compression_threads=compression_threads, |
| 71 |
is_info=is_info, |
| 72 |
package_stream=package_stream, |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
def transmute_stream( |
| 77 |
stem, |
| 78 |
path, |
| 79 |
*, |
| 80 |
compressor: LegacyCompressorOrFactory | None = None, |
| 81 |
compression_level: int | None = None, |
| 82 |
compression_threads: int | None = None, |
| 83 |
is_info: Callable[[str], bool] = lambda filename: filename.startswith("info/"), |
| 84 |
package_stream: Iterator[tuple[tarfile.TarFile, tarfile.TarInfo]], |
| 85 |
): |
| 86 |
""" |
| 87 |
Convert (TarFile, TarInfo) iterator like those produced by |
| 88 |
``stream_conda_component`` to .conda-format under path. Allows for more |
| 89 |
creative data sources. |
| 90 |
|
| 91 |
e.g. recompress ``.conda``: |
| 92 |
|
| 93 |
.. code-block:: python |
| 94 |
|
| 95 |
transmute_stream( |
| 96 |
..., |
| 97 |
package_stream=itertools.chain( |
| 98 |
stream_conda_component("package.conda", component=CondaComponent.pkg), |
| 99 |
stream_conda_component("package.conda", component=CondaComponent.info), |
| 100 |
), |
| 101 |
) |
| 102 |
|
| 103 |
This example could move files between the ``pkg-`` and ``info-`` components |
| 104 |
depending on the ``is_info`` function. |
| 105 |
|
| 106 |
:param stem: output filename without extension |
| 107 |
:param path: destination path for transmuted .conda package |
| 108 |
:param compressor: Legacy ``zstandard`` compressor object (or factory |
| 109 |
returning one) with ``stream_writer(...)``. Mutually exclusive with |
| 110 |
``compression_level`` and ``compression_threads``. |
| 111 |
:param compression_level: zstd compression level for ``compression.zstd`` or |
| 112 |
``backports.zstd`` code path. Defaults to ``ZSTD_COMPRESS_LEVEL`` if not |
| 113 |
specified and ``compressor`` is None. |
| 114 |
:param compression_threads: Number of zstd worker threads for |
| 115 |
``compression.zstd`` or ``backports.zstd`` code path. Defaults to |
| 116 |
``ZSTD_COMPRESS_THREADS`` if not specified and ``compressor`` is None. |
| 117 |
:param is_info: A function that returns True if a file belongs in the |
| 118 |
``info`` component of a `.conda` package. ``conda-package-handling`` |
| 119 |
(not this package ``conda-package-streaming``) uses a set of regular |
| 120 |
expressions to keep expected items in the info- component, while other |
| 121 |
items starting with ``info/`` wind up in the pkg- component. |
| 122 |
:param package_stream: Iterator of (Tarfile, TarInfo) tuples. |
| 123 |
|
| 124 |
:return: Path to transmuted package. |
| 125 |
""" |
| 126 |
output_path = Path(path, f"{stem}.conda") |
| 127 |
with conda_builder( |
| 128 |
stem, |
| 129 |
path, |
| 130 |
compressor=compressor, |
| 131 |
compression_level=compression_level, |
| 132 |
compression_threads=compression_threads, |
| 133 |
is_info=is_info, |
| 134 |
) as conda_tar: |
| 135 |
for tar, member in package_stream: |
| 136 |
if member.isfile(): |
| 137 |
conda_tar.addfile(member, tar.extractfile(member)) |
| 138 |
else: |
| 139 |
conda_tar.addfile(member) |
| 140 |
|
| 141 |
return output_path |
| 142 |
|
| 143 |
|
| 144 |
def transmute_tar_bz2( |
| 145 |
package: str, |
| 146 |
path, |
| 147 |
) -> Path: |
| 148 |
""" |
| 149 |
Convert .conda package to .tar.bz2 format under path. |
| 150 |
|
| 151 |
Can recompress .tar.bz2 packages. |
| 152 |
|
| 153 |
Args: |
| 154 |
package: path to .conda or .tar.bz2 package. |
| 155 |
path: destination path for transmuted package. |
| 156 |
|
| 157 |
Returns: |
| 158 |
Path to transmuted package. |
| 159 |
""" |
| 160 |
assert package.endswith((".tar.bz2", ".conda")), "Unknown extension" |
| 161 |
assert os.path.isdir(path) |
| 162 |
|
| 163 |
incoming_format = ".conda" if package.endswith(".conda") else ".tar.bz2" |
| 164 |
|
| 165 |
file_id = os.path.basename(package)[: -len(incoming_format)] |
| 166 |
|
| 167 |
if incoming_format == ".conda": |
| 168 |
# .tar.bz2 MUST place info/ first. |
| 169 |
components = [CondaComponent.info, CondaComponent.pkg] |
| 170 |
else: |
| 171 |
# .tar.bz2 doesn't filter by component |
| 172 |
components = [CondaComponent.pkg] |
| 173 |
|
| 174 |
output_path = Path(path, f"{file_id}.tar.bz2") |
| 175 |
|
| 176 |
with open(package, "rb") as fileobj, tarfile.open(output_path, "x:bz2") as pkg_tar: |
| 177 |
for component in components: |
| 178 |
stream = iter(stream_conda_component(package, fileobj, component=component)) |
| 179 |
for tar, member in stream: |
| 180 |
if member.isfile(): |
| 181 |
pkg_tar.addfile(member, tar.extractfile(member)) |
| 182 |
else: |
| 183 |
pkg_tar.addfile(member) |
| 184 |
|
| 185 |
return output_path |