向setup.py里添加自定义command
向setup.py里添加自定义command
参考这里
继承distutils.cmd.Command类:
class CCleanCommand(distutils.cmd.Command):
"""clean the source code and the .c file after building .so"""
# 命令的描述,会出现在`python setup.py --help`里
description = 'clean the source code and the .c file after building .so'
# 该命令的所有选项,包括需要给值的选项,以及只是当个flag用的binary选项
user_options = [
# 格式是`(长名字,短名字,描述)`,描述同样会出现在doc里
# binary选项,长名字后面没有等号,最后的值会传给`self.<长名字>`,使用形式`--restore`/`-r`
('restore', 'r', 'moving the source back to its dir'),
# 需要值的选项,长名字后面有等号,最后的值会传给`self.<长名字>`(-会用_代替),使用形式`--key-value=<val>`/`-k <val>`
('key-value=', 'k', 'example for key=value option')
]
def initialize_options(self):
"""设置选项的默认值, 每个选项都要有初始值,否则报错"""
self.restore = False
self.key_value = 123
def finalize_options(self):
"""接收到命令行传过来的值之后的处理, 也可以什么都不干"""
print("restore=", self.restore)
print("key_value=", self.key_value)
self.key_value = f'{self.key_value}'+".exe"
def run(self):
"""命令运行时的操作"""
print("======= command is running =======")
if self.restore:
print('good')
else:
print(self.key_value)
然后需要在setup函数里设置好:
setup(
...,
cmdclass={
'cclean': CCleanCommand
}
)
然后就可以通过python setup.py cclean来使用了:
>>> python setup.py cclean
running cclean
restore= False
key_value= 123
======= command is running =======
123.exe
>>> python setup.py cclean -r
running cclean
restore= 1
key_value= 123
======= command is running =======
good
>>> python setup.py cclean --restore
running cclean
restore= 1
key_value= 123
======= command is running =======
good
>>> python setup.py cclean --restore=1
报错
>>> python setup.py cclean --key-value=456
running cclean
restore= False
key_value= 456
======= command is running =======
456.exe
>>> python setup.py cclean -k 456
running cclean
restore= False
key_value= 456
======= command is running =======
456.exe
>>> python setup.py cclean -k=456
running cclean
restore= False
key_value= =456
======= command is running =======
=456.exe
向setup.py里添加自定义command的更多相关文章
- 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 scrip ...
- python包管理(distutils、easy_install、pip、setup.py/requirements.txt、wheel)
distutils.distutils2 distutils是 python 标准库的一部分,2000年发布.使用它能够进行 python 模块的 安装 和 发布. distutils2 被设计为 d ...
- Command "python setup.py egg_info" failed with error code 10
1:今天系统重装以后,下载了新的版本的python3.6.1.然后想通过pycurl模块测试URL,突然发现windows10下我无法通过pip安装pycurl模块了,报错内容如下 Collectin ...
- Command "python setup.py egg_info" failed with error code 1一种问题的解决方法
问题描述:无论是你在pycharm中直接使用import and install命令,还是pip的时候出现了Command "python setup.py egg_info" f ...
- Command "python setup.py egg_info" failed with error code 1 in C:\Users\w5659\AppData\Local\Temp\pip-install-t7uomu4r\xa dmin\
Error msg: C:\Users\w5659>pip install xadmin Collecting xadmin Using cached https://files.pythonh ...
- pip安装mysql-python报错:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-enRreC/mysql-python/
公司业务开发,用python开发网站;需要使用模块MySQLdb. 我直接pip install MySQLdb,当然不成功了,模块名字因该是mysql-python pip install mysq ...
- python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/
公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...
- Command "python setup.py egg_info" failed with error code 1 in c:\users\w5659\appdata\local\temp\pip-build-fs2yzl\ipython\
Error Msg: Collecting ipython Using cached https://files.pythonhosted.org/packages/5b/e3/4b3082bd7f6 ...
- 解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYSQL-python/
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYS ...
随机推荐
- 「Spark从精通到重新入门(一)」Spark 中不可不知的动态优化
前言 Apache Spark 自 2010 年面世,到现在已经发展为大数据批计算的首选引擎.而在 2020 年 6 月份发布的Spark 3.0 版本也是 Spark 有史以来最大的 Release ...
- R语言学习记录(一)
(R基础) 对象:什么是对象呢,其实就是一个名称而已,在R中存储的数据 就是一个R对象 a <- 1 ###其中'<-'表示的是一个赋值符号 这句话表示的是,将1赋值给a b <- ...
- express系列(1)概述
在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...
- 容器之分类与各种测试(三)——stack
stack是栈,其实现也是使用了双端队列(只要不用双端队列的一端,仅用单端数据进出即完成单端队列的功能),由于queue和stack的实现均是使用deque,没有自己的数据结构和算法,所以这俩也被称为 ...
- 【Android】修改快捷键,前一步默认是Ctrl + Z,修改后一步
我已经忘了,我什么时候已经习惯前一步是Ctrl + Z,后一步是Ctrl + Y Android Studio默认前一步快捷键是相同的,但是后一步就不是了 Ctrl + Y变成删除一行代码,就是下图D ...
- 隐藏状态栏后tableview自动上移20个像素的问题
最近在开发过程中碰到一个很奇怪的问题,将状态栏隐藏掉之后,页面上的tableView会自动上移20个像素. 这是因为在iOS7.0之后,系统会自动调整scrollView的layout 和 conte ...
- 【Linux】【Services】【Project】Cobbler自动化装机
1. 概念 1.1. Cobbler 1.2. PXE 1.3. 2. 版本信息 2.1. OS:Red Hat Enterprise Linux Server release 7.3 (Maipo) ...
- 最基础前端路由实现,事件popstate使用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【C#】【MySQL】【GridView】删除出现Parameter index is out of range
[编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...
- 密码学之Hash散列
一.简介 hash(散列.杂凑)函数,是将任意长度的数据映射到有限长度的域上. 直观解释起来,就是对一串数据m进行杂糅,输出另一段固定长度的数据h,作为这段数据的特征(指纹).也就是说,无论数据块m有 ...