req_uninstall.py 23 KB

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