如何为easy_install / setuptools / distutils 添加安装后脚本?
我想在我的setup.py文件中添加一个钩子,这个钩子会在安装后运行(无论是通过easy_install还是用python setup.py install来安装)。
在我的项目PySmell中,我有一些支持Vim和Emacs的文件。当用户按照正常方式安装PySmell时,这些文件会被复制到实际的包里,用户需要自己去找出来,然后放到他的.vim或.emacs目录里。我希望在安装后能询问用户想把这些文件复制到哪里,或者至少打印出这些文件的位置,并告诉用户该怎么处理。
这样做的最佳方法是什么呢?
谢谢!
我的setup.py文件大致是这样的:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from setuptools import setup
version = __import__('pysmell.pysmell').pysmell.__version__
setup(
name='pysmell',
version = version,
description = 'An autocompletion library for Python',
author = 'Orestis Markou',
author_email = 'orestis@orestis.gr',
packages = ['pysmell'],
entry_points = {
'console_scripts': [ 'pysmell = pysmell.pysmell:main' ]
},
data_files = [
('vim', ['pysmell.vim']),
('emacs', ['pysmell.el']),
],
include_package_data = True,
keywords = 'vim autocomplete',
url = 'http://code.google.com/p/pysmell',
long_description =
"""\
PySmell is a python IDE completion helper.
It tries to statically analyze Python source code, without executing it,
and generates information about a project's structure that IDE tools can
use.
The first target is Vim, because that's what I'm using and because its
completion mechanism is very straightforward, but it's not limited to it.
""",
classifiers = [
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development',
'Topic :: Utilities',
'Topic :: Text Editors',
]
)
编辑:
这里有一个示例,展示了如何使用python setup.py install
:
from setuptools.command.install import install as _install
class install(_install):
def run(self):
_install.run(self)
print post_install_message
setup(
cmdclass={'install': install},
...
目前还没有通过easy_install的方式成功。
2 个回答
作为一种解决办法,你可以把zip_ok选项设置为false,这样你的项目就会以解压后的文件夹形式安装,这样用户就更容易找到编辑器的配置文件了。
在distutils2中,将来可以把东西安装到更多的文件夹里,包括自定义的文件夹,还可以设置安装前后和删除时的操作。
这要看用户是怎么安装你的软件包的。如果用户真的运行了“setup.py install”,那就比较简单了:只需要在安装命令中添加一个子命令(比如叫 install_vim),然后在这个子命令的 run() 方法里把你想要的文件复制到你想要的位置。你可以把这个子命令添加到 install.sub_commands 中,并把命令传递给 setup()。
如果你想在一个二进制文件中添加一个安装后的脚本,那就要看你创建的二进制文件类型了。例如,bdist_rpm、bdist_wininst 和 bdist_msi 这些格式支持安装后脚本,因为它们的打包格式本身就支持这个功能。
而 bdist_egg 则不支持安装后脚本,这是它设计上的一个选择: