Skip to content
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

use the current interpreter's pip or easy_install to install python packages #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 18 additions & 15 deletions src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
3. any other version of collectd is not supported.
"""

import errno
import os
import platform
import re
import shlex
import shutil
import sys
import time
import errno
from collections import namedtuple
from distutils.version import LooseVersion
from glob import glob
Expand All @@ -39,10 +40,10 @@
PLUGIN_CONFIGURATION_INCLUDE_LINE = 'Include "/etc/collectd-cloudwatch.conf"\r\n'
APT_INSTALL_COMMAND = "apt-get install -y "
YUM_INSTALL_COMMAND = "yum install -y "
SYSTEM_DEPENDENCIES = ["python-pip", "python-setuptools"]
PIP_INSTALLATION_FLAGS = " install --quiet --upgrade --force-reinstall "
EASY_INSTALL_COMMAND = "easy_install -U --quiet "
PYTHON_DEPENDENCIES = ["requests"]
SYSTEM_DEPENDENCIES = []
PIP_INSTALL_COMMAND = sys.executable + " -m pip install --quiet --upgrade --force-reinstall "
EASY_INSTALL_COMMAND = sys.executable + " -m easy_install -U --quiet "
PYTHON_DEPENDENCIES = ["requests>=2.4.0"]
FIND_COMMAND = "which {} 2> /dev/null"
COLLECTD_HELP_ARGS = "-help"
CONFIG_FILE_REGEX = re.compile("\sConfig file\s*(.*)\s")
Expand Down Expand Up @@ -236,22 +237,24 @@ class MetadataRequestException(Exception):


def install_python_packages(packages):
try:
Command(detect_pip() + PIP_INSTALLATION_FLAGS + " ".join(packages), "Installing python dependencies", exit_on_failure=True).run()
except CalledProcessError:
Command(EASY_INSTALL_COMMAND + " ".join(packages), "Installing python dependencies", exit_on_failure=True).run()
if packages:
try:
detect_pip()
except CalledProcessError:
command = Command(EASY_INSTALL_COMMAND + " ".join(packages), "Installing python dependencies", exit_on_failure=True)
else:
command = Command(PIP_INSTALL_COMMAND + " ".join(packages), "Installing python dependencies", exit_on_failure=True)
command.run()


def detect_pip():
try:
return get_path_to_executable("pip")
except CalledProcessError:
return get_path_to_executable("python-pip")
Command("{} -m pip".format(sys.executable), "Checking if pip is installed").run()


def install_packages(packages):
command = DISTRIBUTION_TO_INSTALLER[detect_linux_distribution()] + " ".join(packages)
Command(command, "Installing dependencies").run()
if packages:
command = DISTRIBUTION_TO_INSTALLER[detect_linux_distribution()] + " ".join(packages)
Command(command, "Installing dependencies").run()


def detect_linux_distribution():
Expand Down
16 changes: 9 additions & 7 deletions test/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import unittest
from subprocess import CalledProcessError

Expand Down Expand Up @@ -98,21 +99,22 @@ def test_required_command_exits_on_failure(self):
class InstallationTest(unittest.TestCase):
PYTHON_MODULES = ["invalid_module1", "invalid_module2"]

@patch("src.setup.detect_pip")
@patch("src.setup.Command")
def test_install_python_packages_uses_pip_first(self, command_mock):
def test_install_python_packages_uses_pip_first(self, command_mock, detect_pip_mock):
installer.detect_pip = Mock()
installer.detect_pip.return_value = "pip"
installer.detect_pip.return_value = None
command_mock.return_value = Mock()
installer.install_python_packages(self.PYTHON_MODULES)
command_mock.assert_called_with("pip install --quiet --upgrade --force-reinstall " + " ".join(self.PYTHON_MODULES), 'Installing python dependencies', exit_on_failure=True)
command_mock.assert_called_with(sys.executable + " -m pip install --quiet --upgrade --force-reinstall " + " ".join(self.PYTHON_MODULES), 'Installing python dependencies', exit_on_failure=True)

@patch("src.setup.detect_pip")
@patch("src.setup.Command")
def test_install_python_packages_uses_easy_install_when_pip_is_not_available(self, command_mock):
installer.detect_pip = Mock()
installer.detect_pip.side_effect = CalledProcessError(cmd="which python-pip", returncode=1)
def test_install_python_packages_uses_easy_install_when_pip_is_not_available(self, command_mock, detect_pip_mock):
detect_pip_mock.side_effect = CalledProcessError(cmd="{} -m pip".format(sys.executable), returncode=1)
command_mock.return_value = Mock()
installer.install_python_packages(self.PYTHON_MODULES)
command_mock.assert_called_with("easy_install -U --quiet " + " ".join(self.PYTHON_MODULES), "Installing python dependencies", exit_on_failure=True)
command_mock.assert_called_with(sys.executable + " -m easy_install -U --quiet " + " ".join(self.PYTHON_MODULES), "Installing python dependencies", exit_on_failure=True)


class ColorTest(unittest.TestCase):
Expand Down