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文件夹中有打包后的压缩包, ...
随机推荐
- Java容器解析系列(10) Map AbstractMap 详解
前面介绍了List和Queue相关源码,这篇开始,我们先来学习一种java集合中的除Collection外的另一个分支------Map,这一分支的类图结构如下: 这里为什么不先介绍Set相关:因为很 ...
- 自定义Hook
在 class RegForm(form.Form) 中 1.验证两次密码是否相同 from django.core.exceptions import ValidationError def cle ...
- 了解box-sizing 盒子模型
最近看到别人代码有用到box-sizing属性,自己没用过,记录一下 box-sizing:border-box 指定宽度和高度(最小/最大属性)确定元素边框box 理解:假设宽高为100px,设置了 ...
- 简单gitblit与Jenkins结合,持续集成
gitblit是当作git服务器,也就是作为私有的代码仓库,用法类似于Github Jenkins 是自动构建工具,帮忙将仓库中的代码更新到服务器上.可以设置为定时自动构建. 详细摸索了我现在公司的用 ...
- TCP学习总结(四)
TCP连接管理 TCP运输连接有3个阶段, 即: 连接建立,数据传送和连接释放. 1. TCP的连接建立(3次握手) TCP连接的建立采用客户服务器方式.主动发起连接建立的应用进程叫做客户(clien ...
- 启动fiddler导致浏览器无法上网的解决方法
1. 开发fiddler,进入Tools->Fiddler Tools,按照如图3部配置,即可实现无法上网的问题. 2. 见图1: 3.见图2: 4.见图3. 4. 完成以上配置后,重启fidd ...
- 树莓派外网ssh访问holer实现篇
外网ssh访问树莓派 内网的树莓派(Raspberry Pi),只能在局域网内访问,怎样从公网也能ssh登录访问树莓派? 本文将介绍使用holer实现的具体步骤. 1. 准备工作 1.1 安装并启动树 ...
- ABP框架(asp.net core 2.X+Vue)模板项目学习之路(二)--切换MySql数据库
前言: 大家好,今天给大家带来ABP第二篇的分享,在写这篇分享的时候非常的困难,因为发现ABP的框架越是深入.难度也就越大,而且深刻感觉到自己领域驱动开发知识的欠缺,前段时间买了两本有关于领域驱动知识 ...
- centos7 十万并发 关健配置
vim /etc/sysctl.conf,添加 --开机启动加载内核参数:fs.file-max = 65535kernel.sem=250 32000 100 128 /proc/sys/kerne ...
- Prime31
https://prime31.com/plugins