Skip to content
kyellsenPublic

About

Stamp a data folder with the SHA-256 of every file, tell whether it still matches, and keep a catalogue of such folders

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

datastamp

Stamp a data folder with the SHA-256 of every file, tell whether it still matches, and keep a catalogue of such folders

Author: Kyell Jensen · Licence: MIT

A stamped folder carries a SHA256SUMS at its root: one line per file, in sha256sum's own format. datastamp has two parts:

  • The core, with no dependency: read and write that list, compare a folder with it, name a folder's state. Any program that reads data can import it.
  • The catalogue commands, behind the extra catalogue: keep the lists of many folders in one Git repository, with a description of each dataset, fetch published datasets from their sources, record what is new, and place a copy of each entry in its folder.

🔍 Check a folder

Without this package, with what every Linux ships:

cd <folder> && sha256sum -c SHA256SUMS

rclone reads the same list and, unlike sha256sum, also names a file the list does not know. That is why paths are written bare, without ./: rclone matches nothing behind one.

rclone checksum sha256 <folder>/SHA256SUMS <folder> --combined - --exclude /SHA256SUMS --exclude /manifest.yaml

With this package, which finds the same three things:

datastamp verify <folder>           # the content of every listed file
datastamp verify --names <folder>   # the names only: quick, and blind to a changed file
datastamp state <folder>            # the SHA-256 of the list: one value that pins every file

The exit code is 0 when the folder matches, 1 when it differs and 2 when it has no list. These two commands never write.

🐍 Check from a program

from pathlib import Path

import datastamp

raw = Path("~/data/<project>/data/raw").expanduser()

verdict = datastamp.verify(raw, content=False)  # before a run: nothing missing, nothing unlisted
if not verdict.ok:
    raise SystemExit(f"{raw} is not the recorded state: {verdict}")

table = datastamp.checked(raw, "metadata/tree.csv")  # this one file, content included
result_was_computed_from = datastamp.state(raw)  # write this beside the result

📚 Keep a catalogue

A catalogue is a directory, usually a Git repository of its own, marked by a datastamp.toml at its root:

name = "~/catalogue"               # how a placed copy names this catalogue
entries = "catalogue"              # the folder of <REPOSITORY>/<DATASET>/ entries
data_root = "~/datasets"           # where a dataset's folder lies by default
mirror = "remote:/datasets"        # the rclone path `pull` copies from
data_root_readme = "catalogue/DATA_ROOT.md"

Each entry is a folder with a manifest.yaml (what the dataset is, where it came from, each version) and a SHA256SUMS. An entry with a loader is an order for a published dataset; one without describes files that are kept where they were recorded, such as a project's own measurements, and names their folder in root.

datastamp catalogue list                  # the entries, and which are on disk
datastamp catalogue fetch [ID...]         # download from the source, then verify
datastamp catalogue pull ID...            # copy from the mirror, then verify
datastamp catalogue record ID...          # add the files a list does not name yet
datastamp catalogue record ID --replace PATH...   # write named lines again, on purpose
datastamp catalogue place [ID...]         # write each entry's copy into its dataset's folder
datastamp catalogue verify [--names] [ID...]
datastamp catalogue index [--check]       # the table of contents, INDEX.md

The command runs on the catalogue that holds the current directory, or the one DATASTAMP_CATALOGUE names. What one machine does differently goes into a .env beside the marker: DATASTAMP_DATA_ROOT, DATASTAMP_MIRROR, DATASTAMP_RATE_LIMIT.

A source that no shared loader covers gets a loader.py beside its manifest (loader: local), exporting LOADER; datastamp.catalogue.loaders.base has the interface.

📦 Use it from another repository

# pyproject.toml of the consumer, while developing side by side:
dependencies = ["datastamp"]              # or "datastamp[catalogue]" for a catalogue
[tool.uv.sources]
datastamp = { path = "../../packages/datastamp", editable = true }
# for a frozen state, pin a tag instead:
# datastamp = { git = "https://github.com/kyellsen/datastamp.git", tag = "vX.Y.Z" }

🚀 Work on it

This project is built with uv and just.

just setup    # venv, dependencies, git hooks
just fix      # format and autofix lint issues
just check    # the static gate
just test     # unit + integration
just ci       # the gate, then every test tier
  • src/datastamp/ — the core: sums.py (lists and comparison), cli.py (the command)
  • src/datastamp/catalogue/ — the catalogue commands: manifest, loaders, download, the steps around a dataset, pull, index
  • tests/ — unit, integration (against the real sha256sum, rclone and curl), system (the command, end to end)

About

Stamp a data folder with the SHA-256 of every file, tell whether it still matches, and keep a catalogue of such folders

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages