Skip to main content

支持 PEP 621 元数据的 PDM 的 PEP 517 后端

项目描述

PDM-PEP517

又一个PEP 517后端。

派皮 测试 pre-commit.ci 状态 pdm 管理

这是PDM项目的后端,您也可以单独使用它。它读取PEP 621格式的元数据并将其转换为Core metadata

用作 PEP 517 构建后端

编辑您pyproject.toml的如下:

[build-system]
requires = ["pdm-pep517"]
build-backend = "pdm.pep517.api"

工具特定设置

除了 PEP 621 中指定的标准字段外,PDM-PEP517 还采用其他一些设置来更改构建行为。它们应在[tool.pdm.build]表下定义:

[tool.pdm.build]
# Specify where the Python packages live.
package-dir = "src"
# File patterns to include, the paths are relative to the project root.
includes = []
# File patterns to exclude, the paths are relative to the project root.
excludes = []
# File patterns to include in source distribution and exclude in wheel distribution.
source-includes = []
# An extra script to populate the arguments of `setup()`, one can build C extensions with this script. Or a custom build() function to generate files.
setup-script = "build.py"
# If true, the setup-script will run in a generated `setup.py` file.
run-setuptools = false
# Override the Is-Purelib value in the wheel.
is-purelib = true
# Change the editable-backend: path(default) or editables
editable-backend = "editables"

您不必指定所有这些字段,PDM-PEP517 也可以基于 Python 打包的一些最佳实践巧妙地派生这些字段。

动态项目版本

pdm-pep517还可以动态确定项目的版本。为此,您需要version从您的字段中删除pyproject.toml并添加dynamic = ["version"]

[project]
...
- version = "0.1.0" remove this line
+ dynamic = ["version"]

然后在[tool.pdm.version]表格中,指定如何获取版本信息。支持两种方式:

  1. 从给定文件路径中的静态字符串中读取:
[tool.pdm.version]
source = "file"
path = "mypackage/__init__.py"

这样,文件必须包含如下行:

__version__ = "0.1.0" # Single quotes and double quotes are both OK, comments are allowed.
  1. 从 SCM 标签中读取,支持githg
[tool.pdm.version]
source = "scm"

从 SCM 不可用的源代码树构建时,您可以使用 env varPDM_PEP517_SCM_VERSION假装已设置版本。

PDM_PEP517_VERSION=0.1.0 python -m build

将 SCM 版本写入文件

您可以指示 PDM-PEP517 将从 SCM 获取的动态版本写回文件:

[tool.pdm.version]
source = "scm"
write_to = "foo/version.txt"  

默认情况下,PDM-PEP517 只会自己编写 SCM 版本。您可以将模板作为 Python 格式的字符串提供,以创建语法正确的 Python 赋值:

[tool.pdm.version]
source = "scm"
write_to = "foo/_version.py"  
write_template = "__version__ = '{}'"  

请注意,PDM-PEP517 每次都会重写整个文件,因此您不能在该文件中包含其他内容。

自定义构建脚本

使用自定义构建脚本,您可以调用其他工具来生成要包含在轮子中的文件。只需将表setup-script下的字段设置[tool.pdm.build]为脚本的路径即可。

在脚本中,您公开了一个名为 的函数build,该函数接受两个参数:

  • src(str):源目录的路径
  • dst(str):目标目录的路径

例子:

def build(src, dst):
    with open(os.path.join(dst, "myfile.txt"), "w") as f:
        # Put a file in the wheel
        f.write("Hello World!")

请注意,生成的文件层次结构将保留在 wheel: $dst/myfile.txt->$wheel_root/myfile.txt中。

run-setuptoolsistrue时,build将在生成的setup.py文件中调用该函数,并将设置参数作为唯一参数。

例子:

def build(setup_params):
    # add ext_modules to the setup() arguments
    setup_parms.update(ext_modules=[Extension("myextension", ["myextension.c"])])

这将导致setup()如下调用:

setup(
    name="mypackage",
    # Other metadata fields
    ext_modules=[Extension("myextension", ["myextension.c"])],
)

默认情况下,当setup-script设置时,生成的轮子将是特定于平台的,但您可以通过设置is-purelib来覆盖此行为false

支持的配置设置

pdm-pep517允许通过config_settings来修改构建行为。它使用与 相同的选项约定python setup.py bdist_wheel

--python-tag
    Override the python implementation compatibility tag(e.g. cp37, py3, pp3)
--py-limited-api
    Python tag (cp32|cp33|cpNN) for abi3 wheel tag
--plat-name
    Override the platform name(e.g. win_amd64, manylinux2010_x86_64)

例如,您可以使用build提供这些选项:

python -m build --sdist --wheel --outdir dist/ --config-setting="--python-tag=cp37" --config-setting="--plat-name=win_amd64"

pip尚不支持传递config_settings,请坚持build作为推荐的前端。

执照

这个项目是在MIT license下获得许可的。

项目详情