自定义 distutils 命令
我有一个名为“example”的库,我正在将其安装到我的全局站点包目录中。但是,我希望能够安装两个版本,一个用于生产,一个用于测试(我有一个 Web 应用程序和其他以这种方式进行版本控制的东西)。
有没有一种方法可以指定“python setup.py stage”,它不仅可以将不同的egg安装到站点包中,而且还可以将模块从“example”重命名为“example_stage”或类似的名称?
如果 distutils 不能做到这一点,还有其他工具可以吗?
I have a library called "example" that I'm installing into my global site-packages directory. However, I'd like to be able to install two versions, one for production and one for testing (I have a web application and other things that are versioned this way).
Is there a way to specify, say "python setup.py stage" that will not only install a different egg into site-packages, but also rename the module from "example" to "example_stage" or something similar?
If distutils cannot do this, is there any other tool that can?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过在 setup.py 内部子类化 distutils.core.Command ,可以使用 distutils 轻松完成此操作。
例如:
要启用该命令,您必须在 setup() 中引用它:
请注意,您也可以通过这种方式覆盖内置命令,例如我对“clean”所做的操作。 (我不喜欢内置版本如何留下“dist”和“build”目录。)
使用了许多约定:
最好的使用示例就是查看在 PYTHON_DIR/distutils/command 中找到的默认命令之一的源代码,例如 install.py 或 build .py。
This can easily be done with distutils by subclassing distutils.core.Command inside of setup.py.
For example:
To enable the command you must reference it in setup():
Note that you can override built-in commands this way too, such as what I did with 'clean'. (I didn't like how the built-in version left behind the 'dist' and 'build' directories.)
There are a number of conventions that are used:
The best example to use is just to look at the source code for one of the default commands found at PYTHON_DIR/distutils/command such as install.py or build.py.
当然,您可以使用新命令扩展 distutils。在您的 distutil 配置文件中,添加:
这可以位于
distutils
包本身的distutils.cfg
中,也可以位于您家中的..pydistutils.cfg
中目录(Windows 上没有前导点),或当前目录中的setup.cfg
。然后,您的 Python 站点包目录中需要有一个 foo.bar 包。
然后在该包中添加实现新所需命令的类,例如
stage
,子类化 distutils.cmd - 文档很弱,但有很多示例,因为所有现有的 distutils 命令也是以这种方式构建的。Sure, you can extend distutils with new commands. In your distutil configuration file, add:
this can be in
distutils.cfg
in thedistutils
package itself,..pydistutils.cfg
in your home directory (no leading dot on Windows), orsetup.cfg
in the current directory.Then you need a foo.bar package in your Python's site-packages directory.
Then in that package you add the classes implementing your new desired commands, such as
stage
, subclassing distutils.cmd -- the docs are weak, but there are plenty of examples since all the existing distutils commands are also built that way.如果您想使用多个版本,则将 virtualenv 与 virtualenvwrapper 可以提供帮助。
If you'd like to use multiple version then virtualenv with virtualenvwrapper can help.
如果您想要一种使用 distutils 执行此操作的方法,请参阅 Alex 的答案,但我发现 < a href="http://www.blueskyonmars.com/projects/paver/" rel="nofollow noreferrer">Paver 更适合这种事情。它使创建自定义命令或覆盖现有命令变得更加容易。另外,如果您习惯使用 distutils 或 setuptools,那么转换并不是非常困难。
See Alex's answer if you want a way to do this with distutils, but I find Paver to be better for this kind of thing. It makes it a lot easier to make custom commands or override existing ones. Plus the transition isn't terribly difficult if you're used to distutils or setuptools.