Drucken Sie die Git -Version mit `uv run` _and_ venv mit dem Hatch BuildPython

Python-Programme
Anonymous
 Drucken Sie die Git -Version mit `uv run` _and_ venv mit dem Hatch Build

Post by Anonymous »

Ich arbeite an einer mit UV erstellten Python -Anwendung. Die Anwendung befindet sich unter aktiver Entwicklung, und ich möchte in der Lage sein, ihre Ausgänge an die Version der Software zu binden (sie generiert eine Datei mit Ergebnissen, und ich möchte, dass die Version in dieser Ausgabe aufgenommen wird). Ich bin faul, also möchte ich die semantische Version in der PYProject.toml nicht manuell aktualisieren. Ich möchte nur so etwas tun können: < /p>

Code: Select all

> uv run myapp
Usage: myapp ...
Version: {the git hash with dirty indicator}
< /code>
> uv venv
> source .venv/bin/activate
(myapp)> myapp --help
Usage: myapp ...
Version: {the git hash with dirty indicator}
< /code>
In the past (with other compiled languages) I would generate a file at build time that gets packaged into the executable. Later at runtime I would read this file and print out the version. I've gotten something similar to work with uv if I use:
>python write_version_to_file.py
>uv pip install --force-reinstall . # uv run myapp would also work here
< /code>
But this feels clunky because it is two steps, and requires the "force" flag for pip. I feel like it would be cleaner if I could hook into uv in a way that I can dynamically generate my version file right before uv run
oder über eine UV -PIP Installation . Gibt es ein gutes Muster dafür? Würde es einen besseren Weg geben, dies zu tun, ohne zu versuchen, in UV/Hatch zu binden? />
PYProject.toml:

Code: Select all

[tool.hatch.build.hooks.custom]
path = "write_version_to_file.py"
class = "VersionBuildHook"
< /code>
but this also only ran consistently with uv pip install
und nicht uv run
Als Referenz sehen die relevanten Teile meines PyProject.toml aus:

Code: Select all

[project.scripts]
myapp = "mypkg.myapp:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build]
artifacts = ["src/mypkg/version.properties"]

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post