pip/src/pip/_internal/configuration.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

384 lines
14 KiB
Python
Raw Normal View History

"""Configuration management setup
Some terminology:
- name
As written in config files.
- value
Value associated with a name
- key
Name combined with it's section (section.name)
- variant
A single word describing where the configuration key-value pair came from
"""
2020-12-20 20:58:50 +01:00
import configparser
import locale
2017-05-13 23:17:26 +02:00
import os
import sys
from typing import Any, Dict, Iterable, List, NewType, Optional, Tuple
from pip._internal.exceptions import (
2019-07-22 06:45:27 +02:00
ConfigurationError,
ConfigurationFileCouldNotBeLoaded,
)
from pip._internal.utils import appdirs
from pip._internal.utils.compat import WINDOWS
from pip._internal.utils.logging import getLogger
from pip._internal.utils.misc import ensure_dir, enum
RawConfigParser = configparser.RawConfigParser # Shorthand
Kind = NewType("Kind", str)
CONFIG_BASENAME = "pip.ini" if WINDOWS else "pip.conf"
ENV_NAMES_IGNORED = "version", "help"
# The kinds of configurations there are.
kinds = enum(
USER="user", # User Specific
GLOBAL="global", # System Wide
SITE="site", # [Virtual] Environment Specific
ENV="env", # from PIP_CONFIG_FILE
ENV_VAR="env-var", # from Environment Variables
)
OVERRIDE_ORDER = kinds.GLOBAL, kinds.USER, kinds.SITE, kinds.ENV, kinds.ENV_VAR
VALID_LOAD_ONLY = kinds.USER, kinds.GLOBAL, kinds.SITE
logger = getLogger(__name__)
# NOTE: Maybe use the optionx attribute to normalize keynames.
2021-07-24 06:13:10 +02:00
def _normalize_name(name: str) -> str:
"""Make a name consistent regardless of source (environment or file)"""
name = name.lower().replace("_", "-")
if name.startswith("--"):
name = name[2:] # only prefer long opts
return name
2021-07-24 06:13:10 +02:00
def _disassemble_key(name: str) -> List[str]:
if "." not in name:
error_message = (
"Key does not contain dot separated section and key. "
"Perhaps you wanted to use 'global.{}' instead?"
).format(name)
raise ConfigurationError(error_message)
return name.split(".", 1)
2021-07-24 06:13:10 +02:00
def get_configuration_files() -> Dict[Kind, List[str]]:
global_config_files = [
os.path.join(path, CONFIG_BASENAME) for path in appdirs.site_config_dirs("pip")
]
site_config_file = os.path.join(sys.prefix, CONFIG_BASENAME)
legacy_config_file = os.path.join(
os.path.expanduser("~"),
"pip" if WINDOWS else ".pip",
CONFIG_BASENAME,
)
new_config_file = os.path.join(appdirs.user_config_dir("pip"), CONFIG_BASENAME)
return {
kinds.GLOBAL: global_config_files,
kinds.SITE: [site_config_file],
kinds.USER: [legacy_config_file, new_config_file],
}
class Configuration:
"""Handles management of configuration.
Provides an interface to accessing and managing configuration files.
This class converts provides an API that takes "section.key-name" style
keys and stores the value associated with it as "key-name" under the
2017-04-08 06:19:48 +02:00
section "section".
2017-04-08 06:19:48 +02:00
This allows for a clean interface wherein the both the section and the
key-name are preserved in an easy to manage form in the configuration files
and the data stored is also nice.
"""
2021-07-24 06:13:10 +02:00
def __init__(self, isolated: bool, load_only: Optional[Kind] = None) -> None:
2020-12-25 00:00:05 +01:00
super().__init__()
if load_only is not None and load_only not in VALID_LOAD_ONLY:
2017-04-08 07:31:03 +02:00
raise ConfigurationError(
"Got invalid value for load_only - should be one of {}".format(
", ".join(map(repr, VALID_LOAD_ONLY))
)
)
self.isolated = isolated
self.load_only = load_only
# Because we keep track of where we got the data from
2021-07-24 06:13:10 +02:00
self._parsers: Dict[Kind, List[Tuple[str, RawConfigParser]]] = {
variant: [] for variant in OVERRIDE_ORDER
2021-07-24 06:13:10 +02:00
}
self._config: Dict[Kind, Dict[str, Any]] = {
variant: {} for variant in OVERRIDE_ORDER
2021-07-24 06:13:10 +02:00
}
self._modified_parsers: List[Tuple[str, RawConfigParser]] = []
2021-07-24 06:13:10 +02:00
def load(self) -> None:
"""Loads configuration from configuration files and environment"""
self._load_config_files()
2016-12-23 10:09:15 +01:00
if not self.isolated:
self._load_environment_vars()
2021-07-24 06:13:10 +02:00
def get_file_to_edit(self) -> Optional[str]:
2017-04-08 07:33:46 +02:00
"""Returns the file with highest priority in configuration"""
2017-06-13 19:26:11 +02:00
assert self.load_only is not None, "Need to be specified a file to be editing"
2017-04-08 07:33:46 +02:00
try:
return self._get_parser_to_modify()[0]
except IndexError:
return None
2021-07-24 06:13:10 +02:00
def items(self) -> Iterable[Tuple[str, Any]]:
"""Returns key-value pairs like dict.items() representing the loaded
2016-12-23 10:09:15 +01:00
configuration
"""
return self._dictionary.items()
2021-07-24 06:13:10 +02:00
def get_value(self, key: str) -> Any:
2017-01-24 18:42:20 +01:00
"""Get a value from the configuration."""
orig_key = key
key = _normalize_name(key)
try:
return self._dictionary[key]
except KeyError:
# disassembling triggers a more useful error message than simply
# "No such key" in the case that the key isn't in the form command.option
_disassemble_key(key)
raise ConfigurationError(f"No such key - {orig_key}")
2021-07-24 06:13:10 +02:00
def set_value(self, key: str, value: Any) -> None:
"""Modify a value in the configuration."""
key = _normalize_name(key)
2017-05-12 12:38:15 +02:00
self._ensure_have_load_only()
assert self.load_only
2017-06-01 08:08:20 +02:00
fname, parser = self._get_parser_to_modify()
2017-01-24 18:42:20 +01:00
if parser is not None:
section, name = _disassemble_key(key)
# Modify the parser and the configuration
if not parser.has_section(section):
parser.add_section(section)
parser.set(section, name, value)
self._config[self.load_only][key] = value
2017-06-01 08:08:20 +02:00
self._mark_as_modified(fname, parser)
2021-07-24 06:13:10 +02:00
def unset_value(self, key: str) -> None:
"""Unset a value in the configuration."""
orig_key = key
key = _normalize_name(key)
2017-05-12 12:38:15 +02:00
self._ensure_have_load_only()
assert self.load_only
2017-01-24 18:42:20 +01:00
if key not in self._config[self.load_only]:
raise ConfigurationError(f"No such key - {orig_key}")
2017-01-24 18:42:20 +01:00
2017-06-01 08:08:20 +02:00
fname, parser = self._get_parser_to_modify()
2017-01-24 18:42:20 +01:00
if parser is not None:
section, name = _disassemble_key(key)
if not (
parser.has_section(section) and parser.remove_option(section, name)
):
# The option was not removed.
2017-04-08 07:31:03 +02:00
raise ConfigurationError(
2017-05-12 12:34:37 +02:00
"Fatal Internal error [id=1]. Please report as a bug."
2017-04-08 07:31:03 +02:00
)
# The section may be empty after the option was removed.
if not parser.items(section):
parser.remove_section(section)
self._mark_as_modified(fname, parser)
del self._config[self.load_only][key]
2021-07-24 06:13:10 +02:00
def save(self) -> None:
2019-03-12 20:25:09 +01:00
"""Save the current in-memory state."""
2017-05-12 12:38:15 +02:00
self._ensure_have_load_only()
2017-06-01 08:08:20 +02:00
for fname, parser in self._modified_parsers:
logger.info("Writing to %s", fname)
# Ensure directory exists.
2017-06-01 08:08:20 +02:00
ensure_dir(os.path.dirname(fname))
# Ensure directory's permission(need to be writeable)
try:
with open(fname, "w") as f:
parser.write(f)
2023-06-29 11:28:16 +02:00
except OSError as error:
raise ConfigurationError(
2023-06-29 10:51:11 +02:00
f"An error occurred while writing to the configuration file "
f"{fname}: {error}"
)
#
# Private routines
#
2021-07-24 06:13:10 +02:00
def _ensure_have_load_only(self) -> None:
2017-05-12 12:38:15 +02:00
if self.load_only is None:
raise ConfigurationError("Needed a specific file to be modifying.")
2017-06-01 07:55:42 +02:00
logger.debug("Will be working with %s variant only", self.load_only)
2017-05-12 12:38:15 +02:00
@property
2021-07-24 06:13:10 +02:00
def _dictionary(self) -> Dict[str, Any]:
"""A dictionary representing the loaded configuration."""
# NOTE: Dictionaries are not populated if not loaded. So, conditionals
# are not needed here.
retval = {}
for variant in OVERRIDE_ORDER:
retval.update(self._config[variant])
2016-12-23 10:09:15 +01:00
return retval
2021-07-24 06:13:10 +02:00
def _load_config_files(self) -> None:
"""Loads configuration from configuration files"""
config_files = dict(self.iter_config_files())
if config_files[kinds.ENV][0:1] == [os.devnull]:
2017-05-12 08:55:27 +02:00
logger.debug(
"Skipping loading configuration files due to "
2017-05-15 11:39:14 +02:00
"environment's PIP_CONFIG_FILE being os.devnull"
2017-05-12 08:55:27 +02:00
)
return
2017-05-12 08:55:27 +02:00
for variant, files in config_files.items():
2017-06-01 08:08:20 +02:00
for fname in files:
# If there's specific variant set in `load_only`, load only
# that variant, not the others.
if self.load_only is not None and variant != self.load_only:
2017-06-01 08:08:20 +02:00
logger.debug("Skipping file '%s' (variant: %s)", fname, variant)
continue
2017-06-01 08:08:20 +02:00
parser = self._load_file(variant, fname)
# Keeping track of the parsers used
2017-06-01 08:08:20 +02:00
self._parsers[variant].append((fname, parser))
2021-07-24 06:13:10 +02:00
def _load_file(self, variant: Kind, fname: str) -> RawConfigParser:
logger.verbose("For variant '%s', will try loading '%s'", variant, fname)
2017-06-01 08:08:20 +02:00
parser = self._construct_parser(fname)
for section in parser.sections():
2017-05-12 08:56:25 +02:00
items = parser.items(section)
self._config[variant].update(self._normalized_keys(section, items))
return parser
2021-07-24 06:13:10 +02:00
def _construct_parser(self, fname: str) -> RawConfigParser:
parser = configparser.RawConfigParser()
# If there is no such file, don't bother reading it but create the
# parser anyway, to hold the data.
# Doing this is useful when modifying and saving files, where we don't
# need to construct a parser.
2017-06-01 08:08:20 +02:00
if os.path.exists(fname):
locale_encoding = locale.getpreferredencoding(False)
try:
parser.read(fname, encoding=locale_encoding)
except UnicodeDecodeError:
# See https://github.com/pypa/pip/issues/4963
raise ConfigurationFileCouldNotBeLoaded(
2021-10-17 20:56:43 +02:00
reason=f"contains invalid {locale_encoding} characters",
fname=fname,
)
except configparser.Error as error:
# See https://github.com/pypa/pip/issues/4893
raise ConfigurationFileCouldNotBeLoaded(error=error)
return parser
2021-07-24 06:13:10 +02:00
def _load_environment_vars(self) -> None:
"""Loads configuration from environment variables"""
self._config[kinds.ENV_VAR].update(
2020-05-31 22:21:32 +02:00
self._normalized_keys(":env:", self.get_environ_vars())
)
2021-07-24 06:13:10 +02:00
def _normalized_keys(
self, section: str, items: Iterable[Tuple[str, Any]]
) -> Dict[str, Any]:
"""Normalizes items to construct a dictionary with normalized keys.
This routine is where the names become keys and are made the same
regardless of source - configuration files or environment.
"""
normalized = {}
for name, val in items:
2017-05-15 10:01:34 +02:00
key = section + "." + _normalize_name(name)
normalized[key] = val
return normalized
2021-07-24 06:13:10 +02:00
def get_environ_vars(self) -> Iterable[Tuple[str, str]]:
"""Returns a generator with all environmental vars with prefix PIP_"""
for key, val in os.environ.items():
if key.startswith("PIP_"):
name = key[4:].lower()
if name not in ENV_NAMES_IGNORED:
yield name, val
2017-05-15 18:38:02 +02:00
# XXX: This is patched in the tests.
2021-07-24 06:13:10 +02:00
def iter_config_files(self) -> Iterable[Tuple[Kind, List[str]]]:
2017-04-08 06:19:48 +02:00
"""Yields variant and configuration files associated with it.
This should be treated like items of a dictionary. The order
here doesn't affect what gets overridden. That is controlled
by OVERRIDE_ORDER. However this does control the order they are
displayed to the user. It's probably most ergononmic to display
things in the same order as OVERRIDE_ORDER
"""
2017-05-12 08:55:27 +02:00
# SMELL: Move the conditions out of this function
2017-04-08 06:19:48 +02:00
env_config_file = os.environ.get("PIP_CONFIG_FILE", None)
config_files = get_configuration_files()
yield kinds.GLOBAL, config_files[kinds.GLOBAL]
# per-user config is not loaded when env_config_file exists
2017-05-15 09:59:41 +02:00
should_load_user_config = not self.isolated and not (
env_config_file and os.path.exists(env_config_file)
)
2017-05-15 09:59:41 +02:00
if should_load_user_config:
2017-04-08 07:35:49 +02:00
# The legacy config file is overridden by the new config file
yield kinds.USER, config_files[kinds.USER]
# virtualenv config
yield kinds.SITE, config_files[kinds.SITE]
if env_config_file is not None:
yield kinds.ENV, [env_config_file]
else:
yield kinds.ENV, []
2021-07-24 06:13:10 +02:00
def get_values_in_config(self, variant: Kind) -> Dict[str, Any]:
2020-05-02 22:14:35 +02:00
"""Get values present in a config file"""
return self._config[variant]
2021-07-24 06:13:10 +02:00
def _get_parser_to_modify(self) -> Tuple[str, RawConfigParser]:
# Determine which parser to modify
assert self.load_only
parsers = self._parsers[self.load_only]
if not parsers:
2017-04-08 07:31:03 +02:00
# This should not happen if everything works correctly.
raise ConfigurationError(
2017-05-12 12:34:37 +02:00
"Fatal Internal error [id=2]. Please report as a bug."
2017-04-08 07:31:03 +02:00
)
# Use the highest priority parser.
return parsers[-1]
2017-04-08 07:35:49 +02:00
2017-05-12 12:42:35 +02:00
# XXX: This is patched in the tests.
2021-07-24 06:13:10 +02:00
def _mark_as_modified(self, fname: str, parser: RawConfigParser) -> None:
2017-06-01 08:08:20 +02:00
file_parser_tuple = (fname, parser)
2017-04-08 07:35:49 +02:00
if file_parser_tuple not in self._modified_parsers:
self._modified_parsers.append(file_parser_tuple)
2020-02-13 10:54:46 +01:00
2021-07-24 06:13:10 +02:00
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._dictionary!r})"