Skip to content

Commit

Permalink
Always configure URLs for pypi and testpypi (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
anlutro authored and theacodes committed May 21, 2018
1 parent 34c08ef commit a6c09dc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
28 changes: 25 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def test_get_config_no_distutils(tmpdir):
"username": "testuser",
"password": "testpassword",
},
"testpypi": {
"repository": utils.TEST_REPOSITORY,
"username": None,
"password": None,
},
}


Expand All @@ -97,6 +102,18 @@ def test_get_config_no_section(tmpdir):
}


def test_get_config_override_pypi_url(tmpdir):
pypirc = os.path.join(str(tmpdir), ".pypirc")

with open(pypirc, "w") as fp:
fp.write(textwrap.dedent("""
[pypi]
repository = http://pypiproxy
"""))

assert utils.get_config(pypirc)['pypi']['repository'] == 'http://pypiproxy'


def test_get_config_missing(tmpdir):
pypirc = os.path.join(str(tmpdir), ".pypirc")

Expand All @@ -106,7 +123,7 @@ def test_get_config_missing(tmpdir):
"username": None,
"password": None,
},
"pypitest": {
"testpypi": {
"repository": utils.TEST_REPOSITORY,
"username": None,
"password": None
Expand Down Expand Up @@ -143,8 +160,13 @@ def test_get_config_deprecated_pypirc():
assert utils.get_config(deprecated_pypirc_path) == {
"pypi": {
"repository": utils.DEFAULT_REPOSITORY,
"username": 'testusername',
"password": 'testpassword',
"username": "testusername",
"password": "testpassword",
},
"testpypi": {
"repository": utils.TEST_REPOSITORY,
"username": "testusername",
"password": "testpassword",
},
}

Expand Down
71 changes: 28 additions & 43 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys
import argparse
import warnings
import collections

from requests.exceptions import HTTPError

Expand Down Expand Up @@ -48,68 +49,52 @@


def get_config(path="~/.pypirc"):
# even if the config file does not exist, set up the parser
# variable to reduce the number of if/else statements
parser = configparser.RawConfigParser()

# this list will only be used if index-servers
# is not defined in the config file
index_servers = ["pypi", "testpypi"]

# default configuration for each repository
defaults = {"username": None, "password": None}

# Expand user strings in the path
path = os.path.expanduser(path)

if not os.path.isfile(path):
return {"pypi": {"repository": DEFAULT_REPOSITORY,
"username": None,
"password": None
},
"pypitest": {"repository": TEST_REPOSITORY,
"username": None,
"password": None
},
}

# Parse the rc file
parser = configparser.RawConfigParser()
parser.read(path)

# Get a list of repositories from the config file
# format: https://docs.python.org/3/distutils/packageindex.html#pypirc
if (parser.has_section("distutils") and
parser.has_option("distutils", "index-servers")):
repositories = parser.get("distutils", "index-servers").split()
elif parser.has_section("pypi"):
# Special case: if the .pypirc file has a 'pypi' section,
# even if there's no list of index servers,
# be lenient and include that in our list of repositories.
repositories = ['pypi']
else:
repositories = []
if os.path.isfile(path):
parser.read(path)

config = {}
# Get a list of index_servers from the config file
# format: https://docs.python.org/3/distutils/packageindex.html#pypirc
if parser.has_option("distutils", "index-servers"):
index_servers = parser.get("distutils", "index-servers").split()

defaults = {"username": None, "password": None}
if parser.has_section("server-login"):
for key in ["username", "password"]:
if parser.has_option("server-login", key):
defaults[key] = parser.get("server-login", key)

for repository in repositories:
# Skip this repository if it doesn't exist in the config file
if not parser.has_section(repository):
continue
config = collections.defaultdict(lambda: defaults.copy())

# Mandatory configuration and defaults
config[repository] = {
"repository": DEFAULT_REPOSITORY,
"username": None,
"password": None,
}
# don't require users to manually configure URLs for these repositories
config["pypi"]["repository"] = DEFAULT_REPOSITORY
if "testpypi" in index_servers:
config["testpypi"]["repository"] = TEST_REPOSITORY

# Optional configuration values
# optional configuration values for individual repositories
for repository in index_servers:
for key in [
"username", "repository", "password",
"ca_cert", "client_cert",
]:
if parser.has_option(repository, key):
config[repository][key] = parser.get(repository, key)
elif defaults.get(key):
config[repository][key] = defaults[key]

return config
# convert the defaultdict to a regular dict at this point
# to prevent surprising behavior later on
return dict(config)


def get_repository_from_config(config_file, repository, repository_url=None):
Expand Down

0 comments on commit a6c09dc

Please sign in to comment.