forked from ross-g/io_pdx_mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
116 lines (87 loc) · 3.92 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
"""
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 time
import logging
from datetime import datetime, date
# Py2, Py3 compatibility
try:
from urllib.request import urlopen, Request, URLError
except ImportError:
from urllib2 import urlopen, Request, URLError
from . import bl_info, IO_PDX_SETTINGS
UPDATER_LOG = logging.getLogger("io_pdx.updater")
""" ====================================================================================================================
Variables.
========================================================================================================================
"""
TIMEOUT = 1.0 # seconds
API_URL = "https://api.github.com"
""" ====================================================================================================================
Helper functions.
========================================================================================================================
"""
class Github_API(object):
"""
Handles connection to Githubs API to get some data on releases for this repository.
"""
def __init__(self):
self.LATEST_VERSION = None
self.LATEST_URL = None
self.AT_LATEST = None
self.CURRENT_VERSION = float(".".join(map(str, bl_info["version"])))
self.api = API_URL
self.owner = bl_info["author"]
self.repo = bl_info["project_name"]
self.args = {"owner": self.owner, "repo": self.repo, "api": self.api}
self.refresh()
@staticmethod
def get_data(url, t):
req = Request(url)
result = urlopen(req, timeout=t)
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, TIMEOUT)
except URLError as err:
UPDATER_LOG.warning("Unable to check for update. ({})".format(err.reason))
return
except Exception as err:
UPDATER_LOG.error("Failed on check for update. ({})".format(err))
return
self.LATEST_RELEASE = release_list[0]
latest = release_list[0]
# store data
self.LATEST_VERSION = float(latest["tag_name"])
self.LATEST_URL = latest["assets"][0]["browser_download_url"]
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()