9from dataclasses
import dataclass
10from typing
import Any, Callable, Dict, Optional
25 get_best_invocation_for_this_pip,
26 get_best_invocation_for_this_python,
31_DATE_FMT =
"%Y-%m-%dT%H:%M:%SZ"
45 self.
_state: Dict[str, Any] = {}
56 except (OSError, ValueError, KeyError):
65 def get(self, current_time: datetime.datetime) -> Optional[str]:
66 """Check if we have a not-outdated version loaded already."""
70 if "last_check" not in self.
_state:
73 if "pypi_version" not in self.
_state:
76 seven_days_in_seconds = 7 * 24 * 60 * 60
80 seconds_since_last_check = (current_time - last_check).
total_seconds()
81 if seconds_since_last_check > seven_days_in_seconds:
84 return self.
_state[
"pypi_version"]
104 "pypi_version": pypi_version,
107 text =
json.dumps(state, sort_keys=
True, separators=(
",",
":"))
128 pip_cmd = f
"{get_best_invocation_for_this_python()} -m pip"
130 pip_cmd = get_best_invocation_for_this_pip()
132 notice =
"[bold][[reset][blue]notice[reset][bold]][reset]"
136 f
"{notice} A new release of pip is available: "
137 f
"[red]{self.old}[reset] -> [green]{self.new}[reset]"
140 f
"{notice} To update, run: "
141 f
"[green]{escape(pip_cmd)} install --upgrade pip"
147 """Checks whether pkg was installed by pip
149 This is used not to display the upgrade message when pip is in fact
150 installed by system package manager, such as dnf on Fedora.
152 dist = get_default_environment().get_distribution(pkg)
163 suppress_no_index=
True,
170 allow_all_prereleases=
False,
174 link_collector=link_collector,
175 selection_prefs=selection_prefs,
178 if best_candidate
is None:
186 state: SelfCheckState,
188 local_version: DistributionVersion,
189 get_remote_version: Callable[[], Optional[str]],
190) -> Optional[UpgradePrompt]:
191 remote_version_str =
state.get(current_time)
192 if remote_version_str
is None:
194 if remote_version_str
is None:
197 state.set(remote_version_str, current_time)
199 remote_version = parse_version(remote_version_str)
200 logger.debug(
"Remote version of pip: %s", remote_version)
204 logger.debug(
"Was pip installed by pip? %s", pip_installed_by_pip)
205 if not pip_installed_by_pip:
208 local_version_is_older = (
209 local_version < remote_version
212 if local_version_is_older:
213 return UpgradePrompt(old=str(local_version), new=remote_version_str)
219 """Check for an update for pip.
221 Limit the frequency of checks to once per week. State is stored either in
222 the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
223 of the pip script path.
225 installed_dist = get_default_environment().get_distribution(
"pip")
226 if not installed_dist:
235 _get_current_remote_pip_version, session, options
238 if upgrade_prompt
is not None:
241 logger.warning(
"There was an error checking the latest version of pip.")
None set(self, str pypi_version, datetime.datetime current_time)
None __init__(self, str cache_dir)
str _get_statefile_name(str key)
Optional[UpgradePrompt] _self_version_check_logic(*SelfCheckState state, datetime.datetime current_time, DistributionVersion local_version, Callable[[], Optional[str]] get_remote_version)
bool was_installed_by_pip(str pkg)
Optional[str] _get_current_remote_pip_version(PipSession session, optparse.Values options)