如何让easy_install在setup.py中执行自定义命令? [英] How to make easy_install execute custom commands in setup.py?

查看:72
本文介绍了如何让easy_install在setup.py中执行自定义命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 setup.py 除了安装 Python 包之外还可以执行一些自定义操作(例如安装 init.d 脚本、创建目录和文件等)我知道我可以自定义 distutils/setuptools 类来执行我的操作自己的行动.我遇到的问题是,当我 cd 到包目录并执行python setup.py install"时一切正常,但是当我执行easy_install mypackage.tar.gz"时,我的自定义类似乎没有被执行.这是我的 setup.py 文件(在同一个目录中创建一个空的 myfoobar.py 文件进行测试):

I want my setup.py to do some custom actions besides just installing the Python package (like installing an init.d script, creating directories and files, etc.) I know I can customize the distutils/setuptools classes to do my own actions. The problem I am having is that everything works when I cd to the package directory and do "python setup.py install", but my custom classes don't seem to be executed when I do "easy_install mypackage.tar.gz". Here's my setup.py file (create an empty myfoobar.py file in the same dir to test):

import setuptools
from setuptools.command import install as _install

class install(_install.install):
    def initialize_options(self):
        _install.install.initialize_options(self)

    def finalize_options(self):
        _install.install.finalize_options(self)

    def run(self):
        # Why is this never executed when tarball installed with easy_install?
        # It does work with: python setup.py install
        import pdb;pdb.set_trace()
        _install.install.run(self)

setuptools.setup(
    name = 'myfoobar',
    version = '0.1',
    platforms = ['any'],
    description = 'Test package',
    author = 'Someone',
    py_modules = ['myfoobar'],
    cmdclass = {'install': install},
)

即使我从 distutils 导入setup"和install",也会发生同样的事情.有什么想法可以让 easy_install 执行我的自定义类吗?

The same thing happens even if I import "setup" and "install" from distutils. Any ideas how I could make easy_install execute my custom classes?

澄清一下,我不想使用任何额外的东西,比如 Buildout 或 Paver.

To clarify, I don't want to use anything extra, like Buildout or Paver.

推荐答案

无法做到.Enthought 有一个自定义版本的 setuptools 确实支持这一点,但除此之外,它作为自 6 月以来一直在讨论的愿望清单项目在错误跟踪器中.

It cannot be done. Enthought has a custom version of setuptools that does support this, but otherwise it is in the bug tracker as a wishlist item that has been under discussion since June.

但是,有一些方法可以欺骗系统,您可以考虑使用它们.一种方法是让您最重要的模块,即使用包时始终首先导入的模块,在第一次调用时执行安装后操作.那你就得自己清理了,再考虑一下不能写入库的情况,因为是admin安装了包,第一个用户不是admin.

However, there are ways to trick the system and you might consider them. One way is to have your most important module, the one that is always imported first when using your package, do the post install actions the first time it is called. Then you have to clean up after yourself, and consider the case where you cannot write into the library because an admin installed the package and the first user is someone other than admin.

在最坏的情况下,这将涉及为使用该软件包的每个用户创建一个 ~/.mypackage 目录,并为每个新用户重新运行 postinstall 一次.每次导入模块时,它都会检查 ~/.mypackage 是否存在.如果它不存在,它将运行 postinstall 并创建它.如果存在,它会跳过安装后.

In the worst case, this would involve creating a ~/.mypackage directory for ever user who uses the package, and rerunning the postinstall once for each new user. Every time the module is imported, it checks for the existence of ~/.mypackage. If it is not there, it runs the postinstall and creates it. If it is there, it skips the postinstall.

这篇关于如何让easy_install在setup.py中执行自定义命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