From a240a987019d5dc87497e2eeea0f8425658641e4 Mon Sep 17 00:00:00 2001 From: Laszlo Kiss-Kollar Date: Fri, 7 Jun 2019 23:05:34 +0100 Subject: [PATCH] Add `--path` option to `pip list` This makes `pip list` consistent with `pip freeze` which also supports listing packages in arbitrary file paths. --- news/6551.feature | 2 ++ src/pip/_internal/commands/list.py | 13 ++++++- tests/functional/test_list.py | 56 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 news/6551.feature diff --git a/news/6551.feature b/news/6551.feature new file mode 100644 index 00000000000..68487d2183c --- /dev/null +++ b/news/6551.feature @@ -0,0 +1,2 @@ +Add a ``--path`` argument to ``pip list`` to support ``--target`` +installations. diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index d07953b05b3..2fbc31687c7 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -62,7 +62,12 @@ def __init__(self, *args, **kw): action='store_true', default=False, help='Only output packages installed in user-site.') - + self.cmd_opts.add_option( + '--path', + dest='path', + action='append', + help='Restrict to the specified installation path for listing ' + 'packages (can be used multiple times).') cmd_opts.add_option( '--pre', action='store_true', @@ -126,11 +131,17 @@ def run(self, options, args): raise CommandError( "Options --outdated and --uptodate cannot be combined.") + if options.path and (options.user or options.local): + raise CommandError( + "Cannot combine '--path' with '--user' or '--local'" + ) + packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, + paths=options.path, ) # get_not_required must be called firstly in order to find and diff --git a/tests/functional/test_list.py b/tests/functional/test_list.py index 05f419cccc0..e17e61c4fe4 100644 --- a/tests/functional/test_list.py +++ b/tests/functional/test_list.py @@ -504,3 +504,59 @@ def test_list_json(script, data): data = json.loads(result.stdout) assert {'name': 'simple', 'version': '1.0'} in data assert {'name': 'simple2', 'version': '3.0'} in data + + +def test_list_path(tmpdir, script, data): + """ + Test list with --path. + """ + result = script.pip('list', '--path', tmpdir, '--format=json') + assert {'name': 'simple', + 'version': '2.0'} not in json.loads(result.stdout) + + script.pip('install', '--find-links', data.find_links, + '--target', tmpdir, 'simple==2.0') + result = script.pip('list', '--path', tmpdir, '--format=json') + json_result = json.loads(result.stdout) + assert {'name': 'simple', 'version': '2.0'} in json_result + + +def test_list_path_exclude_user(tmpdir, script, data): + """ + Test list with --path and make sure packages from --user are not picked + up. + """ + script.pip_install_local('--find-links', data.find_links, + '--user', 'simple2') + script.pip('install', '--find-links', data.find_links, + '--target', tmpdir, 'simple==1.0') + result = script.pip('list', '--user', '--format=json') + json_result = json.loads(result.stdout) + assert {'name': 'simple2', 'version': '3.0'} in json_result + + result = script.pip('list', '--path', tmpdir, '--format=json') + json_result = json.loads(result.stdout) + assert {'name': 'simple', 'version': '1.0'} in json_result + + +def test_list_path_multiple(tmpdir, script, data): + """ + Test list with multiple --path arguments. + """ + path1 = tmpdir / "path1" + os.mkdir(path1) + path2 = tmpdir / "path2" + os.mkdir(path2) + script.pip('install', '--find-links', data.find_links, + '--target', path1, 'simple==2.0') + script.pip('install', '--find-links', data.find_links, + '--target', path2, 'simple2==3.0') + result = script.pip('list', '--path', path1, '--format=json') + json_result = json.loads(result.stdout) + assert {'name': 'simple', 'version': '2.0'} in json_result + + result = script.pip('list', '--path', path1, '--path', path2, + '--format=json') + json_result = json.loads(result.stdout) + assert {'name': 'simple', 'version': '2.0'} in json_result + assert {'name': 'simple2', 'version': '3.0'} in json_result