如何告诉pbr在包中包含非代码文件

How to tell pbr to include non-code files in package

当我构建 python:alpine 图像时,我只 运行 遇到这个问题。重现它有点痛苦,但这些是步骤:

Docker 容器设置:

$ docker run -it python:3.7-rc-alpine /bin/ash
$ pip install pbr

小包设置,包括非python文件

test
├── .git
├── setup.cfg
├── setup.py
└── src
    └── test
        ├── __init__.py
        ├── test.yml
        └── sub_test
            ├── __init__.py
            └── test.yml

setup.py:

from setuptools import find_packages, setup
setup(
    setup_requires=['pbr'],
    pbr=True,
    package_dir={'': 'src'},
    packages=find_packages(where='src'),
)

setup.cfg:

[metadata]
name = test

所有其他文件都是空的。我用 docker cp test <docker_container>:/test.

将它们复制到容器中

回到容器我现在尝试用 cd test; pip wheel -w wheel . 构建包,test/src/test 中的 test.yml 将包含在其中,但 test/src/test/sub_test 中的那个不会。

我不知道为什么会发生这种情况,因为 pbr on that matter 的(可怜的稀疏,而且我觉得很混乱)文档指出

Just like AUTHORS and ChangeLog, why keep a list of files you wish to include when you can find many of these in git. MANIFEST.in generation ensures almost all files stored in git, with the exception of .gitignore, .gitreview and .pyc files, are automatically included in your distribution.

我找不到允许我明确包含某些文件或文件类型的 pbr 参数,而我希望它存在。

import src/test/sub_test.test.yml 创建 MANIFEST.in 实际上解决了这个问题,但我宁愿理解并避免这种行为。

pbr 需要 git 才能正确编译其要包含的文件列表,因此可以通过在构建包之前将 git 安装到构建环境中来解决该问题。对于高山图像,那将是 apk add --no-cache git.


pbr 使用 .git 文件来确定哪些文件应该是包的一部分,哪些不应该。简短的版本是它从 setup.py 中的 setup 调用中的 packages 参数获取文件列表的交集,以及当前签出的 [=] 中提交或暂存的所有内容25=]分支。

因此,如果项目没有 .git 文件,您还需要另外执行 git init; git add src

'bug' 的原因是 pbr 默默地假设所有 .py 文件都应该添加,不管它们是否被提交,这使得实际问题更难识别。它也只会在找不到 .git 文件时抛出错误,如果它存在则不会抛出错误,但它无法从中获取任何信息,因为 git 未安装。