2008-10-30 67 views
32

我希望能夠在我的setup.py中添加一個鉤子,這個鉤子將在安裝後運行(無論是在easy_install或安裝python setup.py時)。如何將安裝後腳本添加到easy_install/setuptools/distutils?

在我的項目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 = '[email protected]', 
    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

我剛剛發現,調用`setuptools.install.install:run()`明確不能解決`install_requires`設置參數,它看起來像它在不同的方式工作,當你這樣做 – astronaut 2013-09-22 13:11:04

+1

@astronaut使用`do_egg_install`,在這裏討論:http://stackoverflow.com/questions/21915469/python-setuptools-install-requires-is-ignored-when-overriding-cmdclass – 2015-03-27 12:57:14

回答

7

這取決於用戶如何安裝軟件包。如果用戶實際上運行「setup.py install」,則相當簡單:只需將其他子命令添加到install命令(比如install_vim),它的run()方法將在需要的地方複製所需的文件。您可以將子命令添加到install.sub_commands,並將該命令傳遞給setup()。

如果您想要二進制文件中的安裝後腳本,它取決於您正在創建的二進制文件的類型。例如,bdist_rpm,bdist_wininst和bdist_msi支持安裝後腳本,因爲底層打包格式支持安裝後腳本。

bdist_egg不設計支持安裝後的機制:

http://bugs.python.org/setuptools/issue41

0

作爲一個變通辦法,讓你的項目安裝爲解壓縮的目錄,你可以設置zip_ok選項設置爲false ,那麼你的用戶會更容易找到編輯器配置文件。

在distutils2中,可以將東西安裝到更多的目錄中,包括自定義目錄,以及安裝前/後安裝/刪除掛鉤。

相關問題