Skip to content

Proposed fix to #1180 (and maybe #932 & #1104) #1902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@

* Allow the use of ``--no-use-wheel`` within a requirements file. (:pull:`1859`)

* Fixed :issue:`1180`. Added support to respect proxies in ``pip search``. It
also fixes :issue:`932` and :issue:`1104`. (:pull:`1902`)


**1.5.7**

Expand Down
16 changes: 9 additions & 7 deletions pip/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from functools import reduce

from pip.basecommand import Command, SUCCESS
from pip.download import PipXmlrpcTransport
from pip.util import get_terminal_size
from pip.log import logger
from pip.compat import cmp
Expand Down Expand Up @@ -36,9 +37,7 @@ def run(self, options, args):
if not args:
raise CommandError('Missing required argument (search query).')
query = args
index_url = options.index

pypi_hits = self.search(query, index_url)
pypi_hits = self.search(query, options)
hits = transform_hits(pypi_hits)

terminal_width = None
Expand All @@ -50,10 +49,13 @@ def run(self, options, args):
return SUCCESS
return NO_MATCHES_FOUND

def search(self, query, index_url):
pypi = xmlrpc_client.ServerProxy(index_url)
hits = pypi.search({'name': query, 'summary': query}, 'or')
return hits
def search(self, query, options):
index_url = options.index
with self._build_session(options) as session:
transport = PipXmlrpcTransport(index_url, session)
pypi = xmlrpc_client.ServerProxy(index_url, transport)
hits = pypi.search({'name': query, 'summary': query}, 'or')
return hits


def transform_hits(hits):
Expand Down
27 changes: 27 additions & 0 deletions pip/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pip._vendor.cachecontrol import CacheControlAdapter
from pip._vendor.cachecontrol.caches import FileCache
from pip._vendor.lockfile import LockError
from pip._vendor.six.moves import xmlrpc_client


__all__ = ['get_file_content',
Expand Down Expand Up @@ -695,3 +696,29 @@ def unpack_file_url(link, location, download_dir=None):
# a download dir is specified and not already downloaded
if download_dir and not already_downloaded:
_copy_file(from_path, download_dir, content_type, link)


class PipXmlrpcTransport(xmlrpc_client.Transport):
"""Provide a `xmlrpclib.Transport` implementation via a `PipSession`
object.
"""
def __init__(self, index_url, session, use_datetime=False):
xmlrpc_client.Transport.__init__(self, use_datetime)
index_parts = urlparse.urlparse(index_url)
self._scheme = index_parts.scheme
self._session = session

def request(self, host, handler, request_body, verbose=False):
parts = (self._scheme, host, handler, None, None, None)
url = urlparse.urlunparse(parts)
try:
headers = {'Content-Type': 'text/xml'}
response = self._session.post(url, data=request_body,
headers=headers, stream=True)
response.raise_for_status()
self.verbose = verbose
return self.parse_response(response.raw)
except requests.HTTPError as exc:
logger.fatal("HTTP error {0} while getting {1}".format(
exc.response.status_code, url))
raise
19 changes: 9 additions & 10 deletions tests/functional/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
transform_hits,
SearchCommand)
from pip.status_codes import NO_MATCHES_FOUND, SUCCESS
from mock import Mock
from tests.lib import pyversion


Expand Down Expand Up @@ -139,22 +138,22 @@ def test_run_method_should_return_sucess_when_find_packages():
"""
Test SearchCommand.run for found package
"""
options_mock = Mock()
options_mock.index = 'http://pypi.python.org/pypi'
search_cmd = SearchCommand()
status = search_cmd.run(options_mock, ('pip',))
command = SearchCommand()
cmdline = "--index=http://pypi.python.org/pypi pip"
options, args = command.parse_args(cmdline.split())
status = command.run(options, args)
assert status == SUCCESS


def test_run_method_should_return_no_matches_found_when_does_not_find_pkgs():
"""
Test SearchCommand.run for no matches
"""
options_mock = Mock()
options_mock.index = 'https://pypi.python.org/pypi'
search_cmd = SearchCommand()
status = search_cmd.run(options_mock, ('non-existent-package',))
assert status == NO_MATCHES_FOUND, status
command = SearchCommand()
cmdline = "--index=http://pypi.python.org/pypi non-existent-package"
options, args = command.parse_args(cmdline.split())
status = command.run(options, args)
assert status == NO_MATCHES_FOUND


def test_search_should_exit_status_code_zero_when_find_packages(script):
Expand Down