【问题标题】:Default server in multiple server configuration of distutils in ~/.pypirc~/.pypirc 中 distutils 的多服务器配置中的默认服务器
【发布时间】:2011-07-24 23:01:09
【问题描述】:

我想在我的~/.pypirc 文件中拥有多个 PyPI 服务器,以便我可以轻松发布到不同的服务器,具体取决于项目。

我的用例是这样的,我有一些内部项目想要发布到内部 PyPI 服务器 (https://pypi.internal),并且我有一些公共项目想要发布到公共 PyPI。

这是我目前的尝试,但它不起作用。我想默认为internal,如果我想发布到公共服务器,需要添加-r pypi(到setup.py 命令)。

[distutils]
index-servers =
    internal
    pypi

[internal]
repository: https://pypi.internal
username: brad

[pypi]
username: brad

我哪里错了?

【问题讨论】:

    标签: python distutils pypi


    【解决方案1】:

    奇怪的是没有内置支持设置默认值,但这里有两个选项可以帮助您解决它。

    选项 1: 最简单的解决方案可能是保留您的 ~/.pypirc 脚本原封不动,并为您的内部和公共上传创建 shell 别名。这可以让您更好地控制工作流程的自定义内容。

    鉴于此 .pypirc 文件:

    [distutils]
    index-servers =
        pypi
        internal
    
    [pypi]
    repository: http://pypi.python.org/pypi
    username: brad
    password: <pass>
    
    [internal]
    repository: http://localhost:8080
    username: brad
    password: <pass>
    

    创建一些 shell 别名(将这些定义放在 shell 的 rcfile 中,例如 ~/.bashrc):

    alias ppup_internal='python setup.py bdist_egg sdist upload -r internal'
    alias ppup_public='python setup.py bdist_egg sdist upload'
    

    用法:

    % ppup_internal
    ...
    running upload
    Submitting dist/foo-0.0.0.tar.gz to http://localhost:8080
    Server response (200): OK
    

    选项 2: 小技巧:您可以通过修补默认值来解决默认值 setup.py 脚本顶部的存储库名称。

    from distutils import config
    config.PyPIRCCommand.DEFAULT_REPOSITORY = 'internal'
    from setuptools import setup
    
    setup(
        name='foo',
        ...
    

    输出:

    % python setup.py sdist upload 
    ...
    running upload
    Submitting dist/foo-0.0.0.tar.gz to http://localhost:8080
    Server response (200): OK
    
    % python setup.py sdist upload -r pypi
    ...
    running upload
    Submitting dist/foo-0.0.0.tar.gz to http://pypi.python.org/pypi
    Server response (200): OK
    

    背景:如果在.pypirc中定义[distutils]键,上传命令默认为pypi url 当 -r [repo] 参数被省略时。相关代码在distutils.config.PyPIRCCommand

    class PyPIRCCommand(Command):
    
        DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
    
        def _read_pypirc(self):
            if os.path.exists(rc):
                self.announce('Using PyPI login from %s' % rc)
                repository = self.repository or self.DEFAULT_REPOSITORY
                realm = self.realm or self.DEFAULT_REALM
    

    .pypirc 的旧格式需要一个 [server-login] 部分,因为它只定义了一个目标存储库,所以灵活性要差得多。这不是一个可行的选项,因为下面的 [pypi] 部分将无法使用:

    [server-login]
    repository: http://localhost:8080
    username: brad
    password: <pass>
    
    [pypi]
    repository: http://pypi.python.org/pypi
    username: brad
    password: <pass>
    

    现在 distutils 默认使用这个目标:

    % python setup.py sdist upload
    ...
    running upload
    Submitting dist/foo-0.0.0.tar.gz to http://localhost:8080
    Server response (200): OK    
    

    但您无法访问任何其他存储库:它默认为 [server-login] 属性:

    % python setup.py sdist upload -r pypi
    ...
    running upload
    Submitting dist/foo-0.0.0.tar.gz to http://localhost:8080
    Server response (200): OK    
    

    【讨论】:

    • 感谢您为此付出了额外的努力。不支持它是非常令人失望的。 config.PyPIRCCommand.DEFAULT_REPOSITORY = 'internal' 有点混乱,是指.pypirc 中的某个部分,还是指向服务器的URL?
    • 是的,我认为最好列出我能找到的所有选项。字符串 internal 是 .pypirc 中您希望成为默认值的部分的名称。因此,在您的 .pypirc 中,您将本地存储库命名为 [internal],以便 setup.py 中的行将猴子修补默认为 internal
    • 哎呀,我没有意识到我必须单独奖励赏金,你去吧。
    【解决方案2】:

    一个类似的问题是当你想要一个内部的 PyPI 包存储库来安装时。在这种情况下,使用pip install -e . 而不是python setup.py develop 完全避开了这个问题。见Can I use `pip` instead of `easy_install` for `python setup.py install` dependency resolution?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 2010-11-10
      • 2016-03-24
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多