Python setuptools/distutils custom build for the `extra` package with Makefile

后端 未结 2 1711
说谎
说谎 2020-12-25 12:39

Preamble: Python setuptools are used for the package distribution. I have a Python package (let us call it my_package), that has several

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-25 13:20

    Unfortunately, the docs are extremely scarce around the interaction between setup.py and pip, but you should be able to do something like this:

    import subprocess
    
    from setuptools import Command
    from setuptools import setup
    
    
    class CustomInstall(Command):
    
        user_options = []
    
        def initialize_options(self):
            pass
    
        def finalize_options(self):
            pass
    
        def run(self):
            subprocess.call(
                ['touch',
                 '/home/{{YOUR_USERNAME}}/'
                 'and_thats_why_you_should_never_run_pip_as_sudo']
            )
    
    setup(
        name='hack',
        version='0.1',
        cmdclass={'customcommand': CustomInstall}
    )
    

    This gives you a hook into running arbitrary code with commands, and also supports a variety of custom option parsing (not demonstrated here).

    Put this in a setup.py file and try this:

    pip install --install-option="customcommand" .

    Note that this command is executed after the main install sequence, so depending on exactly what you're trying to do, it may not work. See the verbose pip install output:

    (.venv) ayoon:tmp$ pip install -vvv --install-option="customcommand" .
    /home/ayoon/tmp/.venv/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / -
    -global-options / --install-options.                                                                                                                        
      cmdoptions.check_install_build_global(options)
    Processing /home/ayoon/tmp
      Running setup.py (path:/tmp/pip-j57ovc7i-build/setup.py) egg_info for package from file:///home/ayoon/tmp
        Running command python setup.py egg_info
        running egg_info
        creating pip-egg-info/hack.egg-info
        writing pip-egg-info/hack.egg-info/PKG-INFO
        writing dependency_links to pip-egg-info/hack.egg-info/dependency_links.txt
        writing top-level names to pip-egg-info/hack.egg-info/top_level.txt
        writing manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
        reading manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
        writing manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
      Source in /tmp/pip-j57ovc7i-build has version 0.1, which satisfies requirement hack==0.1 from file:///home/ayoon/tmp
    Could not parse version from link: file:///home/ayoon/tmp
    Installing collected packages: hack
      Running setup.py install for hack ...     Running command /home/ayoon/tmp/.venv/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-j57ovc7
    i-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --
    record /tmp/pip-_8hbltc6-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ayoon/tmp/.venv/include/site/python3
    .6/hack customcommand                                                                                                                                       
        running install
        running build
        running install_egg_info
        running egg_info
        writing hack.egg-info/PKG-INFO
        writing dependency_links to hack.egg-info/dependency_links.txt
        writing top-level names to hack.egg-info/top_level.txt
        reading manifest file 'hack.egg-info/SOURCES.txt'
        writing manifest file 'hack.egg-info/SOURCES.txt'
        Copying hack.egg-info to /home/ayoon/tmp/.venv/lib/python3.6/site-packages/hack-0.1-py3.6.egg-info
        running install_scripts
        writing list of installed files to '/tmp/pip-_8hbltc6-record/install-record.txt'
        running customcommand
    done
      Removing source in /tmp/pip-j57ovc7i-build
    Successfully installed hack-0.1
    

提交回复
热议问题