-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdater.py
117 lines (89 loc) · 4.05 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
IO PDX Mesh Python module.
This is designed to allow tools to check if they are out of date or not and supply a download link to the latest.
author : ross-g
"""
import json
import logging
import time
from datetime import date, datetime
from os.path import splitext
# Py2, Py3 compatibility
try:
from urllib.request import Request, URLError, urlopen
except ImportError:
from urllib2 import Request, URLError, urlopen # type: ignore
from . import IO_PDX_INFO, IO_PDX_SETTINGS
UPDATER_LOG = logging.getLogger("io_pdx.updater")
""" ====================================================================================================================
Helper functions.
========================================================================================================================
"""
class Github_API(object):
"""
Handles connection to Githubs API to get some data on releases for this repository.
"""
API_URL = "https://api.github.com"
def __init__(self, owner, repo):
self.api = self.API_URL
self.owner = owner
self.repo = repo
self.args = {"owner": self.owner, "repo": self.repo, "api": self.api}
self.AT_LATEST = False
self.LATEST_VERSION = 0.0
self.LATEST_RELEASE = "https://github.com/{owner}/{repo}/releases/latest".format(**self.args)
self.LATEST_NOTES = ""
self.LATEST_URL = ""
self.CURRENT_VERSION = IO_PDX_INFO["current_git_tag"]
self.refresh()
@staticmethod
def get_data(url, time=1.0):
req = Request(url)
result = urlopen(req, timeout=time)
result_str = result.read()
result.close()
return json.JSONDecoder().decode(result_str.decode())
def refresh(self, force=False):
recheck = True
# only check for updates once per day
last_check_date = IO_PDX_SETTINGS.last_update_check
if last_check_date is not None:
recheck = date.today() > datetime.strptime(last_check_date, "%Y-%m-%d").date()
if recheck or force:
start = time.time()
# get latest release data
releases_url = "{api}/repos/{owner}/{repo}/releases".format(**self.args)
try:
release_list = self.get_data(releases_url)
self.LATEST_RELEASE = release_list[0]
except URLError as err:
UPDATER_LOG.warning("Unable to check for update. ({})".format(err.reason))
return
except IndexError as err:
UPDATER_LOG.warning("Found no releases during update check. ({})".format(err))
except Exception as err:
UPDATER_LOG.error("Failed during update check. ({})".format(err))
return
latest = release_list[0]
# store data
self.LATEST_VERSION = float(latest["tag_name"])
self.LATEST_URL = {
splitext(asset["name"])[0].split("-")[0]: asset["browser_download_url"] for asset in latest["assets"]
}
self.LATEST_NOTES = "{0}\r\nRelease version: {1}\r\n{2}".format(
latest["published_at"].split("T")[0], latest["tag_name"], latest["body"]
)
# cache data to settings
IO_PDX_SETTINGS.github_latest_version = self.LATEST_VERSION
IO_PDX_SETTINGS.github_latest_url = self.LATEST_URL
IO_PDX_SETTINGS.github_latest_notes = self.LATEST_NOTES
IO_PDX_SETTINGS.last_update_check = str(date.today())
UPDATER_LOG.info("Checked for update. ({0:.4f} sec)".format(time.time() - start))
else:
# used cached release data in settings
self.LATEST_VERSION = IO_PDX_SETTINGS.github_latest_version
self.LATEST_URL = IO_PDX_SETTINGS.github_latest_url
self.LATEST_NOTES = IO_PDX_SETTINGS.github_latest_notes
UPDATER_LOG.info("Skipped update check. (already ran today)")
self.AT_LATEST = self.CURRENT_VERSION == self.LATEST_VERSION
github = Github_API(owner=IO_PDX_INFO["maintainer"], repo=IO_PDX_INFO["id"])