repomatic.cache module

Global cache for tool executables, API responses and generated tool configs.

Four subtrees under the user-level cache directory:

Binary cache (bin/): platform-specific tool executables, keyed by {tool}/{version}/{platform}/{executable}. Each cached binary has a .sha256 sidecar written after a verified archive download. Cache hits verify the binary against this sidecar to detect local tampering.

HTTP response cache (http/): JSON API responses from PyPI and GitHub, keyed by {namespace}/{key}.json. Freshness is controlled by a per-caller TTL (seconds); stale entries remain on disk until auto-purge removes them.

Config cache (config/): generated tool configuration files, keyed by {tool}/{digest}/{filename}, where the digest names the content. Two runs that generate different configs for one tool (two checkouts, two branches) write two files, so neither run reads the config of the other. Passed to tools via explicit --config flags so repomatic never writes to the user’s repository.

Locks (locks/): the lock files lock_path() names, which the tool runner holds while a config file sits in the working directory. Neither listed nor cleaned: a lock file deleted while a run holds it lets the next run start without waiting.

Note

The cache module is intentionally a pure storage layer. It does not know about checksums, registries, API semantics, or tool specifications. All trust and freshness decisions belong to the caller.

repomatic.cache.CACHE_LIST_HEADER_DEFS: tuple[ColumnSpec, ...] = (ColumnSpec(id='type', label='Type', description='', max_width=None, optional=False, sortable=True), ColumnSpec(id='name', label='Name', description='', max_width=None, optional=False, sortable=True), ColumnSpec(id='detail', label='Detail', description='', max_width=None, optional=False, sortable=True), ColumnSpec(id='size', label='Size', description='', max_width=None, optional=False, sortable=True), ColumnSpec(id='age', label='Age', description='', max_width=None, optional=False, sortable=True))

Column definitions for the repomatic cache show table.

Lives beside the entry dataclasses it renders; the CLI derives its --sort-by choices from it.

repomatic.cache.CONFIG_DIGEST_LENGTH = 16

Hex digits of the content digest that names a cached config directory.

Sixty-four bits: enough to keep apart every config one cache ever holds.

repomatic.cache.SIDECAR_SUFFIX = '.sha256'

Suffix of the digest sidecar stored beside each cached binary.

Part of the binary cache’s on-disk layout: the listing skips sidecars and the clean removes them along with their entry. Computing, writing, and verifying the digest itself stays with the caller (tooling/binaries.py), per the module note above.

repomatic.cache.cache_dir()[source]

Resolve the cache root directory.

Precedence (highest to lowest):

  1. REPOMATIC_CACHE_DIR environment variable.

  2. cache.dir in [tool.repomatic].

  3. Platform-specific default, from click_extra.get_cache_dir().

Return type:

Path

Returns:

Absolute path to the cache root (may not exist yet).

repomatic.cache.lock_path(name)[source]

Return the path of the lock file called name.

The file lives in the locks/ subtree, which no listing or clean touches.

Parameters:

name (str) – Lock name, unique to what the lock guards.

Return type:

Path

class repomatic.cache.CachedFile(*, path, size, mtime)[source]

Bases: object

One file of a cache subtree, with the facts every entry carries.

The three caches (binaries, HTTP responses, tool configs) differ only in how they name an entry. Each subclass reads its identity fields off the path of the entry, in from_path(). The listing, the age filter and the clean are the same for all three, so they live here.

path: Path

Absolute path to the cached file.

size: int

File size in bytes.

mtime: float

File modification time (seconds since epoch).

kind: ClassVar[str]

The cache this entry belongs to, as the repomatic cache show table spells it.

subdir: ClassVar[str]

Name of the subtree under the cache root.

scope_option: ClassVar[str]

The repomatic cache clean option that narrows a clean to one scope of this cache.

classmethod root()[source]

Return the subtree of this cache under the cache root.

Return type:

Path

classmethod from_path(path, root)[source]

Read an entry off the path of a file below root.

Return type:

Self | None

Returns:

The entry, or None when the file is not an entry of this cache, like a digest sidecar.

classmethod entries()[source]

List the entries of this cache, sorted by path.

Return type:

list[Self]

classmethod clear(scope=None, max_age_days=None)[source]

Remove the entries of this cache that both filters select.

Empty directories left behind are pruned afterwards.

Parameters:
  • scope (str | None) – Keep entries whose scope differs. None selects every entry.

  • max_age_days (int | None) – Keep entries younger than this. None selects every entry.

Return type:

tuple[int, int]

Returns:

Tuple of (files_deleted, bytes_freed).

property scope: str

