Skip to content

Commit

Permalink
Avoid uploading to PyPI when given alternate repository URL
Browse files Browse the repository at this point in the history
Check for repository URL that has no protocol prefix and
raise exception, asking user for https:// or http:// URL.

Assume the user wants to upload to PyPI if there's
no conflicting repository URL given on the command line
AND there's a `pypi` section in .pypirc. Remove assumption
that a 'pypi' section in .pypirc should override CLI argument.

Fixes #269.
  • Loading branch information
brainwane committed Mar 18, 2018
1 parent 7aeebc2 commit 3d2f9a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions twine/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ class UploadToDeprecatedPyPIDetected(Exception):
"""An upload attempt was detected to deprecated legacy PyPI
sites pypi.python.org or testpypi.python.org."""
pass


class UnreachableRepositoryURLDetected(Exception):
"""An upload attempt was detected to a URL without a protocol prefix.
"""
pass
13 changes: 12 additions & 1 deletion twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
except ImportError:
from urllib.parse import urlparse, urlunparse

import twine.exceptions

# Shim for raw_input in python3
if sys.version_info > (3,):
input_func = input
Expand Down Expand Up @@ -66,8 +68,13 @@ def get_config(path="~/.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 = ["pypi"]
repositories = []

config = {}

Expand Down Expand Up @@ -112,6 +119,10 @@ def get_repository_from_config(config_file, repository, repository_url=None):
"username": None,
"password": None,
}
if repository_url and "://" not in repository_url:
raise twine.exceptions.UnreachableRepositoryURLDetected(
"Repository URL {0} has no protocol. Please add 'http://' or "
"'https://'. \n".format(repository_url))
try:
return get_config(config_file)[repository]
except KeyError:
Expand Down

0 comments on commit 3d2f9a9

Please sign in to comment.