click_extra.prebake module¶

Bake build-time metadata into Python source files before compilation.

Compiled binaries (Nuitka, PyInstaller) and git-less runtimes (Docker images, archive checkouts) cannot resolve version or Git metadata at runtime the way click_extra.version.VersionOption does. The values must instead be written into the source before the build, by rewriting the relevant dunder assignments (__version__, __git_short_hash__, …) in place with ast.

This mirrors shadow-rs, which injects build-time constants (BRANCH, SHORT_COMMIT, COMMIT_HASH, COMMIT_DATE, TAG, …) into Rust binaries at compile time. Its BUILD_TIME, BUILD_OS and BUILD_TARGET constants have counterparts here too, in click_extra.version_facts.BUILD_RESOLVERS: those describe the host that produced the binary, which neither the git fields nor the running interpreter can answer once it is running somewhere else.

click_extra.project.discover_package_init_files() finds the files to bake from a project’s packaging metadata.

click_extra.prebake.prebake_version(file_path, local_version)[source]¶

Append a PEP 440 local version identifier to __version__.

The compile-time complement to the runtime click_extra.version.VersionOption.version property: a Nuitka or PyInstaller binary cannot run git at runtime, so the hash must be baked into __version__ in the source file before compilation.

Only a .dev version is touched, and only when it carries no + suffix yet: a release version and an already pre-baked one are left alone, which makes a second run safe.

Parameters:
  • file_path (Path) – the Python source file declaring __version__.

  • local_version (str) – the identifier to append, like a Git short hash.

Return type:

str | None

Returns:

the new version, or None when nothing changed: a release version, an already pre-baked one, or no __version__ at all.

click_extra.prebake.prebake_dunders(file_path, fields)[source]¶

Fill the empty dunder placeholders of a Python source file in one pass.

Reads and parses file_path once, then writes it once, with every placeholder of fields that was found empty filled in. A field whose value is a callable is resolved only when its placeholder is present and still empty, so a resolver that shells out (to git, say) runs for the placeholders that need it, never for a file carrying none. A callable answering None or an empty string leaves its placeholder alone, as does a placeholder already holding a value: running the pass twice is safe.

Placeholders must use empty strings (__field__ = "", not None). The AST matcher only recognizes string literals, and the empty string acts as a falsy sentinel that stays type-consistent with baked values (always str).

A name with no placeholder in the file is skipped: a build bakes every field it knows into every file of a package, and most files declare a few of them. prebake_dunder() is the single-field form, which does warn.

Parameters:
  • file_path (Path) – the Python source file to rewrite.

  • fields (Mapping[str, str | Callable[[], str | None]]) – dunder names (__git_branch__) mapped to the value to bake, or to a callable resolving it on demand.

Return type:

dict[str, str]

Returns:

the names written, each mapped to the value it received. Empty when the file was left untouched.

click_extra.prebake.prebake_dunder(file_path, name, value)[source]¶

Replace an empty dunder variable’s value in a Python source file.

The single-field form of prebake_dunders(), which carries the placeholder rules, with one addition: a file holding no name assignment at all is reported with a warning, since a lone field is asked for on purpose and a typo in its name would otherwise pass unnoticed.

Where prebake_version() appends a PEP 440 local identifier to __version__, this replaces the whole value of any dunder variable that starts empty. Typical use case: injecting a release commit SHA into __git_tag_sha__ = "" at build time.

Parameters:
  • file_path (Path) – the Python source file to rewrite.

  • name (str) – the dunder variable to fill, like __git_tag_sha__.

  • value (str) – the value to bake.

Return type:

str | None

Returns:

the value written, or None when nothing changed: no such variable, or one already holding a value.