Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

outdated.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from __future__ import absolute_import
  2. import datetime
  3. import json
  4. import logging
  5. import os.path
  6. import sys
  7. from pip._vendor import lockfile, pkg_resources
  8. from pip._vendor.packaging import version as packaging_version
  9. from pip._internal.index import PackageFinder
  10. from pip._internal.utils.compat import WINDOWS
  11. from pip._internal.utils.filesystem import check_path_owner
  12. from pip._internal.utils.misc import ensure_dir, get_installed_version
  13. SELFCHECK_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
  14. logger = logging.getLogger(__name__)
  15. class SelfCheckState(object):
  16. def __init__(self, cache_dir):
  17. self.state = {}
  18. self.statefile_path = None
  19. # Try to load the existing state
  20. if cache_dir:
  21. self.statefile_path = os.path.join(cache_dir, "selfcheck.json")
  22. try:
  23. with open(self.statefile_path) as statefile:
  24. self.state = json.load(statefile)[sys.prefix]
  25. except (IOError, ValueError, KeyError):
  26. # Explicitly suppressing exceptions, since we don't want to
  27. # error out if the cache file is invalid.
  28. pass
  29. def save(self, pypi_version, current_time):
  30. # If we do not have a path to cache in, don't bother saving.
  31. if not self.statefile_path:
  32. return
  33. # Check to make sure that we own the directory
  34. if not check_path_owner(os.path.dirname(self.statefile_path)):
  35. return
  36. # Now that we've ensured the directory is owned by this user, we'll go
  37. # ahead and make sure that all our directories are created.
  38. ensure_dir(os.path.dirname(self.statefile_path))
  39. # Attempt to write out our version check file
  40. with lockfile.LockFile(self.statefile_path):
  41. if os.path.exists(self.statefile_path):
  42. with open(self.statefile_path) as statefile:
  43. state = json.load(statefile)
  44. else:
  45. state = {}
  46. state[sys.prefix] = {
  47. "last_check": current_time.strftime(SELFCHECK_DATE_FMT),
  48. "pypi_version": pypi_version,
  49. }
  50. with open(self.statefile_path, "w") as statefile:
  51. json.dump(state, statefile, sort_keys=True,
  52. separators=(",", ":"))
  53. def was_installed_by_pip(pkg):
  54. """Checks whether pkg was installed by pip
  55. This is used not to display the upgrade message when pip is in fact
  56. installed by system package manager, such as dnf on Fedora.
  57. """
  58. try:
  59. dist = pkg_resources.get_distribution(pkg)
  60. return (dist.has_metadata('INSTALLER') and
  61. 'pip' in dist.get_metadata_lines('INSTALLER'))
  62. except pkg_resources.DistributionNotFound:
  63. return False
  64. def pip_version_check(session, options):
  65. """Check for an update for pip.
  66. Limit the frequency of checks to once per week. State is stored either in
  67. the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
  68. of the pip script path.
  69. """
  70. installed_version = get_installed_version("pip")
  71. if not installed_version:
  72. return
  73. pip_version = packaging_version.parse(installed_version)
  74. pypi_version = None
  75. try:
  76. state = SelfCheckState(cache_dir=options.cache_dir)
  77. current_time = datetime.datetime.utcnow()
  78. # Determine if we need to refresh the state
  79. if "last_check" in state.state and "pypi_version" in state.state:
  80. last_check = datetime.datetime.strptime(
  81. state.state["last_check"],
  82. SELFCHECK_DATE_FMT
  83. )
  84. if (current_time - last_check).total_seconds() < 7 * 24 * 60 * 60:
  85. pypi_version = state.state["pypi_version"]
  86. # Refresh the version if we need to or just see if we need to warn
  87. if pypi_version is None:
  88. # Lets use PackageFinder to see what the latest pip version is
  89. finder = PackageFinder(
  90. find_links=options.find_links,
  91. index_urls=[options.index_url] + options.extra_index_urls,
  92. allow_all_prereleases=False, # Explicitly set to False
  93. trusted_hosts=options.trusted_hosts,
  94. process_dependency_links=options.process_dependency_links,
  95. session=session,
  96. )
  97. all_candidates = finder.find_all_candidates("pip")
  98. if not all_candidates:
  99. return
  100. pypi_version = str(
  101. max(all_candidates, key=lambda c: c.version).version
  102. )
  103. # save that we've performed a check
  104. state.save(pypi_version, current_time)
  105. remote_version = packaging_version.parse(pypi_version)
  106. # Determine if our pypi_version is older
  107. if (pip_version < remote_version and
  108. pip_version.base_version != remote_version.base_version and
  109. was_installed_by_pip('pip')):
  110. # Advise "python -m pip" on Windows to avoid issues
  111. # with overwriting pip.exe.
  112. if WINDOWS:
  113. pip_cmd = "python -m pip"
  114. else:
  115. pip_cmd = "pip"
  116. logger.warning(
  117. "You are using pip version %s, however version %s is "
  118. "available.\nYou should consider upgrading via the "
  119. "'%s install --upgrade pip' command.",
  120. pip_version, pypi_version, pip_cmd
  121. )
  122. except Exception:
  123. logger.debug(
  124. "There was an error checking the latest version of pip",
  125. exc_info=True,
  126. )