如何让easy_install执行setup.py中的自定义命令?

发布于 2024-08-05 18:45:01 字数 1136 浏览 3 评论 0原文

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

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 执行我的自定义类吗?

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

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},
)

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-08-12 18:45:01

Paver 将 setuptools 提升到一个新的水平,并允许您编写自定义任务。它允许您扩展典型的 setup.py 文件,并提供一种简单的方法来引导 Paver 环境。

Paver takes setuptools to the next level and lets you write custom tasks. It allows you to extend the typical setup.py file and provides a simple way to bootstrap the Paver environment.

淤浪 2024-08-12 18:45:01

这是不可能的。 Enthought 有一个自定义版本的 setuptools 确实支持这一点,但除此之外,它作为愿望清单项目出现在错误跟踪器中,自 6 月份以来一直在讨论。

但是,有一些方法可以欺骗系统,您可以考虑它们。一种方法是让最重要的模块(使用包时始终首先导入的模块)在第一次调用时执行安装后操作。然后你必须自己清理,并考虑你无法写入库的情况,因为管理员安装了该包,并且第一个用户不是管理员。

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

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文