我想找到将文件包含在 git 未跟踪的 python sdist 中的正确方法。
上下文
我项目中的 .mo
文件未被 git
跟踪(就像其他一些需要在安装时创建的 .txt
文件一样)时间)。
我在 setup.py
中编写了一个小函数来在安装时创建它们,我在 setup()
中调用它:
setup(
.
.
.
data_files=create_extra_files(),
include_package_data=True,
.
.
.
)
注意它们应该属于data_dir
因为the documentation says :
The data_files option can be used to specify additional files needed by the module distribution: configuration files, message catalogs, data files, anything which doesn’t fit in the previous categories.
因此,这适用于 python3 setup.py install
(以及 bdist
)。 .mo
文件生成并存储在正确的位置。
但是如果我想让它与 sdist
一起工作,那么我必须将它们包含在 MANIFEST.in
中(例如 recursive-include mathmaker *.mo
)。 Documentation says indeed :
Changed in version 3.1: All the files that match data_files will be added to the MANIFEST file if no template is provided. See Specifying the files to distribute.
(该链接没有多大帮助)。
我不愿意将 *.mo
文件包含在 MANIFEST.in
中,因为 git 不会跟踪它们。和 check-manifest不喜欢这种情况,它提示 版本控制和 sdist 中的文件列表不匹配!
那么,有没有办法解决这种丑陋的情况?
重现情况的步骤
环境与项目
为避免污染您的环境,请在您选择的目录中创建并激活专用虚拟环境(python3.4+):
$ pyvenv-3.4 v0
$ source v0/bin/activate
(v0) $
在 project0
目录中复制以下树:
.
├── .gitignore
├── MANIFEST.in
├── README.rst
├── setup.py
└── project0
├── __init__.py
├── main.py
└── data
└── dummy_versioned.po
其中 README.rst
、__init__.py
和 dummy_versioned.po
为空。
其他文件的内容:
.gitignore
:build/ dist/ *.egg-info project0/data/*.txt *~
MANIFEST.in
:recursive-include project0 *.po recursive-include project0 *.txt
main.py
:#!/usr/bin/env python3 # -*- coding: utf-8 -*- def entry_point(): with open('project0/data/a_file.txt', mode='rt') as f: print(f.read())
setup.py
:#!/usr/bin/env python3 # -*- coding: utf-8 -*- import platform from setuptools import setup, find_packages def create_files(): txt_file_path = 'project0/data/a_file.txt' with open(txt_file_path, mode='w+') as f: f.write("Some dummy platform information: " + platform.platform()) return [('project0/data', [txt_file_path])] setup( name='project0', version='0.0.1', author='J. Doe', author_email='j.doe@someprovider.com', url='http://myproject.url', packages=find_packages(), data_files=create_files(), include_package_data=True, entry_points={ 'console_scripts': ['myscript0 = project0.main:entry_point'], } )
启动一个本地 git
仓库:
(v0) $ git init
(v0) $ git add .
安装check-manifest
:
(v0) $ pip3 install check-manifest
安装和测试
安装
有效:
(v0) $ python3 setup.py install
.
.
.
copying project0/data/a_file.txt -> build/lib/project0/data
.
.
.
Finished processing dependencies for project0==0.0.1
(v0) $ myscript0
Some dummy platform information: Linux-3.16.0-29-generic-x86_64-with-Ubuntu-14.04-trusty
如果您 rm project0/data/a_file.txt
,则 myscript0
不再工作,但重新安装它并再次工作,正如预期的那样。
构建 sdist 还包括 a_file.txt
:
(v0) $ python3 setup.py sdist
.
.
.
hard linking project0/data/a_file.txt -> project0-0.0.1/project0/data
.
.
.
请注意,要将此文件包含在 sdist 中,看起来有必要(如以下“上下文”部分中所述)在 MANIFEST 中包含
。你会删除这一行吗,recursive-include project0 *.txt
。在python3 setup.py sdist
不会再提到 a_file.txt
(不要忘记删除任何以前的 build/
或 dist/
目录来观察这一点)。
结论
因此,一切正常,但存在差异:a_file.txt
未被 git
跟踪,但包含在 MANIFEST.in 中
。
check-manifest
清楚地告诉:
lists of files in version control and sdist do not match!
missing from VCS:
project0/data/a_file.txt
那么,有没有合适的方法来处理这种情况呢?
最佳答案
据我了解你的问题你想添加要与 git 存储库一起分发的文件,但你不想跟踪它们的更改。
这可以通过以下四个简单步骤完成:
第 0 步:
首先确保 path/a_file.txt
文件中的内容与您要分发的内容相匹配。据我所知,它不能为空,所以如果您只是想让这个文件存在,请向它添加一个换行符/空格字符。
第 1 步:
使用 git add path/a_file.txt
第 2 步:
提交文件 (git commit path/a_file.txt
)
第 3 步:
更新 git 的索引并告诉 git 它应该进一步忽略
文件的变化
git update-index --assume-unchanged path/a_file.txt
如果你想对这个文件做一些更改,应该再次被跟踪,你可以简单地使用 --no-assume-unchanged
标志来设置它
活跃在 git 的索引中,然后提交更改。
注意 .gitignore
文件的创建告诉 git 忽略文件(在克隆存储库的所有机器上)并使用 git add - -force path/a_file.txt
将不起作用,因为 git 会 (force
) 将其添加到索引并跟踪更改。
关于python - 如何在未跟踪 VCS 的 Python sdist 中分发文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38477263/