-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateChecker.py
277 lines (226 loc) · 12.1 KB
/
updateChecker.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# T1nk-R's Mesh Name Synchronizer add-on for Blender
# - part of T1nk-R Utilities for Blender
#
# Version: Please see the version tag under bl_info in __init__.py.
#
# This module is responsible for checking if updates are available.
#
# Module and add-on authored by T1nk-R (https://github.com/gusztavj/)
#
# PURPOSE & USAGE *****************************************************************************************************************
# You can use this add-on to synchronize the names of meshes with the names of their parent objects.
#
# Help, support, updates and anything else: https://github.com/gusztavj/T1nkR-Mesh-Name-Synchronizer
#
# COPYRIGHT ***********************************************************************************************************************
#
# ** MIT License **
#
# Copyright (c) 2023-2024, T1nk-R (Gusztáv Jánvári)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# ** Commercial Use **
#
# I would highly appreciate to get notified via [janvari.gusztav@imprestige.biz](mailto:janvari.gusztav@imprestige.biz) about
# any such usage. I would be happy to learn this work is of your interest, and to discuss options for commercial support and
# other services you may need.
#
# DISCLAIMER **********************************************************************************************************************
# This add-on is provided as-is. Use at your own risk. No warranties, no guarantee, no liability,
# no matter what happens. Still I tried to make sure no weird things happen:
# * This add-on is intended to change the name of the meshes and other data blocks under your Blender objects.
# * This add-on is not intended to modify your objects and other Blender assets in any other way.
# * You shall be able to simply undo consequences made by this add-on.
#
# You may learn more about legal matters on page https://github.com/gusztavj/T1nkR-Mesh-Name-Synchronizer
#
# *********************************************************************************************************************************
from __future__ import annotations
from . import bl_info
import requests
import json
import contextlib
from datetime import datetime, timedelta
from bpy.types import PropertyGroup, Operator, Context
from bpy.props import StringProperty, BoolProperty, IntProperty
# Repository information for help and updates #####################################################################################
class UpdateCheckingInfo:
"""
Information to access the GitHub Update Checker service
"""
_repoSlug = "T1nkR-Mesh-Name-Synchronizer"
"""Slug for the repository"""
_repoBase = "https://github.com/gusztavj"
"""Base address of my repositories"""
_repoApiBase = "https://api.github.com/repos/gusztavj"
"""Base address of my repositories for API calls"""
@staticmethod
def _stripSlashes(uriSegment) -> str:
"""Strips leading and trailing slashes"""
return uriSegment.lstrip("/").rstrip("/")
@staticmethod
def _combineUri(*args: str) -> str:
"""Combines strings into an URI by stripping all leading and trailing slashes beforehand"""
return "/".join( f"{UpdateCheckingInfo._stripSlashes(segment)}" for segment in args )
@staticmethod
def repoUrl() -> str:
"""URL of the repository"""
return UpdateCheckingInfo._combineUri(UpdateCheckingInfo._repoBase, UpdateCheckingInfo._repoSlug)
def repoReleasesUrl() -> str:
"""URL of the releases page of the repository"""
return UpdateCheckingInfo._combineUri(UpdateCheckingInfo._repoBase, UpdateCheckingInfo._repoSlug, "releases")
def repoReleaseApiUrl() -> str:
"""API URL to get latest release information"""
return UpdateCheckingInfo._combineUri(UpdateCheckingInfo._repoApiBase, UpdateCheckingInfo._repoSlug, "releases", "latest")
currentVersion: str = ""
"""Version number of the current version running in `x.y.z` format"""
forceUpdateCheck: bool = False
@staticmethod
def getUpdateCheckingServiceUrl() -> str:
"""URL to the service endpoint of tge GitHub Update Checker service"""
# Production URL
return "https://apps.imprestige.biz/gitHubUpdateChecker/getUpdateInfo"
# Test URL
#return "http://localhost:5000/getUpdateInfo"
@staticmethod
def getRequestBody():
return {
"appInfo":
{
"repoSlug": UpdateCheckingInfo._repoSlug,
"currentVersion": UpdateCheckingInfo.currentVersion
},
"forceUpdateCheck": UpdateCheckingInfo.forceUpdateCheck
}
# Structured update info ##########################################################################################################
class T1nkerMeshNameSynchronizerUpdateInfo(PropertyGroup):
"""
Information about the current and the latest update
"""
checkFrequencyDays: IntProperty(
name="Update check frequency (days)",
default=1
) # type: ignore
"""
Frequency of checking for new updates (days).
"""
updateAvailable: BoolProperty(
name="Is update available",
default=False
) # type: ignore
"""
Tells whether an update is available (`True`).
"""
currentVersion: StringProperty(
name="Installed version",
default=""
) # type: ignore
"""
Version number of the current version running in x.y.z format.
"""
latestVersion: StringProperty(
name="Latest available version",
default=""
) # type: ignore
"""
Version number of the latest release (the release tag from the repo).
"""
latestVersionName: StringProperty(
name="Name of latest version",
default=""
) # type: ignore
"""
Name of the latest release.
"""
lastCheckedTimestamp: StringProperty(
name="When last successful check for updates happened",
default=""
) # type: ignore
"""
Date and time of last successful check for updates.
"""
# Operator for checking updates ###################################################################################################
class T1NKER_OT_MeshNameSynchronizerUpdateChecker(Operator):
"""
Checks for updates
"""
# Properties ==================================================================================================================
# Blender-specific stuff ------------------------------------------------------------------------------------------------------
bl_idname = "t1nker.meshnamesynchronizerupdatechecker"
bl_label = "Check updates for T1nk-R Mesh Names Synchronizer"
bl_description = "Check updates for T1nk-R Mesh Names Synchronizer"
bl_options = {'REGISTER', 'UNDO'}
bl_category = "T1nk-R Utils"
# Other properties ------------------------------------------------------------------------------------------------------------
forceUpdateCheck: BoolProperty(default = False) # type: ignore
"""
Whether to force update check. Use only for testing. Once the operator is called,
this is set back to False to prevent accidental flooding of GitHub.
"""
# Public functions ============================================================================================================
# Perform the operation -------------------------------------------------------------------------------------------------------
def execute(self, context: Context): # sourcery skip: extract-method
"""
Performs update check for the add-on and caches results. The cache expires in some days as specified in
`updateInfo.T1nkerMeshNameSynchronizerUpdateInfo.checkFrequencyDays`, and then new check is performed. Until that the
cached information is served.
Args:
context (bpy.types.Context): A context object passed on by Blender for the current context.
event: The event triggering the operation, as passed on by Blender.
Returns:
{'FINISHED'} or {'ERROR'}, indicating success or failure of the operation.
"""
updateInfo = context.preferences.addons[__package__].preferences.updateInfo
# Check cache expiry only if update check is not forced
if not self.forceUpdateCheck:
# Check if update check shall be performed based on frequency
with contextlib.suppress(Exception):
lastCheckDate = datetime.strptime(updateInfo.lastCheckedTimestamp, '%Y-%m-%d %H:%M:%S')
delta = datetime.now() - lastCheckDate
if delta.days < updateInfo.checkFrequencyDays: # Successfully checked for updates in the last checkFrequencyDays number of days
# Do not flood the repo API, use cached info
return
else: # turn forcing check off to prevent accidental flooding
self.forceUpdateCheck = False
try: # if anything goes wrong we silently fail, no need to perform double-checks
print(f"{__package__}: Trying to check for updates")
# Get installed version (already stored as a list by Blender)
installedVersionTags = bl_info["version"]
updateInfo.currentVersion = ".".join([str(i) for i in installedVersionTags])
UpdateCheckingInfo._repoSlug = "T1nkR-Mesh-Name-Synchronizer"
UpdateCheckingInfo.currentVersion = updateInfo.currentVersion
headers = {'Content-Type': 'application/json'}
payload = UpdateCheckingInfo.getRequestBody()
response = requests.post(UpdateCheckingInfo.getUpdateCheckingServiceUrl(), headers=headers, json=payload, timeout=5)
# For errors, enable raising exceptions
if response.status_code != 200:
response.raise_for_status()
# Being here means a response has been received successfully
repoInfo = response.json()["repository"]
updateInfo.latestVersionName = repoInfo["latestVersionName"]
updateInfo.latestVersion = repoInfo["latestVersion"]
updateInfo.releaseUrl = repoInfo["latestVersion"]
updateInfo.repoUrl = repoInfo["repoUrl"]
updateInfo.updateAvailable = response.json()["updateAvailable"]
# Save timestamp
updateInfo.lastCheckedTimestamp = f"{datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')}"
print(f"{__package__}: Checking for updates completed, there is {'a' if updateInfo.updateAvailable else 'no' } new version available")
except requests.exceptions.Timeout as tex:
# Timeout, let's not bother the user
print(f"{__package__}: Version checking timed out")
updateInfo.updateAvailable = False
except Exception as ex:
print(f"{__package__}: Error during version check: {ex}")
updateInfo.updateAvailable = False
return {'FINISHED'}