简体   繁体   English

〜/ .pypirc中distutils的多服务器配置中的默认服务器

[英]Default server in multiple server configuration of distutils in ~/.pypirc

I want to have multiple PyPI servers in my ~/.pypirc file so I can easily publish to different servers, depending on the project. 我希望在~/.pypirc文件中有多个PyPI服务器,这样我就可以轻松地发布到不同的服务器,具体取决于项目。

My use-case is this, I have some internal projects that I want to publish to an internal PyPI server ( https://pypi.internal ), and I have some public projects that I want to publish to the public PyPI. 我的用例是这样的,我有一些内部项目,我想发布到内部PyPI服务器( https://pypi.internal ),我有一些公共项目,我想发布到公共PyPI。

This is my current attempt, but it doesn't work. 这是我目前的尝试,但它不起作用。 I want to default to internal , and be required to add the -r pypi (to the setup.py command) if I want to publish to the public server. 我想默认为internal ,如果我想发布到公共服务器,则需要添加-r pypi (到setup.py命令)。

[distutils]
index-servers =
    internal
    pypi

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

[pypi]
username: brad

Where am I going wrong? 我哪里错了?

It's strange that there isn't built-in support for setting a default, but here are two options which may help you work around it. 奇怪的是,没有内置支持来设置默认值,但这里有两个选项可以帮助您解决它。

Option 1: Probably the simplest solution would be to leave your ~/.pypirc script intact and create shell aliases for your internal and public uploads. 选项1:最简单的解决方案可能是保留〜/ .pypirc脚本,并为内部和公共上传创建shell别名。 This may give you more control over customizing things for your workflow. 这可以让您更好地控制工作流程的自定义内容。

Given this .pypirc file: 鉴于此.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>

Create some shell aliases (place these definitions in your shell's rcfile, eg ~/.bashrc ): 创建一些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'

Usage: 用法:

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

Option 2: A hack: you can work around the default by patching the default repository name at the top of your setup.py scripts. 选项2:黑客攻击:您可以通过修补setup.py脚本顶部的默认存储库名称来解决默认问题。

from distutils import config
config.PyPIRCCommand.DEFAULT_REPOSITORY = 'internal'
from setuptools import setup

setup(
    name='foo',
    ...

Output: 输出:

% 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

Background: If you define the [distutils] key in .pypirc , the upload command defaults to the pypi url when the -r [repo] argument is omitted. 背景:如果在.pypirc中定义[distutils]键,则在省略-r [repo]参数时,upload命令默认为pypi url The relevant code is in distutils.config.PyPIRCCommand : 相关代码位于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

The old format of .pypirc expected a [server-login] section, which was far less flexible since it only defines a single target repository. .pypirc的旧格式需要一个[server-login]部分,因为它只定义了一个目标存储库,所以它的灵活性要低得多。 This isn't a workable option since the [pypi] section below will be unusable: 这不是一个可行的选项,因为下面的[pypi]部分将无法使用:

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

[pypi]
repository: http://pypi.python.org/pypi
username: brad
password: <pass>

Now by default distutils will use this target: 现在默认情况下,distutils将使用此目标:

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

But you can't access the any other repos: it silently defaults to the [server-login] properties: 但是您无法访问任何其他存储库:它默认默认为[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    

A similar problem is when you want to have an internal PyPI package repository to install from. 类似的问题是当您想要安装内部PyPI包存储库时。 In this scenario using pip install -e . 在这种情况下使用pip install -e . rather than python setup.py develop completely side-steps this problem. 而不是python setup.py develop完全侧面步骤这个问题。 See Can I use `pip` instead of `easy_install` for `python setup.py install` dependency resolution? 请参阅我可以使用`pip`而不是`easy_install`来进行`python setup.py install`依赖解析吗?

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

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