-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more verbose output for interpreter info. (#1347)
Convert `-v` from a boolean to a counter and display more information for `-vv` (tags) and` -vvv` (environment markers and venv affiliation). This should be useful for debugging various issues in the field.
- Loading branch information
Showing
6 changed files
with
228 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import | ||
|
||
import json | ||
import os | ||
|
||
import pytest | ||
|
||
from pex.common import safe_mkdtemp | ||
from pex.interpreter import PythonInterpreter | ||
from pex.pex_builder import PEXBuilder | ||
from pex.testing import PY35, PY36, ensure_python_interpreter | ||
from pex.tools.commands.virtualenv import Virtualenv | ||
from pex.typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
import attr # vendor:skip | ||
from typing import Iterable, Dict, Any | ||
else: | ||
from pex.third_party import attr | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def python35(): | ||
# type: () -> PythonInterpreter | ||
return PythonInterpreter.from_binary(ensure_python_interpreter(PY35)) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def python36(): | ||
# type: () -> PythonInterpreter | ||
return PythonInterpreter.from_binary(ensure_python_interpreter(PY36)) | ||
|
||
|
||
@attr.s(frozen=True) | ||
class InterpreterTool(object): | ||
tools_pex = attr.ib() # type: str | ||
interpreter = attr.ib() # type: PythonInterpreter | ||
other_interpreters = attr.ib(default=()) # type: Iterable[PythonInterpreter] | ||
|
||
@classmethod | ||
def create( | ||
cls, | ||
interpreter, # type: PythonInterpreter | ||
*other_interpreters # type: PythonInterpreter | ||
): | ||
# type: (...) -> InterpreterTool | ||
pex_builder = PEXBuilder(include_tools=True, interpreter=interpreter) | ||
pex_builder.freeze() | ||
return cls( | ||
tools_pex=pex_builder.path(), | ||
interpreter=interpreter, | ||
other_interpreters=other_interpreters, | ||
) | ||
|
||
def run( | ||
self, | ||
*args, # type: str | ||
**env # type: str | ||
): | ||
# type: (...) -> str | ||
cmd = [self.tools_pex, "interpreter"] | ||
if args: | ||
cmd.extend(args) | ||
|
||
environ = os.environ.copy() | ||
interpreters = [self.interpreter] | ||
interpreters.extend(self.other_interpreters) | ||
environ.update( | ||
PEX_PYTHON_PATH=":".join(interpreter.binary for interpreter in interpreters), | ||
PEX_TOOLS="1", | ||
) | ||
environ.update(env) | ||
|
||
_, stdout, _ = self.interpreter.execute(args=cmd, env=environ) | ||
return stdout | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def interpreter_tool( | ||
python35, # type: PythonInterpreter | ||
python36, # type: PythonInterpreter | ||
): | ||
# type: (...) -> InterpreterTool | ||
return InterpreterTool.create(python35, python36) | ||
|
||
|
||
def expected_basic(interpreter): | ||
# type: (PythonInterpreter) -> str | ||
return interpreter.binary | ||
|
||
|
||
def test_basic( | ||
python35, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run() | ||
assert expected_basic(python35) == output.strip() | ||
|
||
|
||
def test_basic_all( | ||
python35, # type: PythonInterpreter | ||
python36, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run("-a") | ||
assert [ | ||
expected_basic(interpreter) for interpreter in (python35, python36) | ||
] == output.splitlines() | ||
|
||
|
||
def expected_verbose(interpreter): | ||
# type: (PythonInterpreter) -> Dict[str, Any] | ||
return { | ||
"path": interpreter.binary, | ||
"platform": str(interpreter.platform), | ||
"requirement": str(interpreter.identity.requirement), | ||
} | ||
|
||
|
||
def test_verbose( | ||
python35, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run("-v") | ||
assert expected_verbose(python35) == json.loads(output) | ||
|
||
|
||
def test_verbose_all( | ||
python35, # type: PythonInterpreter | ||
python36, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run("-va") | ||
assert [expected_verbose(interpreter) for interpreter in (python35, python36)] == [ | ||
json.loads(line) for line in output.splitlines() | ||
] | ||
|
||
|
||
def expected_verbose_verbose(interpreter): | ||
# type: (PythonInterpreter) -> Dict[str, Any] | ||
expected = expected_verbose(interpreter) | ||
expected.update(supported_tags=[str(tag) for tag in interpreter.identity.supported_tags]) | ||
return expected | ||
|
||
|
||
def test_verbose_verbose( | ||
python35, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run("-vv") | ||
assert expected_verbose_verbose(python35) == json.loads(output) | ||
|
||
|
||
def test_verbose_verbose_verbose( | ||
python35, # type: PythonInterpreter | ||
interpreter_tool, # type: InterpreterTool | ||
): | ||
# type: (...) -> None | ||
output = interpreter_tool.run("-vvv") | ||
expected = expected_verbose_verbose(python35) | ||
expected.update(env_markers=python35.identity.env_markers, venv=False) | ||
assert expected == json.loads(output) | ||
|
||
|
||
def test_verbose_verbose_verbose_venv( | ||
python36, # type: PythonInterpreter | ||
): | ||
# type: (...) -> None | ||
venv = Virtualenv.create(venv_dir=safe_mkdtemp(), interpreter=python36, force=True) | ||
assert venv.interpreter.is_venv | ||
|
||
# N.B.: Non-venv-mode PEXes always escape venvs to prevent `sys.path` contamination unless | ||
# `PEX_INHERIT_PATH` is not "false". | ||
output = InterpreterTool.create(venv.interpreter).run("-vvv", PEX_INHERIT_PATH="fallback") | ||
|
||
expected = expected_verbose_verbose(venv.interpreter) | ||
expected.update( | ||
env_markers=venv.interpreter.identity.env_markers, | ||
venv=True, | ||
base_interpreter=python36.binary, | ||
) | ||
assert expected == json.loads(output) |