configuration.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. """Configuration management setup
  2. Some terminology:
  3. - name
  4. As written in config files.
  5. - value
  6. Value associated with a name
  7. - key
  8. Name combined with it's section (section.name)
  9. - variant
  10. A single word describing where the configuration key-value pair came from
  11. """
  12. import configparser
  13. import locale
  14. import logging
  15. import os
  16. import sys
  17. from typing import Any, Dict, Iterable, List, NewType, Optional, Tuple
  18. from pip._internal.exceptions import (
  19. ConfigurationError,
  20. ConfigurationFileCouldNotBeLoaded,
  21. )
  22. from pip._internal.utils import appdirs
  23. from pip._internal.utils.compat import WINDOWS
  24. from pip._internal.utils.misc import ensure_dir, enum
  25. RawConfigParser = configparser.RawConfigParser # Shorthand
  26. Kind = NewType("Kind", str)
  27. CONFIG_BASENAME = 'pip.ini' if WINDOWS else 'pip.conf'
  28. ENV_NAMES_IGNORED = "version", "help"
  29. # The kinds of configurations there are.
  30. kinds = enum(
  31. USER="user", # User Specific
  32. GLOBAL="global", # System Wide
  33. SITE="site", # [Virtual] Environment Specific
  34. ENV="env", # from PIP_CONFIG_FILE
  35. ENV_VAR="env-var", # from Environment Variables
  36. )
  37. OVERRIDE_ORDER = kinds.GLOBAL, kinds.USER, kinds.SITE, kinds.ENV, kinds.ENV_VAR
  38. VALID_LOAD_ONLY = kinds.USER, kinds.GLOBAL, kinds.SITE
  39. logger = logging.getLogger(__name__)
  40. # NOTE: Maybe use the optionx attribute to normalize keynames.
  41. def _normalize_name(name):
  42. # type: (str) -> str
  43. """Make a name consistent regardless of source (environment or file)
  44. """
  45. name = name.lower().replace('_', '-')
  46. if name.startswith('--'):
  47. name = name[2:] # only prefer long opts
  48. return name
  49. def _disassemble_key(name):
  50. # type: (str) -> List[str]
  51. if "." not in name:
  52. error_message = (
  53. "Key does not contain dot separated section and key. "
  54. "Perhaps you wanted to use 'global.{}' instead?"
  55. ).format(name)
  56. raise ConfigurationError(error_message)
  57. return name.split(".", 1)
  58. def get_configuration_files():
  59. # type: () -> Dict[Kind, List[str]]
  60. global_config_files = [
  61. os.path.join(path, CONFIG_BASENAME)
  62. for path in appdirs.site_config_dirs('pip')
  63. ]
  64. site_config_file = os.path.join(sys.prefix, CONFIG_BASENAME)
  65. legacy_config_file = os.path.join(
  66. os.path.expanduser('~'),
  67. 'pip' if WINDOWS else '.pip',
  68. CONFIG_BASENAME,
  69. )
  70. new_config_file = os.path.join(
  71. appdirs.user_config_dir("pip"), CONFIG_BASENAME
  72. )
  73. return {
  74. kinds.GLOBAL: global_config_files,
  75. kinds.SITE: [site_config_file],
  76. kinds.USER: [legacy_config_file, new_config_file],
  77. }
  78. class Configuration:
  79. """Handles management of configuration.
  80. Provides an interface to accessing and managing configuration files.
  81. This class converts provides an API that takes "section.key-name" style
  82. keys and stores the value associated with it as "key-name" under the
  83. section "section".
  84. This allows for a clean interface wherein the both the section and the
  85. key-name are preserved in an easy to manage form in the configuration files
  86. and the data stored is also nice.
  87. """
  88. def __init__(self, isolated, load_only=None):
  89. # type: (bool, Optional[Kind]) -> None
  90. super().__init__()
  91. if load_only is not None and load_only not in VALID_LOAD_ONLY:
  92. raise ConfigurationError(
  93. "Got invalid value for load_only - should be one of {}".format(
  94. ", ".join(map(repr, VALID_LOAD_ONLY))
  95. )
  96. )
  97. self.isolated = isolated
  98. self.load_only = load_only
  99. # Because we keep track of where we got the data from
  100. self._parsers = {
  101. variant: [] for variant in OVERRIDE_ORDER
  102. } # type: Dict[Kind, List[Tuple[str, RawConfigParser]]]
  103. self._config = {
  104. variant: {} for variant in OVERRIDE_ORDER
  105. } # type: Dict[Kind, Dict[str, Any]]
  106. self._modified_parsers = [] # type: List[Tuple[str, RawConfigParser]]
  107. def load(self):
  108. # type: () -> None
  109. """Loads configuration from configuration files and environment
  110. """
  111. self._load_config_files()
  112. if not self.isolated:
  113. self._load_environment_vars()
  114. def get_file_to_edit(self):
  115. # type: () -> Optional[str]
  116. """Returns the file with highest priority in configuration
  117. """
  118. assert self.load_only is not None, \
  119. "Need to be specified a file to be editing"
  120. try:
  121. return self._get_parser_to_modify()[0]
  122. except IndexError:
  123. return None
  124. def items(self):
  125. # type: () -> Iterable[Tuple[str, Any]]
  126. """Returns key-value pairs like dict.items() representing the loaded
  127. configuration
  128. """
  129. return self._dictionary.items()
  130. def get_value(self, key):
  131. # type: (str) -> Any
  132. """Get a value from the configuration.
  133. """
  134. try:
  135. return self._dictionary[key]
  136. except KeyError:
  137. raise ConfigurationError(f"No such key - {key}")
  138. def set_value(self, key, value):
  139. # type: (str, Any) -> None
  140. """Modify a value in the configuration.
  141. """
  142. self._ensure_have_load_only()
  143. assert self.load_only
  144. fname, parser = self._get_parser_to_modify()
  145. if parser is not None:
  146. section, name = _disassemble_key(key)
  147. # Modify the parser and the configuration
  148. if not parser.has_section(section):
  149. parser.add_section(section)
  150. parser.set(section, name, value)
  151. self._config[self.load_only][key] = value
  152. self._mark_as_modified(fname, parser)
  153. def unset_value(self, key):
  154. # type: (str) -> None
  155. """Unset a value in the configuration."""
  156. self._ensure_have_load_only()
  157. assert self.load_only
  158. if key not in self._config[self.load_only]:
  159. raise ConfigurationError(f"No such key - {key}")
  160. fname, parser = self._get_parser_to_modify()
  161. if parser is not None:
  162. section, name = _disassemble_key(key)
  163. if not (parser.has_section(section)
  164. and parser.remove_option(section, name)):
  165. # The option was not removed.
  166. raise ConfigurationError(
  167. "Fatal Internal error [id=1]. Please report as a bug."
  168. )
  169. # The section may be empty after the option was removed.
  170. if not parser.items(section):
  171. parser.remove_section(section)
  172. self._mark_as_modified(fname, parser)
  173. del self._config[self.load_only][key]
  174. def save(self):
  175. # type: () -> None
  176. """Save the current in-memory state.
  177. """
  178. self._ensure_have_load_only()
  179. for fname, parser in self._modified_parsers:
  180. logger.info("Writing to %s", fname)
  181. # Ensure directory exists.
  182. ensure_dir(os.path.dirname(fname))
  183. with open(fname, "w") as f:
  184. parser.write(f)
  185. #
  186. # Private routines
  187. #
  188. def _ensure_have_load_only(self):
  189. # type: () -> None
  190. if self.load_only is None:
  191. raise ConfigurationError("Needed a specific file to be modifying.")
  192. logger.debug("Will be working with %s variant only", self.load_only)
  193. @property
  194. def _dictionary(self):
  195. # type: () -> Dict[str, Any]
  196. """A dictionary representing the loaded configuration.
  197. """
  198. # NOTE: Dictionaries are not populated if not loaded. So, conditionals
  199. # are not needed here.
  200. retval = {}
  201. for variant in OVERRIDE_ORDER:
  202. retval.update(self._config[variant])
  203. return retval
  204. def _load_config_files(self):
  205. # type: () -> None
  206. """Loads configuration from configuration files
  207. """
  208. config_files = dict(self.iter_config_files())
  209. if config_files[kinds.ENV][0:1] == [os.devnull]:
  210. logger.debug(
  211. "Skipping loading configuration files due to "
  212. "environment's PIP_CONFIG_FILE being os.devnull"
  213. )
  214. return
  215. for variant, files in config_files.items():
  216. for fname in files:
  217. # If there's specific variant set in `load_only`, load only
  218. # that variant, not the others.
  219. if self.load_only is not None and variant != self.load_only:
  220. logger.debug(
  221. "Skipping file '%s' (variant: %s)", fname, variant
  222. )
  223. continue
  224. parser = self._load_file(variant, fname)
  225. # Keeping track of the parsers used
  226. self._parsers[variant].append((fname, parser))
  227. def _load_file(self, variant, fname):
  228. # type: (Kind, str) -> RawConfigParser
  229. logger.debug("For variant '%s', will try loading '%s'", variant, fname)
  230. parser = self._construct_parser(fname)
  231. for section in parser.sections():
  232. items = parser.items(section)
  233. self._config[variant].update(self._normalized_keys(section, items))
  234. return parser
  235. def _construct_parser(self, fname):
  236. # type: (str) -> RawConfigParser
  237. parser = configparser.RawConfigParser()
  238. # If there is no such file, don't bother reading it but create the
  239. # parser anyway, to hold the data.
  240. # Doing this is useful when modifying and saving files, where we don't
  241. # need to construct a parser.
  242. if os.path.exists(fname):
  243. try:
  244. parser.read(fname)
  245. except UnicodeDecodeError:
  246. # See https://github.com/pypa/pip/issues/4963
  247. raise ConfigurationFileCouldNotBeLoaded(
  248. reason="contains invalid {} characters".format(
  249. locale.getpreferredencoding(False)
  250. ),
  251. fname=fname,
  252. )
  253. except configparser.Error as error:
  254. # See https://github.com/pypa/pip/issues/4893
  255. raise ConfigurationFileCouldNotBeLoaded(error=error)
  256. return parser
  257. def _load_environment_vars(self):
  258. # type: () -> None
  259. """Loads configuration from environment variables
  260. """
  261. self._config[kinds.ENV_VAR].update(
  262. self._normalized_keys(":env:", self.get_environ_vars())
  263. )
  264. def _normalized_keys(self, section, items):
  265. # type: (str, Iterable[Tuple[str, Any]]) -> Dict[str, Any]
  266. """Normalizes items to construct a dictionary with normalized keys.
  267. This routine is where the names become keys and are made the same
  268. regardless of source - configuration files or environment.
  269. """
  270. normalized = {}
  271. for name, val in items:
  272. key = section + "." + _normalize_name(name)
  273. normalized[key] = val
  274. return normalized
  275. def get_environ_vars(self):
  276. # type: () -> Iterable[Tuple[str, str]]
  277. """Returns a generator with all environmental vars with prefix PIP_"""
  278. for key, val in os.environ.items():
  279. if key.startswith("PIP_"):
  280. name = key[4:].lower()
  281. if name not in ENV_NAMES_IGNORED:
  282. yield name, val
  283. # XXX: This is patched in the tests.
  284. def iter_config_files(self):
  285. # type: () -> Iterable[Tuple[Kind, List[str]]]
  286. """Yields variant and configuration files associated with it.
  287. This should be treated like items of a dictionary.
  288. """
  289. # SMELL: Move the conditions out of this function
  290. # environment variables have the lowest priority
  291. config_file = os.environ.get('PIP_CONFIG_FILE', None)
  292. if config_file is not None:
  293. yield kinds.ENV, [config_file]
  294. else:
  295. yield kinds.ENV, []
  296. config_files = get_configuration_files()
  297. # at the base we have any global configuration
  298. yield kinds.GLOBAL, config_files[kinds.GLOBAL]
  299. # per-user configuration next
  300. should_load_user_config = not self.isolated and not (
  301. config_file and os.path.exists(config_file)
  302. )
  303. if should_load_user_config:
  304. # The legacy config file is overridden by the new config file
  305. yield kinds.USER, config_files[kinds.USER]
  306. # finally virtualenv configuration first trumping others
  307. yield kinds.SITE, config_files[kinds.SITE]
  308. def get_values_in_config(self, variant):
  309. # type: (Kind) -> Dict[str, Any]
  310. """Get values present in a config file"""
  311. return self._config[variant]
  312. def _get_parser_to_modify(self):
  313. # type: () -> Tuple[str, RawConfigParser]
  314. # Determine which parser to modify
  315. assert self.load_only
  316. parsers = self._parsers[self.load_only]
  317. if not parsers:
  318. # This should not happen if everything works correctly.
  319. raise ConfigurationError(
  320. "Fatal Internal error [id=2]. Please report as a bug."
  321. )
  322. # Use the highest priority parser.
  323. return parsers[-1]
  324. # XXX: This is patched in the tests.
  325. def _mark_as_modified(self, fname, parser):
  326. # type: (str, RawConfigParser) -> None
  327. file_parser_tuple = (fname, parser)
  328. if file_parser_tuple not in self._modified_parsers:
  329. self._modified_parsers.append(file_parser_tuple)
  330. def __repr__(self):
  331. # type: () -> str
  332. return f"{self.__class__.__name__}({self._dictionary!r})"