Skip to content

Commit

Permalink
Add option to list config files with pip config
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshks committed Apr 20, 2020
1 parent a531a15 commit 76162b6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/6741.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a subcommand ``list-files`` to ``pip config`` to list available configuration files and their location
16 changes: 15 additions & 1 deletion src/pip/_internal/commands/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
kinds,
)
from pip._internal.exceptions import PipError
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import get_prog, write_output

logger = logging.getLogger(__name__)
Expand All @@ -28,6 +29,7 @@ class ConfigurationCommand(Command):
get: Get the value associated with name
set: Set the name=value
unset: Unset the value associated with name
list-files: List the configuration files
If none of --user, --global and --site are passed, a virtual
environment configuration file is used if one is active and the file
Expand All @@ -43,6 +45,7 @@ class ConfigurationCommand(Command):
%prog [<file-option>] get name
%prog [<file-option>] set name value
%prog [<file-option>] unset name
%prog [<file-option>] list-files
"""

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -93,7 +96,8 @@ def run(self, options, args):
"edit": self.open_in_editor,
"get": self.get_name,
"set": self.set_name_value,
"unset": self.unset_name
"unset": self.unset_name,
"list-files": self.list_config_files
}

# Determine action
Expand Down Expand Up @@ -180,6 +184,16 @@ def unset_name(self, options, args):

self._save_configuration()

def list_config_files(self, options, args):
self._get_n_args(args, "list-files", n=0)

for variant, files in sorted(self.configuration.iter_config_files()):
write_output("%s:", variant)
for fname in files:
with indent_log():
write_output("%s, exists: %r ",
fname, os.path.exists(fname))

def open_in_editor(self, options, args):
editor = self._determine_editor(options)

Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _load_config_files(self):
# type: () -> None
"""Loads configuration from configuration files
"""
config_files = dict(self._iter_config_files())
config_files = dict(self.iter_config_files())
if config_files[kinds.ENV][0:1] == [os.devnull]:
logger.debug(
"Skipping loading configuration files due to "
Expand Down Expand Up @@ -370,7 +370,7 @@ def _get_environ_vars(self):
yield key[4:].lower(), val

# XXX: This is patched in the tests.
def _iter_config_files(self):
def iter_config_files(self):
# type: () -> Iterable[Tuple[Kind, List[str]]]
"""Yields variant and configuration files associated with it.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def test_venv_config_file_found(self, monkeypatch):
cp = pip._internal.configuration.Configuration(isolated=False)

files = []
for _, val in cp._iter_config_files():
for _, val in cp.iter_config_files():
files.extend(val)

assert len(files) == 4
Expand Down

0 comments on commit 76162b6

Please sign in to comment.