typeddfs.utils.checksums

Tools for shasum-like files.

Module Contents

class typeddfs.utils.checksums.Checksums
alg :str
calc_hash(self, path: typeddfs.utils._utils.PathLike) str

Calculates the hash of a file and returns it, hex-encoded.

classmethod default_algorithm(cls) str
delete_any(self, path: typeddfs.utils._utils.PathLike, *, rm_if_empty: bool = False) None

Deletes the filesum and removes path from the dirsum. Ignores missing files.

generate_dirsum(self, directory: typeddfs.utils._utils.PathLike, glob: str = '*') typeddfs.utils.checksum_models.ChecksumMapping

Generates a new hash mapping, calculating hashes for extant files.

Parameters
  • directory – Base directory

  • glob – Glob pattern under directory (cannot be recursive)

Returns

A ChecksumMapping; use .write to write it

get_dirsum_of_dir(self, path: typeddfs.utils._utils.PathLike) pathlib.Path

Returns the path required for the per-directory hash of path.

Example

Utils.get_hash_file("my_dir")  # Path("my_dir", "my_dir.sha256")

get_dirsum_of_file(self, path: typeddfs.utils._utils.PathLike) pathlib.Path

Returns the path required for the per-directory hash of path.

Example

Utils.get_hash_file(Path("my_dir, my_file.txt.gz"))  # Path("my_dir", "my_dir.sha256")

get_filesum_of_file(self, path: typeddfs.utils._utils.PathLike) pathlib.Path

Returns the path required for the per-file hash of path.

Example

Utils.get_hash_file("my_file.txt.gz")  # Path("my_file.txt.gz.sha256")

classmethod guess_algorithm(cls, path: typeddfs.utils._utils.PathLike) str

Guesses the hashlib algorithm used from a hash file.

Parameters

path – The hash file (e.g. my-file.sha256)

Example

Utils.guess_algorithm("my_file.sha1")  # "sha1"

load_dirsum_exact(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping
load_dirsum_of_dir(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping
load_dirsum_of_file(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping
load_filesum_exact(self, path: typeddfs.utils._utils.PathLike) typeddfs.utils.checksum_models.ChecksumFile
load_filesum_of_file(self, path: typeddfs.utils._utils.PathLike) typeddfs.utils.checksum_models.ChecksumFile
classmethod resolve_algorithm(cls, alg: str) str

Finds a hash algorithm by name in hashlib. Converts to lowercase and removes hyphens.

Raises

HashAlgorithmMissingError – If not found

verify_any(self, path: typeddfs.utils._utils.PathLike, *, file_hash: bool, dir_hash: bool, computed: Optional[str]) Optional[str]
verify_hex(self, path: typeddfs.utils._utils.PathLike, expected: str) Optional[str]

Verifies a hash directly from a hex string.

write_any(self, path: typeddfs.utils._utils.PathLike, *, to_file: bool, to_dir: bool, overwrite: Optional[bool] = True) Optional[str]

Adds and/or appends the hex hash of path.

Parameters
  • path – Path to the file to hash

  • to_file – Whether to save a per-file hash

  • to_dir – Whether to save a per-dir hash

  • overwrite – If True, overwrite the file hash and any entry in the dir hash. If False, never overwrite either. If None, never overwrite, but ignore if equal to any existing entries.