Makefile - 我应该如何提取“pyproject.toml”中嵌入的版本号?

发布于 2025-01-16 14:20:36 字数 333 浏览 6 评论 0原文

我有一个带有 pyproject.toml 文件的 python 项目。通常,我将项目的版本号存储在 pyproject.toml 中,如下所示:

% grep version pyproject.toml 
version = "0.0.2"
%

我希望将该版本号放入 Makefile 变量中,无论版本术语周围有多少空格。

我应该如何将 pyproject.toml 版本字符串提取到名为 VERSIONMakefile 环境变量中?

I have a python project with a pyproject.toml file. Typically I store the project's version number in pyproject.toml like this:

% grep version pyproject.toml 
version = "0.0.2"
%

I want to get that version number into a Makefile variable regardless of how many spaces wind up around the version terms.

What should I do to extract the pyproject.toml version string into a Makefile environment variable called VERSION?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£噩梦荏苒 2025-01-23 14:20:36

这似乎是最好的...我把它放在我的 Makefile

# grep the version from pyproject.toml, squeeze multiple spaces, delete double
#   and single quotes, get 3rd val. This command tolerates 
#   multiple whitespace sequences around the version number
VERSION := $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)

特别感谢 @charl-botha 对于 -m 1 grep 参数...gnu 和 bsd grep 支持-m 在这种情况下。

额外信息从 2024 年开始,我停止在 pyproject.toml 中查找版本号...现在我将版本嵌入到我的 __about__.py 中并进行管理所有带有 hatch 的版本,这对于构建 Python 项目来说非常棒(它取代类似的工具诗)。

This seemed to work out the best... I put this in my Makefile

# grep the version from pyproject.toml, squeeze multiple spaces, delete double
#   and single quotes, get 3rd val. This command tolerates 
#   multiple whitespace sequences around the version number
VERSION := $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)

Special thanks to @charl-botha for the -m 1 grep argument... both gnu and bsd grep support -m in this context.

Extra info: As of 2024, I stopped grepping through my pyproject.toml for version numbers... now I embed the version in my __about__.py and manage all versions with hatch, which is just awesome for building Python projects (it replaces tools like poetry).

旧故 2025-01-23 14:20:36

如果您有安装了 tomli 软件包的 Python,或者您使用的是内置 toml 的 Python 3.11(在这种情况下,您将 import tomllib 而不是 tomli) ,并且您的 pyproject.toml 遵循诗歌约定,类似以下内容会很好用:

VERSION := $(shell python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])')

在命令行上进行简单测试:

python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])'

显然您可以轻松调整所需版本的位置> 其他情况下的条目pyproject.toml 约定。

顺便说一句,tomllib 实际上是 tomli,请参阅 https://peps.python.org/pep-0680/#rationale

If you have a Python available with the tomli package installed, or you're on Python 3.11 with toml built-in (in which case you would import tomllib instead of tomli), and your pyproject.toml follows poetry convention, something like the following would work well:

VERSION := $(shell python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])')

Test on command-line with simply:

python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])'

Obviously you can easily adapt the location of the desired version entry in the case of other pyproject.toml conventions.

BTW, tomllib is in fact tomli, see https://peps.python.org/pep-0680/#rationale

尹雨沫 2025-01-23 14:20:36

解析major.minor.patch的替代解决方案,基于@Mike Pennington'答案:

grep -m 1 version pyproject.toml | grep -e '\d.\d.\d' -o

An alternative solution to parse major.minor.patch, based on @Mike Pennington's answer:

grep -m 1 version pyproject.toml | grep -e '\d.\d.\d' -o
ㄟ。诗瑗 2025-01-23 14:20:36

如果这就是该文件真正包含的全部内容,那么它与 makefile 语法非常接近,您可以将其作为 makefile 包含在内:

include pyproject.toml
all: ; echo 'version = $(version)'

$ make
echo 'version = "0.0.2"'
version = "0.0.2"

如果您不想或不能这样做,我会使用 sed 为它:

VERSION := $(shell sed -n 's/^ *version.*=.*"\([^"]*\)".*/\1/p' pyproject.toml)
all: ; echo 'version = $(VERSION)'

$ make
echo 'version = 0.0.2'
version = 0.0.2

If that's all that file really contains, that's sufficiently close to makefile syntax that you can just include it as a makefile:

include pyproject.toml
all: ; echo 'version = $(version)'

$ make
echo 'version = "0.0.2"'
version = "0.0.2"

If you don't want to, or can't, do that, I'd use sed for it:

VERSION := $(shell sed -n 's/^ *version.*=.*"\([^"]*\)".*/\1/p' pyproject.toml)
all: ; echo 'version = $(VERSION)'

$ make
echo 'version = 0.0.2'
version = 0.0.2
我的鱼塘能养鲲 2025-01-23 14:20:36
poetry version --short

您可能需要先poetry install

poetry version --short

You many need to poetry install first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文