Skip to content

Commit

Permalink
Merge pull request #5419 from tomfbiz/3905_Pip_version_check_cache_dir
Browse files Browse the repository at this point in the history
Keep only one selfcheck.json file
  • Loading branch information
pradyunsg authored May 18, 2018
2 parents 6020855 + daae935 commit 21b97e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 86 deletions.
1 change: 1 addition & 0 deletions news/3905.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adjust path to selfcheck.json - remove virtualenv specific path and honor cache-dir in pip.conf
41 changes: 4 additions & 37 deletions src/pip/_internal/utils/outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from pip._internal.compat import WINDOWS
from pip._internal.index import PackageFinder
from pip._internal.locations import USER_CACHE_DIR, running_under_virtualenv
from pip._internal.utils.filesystem import check_path_owner
from pip._internal.utils.misc import ensure_dir, get_installed_version

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


class VirtualenvSelfCheckState(object):
def __init__(self):
self.statefile_path = os.path.join(sys.prefix, "pip-selfcheck.json")

# Load the existing state
try:
with open(self.statefile_path) as statefile:
self.state = json.load(statefile)
except (IOError, ValueError):
self.state = {}

def save(self, pypi_version, current_time):
# Attempt to write out our version check file
with open(self.statefile_path, "w") as statefile:
json.dump(
{
"last_check": current_time.strftime(SELFCHECK_DATE_FMT),
"pypi_version": pypi_version,
},
statefile,
sort_keys=True,
separators=(",", ":")
)


class GlobalSelfCheckState(object):
def __init__(self):
self.statefile_path = os.path.join(USER_CACHE_DIR, "selfcheck.json")
class SelfCheckState(object):
def __init__(self, cache_dir):
self.statefile_path = os.path.join(cache_dir, "selfcheck.json")

# Load the existing state
try:
Expand Down Expand Up @@ -84,13 +58,6 @@ def save(self, pypi_version, current_time):
separators=(",", ":"))


def load_selfcheck_statefile():
if running_under_virtualenv():
return VirtualenvSelfCheckState()
else:
return GlobalSelfCheckState()


def pip_version_check(session, options):
"""Check for an update for pip.
Expand All @@ -106,7 +73,7 @@ def pip_version_check(session, options):
pypi_version = None

try:
state = load_selfcheck_statefile()
state = SelfCheckState(cache_dir=options.cache_dir)

current_time = datetime.datetime.utcnow()
# Determine if we need to refresh the state
Expand Down
61 changes: 12 additions & 49 deletions tests/unit/test_unit_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from contextlib import contextmanager


import freezegun
import pretend
import pytest
Expand Down Expand Up @@ -37,6 +38,7 @@ def _options():
return pretend.stub(
find_links=False, extra_index_urls=[], index_url='default_url',
pre=False, trusted_hosts=False, process_dependency_links=False,
cache_dir='',
)


Expand Down Expand Up @@ -72,16 +74,17 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
save=pretend.call_recorder(lambda v, t: None),
)
monkeypatch.setattr(
outdated, 'load_selfcheck_statefile', lambda: fake_state
outdated, 'SelfCheckState', lambda **kw: fake_state
)

with freezegun.freeze_time(
"1970-01-09 10:00:00",
ignore=[
"six.moves",
"pip._vendor.six.moves",
"pip._vendor.requests.packages.urllib3.packages.six.moves",
]):
"1970-01-09 10:00:00",
ignore=[
"six.moves",
"pip._vendor.six.moves",
"pip._vendor.requests.packages.urllib3.packages.six.moves",
]
):
latest_pypi_version = outdated.pip_version_check(None, _options())

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


def test_virtualenv_state(monkeypatch):
CONTENT = '{"last_check": "1970-01-02T11:00:00Z", "pypi_version": "1.0"}'
fake_file = pretend.stub(
read=pretend.call_recorder(lambda: CONTENT),
write=pretend.call_recorder(lambda s: None),
)

@pretend.call_recorder
@contextmanager
def fake_open(filename, mode='r'):
yield fake_file

monkeypatch.setattr(outdated, 'open', fake_open, raising=False)

monkeypatch.setattr(outdated, 'running_under_virtualenv',
pretend.call_recorder(lambda: True))

monkeypatch.setattr(sys, 'prefix', 'virtually_env')

state = outdated.load_selfcheck_statefile()
state.save('2.0', datetime.datetime.utcnow())

assert len(outdated.running_under_virtualenv.calls) == 1

expected_path = os.path.join('virtually_env', 'pip-selfcheck.json')
assert fake_open.calls == [
pretend.call(expected_path),
pretend.call(expected_path, 'w'),
]

# json.dumps will call this a number of times
assert len(fake_file.write.calls)


def test_global_state(monkeypatch, tmpdir):
def test_self_check_state(monkeypatch, tmpdir):
CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z",
"pypi_version": "1.0"}}'''
fake_file = pretend.stub(
Expand All @@ -164,18 +133,12 @@ def fake_lock(filename):
monkeypatch.setattr(lockfile, 'LockFile', fake_lock)
monkeypatch.setattr(os.path, "exists", lambda p: True)

monkeypatch.setattr(outdated, 'running_under_virtualenv',
pretend.call_recorder(lambda: False))

cache_dir = tmpdir / 'cache_dir'
monkeypatch.setattr(outdated, 'USER_CACHE_DIR', cache_dir)
monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix')

state = outdated.load_selfcheck_statefile()
state = outdated.SelfCheckState(cache_dir=cache_dir)
state.save('2.0', datetime.datetime.utcnow())

assert len(outdated.running_under_virtualenv.calls) == 1

expected_path = cache_dir / 'selfcheck.json'
assert fake_lock.calls == [pretend.call(expected_path)]

Expand Down

0 comments on commit 21b97e4

Please sign in to comment.