Only use bg thread for network calls

- Also use os.utime instead of opening a file pointer for existing files
This commit is contained in:
Dan Ryan
2017-11-03 19:59:55 -04:00
parent ca2ed0eeea
commit 44053b52bb
2 changed files with 9 additions and 8 deletions
+3 -5
View File
@@ -138,13 +138,11 @@ def add_to_path(p):
@background.task
def check_for_updates():
"""Background thread -- beautiful, isn't it?"""
if not need_update_check():
return
try:
checked_for_updates()
r = requests.get('https://pypi.python.org/pypi/pipenv/json', timeout=0.5)
latest = max(map(semver.parse_version_info, r.json()['releases'].keys()))
current = semver.parse_version_info(__version__)
checked_for_updates()
if latest > current:
click.echo('{0}: {1} is now available. You get bonus points for upgrading ($ {})!'.format(
@@ -158,6 +156,7 @@ def check_for_updates():
def ensure_latest_self(user=False):
"""Updates Pipenv to latest version, cleverly."""
checked_for_updates()
try:
r = requests.get('https://pypi.python.org/pypi/pipenv/json', timeout=2)
except requests.RequestException as e:
@@ -165,7 +164,6 @@ def ensure_latest_self(user=False):
sys.exit(1)
latest = max(map(semver.parse_version_info, r.json()['releases'].keys()))
current = semver.parse_version_info(__version__)
checked_for_updates()
if current < latest:
@@ -1624,7 +1622,7 @@ def cli(
# Awesome sauce.
click.echo(crayons.normal(xyzzy, bold=True))
if not update:
if not update and need_update_check():
# Spun off in background thread, not unlike magic.
check_for_updates()
else:
+6 -3
View File
@@ -955,7 +955,7 @@ def need_update_check():
if not os.path.exists(p):
return True
out_of_date_time = time() - (24 * 60 * 60)
if os.path.isfile(p) and os.stat(p).st_mtime <= out_of_date_time:
if os.path.isfile(p) and os.path.getmtime(p) <= out_of_date_time:
return True
else:
return False
@@ -964,6 +964,9 @@ def need_update_check():
def checked_for_updates():
mkdir_p(PIPENV_CACHE_DIR)
p = os.sep.join((PIPENV_CACHE_DIR, '.pipenv_update_check'))
with open(p, 'w') as fh:
fh.write('')
try:
os.utime(p)
except FileNotFoundError:
with open(p, 'w') as fh:
fh.write('')
return True