Skip to content

Fixes for the cases that raise Tracebacks in outdated pip version check #5764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/5433.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Run self-version-check only on commands that may access the index, instead of
trying on every run and failing to do so due to missing options.
1 change: 1 addition & 0 deletions news/5679.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid caching self-version-check information when cache is disabled.
12 changes: 7 additions & 5 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ def main(self, args):

return UNKNOWN_ERROR
finally:
# Check if we're using the latest version of pip available
skip_version_check = (
options.disable_pip_version_check or
getattr(options, "no_index", False)
allow_version_check = (
# Does this command have the index_group options?
hasattr(options, "no_index") and
# Is this command allowed to perform this check?
not (options.disable_pip_version_check or options.no_index)
)
if not skip_version_check:
# Check if we're using the latest version of pip available
if allow_version_check:
session = self._build_session(
options,
retries=0,
Expand Down
23 changes: 16 additions & 7 deletions src/pip/_internal/utils/outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@

class SelfCheckState(object):
def __init__(self, cache_dir):
self.statefile_path = os.path.join(cache_dir, "selfcheck.json")
self.state = {}
self.statefile_path = None

# Load the existing state
try:
with open(self.statefile_path) as statefile:
self.state = json.load(statefile)[sys.prefix]
except (IOError, ValueError, KeyError):
self.state = {}
# Try to load the existing state
if cache_dir:
self.statefile_path = os.path.join(cache_dir, "selfcheck.json")
try:
with open(self.statefile_path) as statefile:
self.state = json.load(statefile)[sys.prefix]
except (IOError, ValueError, KeyError):
# Explicitly suppressing exceptions, since we don't want to
# error out if the cache file is invalid.
pass

def save(self, pypi_version, current_time):
# If we do not have a path to cache in, don't bother saving.
if not self.statefile_path:
return

# Check to make sure that we own the directory
if not check_path_owner(os.path.dirname(self.statefile_path)):
return
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_unit_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,9 @@ def fake_lock(filename):

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


def test_self_check_state_no_cache_dir():
state = outdated.SelfCheckState(cache_dir=False)
assert state.state == {}
assert state.statefile_path is None