Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

python - Pip not picking up a custom install cmdclass

so I found this answer to my exact question, but for some reason it's not working:

$ cat /tmp/testinstall/setup.py:

from setuptools.command.install import install

from setuptools import setup


class verifying_install(install):
    def run(self):
        print "running........"
        install.run(self)
        print "verifying........"


setup(name='test',
      version='1',
      py_modules=['test'],
      include_package_data=True,
      zip_safe=True,
      cmdclass={'install': verifying_install}
)

But then, even though setup.py install works::

?  /tmp/testinstall
$ mktmpenv && cd -
This is a temporary environment. It will be deleted when you run 'deactivate'.

(5bc7db7ca1b34ec5)?  /tmp/testinstall
$ python setup.py install
running install
running........
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
copying test.py -> build/lib.linux-x86_64-2.7
running egg_info
creating test.egg-info
writing test.egg-info/PKG-INFO
writing top-level names to test.egg-info/top_level.txt
writing dependency_links to test.egg-info/dependency_links.txt
writing manifest file 'test.egg-info/SOURCES.txt'
reading manifest file 'test.egg-info/SOURCES.txt'
writing manifest file 'test.egg-info/SOURCES.txt'
running install_lib
copying build/lib.linux-x86_64-2.7/test.py -> /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages
byte-compiling /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages/test.py to test.pyc
running install_egg_info
Copying test.egg-info to /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages/test-1-py2.7.egg-info
running install_scripts
verifying........

(Note the running... and verifying...... lines)

pip install of the directory doesn't work:

(5bc7db7ca1b34ec5)?  /tmp/testinstall
$ deactivate && mktmpenv && cd - && pip install .
Removing temporary environment: 5bc7db7ca1b34ec5
Removing 5bc7db7ca1b34ec5...
New python executable in 4cac61c13d080257/bin/python
Installing Setuptools...done.
Installing Pip....done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
/tmp/testinstall
Unpacking /tmp/testinstall
  Running setup.py egg_info for package from file:///tmp/testinstall

Cleaning up...

And pip install of the sdist doesn't work either:

(4cac61c13d080257)?  /tmp/testinstall
$ python setup.py sdist
running sdist
# ..snip..
creating dist
Creating tar archive
removing 'test-1' (and everything under it)
(4cac61c13d080257)?  /tmp/testinstall
$ deactivate && mktmpenv && cd -
Removing temporary environment: 4cac61c13d080257
Removing 4cac61c13d080257...
New python executable in 9a42f3a58809f1a3/bin/python
Installing Setuptools...done.
Installing Pip...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
/tmp/testinstall

(9a42f3a58809f1a3)?  /tmp/testinstall
$ ls dist
test-1.tar.gz
(9a42f3a58809f1a3)?  /tmp/testinstall
$ pip install dist/test-1.tar.gz
Unpacking ./dist/test-1.tar.gz
  Running setup.py egg_info for package from file:///tmp/testinstall/dist/test-1.tar.gz

Cleaning up...

Note the lack of running... and verifying... words in both of those.

Anybody have any idea what's going on here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ran into this problem just now. It looks like there are many different commands that pip install my-package can translate into.

  1. setup.py install
  2. setup.py egg_info
  3. setup.py develop

So you need to handle each of these different cases.

from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info

'''
BEGIN CUSTOM INSTALL COMMANDS
These classes are used to hook into setup.py's install process. Depending on the context:
$ pip install my-package

Can yield `setup.py install`, `setup.py egg_info`, or `setup.py develop`
'''


def custom_command():
    import sys
    if sys.platform in ['darwin', 'linux']:
        os.system('./custom_command.sh')


class CustomInstallCommand(install):
    def run(self):
        install.run(self)
        custom_command()


class CustomDevelopCommand(develop):
    def run(self):
        develop.run(self)
        custom_command()


class CustomEggInfoCommand(egg_info):
    def run(self):
        egg_info.run(self)
        custom_command()

'''
END CUSTOM INSTALL COMMANDS 
'''

setup(
    ...
    cmdclass={
        'install': CustomInstallCommand,
        'develop': CustomDevelopCommand,
        'egg_info': CustomEggInfoCommand,
    },
    ...
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...