req_uninstall.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. from __future__ import annotations
  2. import functools
  3. import os
  4. import sys
  5. import sysconfig
  6. from collections.abc import Generator, Iterable
  7. from importlib.util import cache_from_source
  8. from typing import Any, Callable
  9. from pip._internal.exceptions import LegacyDistutilsInstall, UninstallMissingRecord
  10. from pip._internal.locations import get_bin_prefix, get_bin_user
  11. from pip._internal.metadata import BaseDistribution
  12. from pip._internal.utils.compat import WINDOWS
  13. from pip._internal.utils.egg_link import egg_link_path_from_location
  14. from pip._internal.utils.logging import getLogger, indent_log
  15. from pip._internal.utils.misc import ask, normalize_path, renames, rmtree
  16. from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory
  17. from pip._internal.utils.virtualenv import running_under_virtualenv
  18. logger = getLogger(__name__)
  19. def _script_names(
  20. bin_dir: str, script_name: str, is_gui: bool
  21. ) -> Generator[str, None, None]:
  22. """Create the fully qualified name of the files created by
  23. {console,gui}_scripts for the given ``dist``.
  24. Returns the list of file names
  25. """
  26. exe_name = os.path.join(bin_dir, script_name)
  27. yield exe_name
  28. if not WINDOWS:
  29. return
  30. yield f"{exe_name}.exe"
  31. yield f"{exe_name}.exe.manifest"
  32. if is_gui:
  33. yield f"{exe_name}-script.pyw"
  34. else:
  35. yield f"{exe_name}-script.py"
  36. def _unique(
  37. fn: Callable[..., Generator[Any, None, None]],
  38. ) -> Callable[..., Generator[Any, None, None]]:
  39. @functools.wraps(fn)
  40. def unique(*args: Any, **kw: Any) -> Generator[Any, None, None]:
  41. seen: set[Any] = set()
  42. for item in fn(*args, **kw):
  43. if item not in seen:
  44. seen.add(item)
  45. yield item
  46. return unique
  47. @_unique
  48. def uninstallation_paths(dist: BaseDistribution) -> Generator[str, None, None]:
  49. """
  50. Yield all the uninstallation paths for dist based on RECORD-without-.py[co]
  51. Yield paths to all the files in RECORD. For each .py file in RECORD, add
  52. the .pyc and .pyo in the same directory.
  53. UninstallPathSet.add() takes care of the __pycache__ .py[co].
  54. If RECORD is not found, raises an error,
  55. with possible information from the INSTALLER file.
  56. https://packaging.python.org/specifications/recording-installed-packages/
  57. """
  58. location = dist.location
  59. assert location is not None, "not installed"
  60. entries = dist.iter_declared_entries()
  61. if entries is None:
  62. raise UninstallMissingRecord(distribution=dist)
  63. for entry in entries:
  64. path = os.path.join(location, entry)
  65. yield path
  66. if path.endswith(".py"):
  67. dn, fn = os.path.split(path)
  68. base = fn[:-3]
  69. path = os.path.join(dn, base + ".pyc")
  70. yield path
  71. path = os.path.join(dn, base + ".pyo")
  72. yield path
  73. def compact(paths: Iterable[str]) -> set[str]:
  74. """Compact a path set to contain the minimal number of paths
  75. necessary to contain all paths in the set. If /a/path/ and
  76. /a/path/to/a/file.txt are both in the set, leave only the
  77. shorter path."""
  78. sep = os.path.sep
  79. short_paths: set[str] = set()
  80. for path in sorted(paths, key=len):
  81. should_skip = any(
  82. path.startswith(shortpath.rstrip("*"))
  83. and path[len(shortpath.rstrip("*").rstrip(sep))] == sep
  84. for shortpath in short_paths
  85. )
  86. if not should_skip:
  87. short_paths.add(path)
  88. return short_paths
  89. def compress_for_rename(paths: Iterable[str]) -> set[str]:
  90. """Returns a set containing the paths that need to be renamed.
  91. This set may include directories when the original sequence of paths
  92. included every file on disk.
  93. """
  94. case_map = {os.path.normcase(p): p for p in paths}
  95. remaining = set(case_map)
  96. unchecked = sorted({os.path.split(p)[0] for p in case_map.values()}, key=len)
  97. wildcards: set[str] = set()
  98. def norm_join(*a: str) -> str:
  99. return os.path.normcase(os.path.join(*a))
  100. for root in unchecked:
  101. if any(os.path.normcase(root).startswith(w) for w in wildcards):
  102. # This directory has already been handled.
  103. continue
  104. all_files: set[str] = set()
  105. all_subdirs: set[str] = set()
  106. for dirname, subdirs, files in os.walk(root):
  107. all_subdirs.update(norm_join(root, dirname, d) for d in subdirs)
  108. all_files.update(norm_join(root, dirname, f) for f in files)
  109. # If all the files we found are in our remaining set of files to
  110. # remove, then remove them from the latter set and add a wildcard
  111. # for the directory.
  112. if not (all_files - remaining):
  113. remaining.difference_update(all_files)
  114. wildcards.add(root + os.sep)
  115. return set(map(case_map.__getitem__, remaining)) | wildcards
  116. def compress_for_output_listing(paths: Iterable[str]) -> tuple[set[str], set[str]]:
  117. """Returns a tuple of 2 sets of which paths to display to user
  118. The first set contains paths that would be deleted. Files of a package
  119. are not added and the top-level directory of the package has a '*' added
  120. at the end - to signify that all it's contents are removed.
  121. The second set contains files that would have been skipped in the above
  122. folders.
  123. """
  124. will_remove = set(paths)
  125. will_skip = set()
  126. # Determine folders and files
  127. folders = set()
  128. files = set()
  129. for path in will_remove:
  130. if path.endswith(".pyc"):
  131. continue
  132. if path.endswith("__init__.py") or ".dist-info" in path:
  133. folders.add(os.path.dirname(path))
  134. files.add(path)
  135. _normcased_files = set(map(os.path.normcase, files))
  136. folders = compact(folders)
  137. # This walks the tree using os.walk to not miss extra folders
  138. # that might get added.
  139. for folder in folders:
  140. for dirpath, _, dirfiles in os.walk(folder):
  141. for fname in dirfiles:
  142. if fname.endswith(".pyc"):
  143. continue
  144. file_ = os.path.join(dirpath, fname)
  145. if (
  146. os.path.isfile(file_)
  147. and os.path.normcase(file_) not in _normcased_files
  148. ):
  149. # We are skipping this file. Add it to the set.
  150. will_skip.add(file_)
  151. will_remove = files | {os.path.join(folder, "*") for folder in folders}
  152. return will_remove, will_skip
  153. class StashedUninstallPathSet:
  154. """A set of file rename operations to stash files while
  155. tentatively uninstalling them."""
  156. def __init__(self) -> None:
  157. # Mapping from source file root to [Adjacent]TempDirectory
  158. # for files under that directory.
  159. self._save_dirs: dict[str, TempDirectory] = {}
  160. # (old path, new path) tuples for each move that may need
  161. # to be undone.
  162. self._moves: list[tuple[str, str]] = []
  163. def _get_directory_stash(self, path: str) -> str:
  164. """Stashes a directory.
  165. Directories are stashed adjacent to their original location if
  166. possible, or else moved/copied into the user's temp dir."""
  167. try:
  168. save_dir: TempDirectory = AdjacentTempDirectory(path)
  169. except OSError:
  170. save_dir = TempDirectory(kind="uninstall")
  171. self._save_dirs[os.path.normcase(path)] = save_dir
  172. return save_dir.path
  173. def _get_file_stash(self, path: str) -> str:
  174. """Stashes a file.
  175. If no root has been provided, one will be created for the directory
  176. in the user's temp directory."""
  177. path = os.path.normcase(path)
  178. head, old_head = os.path.dirname(path), None
  179. save_dir = None
  180. while head != old_head:
  181. try:
  182. save_dir = self._save_dirs[head]
  183. break
  184. except KeyError:
  185. pass
  186. head, old_head = os.path.dirname(head), head
  187. else:
  188. # Did not find any suitable root
  189. head = os.path.dirname(path)
  190. save_dir = TempDirectory(kind="uninstall")
  191. self._save_dirs[head] = save_dir
  192. relpath = os.path.relpath(path, head)
  193. if relpath and relpath != os.path.curdir:
  194. return os.path.join(save_dir.path, relpath)
  195. return save_dir.path
  196. def stash(self, path: str) -> str:
  197. """Stashes the directory or file and returns its new location.
  198. Handle symlinks as files to avoid modifying the symlink targets.
  199. """
  200. path_is_dir = os.path.isdir(path) and not os.path.islink(path)
  201. if path_is_dir:
  202. new_path = self._get_directory_stash(path)
  203. else:
  204. new_path = self._get_file_stash(path)
  205. self._moves.append((path, new_path))
  206. if path_is_dir and os.path.isdir(new_path):
  207. # If we're moving a directory, we need to
  208. # remove the destination first or else it will be
  209. # moved to inside the existing directory.
  210. # We just created new_path ourselves, so it will
  211. # be removable.
  212. os.rmdir(new_path)
  213. renames(path, new_path)
  214. return new_path
  215. def commit(self) -> None:
  216. """Commits the uninstall by removing stashed files."""
  217. for save_dir in self._save_dirs.values():
  218. save_dir.cleanup()
  219. self._moves = []
  220. self._save_dirs = {}
  221. def rollback(self) -> None:
  222. """Undoes the uninstall by moving stashed files back."""
  223. for p in self._moves:
  224. logger.info("Moving to %s\n from %s", *p)
  225. for new_path, path in self._moves:
  226. try:
  227. logger.debug("Replacing %s from %s", new_path, path)
  228. if os.path.isfile(new_path) or os.path.islink(new_path):
  229. os.unlink(new_path)
  230. elif os.path.isdir(new_path):
  231. rmtree(new_path)
  232. renames(path, new_path)
  233. except OSError as ex:
  234. logger.error("Failed to restore %s", new_path)
  235. logger.debug("Exception: %s", ex)
  236. self.commit()
  237. @property
  238. def can_rollback(self) -> bool:
  239. return bool(self._moves)
  240. class UninstallPathSet:
  241. """A set of file paths to be removed in the uninstallation of a
  242. requirement."""
  243. def __init__(self, dist: BaseDistribution) -> None:
  244. self._paths: set[str] = set()
  245. self._refuse: set[str] = set()
  246. self._pth: dict[str, UninstallPthEntries] = {}
  247. self._dist = dist
  248. self._moved_paths = StashedUninstallPathSet()
  249. # Create local cache of normalize_path results. Creating an UninstallPathSet
  250. # can result in hundreds/thousands of redundant calls to normalize_path with
  251. # the same args, which hurts performance.
  252. self._normalize_path_cached = functools.lru_cache(normalize_path)
  253. def _permitted(self, path: str) -> bool:
  254. """
  255. Return True if the given path is one we are permitted to
  256. remove/modify, False otherwise.
  257. """
  258. # aka is_local, but caching normalized sys.prefix
  259. if not running_under_virtualenv():
  260. return True
  261. return path.startswith(self._normalize_path_cached(sys.prefix))
  262. def add(self, path: str) -> None:
  263. head, tail = os.path.split(path)
  264. # we normalize the head to resolve parent directory symlinks, but not
  265. # the tail, since we only want to uninstall symlinks, not their targets
  266. path = os.path.join(self._normalize_path_cached(head), os.path.normcase(tail))
  267. if not os.path.exists(path):
  268. return
  269. if self._permitted(path):
  270. self._paths.add(path)
  271. else:
  272. self._refuse.add(path)
  273. # __pycache__ files can show up after 'installed-files.txt' is created,
  274. # due to imports
  275. if os.path.splitext(path)[1] == ".py":
  276. self.add(cache_from_source(path))
  277. def add_pth(self, pth_file: str, entry: str) -> None:
  278. pth_file = self._normalize_path_cached(pth_file)
  279. if self._permitted(pth_file):
  280. if pth_file not in self._pth:
  281. self._pth[pth_file] = UninstallPthEntries(pth_file)
  282. self._pth[pth_file].add(entry)
  283. else:
  284. self._refuse.add(pth_file)
  285. def remove(self, auto_confirm: bool = False, verbose: bool = False) -> None:
  286. """Remove paths in ``self._paths`` with confirmation (unless
  287. ``auto_confirm`` is True)."""
  288. if not self._paths:
  289. logger.info(
  290. "Can't uninstall '%s'. No files were found to uninstall.",
  291. self._dist.raw_name,
  292. )
  293. return
  294. dist_name_version = f"{self._dist.raw_name}-{self._dist.raw_version}"
  295. logger.info("Uninstalling %s:", dist_name_version)
  296. with indent_log():
  297. if auto_confirm or self._allowed_to_proceed(verbose):
  298. moved = self._moved_paths
  299. for_rename = compress_for_rename(self._paths)
  300. for path in sorted(compact(for_rename)):
  301. moved.stash(path)
  302. logger.verbose("Removing file or directory %s", path)
  303. for pth in self._pth.values():
  304. pth.remove()
  305. logger.info("Successfully uninstalled %s", dist_name_version)
  306. def _allowed_to_proceed(self, verbose: bool) -> bool:
  307. """Display which files would be deleted and prompt for confirmation"""
  308. def _display(msg: str, paths: Iterable[str]) -> None:
  309. if not paths:
  310. return
  311. logger.info(msg)
  312. with indent_log():
  313. for path in sorted(compact(paths)):
  314. logger.info(path)
  315. if not verbose:
  316. will_remove, will_skip = compress_for_output_listing(self._paths)
  317. else:
  318. # In verbose mode, display all the files that are going to be
  319. # deleted.
  320. will_remove = set(self._paths)
  321. will_skip = set()
  322. _display("Would remove:", will_remove)
  323. _display("Would not remove (might be manually added):", will_skip)
  324. _display("Would not remove (outside of prefix):", self._refuse)
  325. if verbose:
  326. _display("Will actually move:", compress_for_rename(self._paths))
  327. return ask("Proceed (Y/n)? ", ("y", "n", "")) != "n"
  328. def rollback(self) -> None:
  329. """Rollback the changes previously made by remove()."""
  330. if not self._moved_paths.can_rollback:
  331. logger.error(
  332. "Can't roll back %s; was not uninstalled",
  333. self._dist.raw_name,
  334. )
  335. return
  336. logger.info("Rolling back uninstall of %s", self._dist.raw_name)
  337. self._moved_paths.rollback()
  338. for pth in self._pth.values():
  339. pth.rollback()
  340. def commit(self) -> None:
  341. """Remove temporary save dir: rollback will no longer be possible."""
  342. self._moved_paths.commit()
  343. @classmethod
  344. def from_dist(cls, dist: BaseDistribution) -> UninstallPathSet:
  345. dist_location = dist.location
  346. info_location = dist.info_location
  347. if dist_location is None:
  348. logger.info(
  349. "Not uninstalling %s since it is not installed",
  350. dist.canonical_name,
  351. )
  352. return cls(dist)
  353. normalized_dist_location = normalize_path(dist_location)
  354. if not dist.local:
  355. logger.info(
  356. "Not uninstalling %s at %s, outside environment %s",
  357. dist.canonical_name,
  358. normalized_dist_location,
  359. sys.prefix,
  360. )
  361. return cls(dist)
  362. if normalized_dist_location in {
  363. p
  364. for p in {sysconfig.get_path("stdlib"), sysconfig.get_path("platstdlib")}
  365. if p
  366. }:
  367. logger.info(
  368. "Not uninstalling %s at %s, as it is in the standard library.",
  369. dist.canonical_name,
  370. normalized_dist_location,
  371. )
  372. return cls(dist)
  373. paths_to_remove = cls(dist)
  374. develop_egg_link = egg_link_path_from_location(dist.raw_name)
  375. # Distribution is installed with metadata in a "flat" .egg-info
  376. # directory. This means it is not a modern .dist-info installation, an
  377. # egg, or legacy editable.
  378. setuptools_flat_installation = (
  379. dist.installed_with_setuptools_egg_info
  380. and info_location is not None
  381. and os.path.exists(info_location)
  382. # If dist is editable and the location points to a ``.egg-info``,
  383. # we are in fact in the legacy editable case.
  384. and not info_location.endswith(f"{dist.setuptools_filename}.egg-info")
  385. )
  386. # Uninstall cases order do matter as in the case of 2 installs of the
  387. # same package, pip needs to uninstall the currently detected version
  388. if setuptools_flat_installation:
  389. if info_location is not None:
  390. paths_to_remove.add(info_location)
  391. installed_files = dist.iter_declared_entries()
  392. if installed_files is not None:
  393. for installed_file in installed_files:
  394. paths_to_remove.add(os.path.join(dist_location, installed_file))
  395. # FIXME: need a test for this elif block
  396. # occurs with --single-version-externally-managed/--record outside
  397. # of pip
  398. elif dist.is_file("top_level.txt"):
  399. try:
  400. namespace_packages = dist.read_text("namespace_packages.txt")
  401. except FileNotFoundError:
  402. namespaces = []
  403. else:
  404. namespaces = namespace_packages.splitlines(keepends=False)
  405. for top_level_pkg in [
  406. p
  407. for p in dist.read_text("top_level.txt").splitlines()
  408. if p and p not in namespaces
  409. ]:
  410. path = os.path.join(dist_location, top_level_pkg)
  411. paths_to_remove.add(path)
  412. paths_to_remove.add(f"{path}.py")
  413. paths_to_remove.add(f"{path}.pyc")
  414. paths_to_remove.add(f"{path}.pyo")
  415. elif dist.installed_by_distutils:
  416. raise LegacyDistutilsInstall(distribution=dist)
  417. elif dist.installed_as_egg:
  418. # package installed by easy_install
  419. # We cannot match on dist.egg_name because it can slightly vary
  420. # i.e. setuptools-0.6c11-py2.6.egg vs setuptools-0.6rc11-py2.6.egg
  421. # XXX We use normalized_dist_location because dist_location my contain
  422. # a trailing / if the distribution is a zipped egg
  423. # (which is not a directory).
  424. paths_to_remove.add(normalized_dist_location)
  425. easy_install_egg = os.path.split(normalized_dist_location)[1]
  426. easy_install_pth = os.path.join(
  427. os.path.dirname(normalized_dist_location),
  428. "easy-install.pth",
  429. )
  430. paths_to_remove.add_pth(easy_install_pth, "./" + easy_install_egg)
  431. elif dist.installed_with_dist_info:
  432. for path in uninstallation_paths(dist):
  433. paths_to_remove.add(path)
  434. elif develop_egg_link:
  435. # PEP 660 modern editable is handled in the ``.dist-info`` case
  436. # above, so this only covers the setuptools-style editable.
  437. with open(develop_egg_link) as fh:
  438. link_pointer = os.path.normcase(fh.readline().strip())
  439. normalized_link_pointer = paths_to_remove._normalize_path_cached(
  440. link_pointer
  441. )
  442. assert os.path.samefile(
  443. normalized_link_pointer, normalized_dist_location
  444. ), (
  445. f"Egg-link {develop_egg_link} (to {link_pointer}) does not match "
  446. f"installed location of {dist.raw_name} (at {dist_location})"
  447. )
  448. paths_to_remove.add(develop_egg_link)
  449. easy_install_pth = os.path.join(
  450. os.path.dirname(develop_egg_link), "easy-install.pth"
  451. )
  452. paths_to_remove.add_pth(easy_install_pth, dist_location)
  453. else:
  454. logger.debug(
  455. "Not sure how to uninstall: %s - Check: %s",
  456. dist,
  457. dist_location,
  458. )
  459. if dist.in_usersite:
  460. bin_dir = get_bin_user()
  461. else:
  462. bin_dir = get_bin_prefix()
  463. # find distutils scripts= scripts
  464. try:
  465. for script in dist.iter_distutils_script_names():
  466. paths_to_remove.add(os.path.join(bin_dir, script))
  467. if WINDOWS:
  468. paths_to_remove.add(os.path.join(bin_dir, f"{script}.bat"))
  469. except (FileNotFoundError, NotADirectoryError):
  470. pass
  471. # find console_scripts and gui_scripts
  472. def iter_scripts_to_remove(
  473. dist: BaseDistribution,
  474. bin_dir: str,
  475. ) -> Generator[str, None, None]:
  476. for entry_point in dist.iter_entry_points():
  477. if entry_point.group == "console_scripts":
  478. yield from _script_names(bin_dir, entry_point.name, False)
  479. elif entry_point.group == "gui_scripts":
  480. yield from _script_names(bin_dir, entry_point.name, True)
  481. for s in iter_scripts_to_remove(dist, bin_dir):
  482. paths_to_remove.add(s)
  483. return paths_to_remove
  484. class UninstallPthEntries:
  485. def __init__(self, pth_file: str) -> None:
  486. self.file = pth_file
  487. self.entries: set[str] = set()
  488. self._saved_lines: list[bytes] | None = None
  489. def add(self, entry: str) -> None:
  490. entry = os.path.normcase(entry)
  491. # On Windows, os.path.normcase converts the entry to use
  492. # backslashes. This is correct for entries that describe absolute
  493. # paths outside of site-packages, but all the others use forward
  494. # slashes.
  495. # os.path.splitdrive is used instead of os.path.isabs because isabs
  496. # treats non-absolute paths with drive letter markings like c:foo\bar
  497. # as absolute paths. It also does not recognize UNC paths if they don't
  498. # have more than "\\sever\share". Valid examples: "\\server\share\" or
  499. # "\\server\share\folder".
  500. if WINDOWS and not os.path.splitdrive(entry)[0]:
  501. entry = entry.replace("\\", "/")
  502. self.entries.add(entry)
  503. def remove(self) -> None:
  504. logger.verbose("Removing pth entries from %s:", self.file)
  505. # If the file doesn't exist, log a warning and return
  506. if not os.path.isfile(self.file):
  507. logger.warning("Cannot remove entries from nonexistent file %s", self.file)
  508. return
  509. with open(self.file, "rb") as fh:
  510. # windows uses '\r\n' with py3k, but uses '\n' with py2.x
  511. lines = fh.readlines()
  512. self._saved_lines = lines
  513. if any(b"\r\n" in line for line in lines):
  514. endline = "\r\n"
  515. else:
  516. endline = "\n"
  517. # handle missing trailing newline
  518. if lines and not lines[-1].endswith(endline.encode("utf-8")):
  519. lines[-1] = lines[-1] + endline.encode("utf-8")
  520. for entry in self.entries:
  521. try:
  522. logger.verbose("Removing entry: %s", entry)
  523. lines.remove((entry + endline).encode("utf-8"))
  524. except ValueError:
  525. pass
  526. with open(self.file, "wb") as fh:
  527. fh.writelines(lines)
  528. def rollback(self) -> bool:
  529. if self._saved_lines is None:
  530. logger.error("Cannot roll back changes to %s, none were made", self.file)
  531. return False
  532. logger.debug("Rolling %s back to previous state", self.file)
  533. with open(self.file, "wb") as fh:
  534. fh.writelines(self._saved_lines)
  535. return True