self_outdated_check.py 7.8 KB

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