转自:https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/
A setup.py
script using distutils
/ setuptools
is the standard way to package Python code. Often, however, we need to perform custom actions for code generation, running tests, profiling, or building documentation, etc., and we’d like to integrate these actions into setup.py. In other words, we’d like to add custom steps to setup.py build
or setup.py install
, or add a new command altogether to setup.py
.
Let’s see how this is done.
Adding Custom setup.py
Commands and Options
Let’s implement a custom command that runs Pylint on all Python files in our project. The high level idea is:
- Implement command as a subclass of
distutils.cmd.Command
; - Add the newly defined command class to the
cmdclass
argument tosetup()
.
To see this in action, let’s add the following to our setup.py
:
import distutils.cmd
import distutils.log
import setuptools
import subprocess
class PylintCommand(distutils.cmd.Command):
"""A custom command to run Pylint on all Python source files."""
description = 'run Pylint on Python source files'
user_options = [
# The format is (long option, short option, description).
('pylint-rcfile=', None, 'path to Pylint config file'),
]
def initialize_options(self):
"""Set default values for options."""
# Each user option must be listed here with their default value.
self.pylint_rcfile = ''
def finalize_options(self):
"""Post-process options."""
if self.pylint_rcfile:
assert os.path.exists(self.pylint_rcfile), (
'Pylint config file %s does not exist.' % self.pylint_rcfile)
def run(self):
"""Run command."""
command = ['/usr/bin/pylint']
if self.pylint_rcfile:
command.append('--rcfile=%s' % self.pylint_rcfile)
command.append(os.getcwd())
self.announce(
'Running command: %s' % str(command),
level=distutils.log.INFO)
subprocess.check_call(command)
setuptools.setup(
cmdclass={
'pylint': PylintCommand,
},
# Usual setup() args.
# ...
)
Now, running python setup.py --help-commands
will show:
Standard commands:
...
Extra commands:
pylint: run Pylint on Python source files
...
We can now run the command we just defined with:
$ python setup.py pylint
…or with a custom option:
$ python setup.py pylint --pylint-rcfile=.pylintrc
To learn more, you can check out documentation on inheriting from distutils.cmd.Command
as well as the source code of some built-in commands, such as build_py
.
Adding Custom Steps to setup.py build
Let’s say we are really paranoid about code style and we’d like to run Pylint as part of setup.py build
. We can do this in the following manner:
- Create a subclass of
setuptools.command.build_py.build_py
(ordistutils.command.build_py.build_py
if usingdistutils
) that invokes our new Pylint command; - Add the newly defined command class to the
cmdclass
argument tosetup()
.
For example, we can implement the following in our setup.py
:
import setuptools.command.build_py
class BuildPyCommand(setuptools.command.build_py.build_py):
"""Custom build command."""
def run(self):
self.run_command('pylint')
setuptools.command.build_py.build_py.run(self)
setuptools.setup(
cmdclass={
'pylint': PylintCommand,
'build_py': BuildPyCommand,
},
# Usual setup() args.
# ...
)
For more examples, I encourage you to check out the setuptools