Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions conan/internal/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"tools.system.package_manager:mode": "Mode for package_manager tools: 'check', 'report', 'report-installed' or 'install'",
"tools.system.package_manager:sudo": "Use 'sudo' when invoking the package manager tools in Linux (False by default)",
"tools.system.package_manager:sudo_askpass": "Use the '-A' argument if using sudo in Linux to invoke the system package manager (False by default)",
"tools.system.pipenv:python_interpreter": "Path to the Python interpreter to be used to create the virtualenv",
"tools.apple:sdk_path": "Path to the SDK to be used",
"tools.apple:enable_bitcode": "(boolean) Enable/Disable Bitcode Apple Clang flags",
"tools.apple:enable_arc": "(boolean) Enable/Disable ARC Apple Clang flags",
Expand Down
23 changes: 21 additions & 2 deletions conan/tools/system/pip_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import venv
import platform
import os
import shutil

from conan.tools.build import cmd_args_to_string
from conan.tools.env.environment import Environment
from conan.errors import ConanException


class PipEnv:
Expand All @@ -23,6 +24,24 @@ def generate(self):
env.prepend_path("PATH", self.bin_dir)
env.vars(self._conanfile).save_script(self.env_name)

@staticmethod
def _default_python():
default_python = shutil.which('python') if platform.system() == "Windows" else shutil.which('python3')
return os.path.realpath(default_python) if default_python else None

def _create_venv(self):
python_interpreter = self._conanfile.conf.get("tools.system.pipenv:python_interpreter") or self._default_python()
if not python_interpreter:
raise ConanException("PipEnv could not find a Python executable path."
"Please set 'tools.system.pipenv:python_interpreter' to '</your/python/full/path>' "
"in the [conf] section of the profile, or in the command line using "
"'-c tools.system.pipenv:python_interpreter=</your/python/full/path>'")

try:
self._conanfile.run(cmd_args_to_string([python_interpreter, '-m', 'venv', self._env_dir]))
except ConanException:
raise ConanException("PipEnv could not create a Python virtual environment.")

def install(self, packages, pip_args=None):
"""
Will try to install the list of pip packages passed as a parameter.
Expand All @@ -34,7 +53,7 @@ def install(self, packages, pip_args=None):
:return: the return code of the executed pip command.
"""

venv.EnvBuilder(clear=True, with_pip=True).create(self._env_dir)
self._create_venv()
args = [self._python_exe, "-m", "pip", "install", "--disable-pip-version-check"]
if pip_args:
args += list(pip_args)
Expand Down
40 changes: 40 additions & 0 deletions test/integration/tools/system/pip_manager_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from conan.tools.system import PipEnv
from unittest.mock import patch
import mock
import pytest
from unittest.mock import PropertyMock
from conan.errors import ConanException
from conan.internal.model.settings import Settings
from conan.test.utils.mocks import ConanFileMock


@patch('shutil.which')
def test_pipenv_conf(mock_shutil_which):
# https://github.com/conan-io/conan/issues/11661
conanfile = ConanFileMock()
conanfile.settings = Settings()
mock_shutil_which.side_effect = Exception()
conanfile.conf.define("tools.system.pipenv:python_interpreter", "/python/interpreter/from/config")
result = "/python/interpreter/from/config -m venv"
pipenv = PipEnv(conanfile, "testenv")

def fake_run(command, win_bash=False, subsystem=None, env=None, ignore_errors=False, quiet=False):
assert result in command
return 100
conanfile.run = fake_run
pipenv._create_venv()


@patch('shutil.which')
def test_pipenv_error_message(mock_shutil_which):
# https://github.com/conan-io/conan/issues/11661
conanfile = ConanFileMock()
conanfile.settings = Settings()
mock_shutil_which.return_value = None
with pytest.raises(ConanException) as exc_info:
pipenv = PipEnv(conanfile, "testenv")
pipenv._create_venv()
assert exc_info.value.args[0] == "PipEnv could not find a Python executable path." \
"Please set 'tools.system.pipenv:python_interpreter' to '</your/python/full/path>' " \
"in the [conf] section of the profile, or in the command line using " \
"'-c tools.system.pipenv:python_interpreter=</your/python/full/path>'"
Loading