如何用简单的后安装脚本扩展distutils?
我需要在模块和程序安装完成后运行一个简单的脚本。可是我发现找不到简单明了的说明来告诉我该怎么做。看起来我需要从distutils.command.install这个东西继承,重写一些方法,然后把这个对象加到设置脚本里。不过具体的细节有点模糊,而且为了这么简单的事情,感觉要花很多力气。有没有人知道更简单的方法可以做到这一点?
4 个回答
8
我没能让Joe Wreschnig的回答正常工作,于是我根据扩展distutils的文档对他的回答进行了调整。我写出了这段代码,在我的电脑上运行得很好。
from distutils import core
from distutils.command.install import install
...
class my_install(install):
def run(self):
install.run(self)
# Custom stuff here
# distutils.command.install actually has some nice helper methods
# and interfaces. I strongly suggest reading the docstrings.
...
distutils.core.setup(..., cmdclass={'install': my_install})
注意:我没有修改Joe的回答,因为我不确定为什么他的回答在我的电脑上不管用。
19
好的,我搞明白了。其实就是扩展其中一个distutils的命令,然后重写它的运行方法。要告诉distutils使用这个新类,你可以用cmdclass这个变量。
from distutils.core import setup
from distutils.command.install_data import install_data
class post_install(install_data):
def run(self):
# Call parent
install_data.run(self)
# Execute commands
print "Running"
setup(name="example",
cmdclass={"install_data": post_install},
...
)
希望这能帮助到其他人。
39
我花了一整天时间研究distutils的源代码,想了解足够多的内容来创建一些自定义命令。虽然过程不太好看,但最终是有效的。
import distutils.core
from distutils.command.install import install
...
class my_install(install):
def run(self):
install.run(self)
# Custom stuff here
# distutils.command.install actually has some nice helper methods
# and interfaces. I strongly suggest reading the docstrings.
...
distutils.core.setup(..., cmdclass=dict(install=my_install), ...)