| 1 |
import logging |
| 2 |
import os |
| 3 |
import re |
| 4 |
import tarfile |
| 5 |
|
| 6 |
from . import streaming, utils |
| 7 |
from .interface import AbstractBaseFormat |
| 8 |
|
| 9 |
LOG = logging.getLogger(__name__) |
| 10 |
|
| 11 |
|
| 12 |
def _sort_file_order(prefix, files): |
| 13 |
"""Sort by filesize, to optimize compression?""" |
| 14 |
info_slash = "info" + os.path.sep |
| 15 |
|
| 16 |
def order(f): |
| 17 |
# we don't care about empty files so send them back via 100000 |
| 18 |
fsize = os.lstat(os.path.join(prefix, f)).st_size or 100000 |
| 19 |
# info/* records will be False == 0, others will be 1. |
| 20 |
info_order = int(not f.startswith(info_slash)) |
| 21 |
if info_order: |
| 22 |
_, ext = os.path.splitext(f) |
| 23 |
# Strip any .dylib.* and .so.* and rename .dylib to .so |
| 24 |
ext = re.sub(r"(\.dylib|\.so).*$", r".so", ext) |
| 25 |
if not ext: |
| 26 |
# Files without extensions should be sorted by dirname |
| 27 |
info_order = 1 + hash(os.path.dirname(f)) % (10**8) |
| 28 |
else: |
| 29 |
info_order = 1 + abs(hash(ext)) % (10**8) |
| 30 |
return info_order, fsize |
| 31 |
|
| 32 |
files_list = list(sorted(files, key=order)) |
| 33 |
|
| 34 |
return files_list |
| 35 |
|
| 36 |
|
| 37 |
def _create_no_libarchive(fullpath, files): |
| 38 |
with tarfile.open(fullpath, "w:bz2") as t: |
| 39 |
for f in files: |
| 40 |
t.add(f, filter=utils.anonymize_tarinfo) |
| 41 |
|
| 42 |
|
| 43 |
def create_compressed_tarball( |
| 44 |
prefix, files, tmpdir, basename, ext, compression_filter, filter_opts="" |
| 45 |
): |
| 46 |
tmp_path = os.path.join(tmpdir, basename) |
| 47 |
files = _sort_file_order(prefix, files) |
| 48 |
|
| 49 |
# add files in order of a) in info directory, b) increasing size so |
| 50 |
# we can access small manifest or json files without decompressing |
| 51 |
# possible large binary or data files |
| 52 |
fullpath = tmp_path + ext |
| 53 |
with utils.tmp_chdir(prefix): |
| 54 |
_create_no_libarchive(fullpath, files) |
| 55 |
return fullpath |
| 56 |
|
| 57 |
|
| 58 |
class CondaTarBZ2(AbstractBaseFormat): |
| 59 |
@staticmethod |
| 60 |
def supported(fn): |
| 61 |
return fn.endswith(".tar.bz2") |
| 62 |
|
| 63 |
@staticmethod |
| 64 |
def extract(fn, dest_dir, **kw): |
| 65 |
if not os.path.isdir(dest_dir): |
| 66 |
os.makedirs(dest_dir) |
| 67 |
if not os.path.isabs(fn): |
| 68 |
fn = os.path.normpath(os.path.join(os.getcwd(), fn)) |
| 69 |
|
| 70 |
streaming._extract(str(fn), str(dest_dir), components=["pkg"]) |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
def create(prefix, file_list, out_fn, out_folder=None, **kw): |
| 74 |
if out_folder is None: |
| 75 |
out_folder = os.getcwd() |
| 76 |
if os.path.isabs(out_fn): |
| 77 |
out_folder = os.path.dirname(out_fn) |
| 78 |
out_file = create_compressed_tarball( |
| 79 |
prefix, |
| 80 |
file_list, |
| 81 |
out_folder, |
| 82 |
os.path.basename(out_fn).replace(".tar.bz2", ""), |
| 83 |
".tar.bz2", |
| 84 |
"bzip2", |
| 85 |
) |
| 86 |
return out_file |
| 87 |
|
| 88 |
@staticmethod |
| 89 |
def get_pkg_details(in_file): |
| 90 |
stat_result = os.stat(in_file) |
| 91 |
size = stat_result.st_size |
| 92 |
md5, sha256 = utils.checksums(in_file, ("md5", "sha256")) |
| 93 |
return {"size": size, "md5": md5, "sha256": sha256} |
| 94 |
|
| 95 |
@staticmethod |
| 96 |
def list_contents(fn, verbose=False, **kw): |
| 97 |
if "://" in fn: |
| 98 |
raise ValueError("Remote .tar.bz2 artifact listing is not supported.") |
| 99 |
if not os.path.isabs(fn): |
| 100 |
fn = os.path.abspath(fn) |
| 101 |
streaming._list(str(fn), components=["pkg"], verbose=verbose) |