pip/src/pip/_internal/commands/configuration.py

281 lines
9.1 KiB
Python
Raw Normal View History

import logging
import os
import subprocess
2018-07-30 06:02:47 +02:00
from pip._internal.cli.base_command import Command
2018-07-30 06:10:59 +02:00
from pip._internal.cli.status_codes import ERROR, SUCCESS
2020-09-23 16:27:09 +02:00
from pip._internal.configuration import Configuration, get_configuration_files, 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
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
2020-09-23 15:08:01 +02:00
from typing import Any, List, Optional
from pip._internal.configuration import Kind
logger = logging.getLogger(__name__)
class ConfigurationCommand(Command):
"""
Manage local and global configuration.
Subcommands:
- list: List the active configuration (or from the file specified)
- edit: Edit the configuration file in an editor
- get: Get the value associated with name
- set: Set the name=value
- unset: Unset the value associated with name
- debug: List the configuration files and values defined under them
If none of --user, --global and --site are passed, a virtual
environment configuration file is used if one is active and the file
exists. Otherwise, all modifications happen on the to the user file by
default.
"""
ignore_require_venv = True
usage = """
%prog [<file-option>] list
%prog [<file-option>] [--editor <editor-path>] edit
%prog [<file-option>] get name
%prog [<file-option>] set name value
%prog [<file-option>] unset name
2020-05-02 22:14:35 +02:00
%prog [<file-option>] debug
"""
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
2017-05-14 08:20:17 +02:00
'--editor',
dest='editor',
action='store',
default=None,
help=(
'Editor to use to edit the file. Uses VISUAL or EDITOR '
'environment variables if not provided.'
2017-05-14 08:20:17 +02:00
)
)
self.cmd_opts.add_option(
'--global',
dest='global_file',
action='store_true',
default=False,
help='Use the system-wide configuration file only'
)
self.cmd_opts.add_option(
'--user',
dest='user_file',
action='store_true',
default=False,
help='Use the user configuration file only'
)
self.cmd_opts.add_option(
'--site',
dest='site_file',
action='store_true',
default=False,
help='Use the current environment configuration file only'
)
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options, args):
# type: (Values, List[str]) -> int
handlers = {
"list": self.list_values,
"edit": self.open_in_editor,
"get": self.get_name,
"set": self.set_name_value,
"unset": self.unset_name,
"debug": self.list_config_values,
}
# Determine action
if not args or args[0] not in handlers:
logger.error(
"Need an action (%s) to perform.",
", ".join(sorted(handlers)),
)
return ERROR
action = args[0]
# Determine which configuration files are to be loaded
# Depends on whether the command is modifying.
try:
load_only = self._determine_file(
options, need_value=(action in ["get", "set", "unset", "edit"])
)
except PipError as e:
logger.error(e.args[0])
return ERROR
# Load a new configuration
self.configuration = Configuration(
2017-05-14 21:20:34 +02:00
isolated=options.isolated_mode, load_only=load_only
)
self.configuration.load()
2017-05-14 08:20:56 +02:00
# Error handling happens here, not in the action-handlers.
try:
2017-05-14 21:20:34 +02:00
handlers[action](options, args[1:])
2017-05-14 08:20:56 +02:00
except PipError as e:
logger.error(e.args[0])
return ERROR
2017-05-14 08:20:56 +02:00
return SUCCESS
def _determine_file(self, options, need_value):
# type: (Values, bool) -> Optional[Kind]
file_options = [key for key, value in (
(kinds.USER, options.user_file),
(kinds.GLOBAL, options.global_file),
(kinds.SITE, options.site_file),
) if value]
if not file_options:
if not need_value:
return None
# Default to user, unless there's a site file.
elif any(
os.path.exists(site_config_file)
for site_config_file in get_configuration_files()[kinds.SITE]
):
return kinds.SITE
else:
return kinds.USER
elif len(file_options) == 1:
return file_options[0]
raise PipError(
"Need exactly one file to operate upon "
"(--user, --site, --global) to perform."
)
2017-05-14 08:20:56 +02:00
def list_values(self, options, args):
# type: (Values, List[str]) -> None
2017-06-13 08:23:59 +02:00
self._get_n_args(args, "list", n=0)
2020-05-31 22:21:32 +02:00
for key, value in sorted(self.configuration.items()):
write_output("%s=%r", key, value)
2017-05-14 08:20:56 +02:00
def get_name(self, options, args):
# type: (Values, List[str]) -> None
2017-06-13 08:23:59 +02:00
key = self._get_n_args(args, "get [name]", n=1)
2017-05-14 08:20:56 +02:00
value = self.configuration.get_value(key)
write_output("%s", value)
2017-05-14 08:20:56 +02:00
def set_name_value(self, options, args):
# type: (Values, List[str]) -> None
2017-06-13 08:23:59 +02:00
key, value = self._get_n_args(args, "set [name] [value]", n=2)
2017-05-14 08:20:56 +02:00
self.configuration.set_value(key, value)
2017-05-14 08:20:56 +02:00
self._save_configuration()
2017-05-14 08:20:56 +02:00
def unset_name(self, options, args):
# type: (Values, List[str]) -> None
2017-06-13 08:23:59 +02:00
key = self._get_n_args(args, "unset [name]", n=1)
2017-05-14 08:20:56 +02:00
self.configuration.unset_value(key)
2017-05-14 08:20:56 +02:00
self._save_configuration()
def list_config_values(self, options, args):
# type: (Values, List[str]) -> None
2020-05-31 22:21:32 +02:00
"""List config key-value pairs across different config files"""
self._get_n_args(args, "debug", n=0)
2020-05-31 22:21:32 +02:00
self.print_env_var_values()
# Iterate over config files and print if they exist, and the
# key-value pairs present in them if they do
for variant, files in sorted(self.configuration.iter_config_files()):
write_output("%s:", variant)
for fname in files:
with indent_log():
2020-05-02 22:14:35 +02:00
file_exists = os.path.exists(fname)
write_output("%s, exists: %r",
fname, file_exists)
2020-05-02 22:14:35 +02:00
if file_exists:
self.print_config_file_values(variant)
def print_config_file_values(self, variant):
# type: (Kind) -> None
2020-05-31 22:21:32 +02:00
"""Get key-value pairs from the file of a variant"""
2020-06-01 01:42:14 +02:00
for name, value in self.configuration.\
2020-05-02 22:14:35 +02:00
get_values_in_config(variant).items():
with indent_log():
2020-05-31 22:21:32 +02:00
write_output("%s: %s", name, value)
def print_env_var_values(self):
# type: () -> None
2020-05-31 22:21:32 +02:00
"""Get key-values pairs present as environment variables"""
write_output("%s:", 'env_var')
with indent_log():
2020-06-01 01:42:14 +02:00
for key, value in sorted(self.configuration.get_environ_vars()):
2020-05-31 22:21:32 +02:00
env_var = 'PIP_{}'.format(key.upper())
write_output("%s=%r", env_var, value)
2017-05-14 08:20:56 +02:00
def open_in_editor(self, options, args):
# type: (Values, List[str]) -> None
2017-05-14 08:20:56 +02:00
editor = self._determine_editor(options)
2017-06-01 08:08:20 +02:00
fname = self.configuration.get_file_to_edit()
if fname is None:
2017-05-14 08:20:56 +02:00
raise PipError("Could not determine appropriate file.")
2017-04-08 07:31:03 +02:00
try:
2017-06-01 08:08:20 +02:00
subprocess.check_call([editor, fname])
2017-05-14 08:20:56 +02:00
except subprocess.CalledProcessError as e:
raise PipError(
"Editor Subprocess exited with exit code {}"
.format(e.returncode)
)
2017-06-13 08:23:59 +02:00
def _get_n_args(self, args, example, n):
# type: (List[str], str, int) -> Any
2017-06-01 07:55:42 +02:00
"""Helper to make sure the command got the right number of arguments
"""
2017-05-14 21:20:34 +02:00
if len(args) != n:
2017-06-01 07:55:42 +02:00
msg = (
'Got unexpected number of arguments, expected {}. '
2017-06-13 08:23:59 +02:00
'(example: "{} config {}")'
2017-06-01 07:55:42 +02:00
).format(n, get_prog(), example)
raise PipError(msg)
2017-05-14 08:20:56 +02:00
if n == 1:
2017-05-14 21:20:34 +02:00
return args[0]
2017-04-08 07:31:03 +02:00
else:
2017-05-14 21:20:34 +02:00
return args
def _save_configuration(self):
# type: () -> None
# We successfully ran a modifying command. Need to save the
# configuration.
try:
self.configuration.save()
except Exception:
logger.exception(
"Unable to save configuration. Please report this as a bug."
)
2017-05-14 08:20:56 +02:00
raise PipError("Internal Error.")
def _determine_editor(self, options):
# type: (Values) -> str
2017-05-14 08:20:56 +02:00
if options.editor is not None:
return options.editor
2017-06-01 07:55:42 +02:00
elif "VISUAL" in os.environ:
return os.environ["VISUAL"]
2017-05-14 08:20:56 +02:00
elif "EDITOR" in os.environ:
return os.environ["EDITOR"]
else:
2017-05-14 08:20:56 +02:00
raise PipError("Could not determine editor to use.")