Skip to content

Commit f6b5164

Browse files
committed
refactor the non_host part for not injecting to custom env
1 parent ff31dc4 commit f6b5164

File tree

4 files changed

+14
-74
lines changed

4 files changed

+14
-74
lines changed

src/pipdeptree/__main__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88
from pipdeptree._cli import get_options
99
from pipdeptree._discovery import get_installed_distributions
1010
from pipdeptree._models import PackageDAG
11-
from pipdeptree._non_host import handle_non_host_target
1211
from pipdeptree._render import render
1312
from pipdeptree._validate import validate
1413

1514

1615
def main(args: Sequence[str] | None = None) -> None | int:
1716
"""CLI - The main function called as entry point."""
1817
options = get_options(args)
19-
result = handle_non_host_target(options)
20-
if result is not None:
21-
return result
2218

23-
pkgs = get_installed_distributions(local_only=options.local_only, user_only=options.user_only)
19+
pkgs = get_installed_distributions(
20+
interpreter=options.python, local_only=options.local_only, user_only=options.user_only
21+
)
2422
tree = PackageDAG.from_pkgs(pkgs)
2523
is_text_output = not any([options.json, options.json_tree, options.output_format])
2624

src/pipdeptree/_discovery.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
from __future__ import annotations
22

3+
import ast
34
import site
5+
import subprocess # noqa: S404
46
import sys
57
from importlib.metadata import Distribution, distributions
8+
from pathlib import Path
69
from typing import Iterable, Tuple
710

811
from packaging.utils import canonicalize_name
912

1013

1114
def get_installed_distributions(
15+
interpreter: str = str(sys.executable),
1216
local_only: bool = False, # noqa: FBT001, FBT002
1317
user_only: bool = False, # noqa: FBT001, FBT002
1418
) -> list[Distribution]:
1519
# See https://docs.python.org/3/library/venv.html#how-venvs-work for more details.
1620
in_venv = sys.prefix != sys.base_prefix
1721
original_dists: Iterable[Distribution] = []
22+
py_path = Path(interpreter).absolute()
1823

1924
if local_only and in_venv:
2025
venv_site_packages = site.getsitepackages([sys.prefix])
2126
original_dists = distributions(path=venv_site_packages)
2227
elif user_only:
2328
original_dists = distributions(path=[site.getusersitepackages()])
29+
elif py_path != Path(sys.executable).absolute():
30+
# We query the interpreter to get its sys.path directly, so we can
31+
# search the customer dists with it.
32+
cmd = [str(py_path), "-c", "import sys; print(sys.path)"]
33+
result = subprocess.run(cmd, stdout=subprocess.PIPE, check=False) # noqa: S603
34+
original_dists = distributions(path=ast.literal_eval(result.stdout.decode("utf-8")))
2435
else:
2536
original_dists = distributions()
2637

src/pipdeptree/_non_host.py

Lines changed: 0 additions & 61 deletions
This file was deleted.

tests/test_non_host.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,3 @@ def test_custom_interpreter(
3939
if sys.version_info >= (3, 12):
4040
expected -= {"setuptools", "wheel"}
4141
assert found == expected, out
42-
43-
monkeypatch.setattr(sys, "argv", [*cmd, "--graph-output", "something"])
44-
with pytest.raises(SystemExit) as context:
45-
main()
46-
out, err = capfd.readouterr()
47-
assert context.value.code == 1
48-
assert not out
49-
assert err == "graphviz functionality is not supported when querying non-host python\n"

0 commit comments

Comments
 (0)