转自:https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/

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:

  1. Implement command as a subclass of distutils.cmd.Command;
  2. Add the newly defined command class to the cmdclass argument to setup().

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:

  1. Create a subclass of setuptools.command.build_py.build_py (or distutils.command.build_py.build_py if using distutils) that invokes our new Pylint command;
  2. Add the newly defined command class to the cmdclass argument to setup().

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的更多相关文章

  1. Add custom daemon on Linux System

    Ubuntu add custom service(daemon) Task 需要在系统启动的时候自动启动一个服务(后台程序),在系统关闭的时候关闭服务. 比如在部署某个应用之前,需要将某个任务设置成 ...

  2. [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 ...

  3. 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 ...

  4. 配置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 ...

  5. 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 ...

  6. 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 ...

  7. Add custom field in Material Master

    1.Add fields in the Append Structure of table MARA. 2.Configure SPRO IMG -> Logistics General -&g ...

  8. grafana add custom dashboard

    grafana-dashboard-json prometheus-operator helm 中的grafana dashboard 扩展的时候,需要转换下载(https://grafana.com ...

  9. 【mlflow】打包:npm run build + python setup.py sdist

    mlflow是一个开源机器学习平台 最近需要使用一个它的最新版本,但是这个最新版本没有git包,无法通过pip install安装,需要打包安装. 打包完之后在项目的dist文件夹中有打包后的压缩包, ...

随机推荐

  1. C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去

    C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去 Form settingForm = new Form(); setForm deviceSet ...

  2. Flask项目笔记

    一.jsonify  jsonify 是flask的函数,可以将字典转换成json数据返回给浏览器二. 钩子函数 @app.before_first_request:第一次请求调用,用于初始化数据 @ ...

  3. linux安装mysql(shell一键安装)

    1. 相关文件(install_mysql.sh.my.cnf.mysqld相关内容在文中最后面) 2. 将上面的文件上传到linux服务器某一目录下 3.给install_mysql.sh赋执行权限 ...

  4. 如何用ESP8266实现网页配置(web)

    准备工作 准备一个深圳四博智联科技有限公司的ESP-F 模组.或者四博智联科技的NODEMCU 当我们拿到ESP-F模块后,可以按照以下接线进行测试: 即 VCC.EN 接 3.3v.GPIO15 G ...

  5. 问题1:Oracle数据库监听启动失败(重启监听,提示The listener supports no services)

    编辑监听文件:/home/DB/oracle/11gR2/db/network/admin/listener.ora 在文件内添加静态监听实例,如下内容: SID_LIST_LISTENER =(SI ...

  6. 阻止事件冒泡传播stopPropagation() 阻止自身默认行为preventdefault()

    stopPropagation       简单理解:子元素的点击事件  不会去触发父元素的点击事件 preventdefault       简单理解:当点击提交按钮时(submit)   阻止对表 ...

  7. better-scroll无法滚动的问题。

    better-scroll无法滚动的问题.1遇见better-scroll(以下简称:BS)无法滚动,可从两方面去考虑.一是层级关系出错,二是计算高度出错.###1,层级关系BS的基本结构是:一个wr ...

  8. Spring动态获取已注入的对象的方法

    1.根据类获取对象 @Autowired ApplicationContext context; GenericMapper<T,String> dao=(GenericMapper< ...

  9. sql server 字符串根据指定分隔符进行分组

    SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO --功能:分拆字符串 --参数:@String:要分拆的字符串:@Delimiter:分割符号 --返回值: ...

  10. Java学习笔记(7)

    File类用于处理文件和目录 isDirectory()用于检查当前对象是否目录,isFile()用于检查当前对象是否文件 构造File对象时,传入的路径不一定要求存在,要通过exists()方法判断 ...