comparison venv/lib/python2.7/site-packages/pip/utils/outdated.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 from __future__ import absolute_import
2
3 import datetime
4 import errno
5 import json
6 import logging
7 import os.path
8 import sys
9
10 from pip._vendor import lockfile
11 from pip._vendor import pkg_resources
12
13 from pip.compat import total_seconds
14 from pip.index import PyPI
15 from pip.locations import USER_CACHE_DIR, running_under_virtualenv
16 from pip.utils.filesystem import check_path_owner
17
18
19 SELFCHECK_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
20
21
22 logger = logging.getLogger(__name__)
23
24
25 class VirtualenvSelfCheckState(object):
26 def __init__(self):
27 self.statefile_path = os.path.join(sys.prefix, "pip-selfcheck.json")
28
29 # Load the existing state
30 try:
31 with open(self.statefile_path) as statefile:
32 self.state = json.load(statefile)
33 except (IOError, ValueError):
34 self.state = {}
35
36 def save(self, pypi_version, current_time):
37 # Attempt to write out our version check file
38 with open(self.statefile_path, "w") as statefile:
39 json.dump(
40 {
41 "last_check": current_time.strftime(SELFCHECK_DATE_FMT),
42 "pypi_version": pypi_version,
43 },
44 statefile,
45 sort_keys=True,
46 separators=(",", ":")
47 )
48
49
50 class GlobalSelfCheckState(object):
51 def __init__(self):
52 self.statefile_path = os.path.join(USER_CACHE_DIR, "selfcheck.json")
53
54 # Load the existing state
55 try:
56 with open(self.statefile_path) as statefile:
57 self.state = json.load(statefile)[sys.prefix]
58 except (IOError, ValueError, KeyError):
59 self.state = {}
60
61 def save(self, pypi_version, current_time):
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
66 # Now that we've ensured the directory is owned by this user, we'll go
67 # ahead and make sure that all our directories are created.
68 try:
69 os.makedirs(os.path.dirname(self.statefile_path))
70 except OSError as exc:
71 if exc.errno != errno.EEXIST:
72 raise
73
74 # Attempt to write out our version check file
75 with lockfile.LockFile(self.statefile_path):
76 if os.path.exists(self.statefile_path):
77 with open(self.statefile_path) as statefile:
78 state = json.load(statefile)
79 else:
80 state = {}
81
82 state[sys.prefix] = {
83 "last_check": current_time.strftime(SELFCHECK_DATE_FMT),
84 "pypi_version": pypi_version,
85 }
86
87 with open(self.statefile_path, "w") as statefile:
88 json.dump(state, statefile, sort_keys=True,
89 separators=(",", ":"))
90
91
92 def load_selfcheck_statefile():
93 if running_under_virtualenv():
94 return VirtualenvSelfCheckState()
95 else:
96 return GlobalSelfCheckState()
97
98
99 def pip_version_check(session):
100 """Check for an update for pip.
101
102 Limit the frequency of checks to once per week. State is stored either in
103 the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
104 of the pip script path.
105 """
106 import pip # imported here to prevent circular imports
107 pypi_version = None
108
109 try:
110 state = load_selfcheck_statefile()
111
112 current_time = datetime.datetime.utcnow()
113 # Determine if we need to refresh the state
114 if "last_check" in state.state and "pypi_version" in state.state:
115 last_check = datetime.datetime.strptime(
116 state.state["last_check"],
117 SELFCHECK_DATE_FMT
118 )
119 if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60:
120 pypi_version = state.state["pypi_version"]
121
122 # Refresh the version if we need to or just see if we need to warn
123 if pypi_version is None:
124 resp = session.get(
125 PyPI.pip_json_url,
126 headers={"Accept": "application/json"},
127 )
128 resp.raise_for_status()
129 pypi_version = resp.json()["info"]["version"]
130
131 # save that we've performed a check
132 state.save(pypi_version, current_time)
133
134 pip_version = pkg_resources.parse_version(pip.__version__)
135
136 # Determine if our pypi_version is older
137 if pip_version < pkg_resources.parse_version(pypi_version):
138 logger.warning(
139 "You are using pip version %s, however version %s is "
140 "available.\nYou should consider upgrading via the "
141 "'pip install --upgrade pip' command." % (pip.__version__,
142 pypi_version)
143 )
144
145 except Exception:
146 logger.debug(
147 "There was an error checking the latest version of pip",
148 exc_info=True,
149 )