configuration.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import subprocess
  5. from optparse import Values
  6. from typing import Any, Callable
  7. from pip._internal.cli.base_command import Command
  8. from pip._internal.cli.status_codes import ERROR, SUCCESS
  9. from pip._internal.configuration import (
  10. Configuration,
  11. Kind,
  12. get_configuration_files,
  13. kinds,
  14. )
  15. from pip._internal.exceptions import PipError
  16. from pip._internal.utils.logging import indent_log
  17. from pip._internal.utils.misc import get_prog, write_output
  18. logger = logging.getLogger(__name__)
  19. class ConfigurationCommand(Command):
  20. """
  21. Manage local and global configuration.
  22. Subcommands:
  23. - list: List the active configuration (or from the file specified)
  24. - edit: Edit the configuration file in an editor
  25. - get: Get the value associated with command.option
  26. - set: Set the command.option=value
  27. - unset: Unset the value associated with command.option
  28. - debug: List the configuration files and values defined under them
  29. Configuration keys should be dot separated command and option name,
  30. with the special prefix "global" affecting any command. For example,
  31. "pip config set global.index-url https://example.org/" would configure
  32. the index url for all commands, but "pip config set download.timeout 10"
  33. would configure a 10 second timeout only for "pip download" commands.
  34. If none of --user, --global and --site are passed, a virtual
  35. environment configuration file is used if one is active and the file
  36. exists. Otherwise, all modifications happen to the user file by
  37. default.
  38. """
  39. ignore_require_venv = True
  40. usage = """
  41. %prog [<file-option>] list
  42. %prog [<file-option>] [--editor <editor-path>] edit
  43. %prog [<file-option>] get command.option
  44. %prog [<file-option>] set command.option value
  45. %prog [<file-option>] unset command.option
  46. %prog [<file-option>] debug
  47. """
  48. def add_options(self) -> None:
  49. self.cmd_opts.add_option(
  50. "--editor",
  51. dest="editor",
  52. action="store",
  53. default=None,
  54. help=(
  55. "Editor to use to edit the file. Uses VISUAL or EDITOR "
  56. "environment variables if not provided."
  57. ),
  58. )
  59. self.cmd_opts.add_option(
  60. "--global",
  61. dest="global_file",
  62. action="store_true",
  63. default=False,
  64. help="Use the system-wide configuration file only",
  65. )
  66. self.cmd_opts.add_option(
  67. "--user",
  68. dest="user_file",
  69. action="store_true",
  70. default=False,
  71. help="Use the user configuration file only",
  72. )
  73. self.cmd_opts.add_option(
  74. "--site",
  75. dest="site_file",
  76. action="store_true",
  77. default=False,
  78. help="Use the current environment configuration file only",
  79. )
  80. self.parser.insert_option_group(0, self.cmd_opts)
  81. def handler_map(self) -> dict[str, Callable[[Values, list[str]], None]]:
  82. return {
  83. "list": self.list_values,
  84. "edit": self.open_in_editor,
  85. "get": self.get_name,
  86. "set": self.set_name_value,
  87. "unset": self.unset_name,
  88. "debug": self.list_config_values,
  89. }
  90. def run(self, options: Values, args: list[str]) -> int:
  91. handler_map = self.handler_map()
  92. # Determine action
  93. if not args or args[0] not in handler_map:
  94. logger.error(
  95. "Need an action (%s) to perform.",
  96. ", ".join(sorted(handler_map)),
  97. )
  98. return ERROR
  99. action = args[0]
  100. # Determine which configuration files are to be loaded
  101. # Depends on whether the command is modifying.
  102. try:
  103. load_only = self._determine_file(
  104. options, need_value=(action in ["get", "set", "unset", "edit"])
  105. )
  106. except PipError as e:
  107. logger.error(e.args[0])
  108. return ERROR
  109. # Load a new configuration
  110. self.configuration = Configuration(
  111. isolated=options.isolated_mode, load_only=load_only
  112. )
  113. self.configuration.load()
  114. # Error handling happens here, not in the action-handlers.
  115. try:
  116. handler_map[action](options, args[1:])
  117. except PipError as e:
  118. logger.error(e.args[0])
  119. return ERROR
  120. return SUCCESS
  121. def _determine_file(self, options: Values, need_value: bool) -> Kind | None:
  122. file_options = [
  123. key
  124. for key, value in (
  125. (kinds.USER, options.user_file),
  126. (kinds.GLOBAL, options.global_file),
  127. (kinds.SITE, options.site_file),
  128. )
  129. if value
  130. ]
  131. if not file_options:
  132. if not need_value:
  133. return None
  134. # Default to user, unless there's a site file.
  135. elif any(
  136. os.path.exists(site_config_file)
  137. for site_config_file in get_configuration_files()[kinds.SITE]
  138. ):
  139. return kinds.SITE
  140. else:
  141. return kinds.USER
  142. elif len(file_options) == 1:
  143. return file_options[0]
  144. raise PipError(
  145. "Need exactly one file to operate upon "
  146. "(--user, --site, --global) to perform."
  147. )
  148. def list_values(self, options: Values, args: list[str]) -> None:
  149. self._get_n_args(args, "list", n=0)
  150. for key, value in sorted(self.configuration.items()):
  151. for key, value in sorted(value.items()):
  152. write_output("%s=%r", key, value)
  153. def get_name(self, options: Values, args: list[str]) -> None:
  154. key = self._get_n_args(args, "get [name]", n=1)
  155. value = self.configuration.get_value(key)
  156. write_output("%s", value)
  157. def set_name_value(self, options: Values, args: list[str]) -> None:
  158. key, value = self._get_n_args(args, "set [name] [value]", n=2)
  159. self.configuration.set_value(key, value)
  160. self._save_configuration()
  161. def unset_name(self, options: Values, args: list[str]) -> None:
  162. key = self._get_n_args(args, "unset [name]", n=1)
  163. self.configuration.unset_value(key)
  164. self._save_configuration()
  165. def list_config_values(self, options: Values, args: list[str]) -> None:
  166. """List config key-value pairs across different config files"""
  167. self._get_n_args(args, "debug", n=0)
  168. self.print_env_var_values()
  169. # Iterate over config files and print if they exist, and the
  170. # key-value pairs present in them if they do
  171. for variant, files in sorted(self.configuration.iter_config_files()):
  172. write_output("%s:", variant)
  173. for fname in files:
  174. with indent_log():
  175. file_exists = os.path.exists(fname)
  176. write_output("%s, exists: %r", fname, file_exists)
  177. if file_exists:
  178. self.print_config_file_values(variant, fname)
  179. def print_config_file_values(self, variant: Kind, fname: str) -> None:
  180. """Get key-value pairs from the file of a variant"""
  181. for name, value in self.configuration.get_values_in_config(variant).items():
  182. with indent_log():
  183. if name == fname:
  184. for confname, confvalue in value.items():
  185. write_output("%s: %s", confname, confvalue)
  186. def print_env_var_values(self) -> None:
  187. """Get key-values pairs present as environment variables"""
  188. write_output("%s:", "env_var")
  189. with indent_log():
  190. for key, value in sorted(self.configuration.get_environ_vars()):
  191. env_var = f"PIP_{key.upper()}"
  192. write_output("%s=%r", env_var, value)
  193. def open_in_editor(self, options: Values, args: list[str]) -> None:
  194. editor = self._determine_editor(options)
  195. fname = self.configuration.get_file_to_edit()
  196. if fname is None:
  197. raise PipError("Could not determine appropriate file.")
  198. elif '"' in fname:
  199. # This shouldn't happen, unless we see a username like that.
  200. # If that happens, we'd appreciate a pull request fixing this.
  201. raise PipError(
  202. f'Can not open an editor for a file name containing "\n{fname}'
  203. )
  204. try:
  205. subprocess.check_call(f'{editor} "{fname}"', shell=True)
  206. except FileNotFoundError as e:
  207. if not e.filename:
  208. e.filename = editor
  209. raise
  210. except subprocess.CalledProcessError as e:
  211. raise PipError(f"Editor Subprocess exited with exit code {e.returncode}")
  212. def _get_n_args(self, args: list[str], example: str, n: int) -> Any:
  213. """Helper to make sure the command got the right number of arguments"""
  214. if len(args) != n:
  215. msg = (
  216. f"Got unexpected number of arguments, expected {n}. "
  217. f'(example: "{get_prog()} config {example}")'
  218. )
  219. raise PipError(msg)
  220. if n == 1:
  221. return args[0]
  222. else:
  223. return args
  224. def _save_configuration(self) -> None:
  225. # We successfully ran a modifying command. Need to save the
  226. # configuration.
  227. try:
  228. self.configuration.save()
  229. except Exception:
  230. logger.exception(
  231. "Unable to save configuration. Please report this as a bug."
  232. )
  233. raise PipError("Internal Error.")
  234. def _determine_editor(self, options: Values) -> str:
  235. if options.editor is not None:
  236. return options.editor
  237. elif "VISUAL" in os.environ:
  238. return os.environ["VISUAL"]
  239. elif "EDITOR" in os.environ:
  240. return os.environ["EDITOR"]
  241. else:
  242. raise PipError("Could not determine editor to use.")