How To Add Custom Build Steps and Commands To setup.py
转自: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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2014-04-24 oracel SQL多表查询优化
2014-04-24 sql 语句执行顺序