| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import hashlib |
| 4 |
import os |
| 5 |
from itertools import chain |
| 6 |
from pathlib import Path |
| 7 |
from tempfile import TemporaryDirectory |
| 8 |
|
| 9 |
from conda_package_streaming import package_streaming |
| 10 |
|
| 11 |
|
| 12 |
def validate_converted_files_match( |
| 13 |
src_file_or_folder, subject, reference_ext="" |
| 14 |
): # pragma: nocover |
| 15 |
# No longer used by conda-package-handling |
| 16 |
from .api import extract |
| 17 |
|
| 18 |
with TemporaryDirectory() as tmpdir: |
| 19 |
assert tmpdir is not None |
| 20 |
if os.path.isdir(src_file_or_folder): |
| 21 |
src_folder = src_file_or_folder |
| 22 |
else: |
| 23 |
extract(src_file_or_folder + reference_ext, dest_dir=os.path.join(tmpdir, "src")) |
| 24 |
src_folder = os.path.join(tmpdir, "src") |
| 25 |
|
| 26 |
converted_folder = os.path.join(tmpdir, "converted") |
| 27 |
extract(subject, dest_dir=converted_folder) |
| 28 |
|
| 29 |
missing_files = set() |
| 30 |
mismatch_size = set() |
| 31 |
for root, dirs, files in os.walk(src_folder): |
| 32 |
for f in files: |
| 33 |
absfile = os.path.join(root, f) |
| 34 |
rp = os.path.relpath(absfile, src_folder) |
| 35 |
destpath = os.path.join(converted_folder, rp) |
| 36 |
if not os.path.islink(destpath): |
| 37 |
if not os.path.isfile(destpath): |
| 38 |
missing_files.add(rp) |
| 39 |
elif os.stat(absfile).st_size != os.stat(destpath).st_size: |
| 40 |
mismatch_size.add(rp) |
| 41 |
return src_file_or_folder, missing_files, mismatch_size |
| 42 |
|
| 43 |
|
| 44 |
def hash_fn(): |
| 45 |
return hashlib.blake2b() |
| 46 |
|
| 47 |
|
| 48 |
IGNORE_FIELDS = { |
| 49 |
"uid", |
| 50 |
"gid", |
| 51 |
"mtime", |
| 52 |
"uname", |
| 53 |
"gname", |
| 54 |
"chksum", |
| 55 |
} #: ignore if not strict |
| 56 |
|
| 57 |
|
| 58 |
def validate_converted_files_match_streaming( |
| 59 |
src: str | Path, reference: str | Path, *, strict=True |
| 60 |
): |
| 61 |
""" |
| 62 |
Check that two .tar.bz2 or .conda files (either of src_file and |
| 63 |
reference_file can be either format) match exactly, down to the timestamps |
| 64 |
etc. |
| 65 |
|
| 66 |
Does not check outside of the info- and pkg- components of a .conda. |
| 67 |
(conda's metadata.json, which gives the version "2" of the format) |
| 68 |
|
| 69 |
If strict = True, also check for matching uid, gid, mtime, uname, gname. |
| 70 |
""" |
| 71 |
source_set = {} |
| 72 |
reference_set = {} |
| 73 |
ignore_fields = {"chksum"} if strict else IGNORE_FIELDS |
| 74 |
|
| 75 |
def get_fileset(filename: str | Path): |
| 76 |
fileset = {} |
| 77 |
components = ["info", "pkg"] if os.fspath(filename).endswith(".conda") else ["pkg"] |
| 78 |
with open(filename, "rb") as conda_file: |
| 79 |
for component in components: |
| 80 |
for tar, member in package_streaming.stream_conda_component( |
| 81 |
filename, conda_file, component |
| 82 |
): |
| 83 |
info = {k: v for k, v in member.get_info().items() if k not in ignore_fields} |
| 84 |
|
| 85 |
if member.isfile(): |
| 86 |
hasher = hash_fn() |
| 87 |
fd = tar.extractfile(member) |
| 88 |
assert fd is not None |
| 89 |
for block in iter(lambda: fd.read(1 << 18), b""): # type: ignore |
| 90 |
hasher.update(block) |
| 91 |
|
| 92 |
info["digest"] = hasher.hexdigest() |
| 93 |
|
| 94 |
fileset[info["name"]] = info |
| 95 |
|
| 96 |
return fileset |
| 97 |
|
| 98 |
source_set = get_fileset(src) |
| 99 |
reference_set = get_fileset(reference) |
| 100 |
|
| 101 |
missing = [] |
| 102 |
mismatched = [] |
| 103 |
|
| 104 |
if source_set != reference_set: |
| 105 |
for file in chain(source_set, reference_set): |
| 106 |
if not (file in source_set and file in reference_set): |
| 107 |
missing.append(file) |
| 108 |
elif source_set[file] != reference_set[file]: |
| 109 |
mismatched.append(file) |
| 110 |
|
| 111 |
return src, missing, mismatched |