如何从 setup.py 中读取 pyproject.toml 中的项目依赖项,以避免在两个文件中重复信息?

How do I read project dependencies from pyproject.toml from my setup.py, to avoid duplicating the information in both files?

我们正在升级以使用 BeeWare's Briefcase 0.3.1 进行打包,它使用 pyproject.toml 而不是 setup.py 来指定如何打包,包括将哪些依赖项包含在一个包中。

这是 公文包 pyproject.toml 的最小示例:

[tool.briefcase.app.exampleapp]
formal_name = "exampleapp"
description = "something"
requires = ['PyQt5', 'qtconsole']
sources = ['exampleapp']

我们想从 setup.py 访问需求列表,这样我们就不必在两个文件中复制它,并使它们保持同步。我们还没有准备好从 setuptools, this is only for packaging. The alternative is of course to let setup.py auto-generate the pyproject.toml file, but that seems a little backwards to the intention with PEP 518.

切换

据我所知,公文包实际上不兼容PEP 517(至少默认情况下不兼容)。它使用 pyproject.toml 文件,但没有填满 [build-system] 部分,因此应该可以在该文件中设置实际的 PEP 517 构建后端不会引起冲突。

pyproject.toml

[build-system]
build-backend = 'setuptools.build_meta'
requires = [
    'setuptools',
    'toml',
]

[tool.briefcase.app.exampleapp]
formal_name = 'exampleapp'
description = 'something'
requires = ['PyQt5', 'qtconsole']
sources = ['exampleapp']

setup.py

#!/usr/bin/env python3

import pathlib

import pkg_resources
import setuptools
import toml

def _parse_briefcase_toml(pyproject_path, app_name):
    pyproject_text = pyproject_path.read_text()
    pyproject_data = toml.loads(pyproject_text)
    briefcase_data = pyproject_data['tool']['briefcase']
    app_data = briefcase_data['app'][app_name]
    setup_data = {
        'name': pkg_resources.safe_name(app_data['formal_name']),
        'version': briefcase_data['version'],
        'install_requires': app_data['requires'],
        # ...
    }
    return setup_data

def _setup():
    app_name = 'exampleapp'
    pyproject_path = pathlib.Path('pyproject.toml')
    setup_data = _parse_briefcase_toml(pyproject_path, app_name)
    setuptools.setup(**setup_data)

if __name__ == '__main__':
    _setup()

然后 pip 和其他 PEP 517 兼容的前端应该能够通过委托给 [=28= 来构建和安装项目]setuptools 同时注意正确设置包含 setuptoolstoml.

的构建环境

我想也可以让 briefcase 处理 pyproject.toml 文件的解析(可能使用 briefcase.config.parse_config(...)),但没有记录在案不知道这些API的稳定性如何