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
cmdclassargument 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_pyif usingdistutils) that invokes our new Pylint command; - Add the newly defined command class to the
cmdclassargument 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
How To Add Custom Build Steps and Commands To setup.py的更多相关文章
- Add custom daemon on Linux System
Ubuntu add custom service(daemon) Task 需要在系统启动的时候自动启动一个服务(后台程序),在系统关闭的时候关闭服务. 比如在部署某个应用之前,需要将某个任务设置成 ...
- [webgrid] – header - (How to Add custom html to Header in WebGrid)
How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...
- Add custom and listview web part to a page in wiki page using powershell
As we know, Adding list view web part is different from custom web part using powershell, what's mor ...
- 配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XXXXX' to ALLOWED_HOSTS,在setting.py中添加‘*”无效的原因
配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XX ...
- Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations
In a previous post we learned how we can use the inputs and outputs properties to set properties or ...
- Gradle Goodness: Add Incremental Build Support to Tasks
Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...
- Add custom field in Material Master
1.Add fields in the Append Structure of table MARA. 2.Configure SPRO IMG -> Logistics General -&g ...
- grafana add custom dashboard
grafana-dashboard-json prometheus-operator helm 中的grafana dashboard 扩展的时候,需要转换下载(https://grafana.com ...
- 【mlflow】打包:npm run build + python setup.py sdist
mlflow是一个开源机器学习平台 最近需要使用一个它的最新版本,但是这个最新版本没有git包,无法通过pip install安装,需要打包安装. 打包完之后在项目的dist文件夹中有打包后的压缩包, ...
随机推荐
- Application 、Cookie和 Session 两种会话有什么不同
Application储存在服务端,没有时间限制,服务器关闭即销毁 Session存储在服务端,客户端,关闭即销毁(长时间不使用,且浏览器未关闭,默认自动销毁时间是20分钟) Cookie储存在客户端 ...
- ecplise包的层次结构选择
ecplise包的层次结构选择 平坦方式: 分层方式:
- LIMIT用法
select * from employees order by hire_date DESC LIMIT 0,3; 直接给语句说明:根据hire_date 降序排列,LIMIT 第一个参数表示从第几 ...
- SharePoint Framework解决方案管理参考(一)
博客地址:http://blog.csdn.net/FoxDave 使用SPFx,你的企业可以轻松构建解决方案跟Office 365和SharePoint Online集成.SPFx解决方案基于现代w ...
- 解决sublime text 3使用Install Package时出现There are no packages available for installation问题
package control一直弹出There are no packages available for installation,由于国内环境屏蔽了https://packagecontrol. ...
- Web开发常见的几个漏洞解决方法 (转)
基本上,参加的安全测试(渗透测试)的网站,可能或多或少存在下面几个漏洞:SQL注入漏洞.跨站脚本攻击漏洞.登陆后台管理页面.IIS短文件/文件夹漏洞.系统敏感信息泄露. 1.测试的步骤及内容 这些安全 ...
- 三星Galaxy S8 刷机经验记录
这段时间用上了三星S8,由于原生系统太耗电,所以萌生了root的想法.写这篇博客记录下这段时间的各种尝试. Root过程说明: 友情提示,道路千万条,安全第一条.开始捣鼓手机之前请一定准备好官方的救砖 ...
- 免费的DDos网络测试工具集合
今天晚上看YT上的hulk VS monster Dogs 然后想看电影资源,给我推送了hulk这款工具了解下,发现了一些东西,收藏下 1.卢瓦(LOIC) (Low Orbit Ion Canon) ...
- DevOps 开源工具
1. 开发工具 版本控制&协作开发 版本控制系统 Git Git 是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.开源中国 Git 代码托管平台:http:/ ...
- ng2 配置端口号
ng2 默认端口号4200 若要配置,用两种方法 (1)可以使用以下命令 ng server --port 4201 (2)找到node_modules/angular-cli/lib/confi ...