Skip to content

Commit

Permalink
Merge pull request #5764 from pradyunsg/bugfix/errors-in-outdated-check
Browse files Browse the repository at this point in the history
Fixes for the cases that raise Tracebacks in outdated pip version check
  • Loading branch information
pradyunsg committed Sep 7, 2018
2 parents 0d9c05e + 1e0247f commit 1e1ff21
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
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

0 comments on commit 1e1ff21

Please sign in to comment.