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. day25 组合和内置函数

    day25 组合和内置函数 一.组合 # 解决类与类之间代码冗余问题: 1. 继承 2. 组合 组合:一个对象拥有一个属性, 属性的值必须是另外一个对象 继承满足的是:什么是什么的关系 # is-a ...

  2. Hadoop 相关知识点(一)

    作业提交流程(MR执行过程) Mapreduce2.x Client:用来提交作业 ResourceManager:协调集群上的计算资源的分配 NodeManager:负责启动和监控集群上的计算容器( ...

  3. WebService学习总览

    [1]WebService简介 https://blog.csdn.net/xtayfjpk/article/details/12256663 [2]CXF中Web服务请求处理流程 https://b ...

  4. sonic 安装记录

    https://github.com/valeriansaliou/sonic $ rustc --versionrustc 1.50.0-dev ubantu环境 rocksdb 安装依赖 apt ...

  5. JDBCUtils工具类的属性

    package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...

  6. 【React】组件书写记录

    时钟组件: 组件形式:数字时钟 https://blog.csdn.net/hahahahahahahaha__1/article/details/80688920 Javascript获取时间方法: ...

  7. 【简】题解 AWSL090429 【聚会】

    这题直接换根dp 记录在要转移的点的子树中有多少牛 #include<bits/stdc++.h> using namespace std; #define ll long long #d ...

  8. 沉淀vue相关知识(主要还是个人积累用)

    路由懒加载的配置: const Home= () =>import('../components/Home') //使用ES6中的路由懒加载的方式 const About= () =>im ...

  9. 度量驱动的DevOps实现

    目录 一.简介 二.度量是什么 三.实践 四.QA问答 一.简介 Wiki上讲:DevOps(Development和Operations的组合词)是一种重视"软件开发人员(Dev)&quo ...

  10. Pytorch入门中 —— 搭建网络模型

    本节内容参照小土堆的pytorch入门视频教程,主要通过查询文档的方式讲解如何搭建卷积神经网络.学习时要学会查询文档,这样会比直接搜索良莠不齐的博客更快.更可靠.讲解的内容主要是pytorch核心包中 ...