From cd7c25dece860c46b27588fdd14ad1eafca2d510 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Wed, 16 May 2018 15:44:24 -0400 Subject: [PATCH 1/9] removed VirtualenvSelfCheckState per stufft --- src/pip/_internal/utils/outdated.py | 34 +---------------------- tests/unit/test_unit_outdated.py | 42 ++--------------------------- 2 files changed, 3 insertions(+), 73 deletions(-) diff --git a/src/pip/_internal/utils/outdated.py b/src/pip/_internal/utils/outdated.py index f8f64669d51..b6d191b3781 100644 --- a/src/pip/_internal/utils/outdated.py +++ b/src/pip/_internal/utils/outdated.py @@ -21,31 +21,6 @@ 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") @@ -84,13 +59,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. @@ -106,7 +74,7 @@ def pip_version_check(session, options): pypi_version = None try: - state = load_selfcheck_statefile() + state = GlobalSelfCheckState() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index 531ff02d27d..f76ecbf967b 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -72,7 +72,7 @@ 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, 'GlobalSelfCheckState', lambda: fake_state ) with freezegun.freeze_time( @@ -105,40 +105,6 @@ 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): CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z", "pypi_version": "1.0"}}''' @@ -164,18 +130,14 @@ 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.GlobalSelfCheckState() 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)] From 631c080cc881803c7ae9f2c08bc6d937669edd91 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Wed, 16 May 2018 17:55:34 -0400 Subject: [PATCH 2/9] use options to get cache path --- src/pip/_internal/utils/outdated.py | 6 +++--- tests/unit/test_unit_outdated.py | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/pip/_internal/utils/outdated.py b/src/pip/_internal/utils/outdated.py index b6d191b3781..1a1f70eec90 100644 --- a/src/pip/_internal/utils/outdated.py +++ b/src/pip/_internal/utils/outdated.py @@ -22,8 +22,8 @@ class GlobalSelfCheckState(object): - def __init__(self): - self.statefile_path = os.path.join(USER_CACHE_DIR, "selfcheck.json") + def __init__(self, cache_dir): + self.statefile_path = os.path.join(cache_dir, "selfcheck.json") # Load the existing state try: @@ -74,7 +74,7 @@ def pip_version_check(session, options): pypi_version = None try: - state = GlobalSelfCheckState() + state = GlobalSelfCheckState(cache_dir=options.cache_dir) current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index f76ecbf967b..ed50f778333 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -3,6 +3,7 @@ import sys from contextlib import contextmanager + import freezegun import pretend import pytest @@ -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='', ) @@ -72,7 +74,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, save=pretend.call_recorder(lambda v, t: None), ) monkeypatch.setattr( - outdated, 'GlobalSelfCheckState', lambda: fake_state + outdated, 'GlobalSelfCheckState', lambda **kw: fake_state ) with freezegun.freeze_time( @@ -135,7 +137,7 @@ def fake_lock(filename): monkeypatch.setattr(outdated, 'USER_CACHE_DIR', cache_dir) monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix') - state = outdated.GlobalSelfCheckState() + state = outdated.GlobalSelfCheckState(cache_dir=cache_dir) state.save('2.0', datetime.datetime.utcnow()) expected_path = cache_dir / 'selfcheck.json' From 724989baa7a424840ed3d7eb0455c4fa3e9f90d1 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Wed, 16 May 2018 18:15:22 -0400 Subject: [PATCH 3/9] add news --- news/3905.bugfix | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 news/3905.bugfix diff --git a/news/3905.bugfix b/news/3905.bugfix new file mode 100644 index 00000000000..0c0c822192a --- /dev/null +++ b/news/3905.bugfix @@ -0,0 +1,8 @@ +Changed rules on where pip version lock file selfcheck.json is stored. + +Previously, the lock file was either stored at ``USER_CACHE``/selfcheck.json, +or for vitual enviorments it is stored at ``sys.prefix``\pip-selfcheck.json + +Now regardless of whether the user is in a virtual environment, the lock file +is stored either in selfcheck.json in the path specified in cache-dir in +pip.conf, or if none is specified it is stored at``USER_CACHE``/selfcheck.json From cfa65e68a469ffffd0f6a3172d0e1366280dc85d Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Wed, 16 May 2018 22:03:33 -0400 Subject: [PATCH 4/9] fix lint errors --- tests/unit/test_unit_outdated.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index ed50f778333..cd765ef11c6 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -78,12 +78,12 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, ) 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 From 16528ed5f1b7fd18b68dc7200182f10a734b7508 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Thu, 17 May 2018 09:39:26 -0400 Subject: [PATCH 5/9] Cleanup per PR comments --- news/3905.bugfix | 9 +-------- src/pip/_internal/utils/outdated.py | 5 ++--- tests/unit/test_unit_outdated.py | 4 ++-- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/news/3905.bugfix b/news/3905.bugfix index 0c0c822192a..a719866fc9a 100644 --- a/news/3905.bugfix +++ b/news/3905.bugfix @@ -1,8 +1 @@ -Changed rules on where pip version lock file selfcheck.json is stored. - -Previously, the lock file was either stored at ``USER_CACHE``/selfcheck.json, -or for vitual enviorments it is stored at ``sys.prefix``\pip-selfcheck.json - -Now regardless of whether the user is in a virtual environment, the lock file -is stored either in selfcheck.json in the path specified in cache-dir in -pip.conf, or if none is specified it is stored at``USER_CACHE``/selfcheck.json +Adjust path to selfcheck.json - remove virtualenv specific path and honor cache-dir in pip.conf diff --git a/src/pip/_internal/utils/outdated.py b/src/pip/_internal/utils/outdated.py index 1a1f70eec90..d7246da55d9 100644 --- a/src/pip/_internal/utils/outdated.py +++ b/src/pip/_internal/utils/outdated.py @@ -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 @@ -21,7 +20,7 @@ logger = logging.getLogger(__name__) -class GlobalSelfCheckState(object): +class SelfCheckState(object): def __init__(self, cache_dir): self.statefile_path = os.path.join(cache_dir, "selfcheck.json") @@ -74,7 +73,7 @@ def pip_version_check(session, options): pypi_version = None try: - state = GlobalSelfCheckState(cache_dir=options.cache_dir) + state = SelfCheckState(cache_dir=options.cache_dir) current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index cd765ef11c6..e1f45777b6b 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -74,7 +74,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, save=pretend.call_recorder(lambda v, t: None), ) monkeypatch.setattr( - outdated, 'GlobalSelfCheckState', lambda **kw: fake_state + outdated, 'SelfCheckState', lambda **kw: fake_state ) with freezegun.freeze_time( @@ -137,7 +137,7 @@ def fake_lock(filename): monkeypatch.setattr(outdated, 'USER_CACHE_DIR', cache_dir) monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix') - state = outdated.GlobalSelfCheckState(cache_dir=cache_dir) + state = outdated.SelfCheckState(cache_dir=cache_dir) state.save('2.0', datetime.datetime.utcnow()) expected_path = cache_dir / 'selfcheck.json' From 1056580303ca68dda5aba1a52fa2521fe55ee75a Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Thu, 17 May 2018 10:36:42 -0400 Subject: [PATCH 6/9] fix test failure --- tests/unit/test_unit_outdated.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index e1f45777b6b..a70d79c8e1e 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -107,7 +107,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, assert len(outdated.logger.warning.calls) == 0 -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( @@ -134,7 +134,6 @@ def fake_lock(filename): cache_dir = tmpdir / 'cache_dir' - monkeypatch.setattr(outdated, 'USER_CACHE_DIR', cache_dir) monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix') state = outdated.SelfCheckState(cache_dir=cache_dir) From 32c3b56ebe906f4fda38518ef55c1ed2e95b5294 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Thu, 17 May 2018 11:03:21 -0400 Subject: [PATCH 7/9] fix lint issues --- tests/unit/test_unit_outdated.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index a70d79c8e1e..7449be0501c 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -83,7 +83,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, "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 @@ -132,7 +132,6 @@ def fake_lock(filename): monkeypatch.setattr(lockfile, 'LockFile', fake_lock) monkeypatch.setattr(os.path, "exists", lambda p: True) - cache_dir = tmpdir / 'cache_dir' monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix') From 60a2841f0ba7ae429fe28b2617612555e4073ba2 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Thu, 17 May 2018 11:30:10 -0400 Subject: [PATCH 8/9] flake8 fix --- tests/unit/test_unit_outdated.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index 7449be0501c..26a28f723a3 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -83,7 +83,8 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver, "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 From 56418c0451adfcd5cb595eb257ce05a6e4391404 Mon Sep 17 00:00:00 2001 From: Tom Freudenheim Date: Thu, 17 May 2018 13:42:31 -0400 Subject: [PATCH 9/9] removed py 3.3 (copied from PR #5423) --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5aea53a0c96..aac434c96d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,8 +22,6 @@ matrix: - env: TOXENV=py36 python: 3.6 # All the other Py3 versions - - env: TOXENV=py33 - python: 3.3 - env: TOXENV=py34 python: 3.4 - env: TOXENV=py35