我记下了一个setup.py,仅当您的用户使用pip < 1.2(例如在Ubuntu 12.04上)时才需要。如果每个人都拥有1.2或更高的点数,那么您需要的唯一东西是packages=[..., 'twisted.plugins']
。
通过防止PIP从写行“twisted
”到.egg-info/top_level.txt
,您可以继续使用packages=[..., 'twisted.plugins']
并有一个工作pip uninstall
不删除所有的twisted/
。这涉及monkeypatching setuptools /分布在您的setup.py
的顶部附近。下面是一个示例setup.py
:
from distutils.core import setup
# When pip installs anything from packages, py_modules, or ext_modules that
# includes a twistd plugin (which are installed to twisted/plugins/),
# setuptools/distribute writes a Package.egg-info/top_level.txt that includes
# "twisted". If you later uninstall Package with `pip uninstall Package`,
# pip <1.2 removes all of twisted/ instead of just Package's twistd plugins.
# See https://github.com/pypa/pip/issues/355 (now fixed)
#
# To work around this problem, we monkeypatch
# setuptools.command.egg_info.write_toplevel_names to not write the line
# "twisted". This fixes the behavior of `pip uninstall Package`. Note that
# even with this workaround, `pip uninstall Package` still correctly uninstalls
# Package's twistd plugins from twisted/plugins/, since pip also uses
# Package.egg-info/installed-files.txt to determine what to uninstall,
# and the paths to the plugin files are indeed listed in installed-files.txt.
try:
from setuptools.command import egg_info
egg_info.write_toplevel_names
except (ImportError, AttributeError):
pass
else:
def _top_level_package(name):
return name.split('.', 1)[0]
def _hacked_write_toplevel_names(cmd, basename, filename):
pkgs = dict.fromkeys(
[_top_level_package(k)
for k in cmd.distribution.iter_distribution_names()
if _top_level_package(k) != "twisted"
]
)
cmd.write_file("top-level names", filename, '\n'.join(pkgs) + '\n')
egg_info.write_toplevel_names = _hacked_write_toplevel_names
setup(
name='MyPackage',
version='1.0',
description="You can do anything with MyPackage, anything at all.",
url="http://example.com/",
author="John Doe",
author_email="[email protected]",
packages=['mypackage', 'twisted.plugins'],
# You may want more options here, including install_requires=,
# package_data=, and classifiers=
)
# Make Twisted regenerate the dropin.cache, if possible. This is necessary
# because in a site-wide install, dropin.cache cannot be rewritten by
# normal users.
try:
from twisted.plugin import IPlugin, getPlugins
except ImportError:
pass
else:
list(getPlugins(IPlugin))
我已经与pip install
,pip install --user
和easy_install
测试这一点。使用任何安装方法,上面的monkeypatch和pip uninstall
都可以正常工作。
您可能想知道:我是否需要清除monkeypatch以避免搞乱下一次安装? (例如pip install --no-deps MyPackage Twisted
;你不想影响Twisted的top_level.txt
。)答案是否定的; monkeypatch不会影响其他安装,因为pip
会为每次安装生成新的python
。
相关:请记住,在您的项目中,您必须not have a filetwisted/plugins/__init__.py
。如果你看到在安装过程中这样的警告:
package init file 'twisted/plugins/__init__.py' not found (or not a regular file)
这是完全正常的,你应该不试图通过增加一个__init__.py
来解决它。
的'package_data'失败的描述将是有益的。 – Glyph
这并没有回答这个问题的显式部分,即如何指定要安装的文件,但是它确实解决了(重新)生成插件缓存(稍微清晰一些) )http://stackoverflow.com/questions/1321270/how-to-extend-distutils-with-a-simple-post-install-script – Glyph