PDM的PEP 517后端,支持PEP 621元数据
项目描述
PDM-PEP517
该项目已被重命名并重新发布为pdm-backend
另一个PEP 517后端。
这是PDM项目的后端,同时您也可以单独使用它。它读取PEP 621格式的元数据并将其转换为核心元数据。
作为PEP 517构建后端使用
按照以下方式编辑您的pyproject.toml
[build-system]
requires = ["pdm-pep517"]
build-backend = "pdm.pep517.api"
工具特定设置
除了PEP 621中指定的标准字段外,pdmpet517还尊重一些其他设置以改变构建行为。它们应在[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"
您不必指定所有这些,pdmpet517还可以根据Python打包的最佳实践智能地推导这些字段。
动态项目版本
pdm-pep517
还可以动态确定项目的版本。要这样做,您需要在pyproject.toml
中省略version
字段并添加dynamic = ["version"]
[project]
...
- version = "0.1.0" remove this line
+ dynamic = ["version"]
然后在[tool.pdm.version]
表中,指定如何获取版本信息。支持两种方式
- 从给定文件路径中的静态字符串中读取
[tool.pdm.version]
source = "file"
path = "mypackage/__init__.py"
在这种情况下,文件必须包含如下行
__version__ = "0.1.0" # Single quotes and double quotes are both OK, comments are allowed.
- 从SCM标签读取,支持
git
和hg
[tool.pdm.version]
source = "scm"
当从一个没有源代码管理(SCM)的源树构建时,您可以使用环境变量 PDM_PEP517_SCM_VERSION
来模拟设置版本。
PDM_PEP517_SCM_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每次都会重写整个文件,因此该文件中不能有其他内容。
自定义构建脚本
使用自定义构建脚本,您可以调用其他工具生成要包含在wheel中的文件。只需将[tool.pdm.build]
表下的setup-script
字段设置为脚本的路径即可。
在脚本中,您暴露一个名为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-setuptools
为true
时,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
时,生成的wheel将是平台特定的,但您可以通过将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许可证授权。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定该选择哪一个,请了解有关 安装包 的更多信息。