|
| 1 | +--[[ |
| 2 | + This file is part of darktable, |
| 3 | + copyright (c) 2015 Tobias Ellinghaus |
| 4 | +
|
| 5 | + darktable is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + darktable is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with darktable. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +]] |
| 18 | +--[[ |
| 19 | +CHECK FOR UPDATES |
| 20 | +a simple script that will automatically look for newer releases on github and inform |
| 21 | +when there is something. it will only check on startup and only once a week. |
| 22 | +
|
| 23 | +USAGE |
| 24 | +* install luasec and cjson for Lua 5.2 on your system |
| 25 | +* require this script from your main lua file |
| 26 | +* restart darktable |
| 27 | +
|
| 28 | +]] |
| 29 | + |
| 30 | +local dt = require "darktable" |
| 31 | +local https = require "ssl.https" |
| 32 | +local cjson = require "cjson" |
| 33 | + |
| 34 | +dt.configuration.check_version(...,{2,0,0}) |
| 35 | + |
| 36 | +-- compare two version strings of the form "major.minor.patch" |
| 37 | +-- returns -1, 0, 1 if the first version is smaller, equal, greater than the second version, |
| 38 | +-- or nil if one or both are of the wrong format |
| 39 | +-- strings like "release-1.2.3" and "1.2.3+456~gb00b5" are fine, too |
| 40 | +local function compare_versions(a, b) |
| 41 | + local a_major, a_minor, a_patch = a:match("(%d+)%.(%d+)%.(%d+)") |
| 42 | + local b_major, b_minor, b_patch = b:match("(%d+)%.(%d+)%.(%d+)") |
| 43 | + if a_major and a_minor and a_patch and b_major and b_minor and b_patch then |
| 44 | + if a_major < b_major then return -1 end |
| 45 | + if a_major > b_major then return 1 end |
| 46 | + |
| 47 | + if a_minor < b_minor then return -1 end |
| 48 | + if a_minor > b_minor then return 1 end |
| 49 | + |
| 50 | + if a_patch < b_patch then return -1 end |
| 51 | + if a_patch > b_patch then return 1 end |
| 52 | + |
| 53 | + return 0 |
| 54 | + else |
| 55 | + return |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | + |
| 60 | +-- check stored timestamp and skip the check if the last time was not too long ago |
| 61 | +-- for now we are assuming that os.time() returns seconds. that's not guaranteed but the case on many systems. |
| 62 | +-- the reference date doesn't matter, as long as it's currently positive (we start with 0 the first time) |
| 63 | +-- see http://lua-users.org/wiki/DateAndTime |
| 64 | +local now = os.time() |
| 65 | +local back_then = dt.preferences.read("check_for_updates", "timestamp", "integer") |
| 66 | + |
| 67 | +-- check once a week |
| 68 | +if now > (back_then + 60 * 60 * 24 * 7) then |
| 69 | + |
| 70 | + -- try to get the latest release's version from github and compare to what we are running |
| 71 | + -- see https://developer.github.com/v3/repos/releases/ for the api docs |
| 72 | + -- just ignore when anything fails and retry at some other time |
| 73 | + local result, error_code = https.request("https://api.github.com/repos/darktable-org/darktable/releases/latest") |
| 74 | + |
| 75 | + if error_code == 200 then |
| 76 | + local name = cjson.decode(result)["name"] -- http://www.kyne.com.au/~mark/software/lua-cjson-manual.html |
| 77 | + if name then |
| 78 | + local cmp = compare_versions(name, dt.configuration.version) |
| 79 | + if cmp then |
| 80 | + if cmp > 0 then |
| 81 | + dt.print("there seems to be a newer release than what you are running. better update") |
| 82 | + end |
| 83 | + -- update timestamp to not check again for a while |
| 84 | + dt.preferences.write("check_for_updates", "timestamp", "integer", now) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | +end |
0 commit comments