index_config.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """Forward the user's package-index and transport configuration into uv.
  2. PM strips ambient ``UV_*`` so a caller's uv settings cannot steer which
  3. project, interpreter or cache an operation uses (pm.environment). Index and
  4. transport knobs are different: on mirrored or air-gapped networks they are the
  5. only way any dependency resolves at all (#88453, #94613, #95608). Only those
  6. cross the boundary; the lockfile stays authoritative for what gets installed.
  7. uv never reads pip's configuration, so a pip-only mirror (``PIP_INDEX_URL`` or
  8. ``index-url`` in pip.conf) is bridged to ``UV_INDEX_URL`` unless uv already has
  9. an index of its own. Stdlib only: the bootstrap runner imports this before any
  10. dependency exists.
  11. """
  12. from __future__ import annotations
  13. from collections.abc import Mapping
  14. import configparser
  15. import os
  16. from pathlib import Path
  17. import sys
  18. # Explicit uv index / transport settings that survive into uv. UV_INDEX_<NAME>_
  19. # {USERNAME,PASSWORD} credentials match by prefix in is_forwarded().
  20. FORWARDED_UV_SETTINGS = frozenset({
  21. "UV_INDEX_URL", "UV_EXTRA_INDEX_URL", "UV_DEFAULT_INDEX", "UV_INDEX", "UV_NO_INDEX",
  22. "UV_FIND_LINKS", "UV_INDEX_STRATEGY", "UV_KEYRING_PROVIDER",
  23. "UV_NATIVE_TLS", "UV_INSECURE_HOST", "UV_HTTP_TIMEOUT",
  24. })
  25. _UV_INDEX_KNOBS = ("UV_INDEX_URL", "UV_DEFAULT_INDEX", "UV_INDEX")
  26. # pip knob → uv knob, applied only when uv has no value of its own.
  27. _PIP_TO_UV = (
  28. ("PIP_EXTRA_INDEX_URL", "UV_EXTRA_INDEX_URL"),
  29. ("PIP_TRUSTED_HOST", "UV_INSECURE_HOST"),
  30. )
  31. TIMEOUT_HINT = ("uv timed out. If your network needs a package mirror, set index-url in "
  32. "pip.conf (bridged to uv automatically) or UV_INDEX_URL; raise UV_HTTP_TIMEOUT "
  33. "for slow links.")
  34. def is_forwarded(key: str) -> bool:
  35. return key in FORWARDED_UV_SETTINGS or key.startswith("UV_INDEX_")
  36. # Settings that redirect WHERE packages resolve from (index URLs, find-links,
  37. # resolution strategy, index credentials). These are what no_config isolation
  38. # must strip from the child: re-resolving the official-index runtime lockfile
  39. # against a mirror is exactly what trips `uv sync --locked` (#124418/#125071).
  40. # Transport knobs (UV_NATIVE_TLS, UV_INSECURE_HOST, UV_HTTP_TIMEOUT) are NOT
  41. # index redirects — corporate networks need them to reach the pinned URLs at
  42. # all, so they survive no_config isolation.
  43. INDEX_REDIRECT_SETTINGS = frozenset({
  44. "UV_INDEX_URL", "UV_EXTRA_INDEX_URL", "UV_DEFAULT_INDEX", "UV_INDEX", "UV_NO_INDEX",
  45. "UV_FIND_LINKS", "UV_INDEX_STRATEGY", "UV_KEYRING_PROVIDER",
  46. })
  47. def is_index_redirect(key: str) -> bool:
  48. return key in INDEX_REDIRECT_SETTINGS or key.startswith("UV_INDEX_")
  49. def pip_config_candidates(env: Mapping[str, str]) -> list[Path]:
  50. """pip's config files, lowest precedence first, as ``pip._internal.configuration`` ranks them.
  51. Global, then user (skipped entirely when ``PIP_CONFIG_FILE`` names an existing file), then
  52. the interpreter's ``sys.prefix`` site file, then ``PIP_CONFIG_FILE`` itself on top.
  53. ``RawConfigParser.read`` applies them in order, so the last file wins.
  54. ``PIP_CONFIG_FILE=os.devnull`` disables all of them.
  55. """
  56. explicit = env.get("PIP_CONFIG_FILE", "")
  57. if explicit == os.devnull:
  58. return []
  59. home = Path.home()
  60. if sys.platform == "win32":
  61. name = "pip.ini"
  62. global_files = [Path(env.get("ProgramData") or r"C:\ProgramData") / "pip" / name]
  63. user_files = [home / "pip" / name,
  64. Path(env.get("APPDATA") or home / "AppData" / "Roaming") / "pip" / name]
  65. elif sys.platform == "darwin":
  66. name = "pip.conf"
  67. global_files = [Path("/Library/Application Support/pip") / name]
  68. app_support = home / "Library" / "Application Support" / "pip"
  69. user_files = [home / ".pip" / name,
  70. (app_support if app_support.is_dir() else home / ".config" / "pip") / name]
  71. else:
  72. name = "pip.conf"
  73. xdg_dirs = (env.get("XDG_CONFIG_DIRS") or "/etc/xdg").split(os.pathsep)
  74. global_files = [Path(d) / "pip" / name for d in xdg_dirs if d] + [Path("/etc") / name]
  75. user_files = [home / ".pip" / name, Path(env.get("XDG_CONFIG_HOME") or home / ".config") / "pip" / name]
  76. explicit_files = [Path(explicit)] if explicit else []
  77. if explicit_files and explicit_files[0].is_file():
  78. user_files = []
  79. return global_files + user_files + [Path(sys.prefix) / name] + explicit_files
  80. def pip_conf_index_url(env: Mapping[str, str]) -> str | None:
  81. # Raw: pip does not interpolate, and mirror URLs carry percent-encoded credentials.
  82. parser = configparser.RawConfigParser()
  83. try:
  84. parser.read(str(path) for path in pip_config_candidates(env))
  85. if not parser.has_section("global"):
  86. return None
  87. return parser.get("global", "index-url", fallback="").strip() or None
  88. except configparser.Error:
  89. return None
  90. NPM_PUBLIC_REGISTRY = "https://registry.npmjs.org/"
  91. def npm_registry(env: Mapping[str, str]) -> str:
  92. """The npm registry the user configured, as npm resolves it: ``npm_config_registry`` (any
  93. case) beats ``registry=`` in the user npmrc (``npm_config_userconfig`` or ``~/.npmrc``)."""
  94. lowered = {key.lower(): value for key, value in env.items()}
  95. value = (lowered.get("npm_config_registry") or "").strip()
  96. if not value:
  97. npmrc = Path(lowered.get("npm_config_userconfig") or Path.home() / ".npmrc").expanduser()
  98. try:
  99. lines = npmrc.read_text(encoding="utf-8-sig").splitlines()
  100. except (OSError, UnicodeDecodeError):
  101. lines = []
  102. for line in lines:
  103. key, sep, candidate = line.partition("=")
  104. if sep and key.strip().lower() == "registry":
  105. value = candidate.strip().strip("\"'")
  106. return value.rstrip("/") + "/" if value.startswith(("https://", "http://")) else NPM_PUBLIC_REGISTRY
  107. def npm_registry_url(url: str, env: Mapping[str, str]) -> str:
  108. """*url* on the configured npm registry; non-npm URLs are returned unchanged."""
  109. if not url.startswith(NPM_PUBLIC_REGISTRY):
  110. return url
  111. return npm_registry(env) + url[len(NPM_PUBLIC_REGISTRY):]
  112. def bridged_index_settings(ambient: Mapping[str, str]) -> dict[str, str]:
  113. """The uv index/transport settings *ambient* asks for, pip knobs translated.
  114. ``PIP_INDEX_URL`` beats pip.conf, as in pip; any explicit uv index knob beats both.
  115. """
  116. settings = {key: value for key, value in ambient.items() if is_forwarded(key)}
  117. if not any(settings.get(key) for key in _UV_INDEX_KNOBS):
  118. index_url = (ambient.get("PIP_INDEX_URL") or "").strip() or pip_conf_index_url(ambient)
  119. if index_url:
  120. settings["UV_INDEX_URL"] = index_url
  121. for pip_key, uv_key in _PIP_TO_UV:
  122. value = (ambient.get(pip_key) or "").strip()
  123. if value and not settings.get(uv_key):
  124. settings[uv_key] = value
  125. return settings