self_outdated_check.py 6.6 KB

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