Skip to content

Commit 21b97e4

Browse files
authored
Merge pull request #5419 from tomfbiz/3905_Pip_version_check_cache_dir
Keep only one selfcheck.json file
2 parents 6020855 + daae935 commit 21b97e4

File tree

3 files changed

+17
-86
lines changed

3 files changed

+17
-86
lines changed

news/3905.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adjust path to selfcheck.json - remove virtualenv specific path and honor cache-dir in pip.conf

src/pip/_internal/utils/outdated.py

+4-37
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from pip._internal.compat import WINDOWS
1313
from pip._internal.index import PackageFinder
14-
from pip._internal.locations import USER_CACHE_DIR, running_under_virtualenv
1514
from pip._internal.utils.filesystem import check_path_owner
1615
from pip._internal.utils.misc import ensure_dir, get_installed_version
1716

@@ -21,34 +20,9 @@
2120
logger = logging.getLogger(__name__)
2221

2322

24-
class VirtualenvSelfCheckState(object):
25-
def __init__(self):
26-
self.statefile_path = os.path.join(sys.prefix, "pip-selfcheck.json")
27-
28-
# Load the existing state
29-
try:
30-
with open(self.statefile_path) as statefile:
31-
self.state = json.load(statefile)
32-
except (IOError, ValueError):
33-
self.state = {}
34-
35-
def save(self, pypi_version, current_time):
36-
# Attempt to write out our version check file
37-
with open(self.statefile_path, "w") as statefile:
38-
json.dump(
39-
{
40-
"last_check": current_time.strftime(SELFCHECK_DATE_FMT),
41-
"pypi_version": pypi_version,
42-
},
43-
statefile,
44-
sort_keys=True,
45-
separators=(",", ":")
46-
)
47-
48-
49-
class GlobalSelfCheckState(object):
50-
def __init__(self):
51-
self.statefile_path = os.path.join(USER_CACHE_DIR, "selfcheck.json")
23+
class SelfCheckState(object):
24+
def __init__(self, cache_dir):
25+
self.statefile_path = os.path.join(cache_dir, "selfcheck.json")
5226

5327
# Load the existing state
5428
try:
@@ -84,13 +58,6 @@ def save(self, pypi_version, current_time):
8458
separators=(",", ":"))
8559

8660

87-
def load_selfcheck_statefile():
88-
if running_under_virtualenv():
89-
return VirtualenvSelfCheckState()
90-
else:
91-
return GlobalSelfCheckState()
92-
93-
9461
def pip_version_check(session, options):
9562
"""Check for an update for pip.
9663
@@ -106,7 +73,7 @@ def pip_version_check(session, options):
10673
pypi_version = None
10774

10875
try:
109-
state = load_selfcheck_statefile()
76+
state = SelfCheckState(cache_dir=options.cache_dir)
11077

11178
current_time = datetime.datetime.utcnow()
11279
# Determine if we need to refresh the state

tests/unit/test_unit_outdated.py

+12-49
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
from contextlib import contextmanager
55

6+
67
import freezegun
78
import pretend
89
import pytest
@@ -37,6 +38,7 @@ def _options():
3738
return pretend.stub(
3839
find_links=False, extra_index_urls=[], index_url='default_url',
3940
pre=False, trusted_hosts=False, process_dependency_links=False,
41+
cache_dir='',
4042
)
4143

4244

@@ -72,16 +74,17 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
7274
save=pretend.call_recorder(lambda v, t: None),
7375
)
7476
monkeypatch.setattr(
75-
outdated, 'load_selfcheck_statefile', lambda: fake_state
77+
outdated, 'SelfCheckState', lambda **kw: fake_state
7678
)
7779

7880
with freezegun.freeze_time(
79-
"1970-01-09 10:00:00",
80-
ignore=[
81-
"six.moves",
82-
"pip._vendor.six.moves",
83-
"pip._vendor.requests.packages.urllib3.packages.six.moves",
84-
]):
81+
"1970-01-09 10:00:00",
82+
ignore=[
83+
"six.moves",
84+
"pip._vendor.six.moves",
85+
"pip._vendor.requests.packages.urllib3.packages.six.moves",
86+
]
87+
):
8588
latest_pypi_version = outdated.pip_version_check(None, _options())
8689

8790
# See we return None if not installed_version
@@ -105,41 +108,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
105108
assert len(outdated.logger.warning.calls) == 0
106109

107110

108-
def test_virtualenv_state(monkeypatch):
109-
CONTENT = '{"last_check": "1970-01-02T11:00:00Z", "pypi_version": "1.0"}'
110-
fake_file = pretend.stub(
111-
read=pretend.call_recorder(lambda: CONTENT),
112-
write=pretend.call_recorder(lambda s: None),
113-
)
114-
115-
@pretend.call_recorder
116-
@contextmanager
117-
def fake_open(filename, mode='r'):
118-
yield fake_file
119-
120-
monkeypatch.setattr(outdated, 'open', fake_open, raising=False)
121-
122-
monkeypatch.setattr(outdated, 'running_under_virtualenv',
123-
pretend.call_recorder(lambda: True))
124-
125-
monkeypatch.setattr(sys, 'prefix', 'virtually_env')
126-
127-
state = outdated.load_selfcheck_statefile()
128-
state.save('2.0', datetime.datetime.utcnow())
129-
130-
assert len(outdated.running_under_virtualenv.calls) == 1
131-
132-
expected_path = os.path.join('virtually_env', 'pip-selfcheck.json')
133-
assert fake_open.calls == [
134-
pretend.call(expected_path),
135-
pretend.call(expected_path, 'w'),
136-
]
137-
138-
# json.dumps will call this a number of times
139-
assert len(fake_file.write.calls)
140-
141-
142-
def test_global_state(monkeypatch, tmpdir):
111+
def test_self_check_state(monkeypatch, tmpdir):
143112
CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z",
144113
"pypi_version": "1.0"}}'''
145114
fake_file = pretend.stub(
@@ -164,18 +133,12 @@ def fake_lock(filename):
164133
monkeypatch.setattr(lockfile, 'LockFile', fake_lock)
165134
monkeypatch.setattr(os.path, "exists", lambda p: True)
166135

167-
monkeypatch.setattr(outdated, 'running_under_virtualenv',
168-
pretend.call_recorder(lambda: False))
169-
170136
cache_dir = tmpdir / 'cache_dir'
171-
monkeypatch.setattr(outdated, 'USER_CACHE_DIR', cache_dir)
172137
monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix')
173138

174-
state = outdated.load_selfcheck_statefile()
139+
state = outdated.SelfCheckState(cache_dir=cache_dir)
175140
state.save('2.0', datetime.datetime.utcnow())
176141

177-
assert len(outdated.running_under_virtualenv.calls) == 1
178-
179142
expected_path = cache_dir / 'selfcheck.json'
180143
assert fake_lock.calls == [pretend.call(expected_path)]
181144

0 commit comments

Comments
 (0)