Bugfix for not displaying information when there is no update available + simplification

This commit is contained in:
Virgil Grigoras 2018-09-10 11:20:34 +02:00
parent f7872aded0
commit b2b092c190
2 changed files with 80 additions and 69 deletions

View File

@ -568,7 +568,7 @@ def get_current_version_info():
return False return False
if is_sha1(content[0]) and len(content[1]) > 0: if is_sha1(content[0]) and len(content[1]) > 0:
return {'sha': content[0], 'datetime': content[1]} return {'hash': content[0], 'datetime': content[1]}
except FileNotFoundError: except FileNotFoundError:
return False return False
return False return False

View File

@ -1090,6 +1090,8 @@ def get_update_status():
'message': '', 'message': '',
'current_commit_hash': '' 'current_commit_hash': ''
} }
parents = []
repository_url = 'https://api.github.com/repos/janeczku/calibre-web' repository_url = 'https://api.github.com/repos/janeczku/calibre-web'
tz = datetime.timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone) tz = datetime.timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone)
@ -1098,7 +1100,7 @@ def get_update_status():
if version is False: if version is False:
status['current_commit_hash'] = _(u'Unknown') status['current_commit_hash'] = _(u'Unknown')
else: else:
status['current_commit_hash'] = version['sha'] status['current_commit_hash'] = version['hash']
try: try:
r = requests.get(repository_url + '/git/refs/heads/master') r = requests.get(repository_url + '/git/refs/heads/master')
@ -1114,83 +1116,92 @@ def get_update_status():
status['message'] = _(u'General error') status['message'] = _(u'General error')
if status['message'] != '': if status['message'] != '':
status['success'] = False
return json.dumps(status) return json.dumps(status)
if 'object' in commit and commit['object']['sha'] != status['current_commit_hash']: if 'object' not in commit:
# a new update is available status['message'] = _(u'Unexpected data while reading update information')
try: return json.dumps(status)
r = requests.get(repository_url + '/git/commits/' + commit['object']['sha'])
r.raise_for_status()
update_data = r.json()
except requests.exceptions.HTTPError as ex:
status['error'] = _(u'HTTP Error') + ' ' + str(ex)
except requests.exceptions.ConnectionError:
status['error'] = _(u'Connection error')
except requests.exceptions.Timeout:
status['error'] = _(u'Timeout while establishing connection')
except requests.exceptions.RequestException:
status['error'] = _(u'General error')
if status['message'] != '': if commit['object']['sha'] == status['current_commit_hash']:
status['success'] = False status.update({
return json.dumps(status) 'update': False,
'success': True,
'message': _(u'Now update available. You already have the latest version installed')
})
return json.dumps(status)
if 'committer' in update_data and 'message' in update_data: # a new update is available
parents = [] status['update'] = True
status['update'] = True try:
status['success'] = True r = requests.get(repository_url + '/git/commits/' + commit['object']['sha'])
status['message'] = _(u'A new update is available. Click on the button below update to the latest version.') r.raise_for_status()
update_data = r.json()
except requests.exceptions.HTTPError as ex:
status['error'] = _(u'HTTP Error') + ' ' + str(ex)
except requests.exceptions.ConnectionError:
status['error'] = _(u'Connection error')
except requests.exceptions.Timeout:
status['error'] = _(u'Timeout while establishing connection')
except requests.exceptions.RequestException:
status['error'] = _(u'General error')
new_commit_date = datetime.datetime.strptime( if status['message'] != '':
update_data['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') - tz return json.dumps(status)
parents.append(
[
format_datetime(new_commit_date, format='short', locale=get_locale()),
update_data['message'],
update_data['sha']
]
)
# it only makes sense to analyze the parents if we know the current commit hash if 'committer' in update_data and 'message' in update_data:
if status['current_commit_hash'] != '': status['success'] = True
try: status['message'] = _(u'A new update is available. Click on the button below update to the latest version.')
parent_commit = update_data['parents'][0]
# limit the maximum search depth
remaining_parents_cnt = 10
except IndexError:
remaining_parents_cnt = None
if remaining_parents_cnt is not None: new_commit_date = datetime.datetime.strptime(
while True: update_data['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') - tz
if remaining_parents_cnt == 0: parents.append(
[
format_datetime(new_commit_date, format='short', locale=get_locale()),
update_data['message'],
update_data['sha']
]
)
# it only makes sense to analyze the parents if we know the current commit hash
if status['current_commit_hash'] != '':
try:
parent_commit = update_data['parents'][0]
# limit the maximum search depth
remaining_parents_cnt = 10
except IndexError:
remaining_parents_cnt = None
if remaining_parents_cnt is not None:
while True:
if remaining_parents_cnt == 0:
break
# check if we are more than one update behind if so, go up the tree
if parent_commit['sha'] != status['current_commit_hash']:
try:
r = requests.get(parent_commit['url'])
r.raise_for_status()
parent_data = r.json()
parent_commit_date = datetime.datetime.strptime(
parent_data['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') - tz
parent_commit_date = format_datetime(
parent_commit_date, format='short', locale=get_locale())
parents.append([parent_commit_date, parent_data['message'], parent_data['sha']])
parent_commit = parent_data['parents'][0]
remaining_parents_cnt -= 1
except Exception:
# it isn't crucial if we can't get information about the parent
break break
else:
# parent is our current version
break
# check if we are more than one update behind if so, go up the tree else:
if parent_commit['sha'] != status['current_commit_hash']: status['success'] = False
try: status['message'] = _(u'Could not fetch update information')
r = requests.get(parent_commit['url'])
r.raise_for_status()
parent_data = r.json()
parent_commit_date = datetime.datetime.strptime(
parent_data['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') - tz
parent_commit_date = format_datetime(
parent_commit_date, format='short', locale=get_locale())
parents.append([parent_commit_date, parent_data['message'], parent_data['sha']])
parent_commit = parent_data['parents'][0]
remaining_parents_cnt -= 1
except Exception:
# it isn't crucial if we can't get information about the parent
break
else:
# parent is our current version
break
else:
status['success'] = False
status['message'] = _(u'Could not fetch update information')
status['history'] = parents status['history'] = parents
return json.dumps(status) return json.dumps(status)