Merge pull request #4067 from shin-/portable-find-exe

Replace "which" calls with the portable find_executable function
This commit is contained in:
Aanand Prasad 2016-10-24 16:15:21 -07:00 committed by GitHub
commit 8b5782ba9f
3 changed files with 9 additions and 9 deletions

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
import contextlib import contextlib
import logging import logging
import socket import socket
from distutils.spawn import find_executable
from textwrap import dedent from textwrap import dedent
from docker.errors import APIError from docker.errors import APIError
@ -13,7 +14,6 @@ from requests.exceptions import SSLError
from requests.packages.urllib3.exceptions import ReadTimeoutError from requests.packages.urllib3.exceptions import ReadTimeoutError
from ..const import API_VERSION_TO_ENGINE_VERSION from ..const import API_VERSION_TO_ENGINE_VERSION
from .utils import call_silently
from .utils import is_docker_for_mac_installed from .utils import is_docker_for_mac_installed
from .utils import is_mac from .utils import is_mac
from .utils import is_ubuntu from .utils import is_ubuntu
@ -90,11 +90,11 @@ def exit_with_error(msg):
def get_conn_error_message(url): def get_conn_error_message(url):
if call_silently(['which', 'docker']) != 0: if find_executable('docker') is None:
return docker_not_found_msg("Couldn't connect to Docker daemon.") return docker_not_found_msg("Couldn't connect to Docker daemon.")
if is_docker_for_mac_installed(): if is_docker_for_mac_installed():
return conn_error_docker_for_mac return conn_error_docker_for_mac
if call_silently(['which', 'docker-machine']) == 0: if find_executable('docker-machine') is not None:
return conn_error_docker_machine return conn_error_docker_machine
return conn_error_generic.format(url=url) return conn_error_generic.format(url=url)

View File

@ -10,6 +10,7 @@ import pipes
import re import re
import subprocess import subprocess
import sys import sys
from distutils.spawn import find_executable
from inspect import getdoc from inspect import getdoc
from operator import attrgetter from operator import attrgetter
@ -1063,9 +1064,8 @@ def exit_if(condition, message, exit_code):
def call_docker(args): def call_docker(args):
try: executable_path = find_executable('docker')
executable_path = subprocess.check_output(["which", "docker"]).strip() if not executable_path:
except subprocess.CalledProcessError:
raise UserError(errors.docker_not_found_msg("Couldn't find `docker` binary.")) raise UserError(errors.docker_not_found_msg("Couldn't find `docker` binary."))
args = [executable_path] + args args = [executable_path] + args

View File

@ -16,9 +16,9 @@ def mock_logging():
yield mock_log yield mock_log
def patch_call_silently(side_effect): def patch_find_executable(side_effect):
return mock.patch( return mock.patch(
'compose.cli.errors.call_silently', 'compose.cli.errors.find_executable',
autospec=True, autospec=True,
side_effect=side_effect) side_effect=side_effect)
@ -27,7 +27,7 @@ class TestHandleConnectionErrors(object):
def test_generic_connection_error(self, mock_logging): def test_generic_connection_error(self, mock_logging):
with pytest.raises(errors.ConnectionError): with pytest.raises(errors.ConnectionError):
with patch_call_silently([0, 1]): with patch_find_executable(['/bin/docker', None]):
with handle_connection_errors(mock.Mock()): with handle_connection_errors(mock.Mock()):
raise ConnectionError() raise ConnectionError()