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

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

  2. python包管理(distutils、easy_install、pip、setup.py/requirements.txt、wheel)

    distutils.distutils2 distutils是 python 标准库的一部分,2000年发布.使用它能够进行 python 模块的 安装 和 发布. distutils2 被设计为 d ...

  3. Command "python setup.py egg_info" failed with error code 10

    1:今天系统重装以后,下载了新的版本的python3.6.1.然后想通过pycurl模块测试URL,突然发现windows10下我无法通过pip安装pycurl模块了,报错内容如下 Collectin ...

  4. Command "python setup.py egg_info" failed with error code 1一种问题的解决方法

    问题描述:无论是你在pycharm中直接使用import and install命令,还是pip的时候出现了Command "python setup.py egg_info" f ...

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

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

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

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

  9. 解决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 ...

随机推荐

  1. deque、queue和stack深度探索(上)

    deque是可双端扩展的双端队列,蓝色部分就是它的迭代器类,拥有四个指针,第一个cur用来指向当前元素,first指向当前buffer头部,last指向当前buffer尾部,node指向map自己当前 ...

  2. ORACLE 服务器验证

    位于$ORACLE_HOME/network/admin/sqlnet.oraSQLNET.AUTHENTICATION_SERVICES=none|all|ntsnone:关闭操作系统认证,只能密码 ...

  3. Swift Storyboard找不到类文件

    Swift语言引入了Module概念,在通过关键字@objc(类名)做转换的时候,由于Storyboard没有及时更新Module属性,会导致如下两种类型错误: 1 用@objc(类名)标记的Swif ...

  4. Android消除Toast延迟显示

    Toast可以用来显示音量改变或者保存更新消息,如果用户一直点击,Toast会排队一个一个的,直到消息队列全部显示完,这样的效果显然是不好的,下面来看解决方法    Toast.makeText(ac ...

  5. Data Calendar

    1.Date对象 Date类在java.util包中.使用Date类的无参数构造方法创建的对象可以获取本地当前时间. 用Date的构造方法Date(long time)创建的Date对象表 示相对19 ...

  6. Linux:变量$#,$@,$0,$1,$2,$*,$$,$?

    写一个简单的脚本 vim var 脚本内容如下: #!/bin/sh echo "the number of parameters passed to the script: $#" ...

  7. Linux:sqlplus

    [oracle@hb shell_test]$ cat echo_time #!/bin/sh 一.最简单的调用sqlplus sqlplus -S "sys/unimas as sysdb ...

  8. ES6(模板字符串,三点运算符,Symbol,iterator接口)

    模板字符串 作用:简化字符串的拼接 模板字符串必须用``包含 变化的部分使用${xxx}包含 对象的简写方式 同名的属性可以省略不写 可以省略函数的function 箭头函数 箭头函数的特点 箭头函数 ...

  9. 【Java 基础】Java Enum

    概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承jav ...

  10. uni-app使用腾讯地图注意点

    地图map组件使用腾讯地图自定义样式: 1:在使用地图map组件腾讯地图时,获取本地定位,经纬度转地址与地址转经纬度解析时,小程序可以直接使用.但是h5版本会报跨域问题,目前前端没有找到更好的解决方法 ...