self_outdated_check.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import datetime
  5. import hashlib
  6. import json
  7. import logging
  8. import os.path
  9. import sys
  10. from pip._vendor import pkg_resources
  11. from pip._vendor.packaging import version as packaging_version
  12. from pip._vendor.six import ensure_binary
  13. from pip._internal.collector import LinkCollector
  14. from pip._internal.index import PackageFinder
  15. from pip._internal.models.search_scope import SearchScope
  16. from pip._internal.models.selection_prefs import SelectionPreferences
  17. from pip._internal.utils.compat import WINDOWS
  18. from pip._internal.utils.filesystem import (
  19. adjacent_tmp_file,
  20. check_path_owner,
  21. replace,
  22. )
  23. from pip._internal.utils.misc import (
  24. ensure_dir,
  25. get_installed_version,
  26. redact_auth_from_url,
  27. )
  28. from pip._internal.utils.packaging import get_installer
  29. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  30. if MYPY_CHECK_RUNNING:
  31. import optparse
  32. from optparse import Values
  33. from typing import Any, Dict, Text, Union
  34. from pip._internal.network.session import PipSession
  35. SELFCHECK_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
  36. logger = logging.getLogger(__name__)
  37. def make_link_collector(
  38. session, # type: PipSession
  39. options, # type: Values
  40. suppress_no_index=False, # type: bool
  41. ):
  42. # type: (...) -> LinkCollector
  43. """
  44. :param session: The Session to use to make requests.
  45. :param suppress_no_index: Whether to ignore the --no-index option
  46. when constructing the SearchScope object.
  47. """
  48. index_urls = [options.index_url] + options.extra_index_urls
  49. if options.no_index and not suppress_no_index:
  50. logger.debug(
  51. 'Ignoring indexes: %s',
  52. ','.join(redact_auth_from_url(url) for url in index_urls),
  53. )
  54. index_urls = []
  55. # Make sure find_links is a list before passing to create().
  56. find_links = options.find_links or []
  57. search_scope = SearchScope.create(
  58. find_links=find_links, index_urls=index_urls,
  59. )
  60. link_collector = LinkCollector(session=session, search_scope=search_scope)
  61. return link_collector
  62. def _get_statefile_name(key):
  63. # type: (Union[str, Text]) -> str
  64. key_bytes = ensure_binary(key)
  65. name = hashlib.sha224(key_bytes).hexdigest()
  66. return name
  67. class SelfCheckState(object):
  68. def __init__(self, cache_dir):
  69. # type: (str) -> None
  70. self.state = {} # type: Dict[str, Any]
  71. self.statefile_path = None
  72. # Try to load the existing state
  73. if cache_dir:
  74. self.statefile_path = os.path.join(
  75. cache_dir, "selfcheck", _get_statefile_name(self.key)
  76. )
  77. try:
  78. with open(self.statefile_path) as statefile:
  79. self.state = json.load(statefile)
  80. except (IOError, ValueError, KeyError):
  81. # Explicitly suppressing exceptions, since we don't want to
  82. # error out if the cache file is invalid.
  83. pass
  84. @property
  85. def key(self):
  86. return sys.prefix
  87. def save(self, pypi_version, current_time):
  88. # type: (str, datetime.datetime) -> None
  89. # If we do not have a path to cache in, don't bother saving.
  90. if not self.statefile_path:
  91. return
  92. # Check to make sure that we own the directory
  93. if not check_path_owner(os.path.dirname(self.statefile_path)):
  94. return
  95. # Now that we've ensured the directory is owned by this user, we'll go
  96. # ahead and make sure that all our directories are created.
  97. ensure_dir(os.path.dirname(self.statefile_path))
  98. state = {
  99. # Include the key so it's easy to tell which pip wrote the
  100. # file.
  101. "key": self.key,
  102. "last_check": current_time.strftime(SELFCHECK_DATE_FMT),
  103. "pypi_version": pypi_version,
  104. }
  105. text = json.dumps(state, sort_keys=True, separators=(",", ":"))
  106. with adjacent_tmp_file(self.statefile_path) as f:
  107. f.write(ensure_binary(text))
  108. try:
  109. # Since we have a prefix-specific state file, we can just
  110. # overwrite whatever is there, no need to check.
  111. replace(f.name, self.statefile_path)
  112. except OSError:
  113. # Best effort.
  114. pass
  115. def was_installed_by_pip(pkg):
  116. # type: (str) -> bool
  117. """Checks whether pkg was installed by pip
  118. This is used not to display the upgrade message when pip is in fact
  119. installed by system package manager, such as dnf on Fedora.
  120. """
  121. try:
  122. dist = pkg_resources.get_distribution(pkg)
  123. return "pip" == get_installer(dist)
  124. except pkg_resources.DistributionNotFound:
  125. return False
  126. def pip_self_version_check(session, options):
  127. # type: (PipSession, optparse.Values) -> None
  128. """Check for an update for pip.
  129. Limit the frequency of checks to once per week. State is stored either in
  130. the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
  131. of the pip script path.
  132. """
  133. installed_version = get_installed_version("pip")
  134. if not installed_version:
  135. return
  136. pip_version = packaging_version.parse(installed_version)
  137. pypi_version = None
  138. try:
  139. state = SelfCheckState(cache_dir=options.cache_dir)
  140. current_time = datetime.datetime.utcnow()
  141. # Determine if we need to refresh the state
  142. if "last_check" in state.state and "pypi_version" in state.state:
  143. last_check = datetime.datetime.strptime(
  144. state.state["last_check"],
  145. SELFCHECK_DATE_FMT
  146. )
  147. if (current_time - last_check).total_seconds() < 7 * 24 * 60 * 60:
  148. pypi_version = state.state["pypi_version"]
  149. # Refresh the version if we need to or just see if we need to warn
  150. if pypi_version is None:
  151. # Lets use PackageFinder to see what the latest pip version is
  152. link_collector = make_link_collector(
  153. session,
  154. options=options,
  155. suppress_no_index=True,
  156. )
  157. # Pass allow_yanked=False so we don't suggest upgrading to a
  158. # yanked version.
  159. selection_prefs = SelectionPreferences(
  160. allow_yanked=False,
  161. allow_all_prereleases=False, # Explicitly set to False
  162. )
  163. finder = PackageFinder.create(
  164. link_collector=link_collector,
  165. selection_prefs=selection_prefs,
  166. )
  167. best_candidate = finder.find_best_candidate("pip").best_candidate
  168. if best_candidate is None:
  169. return
  170. pypi_version = str(best_candidate.version)
  171. # save that we've performed a check
  172. state.save(pypi_version, current_time)
  173. remote_version = packaging_version.parse(pypi_version)
  174. local_version_is_older = (
  175. pip_version < remote_version and
  176. pip_version.base_version != remote_version.base_version and
  177. was_installed_by_pip('pip')
  178. )
  179. # Determine if our pypi_version is older
  180. if not local_version_is_older:
  181. return
  182. # Advise "python -m pip" on Windows to avoid issues
  183. # with overwriting pip.exe.
  184. if WINDOWS:
  185. pip_cmd = "python -m pip"
  186. else:
  187. pip_cmd = "pip"
  188. logger.warning(
  189. "You are using pip version %s; however, version %s is "
  190. "available.\nYou should consider upgrading via the "
  191. "'%s install --upgrade pip' command.",
  192. pip_version, pypi_version, pip_cmd
  193. )
  194. except Exception:
  195. logger.debug(
  196. "There was an error checking the latest version of pip",
  197. exc_info=True,
  198. )