Skip to content

Commit 0f5b325

Browse files
committed
Merge pull request #2199 from msabramo/create_Index_class_and_PyPI_instance
Reduce duplication of "pypi.python.org"
2 parents e127d30 + 954b818 commit 0f5b325

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

pip/cmdoptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import copy
1313
from optparse import OptionGroup, SUPPRESS_HELP, Option
14+
from pip.index import PyPI
1415
from pip.locations import CA_BUNDLE_PATH, USER_CACHE_DIR, src_prefix
1516

1617

@@ -192,7 +193,7 @@ def make(self):
192193
'-i', '--index-url', '--pypi-url',
193194
dest='index_url',
194195
metavar='URL',
195-
default='https://pypi.python.org/simple/',
196+
default=PyPI.simple_url,
196197
help='Base URL of Python Package Index (default %default).')
197198

198199
extra_index_url = OptionMaker(

pip/commands/search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pip.basecommand import Command, SUCCESS
88
from pip.download import PipXmlrpcTransport
9+
from pip.index import PyPI
910
from pip.utils import get_terminal_size
1011
from pip.utils.logging import indent_log
1112
from pip.exceptions import CommandError
@@ -30,7 +31,7 @@ def __init__(self, *args, **kw):
3031
'--index',
3132
dest='index',
3233
metavar='URL',
33-
default='https://pypi.python.org/pypi',
34+
default=PyPI.pypi_url,
3435
help='Base URL of Python Package Index (default %default)')
3536

3637
self.parser.insert_option_group(0, self.cmd_opts)

pip/index.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@
4747
logger = logging.getLogger(__name__)
4848

4949

50+
class Index(object):
51+
def __init__(self, url):
52+
self.url = url
53+
self.netloc = urllib_parse.urlsplit(url).netloc
54+
self.simple_url = self.url_to_path('simple')
55+
self.pypi_url = self.url_to_path('pypi')
56+
self.pip_json_url = self.url_to_path('pypi/pip/json')
57+
58+
def url_to_path(self, path):
59+
return urllib_parse.urljoin(self.url, path)
60+
61+
62+
PyPI = Index('https://pypi.python.org/')
63+
64+
5065
class PackageFinder(object):
5166
"""This finds packages.
5267
@@ -302,7 +317,7 @@ def mkurl_pypi_url(url):
302317
)
303318

304319
page = self._get_page(main_index_url, req)
305-
if page is None and 'pypi.python.org' not in str(main_index_url):
320+
if page is None and PyPI.netloc not in str(main_index_url):
306321
warnings.warn(
307322
"Failed to find %r at %s. It is suggested to upgrade "
308323
"your index to support normalized names as the name in "
@@ -703,7 +718,7 @@ def _link_package_versions(self, link, search_name):
703718
and comes_from is not None
704719
and urllib_parse.urlparse(
705720
comes_from.url
706-
).netloc.endswith("pypi.python.org")):
721+
).netloc.endswith(PyPI.netloc)):
707722
if not wheel.supported(tags=supported_tags_noarch):
708723
logger.debug(
709724
"Skipping %s because it is a pypi-hosted binary "

pip/utils/outdated.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pip._vendor import pkg_resources
1111

1212
from pip.compat import total_seconds
13+
from pip.index import PyPI
1314
from pip.locations import USER_CACHE_DIR, running_under_virtualenv
1415

1516

@@ -104,7 +105,7 @@ def pip_version_check(session):
104105
# Refresh the version if we need to or just see if we need to warn
105106
if pypi_version is None:
106107
resp = session.get(
107-
"https://pypi.python.org/pypi/pip/json",
108+
PyPI.pip_json_url,
108109
headers={"Accept": "application/json"},
109110
)
110111
resp.raise_for_status()

0 commit comments

Comments
 (0)