奇怪的是,没有对设置默认值的内置支持,但这里有两个选项可以帮助您解决它。
选项 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: hack:您可以通过在 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]键,则在省略-r [repo]参数时,上传命令默认为pypi url 。相关代码在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