The name a cache clean filter matches this entry on.

Doubles as the table’s subject column: the thing a reader identifies the entry by (--tool ruff, --namespace pypi) is the same thing the listing shows them, so one property serves both.

property detail: str

What distinguishes this entry from its siblings in the same scope.

remove()[source]

Delete the files of this entry.

Return type:

None

is_fresh(max_age_days)[source]

Whether this entry is younger than the age cutoff.

A None cutoff keeps nothing: age-unfiltered clears delete every entry the caller’s other filters matched.

Return type:

bool

as_row()[source]

Render this entry as one repomatic cache show table row.

Return type:

tuple[str, str, str, str, str]

class repomatic.cache.BinaryCacheEntry(*, path, size, mtime, tool, version, platform, executable)[source]

Bases: CachedFile

A single cached binary with its metadata.

tool: str

Tool name (registry key).

version: str

Pinned version string.

platform: str

Platform key, like linux-x64 or macos-arm64.

executable: str

Executable filename.

kind: ClassVar[str] = 'binary'

The cache this entry belongs to, as the repomatic cache show table spells it.

subdir: ClassVar[str] = 'bin'

Name of the subtree under the cache root.

scope_option: ClassVar[str] = 'tool'

The repomatic cache clean option that narrows a clean to one scope of this cache.

classmethod from_path(path, root)[source]

Read the four levels of {tool}/{version}/{platform}/{executable}.

A digest sidecar is part of its binary’s entry, not an entry itself.

Return type:

Self | None

property scope: str

The name a cache clean filter matches this entry on.

Doubles as the table’s subject column: the thing a reader identifies the entry by (--tool ruff, --namespace pypi) is the same thing the listing shows them, so one property serves both.

property detail: str

What distinguishes this entry from its siblings in the same scope.

remove()[source]

Delete the binary and its digest sidecar.

Return type:

None

class repomatic.cache.HttpCacheEntry(*, path, size, mtime, namespace, key)[source]

Bases: CachedFile

A single cached HTTP response with its metadata.

namespace: str

Cache namespace, like pypi or github-release-tags.

key: str

Cache key within the namespace, like requests or astral-sh/ruff.

kind: ClassVar[str] = 'http'

The cache this entry belongs to, as the repomatic cache show table spells it.

subdir: ClassVar[str] = 'http'

Name of the subtree under the cache root.

scope_option: ClassVar[str] = 'namespace'

The repomatic cache clean option that narrows a clean to one scope of this cache.

classmethod from_path(path, root)[source]

Read {namespace}/{key}.json, where the key can hold slashes.

Return type:

Self | None

property scope: str

The name a cache clean filter matches this entry on.

Doubles as the table’s subject column: the thing a reader identifies the entry by (--tool ruff, --namespace pypi) is the same thing the listing shows them, so one property serves both.

property detail: str

What distinguishes this entry from its siblings in the same scope.

class repomatic.cache.ConfigCacheEntry(*, path, size, mtime, tool, digest, filename)[source]

Bases: CachedFile

A single cached tool configuration file with its metadata.

tool: str

Tool name (registry key).

digest: str

Start of the SHA-256 of the content, which names the entry.

filename: str

Config filename, like yamllint.yaml or biome.json.

kind: ClassVar[str] = 'config'

The cache this entry belongs to, as the repomatic cache show table spells it.

subdir: ClassVar[str] = 'config'

Name of the subtree under the cache root.

scope_option: ClassVar[str] = 'tool'

The repomatic cache clean option that narrows a clean to one scope of this cache.

classmethod from_path(path, root)[source]

Read {tool}/{digest}/{filename}.

A file directly in the tool directory, with no digest level, is still an entry: the clean has to reach it.

Return type:

Self | None

property scope: str

The name a cache clean filter matches this entry on.

Doubles as the table’s subject column: the thing a reader identifies the entry by (--tool ruff, --namespace pypi) is the same thing the listing shows them, so one property serves both.

property detail: str

What distinguishes this entry from its siblings in the same scope.

repomatic.cache.CACHE_KINDS: tuple[type[CachedFile], ...] = (<class 'repomatic.cache.BinaryCacheEntry'>, <class 'repomatic.cache.HttpCacheEntry'>, <class 'repomatic.cache.ConfigCacheEntry'>)

Every cache, in the order repomatic cache show lists them.

repomatic.cache.cached_binary_path(name, version, platform_key, executable)[source]

Construct the cache path for a binary (does not check existence).

Parameters:
  • name (str) – Tool name.

  • version (str) – Pinned version.

  • platform_key (str) – Platform key, like linux-x64.

  • executable (str) – Executable filename.

