Skip to content

Commit

Permalink
Allow querying non-host python intepreters
Browse files Browse the repository at this point in the history
This way you can install once (for example via pipx) and reuse it for any
existing python environment.

Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Jul 24, 2020
1 parent abe1347 commit 5f3932a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addons:
- graphviz
before_install:
- pip install -U pip>=8.0.2
- pip install pytest
- pip install pytest virtualenv>=20
- pip install graphviz
install: pip install .
script: pytest -v tests/test_pipdeptree.py
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pytest-cov
jinja2
ipython
flake8
virtualenv>=20
32 changes: 31 additions & 1 deletion pipdeptree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from operator import attrgetter
import json
from importlib import import_module
import subprocess

try:
from collections import OrderedDict
Expand All @@ -18,6 +19,17 @@
except ImportError:
from collections import Mapping


def _patch_sub_invocation():
if os.environ.get('_PIP_DEPTREE_SUB_CALL') == '1':
here = os.path.abspath(os.path.dirname(__file__))
if os.path.abspath(sys.path[0]) == here:
del sys.path[0]


# needs to happen before we import pip as the workset is calculated at load
_patch_sub_invocation()

try:
from pip._internal.utils.misc import get_installed_distributions
from pip._internal.operations.freeze import FrozenRequirement
Expand Down Expand Up @@ -711,6 +723,9 @@ def get_parser():
version='{0}'.format(__version__))
parser.add_argument('-f', '--freeze', action='store_true',
help='Print names so as to write freeze files')
parser.add_argument('--python', default=sys.executable,
help='Python to use to look for packages in it (default: where'
' installed)')
parser.add_argument('-a', '--all', action='store_true',
help='list all deps at top level')
parser.add_argument('-l', '--local-only',
Expand Down Expand Up @@ -777,7 +792,22 @@ def _get_args():

def main():
args = _get_args()

of_python = os.path.abspath(args.python)
if of_python != os.path.abspath(sys.executable):
if args.output_format:
print("graphviz functionality is not supported when querying"
" non-host python", file=sys.stderr)
raise SystemExit(1)
argv = sys.argv[1:]
py_at = argv.index('--python')
del argv[py_at]
del argv[py_at]
cmd = [args.python, __file__]
cmd.extend(argv)
env = os.environ.copy()
env['_PIP_DEPTREE_SUB_CALL'] = '1'
return_code = subprocess.call(cmd, env=env)
return return_code
pkgs = get_installed_distributions(local_only=args.local_only,
user_only=args.user_only)

Expand Down
23 changes: 23 additions & 0 deletions tests/test_pipdeptree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mock

import pytest
import virtualenv

import pipdeptree as p

Expand Down Expand Up @@ -520,3 +521,25 @@ def test_parser_svg():
args = parser.parse_args(['--graph-output', 'svg'])
assert args.output_format == 'svg'
assert not args.json


def test_custom_interpreter(tmp_path, monkeypatch, capfd):
result = virtualenv.cli_run([str(tmp_path), '--activators', ''])
cmd = [sys.executable, '--python', str(result.creator.exe)]
monkeypatch.setattr(sys, 'argv', cmd)
p.main()
out, _ = capfd.readouterr()
found = {i.split('==')[0] for i in out.splitlines()}
assert found == {'pip', 'setuptools', 'wheel'}, out

monkeypatch.setattr(sys, 'argv', cmd + ['--graph-output', 'something'])
with pytest.raises(SystemExit) as context:
p.main()
out, err = capfd.readouterr()
assert context.value.code == 1
assert not out
assert err == 'graphviz functionality is not supported when querying' \
' non-host python\n'



1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ deps =
pip>=8.0.2
pytest
pytest-cov
virtualenv>=20

0 comments on commit 5f3932a

Please sign in to comment.