Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../lib/python2..../site-pac.../pip/utils
File: outdated.py
from __future__ import absolute_import
[0] Fix | Delete
[1] Fix | Delete
import datetime
[2] Fix | Delete
import json
[3] Fix | Delete
import logging
[4] Fix | Delete
import os.path
[5] Fix | Delete
import sys
[6] Fix | Delete
[7] Fix | Delete
from pip._vendor import lockfile
[8] Fix | Delete
from pip._vendor.packaging import version as packaging_version
[9] Fix | Delete
[10] Fix | Delete
from pip.compat import total_seconds, WINDOWS
[11] Fix | Delete
from pip.models import PyPI
[12] Fix | Delete
from pip.locations import USER_CACHE_DIR, running_under_virtualenv
[13] Fix | Delete
from pip.utils import ensure_dir, get_installed_version
[14] Fix | Delete
from pip.utils.filesystem import check_path_owner
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
SELFCHECK_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
logger = logging.getLogger(__name__)
[21] Fix | Delete
[22] Fix | Delete
[23] Fix | Delete
class VirtualenvSelfCheckState(object):
[24] Fix | Delete
def __init__(self):
[25] Fix | Delete
self.statefile_path = os.path.join(sys.prefix, "pip-selfcheck.json")
[26] Fix | Delete
[27] Fix | Delete
# Load the existing state
[28] Fix | Delete
try:
[29] Fix | Delete
with open(self.statefile_path) as statefile:
[30] Fix | Delete
self.state = json.load(statefile)
[31] Fix | Delete
except (IOError, ValueError):
[32] Fix | Delete
self.state = {}
[33] Fix | Delete
[34] Fix | Delete
def save(self, pypi_version, current_time):
[35] Fix | Delete
# Attempt to write out our version check file
[36] Fix | Delete
with open(self.statefile_path, "w") as statefile:
[37] Fix | Delete
json.dump(
[38] Fix | Delete
{
[39] Fix | Delete
"last_check": current_time.strftime(SELFCHECK_DATE_FMT),
[40] Fix | Delete
"pypi_version": pypi_version,
[41] Fix | Delete
},
[42] Fix | Delete
statefile,
[43] Fix | Delete
sort_keys=True,
[44] Fix | Delete
separators=(",", ":")
[45] Fix | Delete
)
[46] Fix | Delete
[47] Fix | Delete
[48] Fix | Delete
class GlobalSelfCheckState(object):
[49] Fix | Delete
def __init__(self):
[50] Fix | Delete
self.statefile_path = os.path.join(USER_CACHE_DIR, "selfcheck.json")
[51] Fix | Delete
[52] Fix | Delete
# Load the existing state
[53] Fix | Delete
try:
[54] Fix | Delete
with open(self.statefile_path) as statefile:
[55] Fix | Delete
self.state = json.load(statefile)[sys.prefix]
[56] Fix | Delete
except (IOError, ValueError, KeyError):
[57] Fix | Delete
self.state = {}
[58] Fix | Delete
[59] Fix | Delete
def save(self, pypi_version, current_time):
[60] Fix | Delete
# Check to make sure that we own the directory
[61] Fix | Delete
if not check_path_owner(os.path.dirname(self.statefile_path)):
[62] Fix | Delete
return
[63] Fix | Delete
[64] Fix | Delete
# Now that we've ensured the directory is owned by this user, we'll go
[65] Fix | Delete
# ahead and make sure that all our directories are created.
[66] Fix | Delete
ensure_dir(os.path.dirname(self.statefile_path))
[67] Fix | Delete
[68] Fix | Delete
# Attempt to write out our version check file
[69] Fix | Delete
with lockfile.LockFile(self.statefile_path):
[70] Fix | Delete
if os.path.exists(self.statefile_path):
[71] Fix | Delete
with open(self.statefile_path) as statefile:
[72] Fix | Delete
state = json.load(statefile)
[73] Fix | Delete
else:
[74] Fix | Delete
state = {}
[75] Fix | Delete
[76] Fix | Delete
state[sys.prefix] = {
[77] Fix | Delete
"last_check": current_time.strftime(SELFCHECK_DATE_FMT),
[78] Fix | Delete
"pypi_version": pypi_version,
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
with open(self.statefile_path, "w") as statefile:
[82] Fix | Delete
json.dump(state, statefile, sort_keys=True,
[83] Fix | Delete
separators=(",", ":"))
[84] Fix | Delete
[85] Fix | Delete
[86] Fix | Delete
def load_selfcheck_statefile():
[87] Fix | Delete
if running_under_virtualenv():
[88] Fix | Delete
return VirtualenvSelfCheckState()
[89] Fix | Delete
else:
[90] Fix | Delete
return GlobalSelfCheckState()
[91] Fix | Delete
[92] Fix | Delete
[93] Fix | Delete
def pip_installed_by_pip():
[94] Fix | Delete
"""Checks whether pip was installed by pip
[95] Fix | Delete
[96] Fix | Delete
This is used not to display the upgrade message when pip is in fact
[97] Fix | Delete
installed by system package manager, such as dnf on Fedora.
[98] Fix | Delete
"""
[99] Fix | Delete
import pkg_resources
[100] Fix | Delete
try:
[101] Fix | Delete
dist = pkg_resources.get_distribution('pip')
[102] Fix | Delete
return (dist.has_metadata('INSTALLER') and
[103] Fix | Delete
'pip' in dist.get_metadata_lines('INSTALLER'))
[104] Fix | Delete
except pkg_resources.DistributionNotFound:
[105] Fix | Delete
return False
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
def pip_version_check(session):
[109] Fix | Delete
"""Check for an update for pip.
[110] Fix | Delete
[111] Fix | Delete
Limit the frequency of checks to once per week. State is stored either in
[112] Fix | Delete
the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
[113] Fix | Delete
of the pip script path.
[114] Fix | Delete
"""
[115] Fix | Delete
installed_version = get_installed_version("pip")
[116] Fix | Delete
if installed_version is None:
[117] Fix | Delete
return
[118] Fix | Delete
[119] Fix | Delete
pip_version = packaging_version.parse(installed_version)
[120] Fix | Delete
pypi_version = None
[121] Fix | Delete
[122] Fix | Delete
try:
[123] Fix | Delete
state = load_selfcheck_statefile()
[124] Fix | Delete
[125] Fix | Delete
current_time = datetime.datetime.utcnow()
[126] Fix | Delete
# Determine if we need to refresh the state
[127] Fix | Delete
if "last_check" in state.state and "pypi_version" in state.state:
[128] Fix | Delete
last_check = datetime.datetime.strptime(
[129] Fix | Delete
state.state["last_check"],
[130] Fix | Delete
SELFCHECK_DATE_FMT
[131] Fix | Delete
)
[132] Fix | Delete
if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60:
[133] Fix | Delete
pypi_version = state.state["pypi_version"]
[134] Fix | Delete
[135] Fix | Delete
# Refresh the version if we need to or just see if we need to warn
[136] Fix | Delete
if pypi_version is None:
[137] Fix | Delete
resp = session.get(
[138] Fix | Delete
PyPI.pip_json_url,
[139] Fix | Delete
headers={"Accept": "application/json"},
[140] Fix | Delete
)
[141] Fix | Delete
resp.raise_for_status()
[142] Fix | Delete
pypi_version = [
[143] Fix | Delete
v for v in sorted(
[144] Fix | Delete
list(resp.json()["releases"]),
[145] Fix | Delete
key=packaging_version.parse,
[146] Fix | Delete
)
[147] Fix | Delete
if not packaging_version.parse(v).is_prerelease
[148] Fix | Delete
][-1]
[149] Fix | Delete
[150] Fix | Delete
# save that we've performed a check
[151] Fix | Delete
state.save(pypi_version, current_time)
[152] Fix | Delete
[153] Fix | Delete
remote_version = packaging_version.parse(pypi_version)
[154] Fix | Delete
[155] Fix | Delete
# Determine if our pypi_version is older
[156] Fix | Delete
if (pip_version < remote_version and
[157] Fix | Delete
pip_version.base_version != remote_version.base_version and
[158] Fix | Delete
pip_installed_by_pip()):
[159] Fix | Delete
# Advise "python -m pip" on Windows to avoid issues
[160] Fix | Delete
# with overwriting pip.exe.
[161] Fix | Delete
if WINDOWS:
[162] Fix | Delete
pip_cmd = "python -m pip"
[163] Fix | Delete
else:
[164] Fix | Delete
pip_cmd = "pip"
[165] Fix | Delete
logger.warning(
[166] Fix | Delete
"You are using pip version %s, however version %s is "
[167] Fix | Delete
"available.\nYou should consider upgrading via the "
[168] Fix | Delete
"'%s install --upgrade pip' command.",
[169] Fix | Delete
pip_version, pypi_version, pip_cmd
[170] Fix | Delete
)
[171] Fix | Delete
[172] Fix | Delete
except Exception:
[173] Fix | Delete
logger.debug(
[174] Fix | Delete
"There was an error checking the latest version of pip",
[175] Fix | Delete
exc_info=True,
[176] Fix | Delete
)
[177] Fix | Delete
[178] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function