Return type:

Path

Returns:

Absolute path where the binary would be cached.

repomatic.cache.binary_sidecar_path(binary_path)[source]

Return the digest sidecar path for a cached binary.

Parameters:

binary_path (Path) – Path to the cached binary.

Return type:

Path

Returns:

Path of the sidecar file next to it.

repomatic.cache.get_cached_binary(name, version, platform_key, executable)[source]

Return the cached binary path if it exists and is executable.

Does not verify the checksum. The caller is responsible for integrity checks since it owns the checksum value and the skip_checksum flag.

Parameters:
  • name (str) – Tool name.

  • version (str) – Pinned version.

  • platform_key (str) – Platform key.

  • executable (str) – Executable filename.

Return type:

Path | None

Returns:

Path to the cached binary, or None if not cached.

repomatic.cache.store_binary(name, version, platform_key, source)[source]

Copy an extracted binary into the cache atomically.

Writes to a temporary file in the target directory, then renames to the final name. This is atomic on POSIX (same-filesystem rename) and safe on Windows (Path.replace overwrites atomically).

Triggers auto_purge() after a successful store.

Parameters:
  • name (str) – Tool name.

  • version (str) – Pinned version.

  • platform_key (str) – Platform key.

  • source (Path) – Path to the extracted binary to cache.

Return type:

Path | None

Returns:

Path to the cached binary, or None when the cache is unwritable (a read-only cache root, a restricted CI mount): callers fall back to their staging copy, matching store_response() and store_config().

repomatic.cache.get_cached_response(namespace, key, max_age_seconds)[source]

Return a cached HTTP response if it exists and is fresh.

Parameters:
  • namespace (str) – Cache namespace, like pypi or github-release-tags.

  • key (str) – Cache key, may contain / for nested paths.

  • max_age_seconds (int) – Maximum age in seconds. Entries with mtime older than this are considered stale and ignored. <= 0 disables the cache (always returns None).

Return type:

bytes | None

Returns:

Raw cached response bytes, or None if not cached or stale.

repomatic.cache.store_response(namespace, key, data)[source]

Store an HTTP response in the cache atomically.

Uses the same write-to-temp-then-rename pattern as store_binary(). Triggers auto_purge() after a successful store.

Parameters:
  • namespace (str) – Cache namespace.

  • key (str) – Cache key, may contain / for nested paths.

  • data (bytes) – Raw response bytes to cache.

Return type:

Path | None

Returns:

Path to the cached response file, or None if the write failed (permissions, read-only filesystem, sandbox restrictions).

repomatic.cache.store_config(tool_name, filename, content)[source]

Store a generated tool config in the cache atomically.

The digest of content names the directory of the file, so the same content always lands on the same path, and different content never overwrites it. A run storing a config refreshes its age, which keeps a config in use out of reach of auto_purge().

Parameters:
  • tool_name (str) – Tool name (registry key).

  • filename (str) – Config filename, like yamllint.yaml.

  • content (str) – Config file content as text.

Return type:

Path | None

Returns:

Path to the cached config file, or None if the write failed (permissions, read-only filesystem, sandbox restrictions).

repomatic.cache.cache_rows()[source]

List every cached file across the three caches, as table rows.

Backs repomatic cache show: each entry renders itself (CachedFile.as_row()), so the command stays a print call and a new cache kind shows up in the listing by existing.

Return type:

tuple[list[tuple[str, str, str, str, str]], int]

Returns:

(rows, total_size), rows ordered as CACHE_KINDS.

repomatic.cache.clean(*, tool=None, namespace=None, max_age_days=None)[source]

Remove cached entries, as repomatic cache clean selects them.

A scope filter reaches only the caches it applies to: tool narrows the binary and config caches and leaves HTTP responses alone, namespace the reverse. With neither filter, every cache is cleaned.

Parameters:
  • tool (str | None) – Only remove the binary and config entries of this tool.

  • namespace (str | None) – Only remove the HTTP responses of this namespace.

  • max_age_days (int | None) – Only remove entries older than this many days.

Return type:

tuple[int, int]

Returns:

Tuple of (files_deleted, bytes_freed) over every cache cleaned.

repomatic.cache.auto_purge()[source]

Remove cached entries older than the configured TTL.

Called automatically after every store, and runs at most once per cache root per process (see _PURGED_ROOTS). Purges every cache. Resolves the TTL from REPOMATIC_CACHE_MAX_AGE env var, then cache.max-age in [tool.repomatic], then the CacheConfig.max_age field default. Set to 0 to disable.

Return type:

None