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.versionproperty: a Nuitka or PyInstaller binary cannot rungitat runtime, so the hash must be baked into__version__in the source file before compilation.Only a
.devversion 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:
- Return type:
- Returns:
the new version, or
Nonewhen 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_pathonce, then writes it once, with every placeholder offieldsthat 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 (togit, say) runs for the placeholders that need it, never for a file carrying none. A callable answeringNoneor 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__ = "", notNone). The AST matcher only recognizes string literals, and the empty string acts as a falsy sentinel that stays type-consistent with baked values (alwaysstr).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:
- Return type:
- 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 nonameassignment 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:
- Return type:
- Returns:
the value written, or
Nonewhen nothing changed: no such variable, or one already holding a value.