简体   繁体   English

如何让VirtualEnv使用自定义版本的setuptools?

[英]How do I make VirtualEnv use a custom version of setuptools?

The large corporation that I work for uses a custom version of Setuptools. 我工作的大公司使用自定义版本的Setuptools。 This private fork of setuptools is intended to deal with certain networking and security difficulties that are unique to our organization. 这个私有的setuptools分支旨在解决我们组织特有的某些网络和安全问题。 The bottom line is that neither the standard Setuptools nor Distribute would work as expected on our environment. 最重要的是,标准的Setuptools和Distribute都不会在我们的环境中按预期工作。

Id like to start using Ian Bicking's excellent VirtualEnv tool on systems, particularly in our test systems where we need to be able to set up a large number of sandboxed areas for test-code - eg in our continuous integration environment. 我想开始在系统上使用Ian Bicking优秀的VirtualEnv工具,特别是在我们需要能够为测试代码设置大量沙盒区域的测试系统中 - 例如在我们的持续集成环境中。

Unfortunately any time I try to build a new virtual environment the virtualenv tool tries to obtain and install the latest official version of Setuptools. 不幸的是,每当我尝试构建一个新的虚拟环境时,virtualenv工具都会尝试获取并安装最新的Setuptools 官方版本。 This would fail for the reason stated above, and also because the corporate firewall would block the action. 由于上述原因,这将失败,并且还因为公司防火墙会阻止该操作。

Instead of installing the official version: 而不是安装正式版:

setuptools-0.6c11-py2.4.egg

I'd like to install our customized version which might be called something like: 我想安装我们的定制版本,可能会被称为:

setuptools-foo-0.6c11-py2.4.egg

This egg can always be guaranteed to be found in the system's global site-packages. 始终可以保证在系统的全局站点包中找到此蛋。 I can also guarantee that it's present in all of our corporate egg servers. 我还可以保证它存在于我们所有的公司鸡蛋服务器中。

Can you help me make my virtualenv use my customized setuptools instead of the regular version of setuptools. 你能帮助我让我的virtualenv使用我的自定义setuptools而不是常规版本的setuptools。

The name is hardcoded in virtualenv.py. 该名称在virtualenv.py中是硬编码的。 You have to either patch virtualenv.py or name your patched setuptools egg 'setuptools-0.6c11-py2.4.egg' 您必须修补virtualenv.py或命名修补的setuptools egg'settingtools-0.6c11-py2.4.egg'

I've taken to writing my own wrapper scripts which import virtualenv. 我已经开始编写自己的包装脚本,导入virtualenv。 The main reason is that I use dpkgs to install most of my dependencies, including distribute , so I like to avoid downloading additional copies when I create a new environment - this has a bonus that it runs much faster. 主要原因是我使用dpkgs来安装我的大多数依赖项,包括distribute ,所以我想在创建一个新环境时避免下载额外的副本 - 这样可以让它运行得更快。

Here is a baseline wrapper you can use to start with. 这是一个可以用来开始的基线包装器。 I've added a comment where you could insert some code to symlink/copy your custom setuptools code into the virtualenv: 我添加了一个注释,您可以在其中插入一些代码以将自定义setuptools代码符号链接/复制到virtualenv:

import os, subprocess, sys, virtualenv

# virtualenv changed its internal api slightly after 1.5. 
NEW_API = (1, 5)

def get_version(version):
    return tuple([int(v) for v in version.split('.')])

def main():
    # set the logging level here
    level = virtualenv.Logger.level_for_integer(0)
    logger = virtualenv.Logger([(level, sys.stdout)])
    virtualenv.logger = logger

    # insert your command-line parsing code here, if needed
    root = sys.argv[1]

    home, lib, inc, bin = virtualenv.path_locations(root)
    result = virtualenv.install_python(home, lib, inc, bin,
            site_packages=True, clear=False)
    pyexec = os.path.abspath(result)
    version = get_version(virtualenv.virtualenv_version)
    if version < NEW_API:
        virtualenv.install_distutils(lib, home)
    else:
        virtualenv.install_distutils(home)
    virtualenv.install_activate(home, bin)

    # insert whatever post-virtualenv-setup code you need here 

if __name__ == '__main__':
    main()

Usage: 用法:

% python wrapper.py [path]

There's the option --extra-search-dir that allows to define a local directory containing the desired version of setuptools. 有一个选项--extra-search-dir ,允许定义包含所需版本的setuptools的本地目录。 This is explained in the docs . 这在文档中有解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM