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

发布于 2024-10-27 10:12:17 字数 478 浏览 1 评论 0原文

我希望在我的 ~/.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

我哪里错了?

I want to have multiple PyPI servers in my ~/.pypirc file so I can easily publish to different servers, depending on the project.

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.

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.

[distutils]
index-servers =
    internal
    pypi

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

[pypi]
username: brad

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-11-03 10:12:17

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

选项 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的rc文件中,例如~/.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    

但您无法访问任何其他存储库:它默认为[服务器登录]属性:

% 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    

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. This may give you more control over customizing things for your workflow.

Given this .pypirc file:

[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, e.g. ~/.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.

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. The relevant code is in 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. This isn't a workable option since the [pypi] section below will be unusable:

[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:

% 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:

% 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    
爱你是孤单的心事 2024-11-03 10:12:17

当您想要从内部安装 PyPI 包存储库时,也会遇到类似的问题。在这种情况下,使用 pip install -e . 而不是 python setup.pydevelop 可以完全回避这个问题。请参阅我可以使用 `pip` 而不是 `easy_install` 来解析 `python setup.py install` 依赖项吗?

A similar problem is when you want to have an internal PyPI package repository to install from. In this scenario using pip install -e . rather than python setup.py develop completely side-steps this problem. See Can I use `pip` instead of `easy_install` for `python setup.py install` dependency resolution?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文