python 自定义 distutils 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1710839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:55:18  来源:igfitidea点击:

Custom distutils commands

pythondeploymentdistutils

提问by dave paola

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

我有一个名为“example”的库,我将它安装到我的全局站点包目录中。但是,我希望能够安装两个版本,一个用于生产,一个用于测试(我有一个 Web 应用程序和其他以这种方式进行版本控制的东西)。

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?

有没有办法指定,比如说“python setup.py stage”,它不仅会在站点包中安装一个不同的蛋,而且还会将模块从“example”重命名为“example_stage”或类似的东西?

If distutils cannot do this, is there any other tool that can?

如果 distutils 不能做到这一点,还有其他工具可以吗?

采纳答案by Alex Martelli

Sure, you can extend distutils with new commands. In your distutil configuration file, add:

当然,您可以使用新命令扩展 distutils。在您的 distutil 配置文件中,添加:

 [global]
 command-packages=foo.bar

this can be in distutils.cfgin the distutilspackage itself, ..pydistutils.cfgin your home directory (no leading dot on Windows), or setup.cfgin the current directory.

这可以在distutils.cfgdistutils包装本身,..pydistutils.cfg在你的主目录(无领导点在Windows上),或setup.cfg在当前目录。

Then you need a foo.bar package in your Python's site-packages directory.

然后你需要在你的 Python 的 site-packages 目录中有一个 foo.bar 包。

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.

然后在该包中添加实现新所需命令的类,例如stage,子类化distutils.cmd—— 文档很弱,但有很多示例,因为所有现有的 distutils 命令也是以这种方式构建的。

回答by jathanism

This can easily be done with distutils by subclassing distutils.core.Commandinside of setup.py.

这可以很容易地通过继承与distutils的做distutils.core.Commandsetup.py内。

For example:

例如:

from distutils.core import setup, Command
import os, sys

class CleanCommand(Command):
    description = "custom clean command that forcefully removes dist/build directories"
    user_options = []
    def initialize_options(self):
        self.cwd = None
    def finalize_options(self):
        self.cwd = os.getcwd()
    def run(self):
        assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd
        os.system('rm -rf ./build ./dist')  

To enable the command you must reference it in setup():

要启用该命令,您必须在 setup() 中引用它:

setup(
     # stuff omitted for conciseness.
     cmdclass={
        'clean': CleanCommand
}

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

请注意,您也可以通过这种方式覆盖内置命令,例如我对“clean”所做的。(我不喜欢内置版本如何留下“dist”和“build”目录。)

% python setup.py --help-commands | grep clean
  clean            custom clean command that forcefully removes dist/build dirs.

There are a number of conventions that are used:

有许多使用的约定:

  • You specify any command-line arguments with user_options.
  • You declare any variables you would use with the initialize_options()method, which is called after initialization to setup your custom namespace for the subclass.
  • The finalize_options()method is called right before run().
  • The guts of the command itself will occur in run()so be sure to do any other prep work before that.
  • 您可以使用user_options指定任何命令行参数。
  • 您可以通过initialize_options()方法声明您将使用的任何变量,该方法在初始化后被调用以设置您的子类的自定义命名空间。
  • finalize_options()方法被调用之前正确的run()
  • 命令本身的内容将发生在run() 中,因此请确保在此之前进行任何其他准备工作。

The best example to use is just to look at the source code for one of the default commands found at PYTHON_DIR/distutils/commandsuch as install.pyor build.py.

最好的例子就是查看在PYTHON_DIR/distutils/command 中找到的默认命令之一的源代码,例如install.pybuild.py

回答by jfs

If you'd like to use multiple version then virtualenvwith virtualenvwrappercan help.

如果您想使用多个版本,那么的virtualenvvirtualenvwrapper可以提供帮助。

回答by Jason Baker

See Alex's answerif you want a way to do this with distutils, but I find Paverto 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.

如果你想用 distutils 来做这件事,请参阅Alex 的回答,但我发现Paver更适合这种事情。它使创建自定义命令或覆盖现有命令变得更加容易。另外,如果您习惯了 distutils 或 setuptools,转换并不是非常困难。