Skip to content

Commit a73123a

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

File tree

4 files changed

+25
-77
lines changed

4 files changed

+25
-77
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: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
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()
23+
using_custom_interpreter = py_path != Path(sys.executable).absolute()
1824

19-
if local_only and in_venv:
25+
if user_only:
26+
original_dists = distributions(path=[site.getusersitepackages()])
27+
elif using_custom_interpreter:
28+
# We query the interpreter directly to get the `path` which is used by distributions(),
29+
# so we can search the customer dists with it.
30+
# If --python and --local-only are given at the same time, it means the dists only in
31+
# customer's env are needed. (.e.g --system-site-packages is given when creating venv)
32+
if local_only:
33+
args = "import sys, site; print(site.getsitepackages([sys.prefix]))"
34+
else:
35+
args = "import sys; print(sys.path)"
36+
37+
cmd = [str(py_path), "-c", args]
38+
result = subprocess.run(cmd, stdout=subprocess.PIPE, check=False) # noqa: S603
39+
original_dists = distributions(path=ast.literal_eval(result.stdout.decode("utf-8")))
40+
elif local_only and in_venv:
2041
venv_site_packages = site.getsitepackages([sys.prefix])
2142
original_dists = distributions(path=venv_site_packages)
22-
elif user_only:
23-
original_dists = distributions(path=[site.getusersitepackages()])
2443
else:
2544
original_dists = distributions()
2645

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)