diff --git a/official/check_for_updates.lua b/official/check_for_updates.lua new file mode 100644 index 0000000..1e7977b --- /dev/null +++ b/official/check_for_updates.lua @@ -0,0 +1,89 @@ +--[[ + This file is part of darktable, + copyright (c) 2015 Tobias Ellinghaus + + darktable is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + darktable is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with darktable. If not, see . +]] +--[[ +CHECK FOR UPDATES +a simple script that will automatically look for newer releases on github and inform +when there is something. it will only check on startup and only once a week. + +USAGE +* install luasec and cjson for Lua 5.2 on your system +* require this script from your main lua file +* restart darktable + +]] + +local dt = require "darktable" +local https = require "ssl.https" +local cjson = require "cjson" + +dt.configuration.check_version(...,{2,0,0}) + +-- compare two version strings of the form "major.minor.patch" +-- returns -1, 0, 1 if the first version is smaller, equal, greater than the second version, +-- or nil if one or both are of the wrong format +-- strings like "release-1.2.3" and "1.2.3+456~gb00b5" are fine, too +local function compare_versions(a, b) + local a_major, a_minor, a_patch = a:match("(%d+)%.(%d+)%.(%d+)") + local b_major, b_minor, b_patch = b:match("(%d+)%.(%d+)%.(%d+)") + if a_major and a_minor and a_patch and b_major and b_minor and b_patch then + if a_major < b_major then return -1 end + if a_major > b_major then return 1 end + + if a_minor < b_minor then return -1 end + if a_minor > b_minor then return 1 end + + if a_patch < b_patch then return -1 end + if a_patch > b_patch then return 1 end + + return 0 + else + return + end +end + + +-- check stored timestamp and skip the check if the last time was not too long ago +-- for now we are assuming that os.time() returns seconds. that's not guaranteed but the case on many systems. +-- the reference date doesn't matter, as long as it's currently positive (we start with 0 the first time) +-- see http://lua-users.org/wiki/DateAndTime +local now = os.time() +local back_then = dt.preferences.read("check_for_updates", "timestamp", "integer") + +-- check once a week +if now > (back_then + 60 * 60 * 24 * 7) then + + -- try to get the latest release's version from github and compare to what we are running + -- see https://developer.github.com/v3/repos/releases/ for the api docs + -- just ignore when anything fails and retry at some other time + local result, error_code = https.request("https://api.github.com/repos/darktable-org/darktable/releases/latest") + + if error_code == 200 then + local name = cjson.decode(result)["name"] -- http://www.kyne.com.au/~mark/software/lua-cjson-manual.html + if name then + local cmp = compare_versions(name, dt.configuration.version) + if cmp then + if cmp > 0 then + dt.print("there seems to be a newer release than what you are running. better update") + end + -- update timestamp to not check again for a while + dt.preferences.write("check_for_updates", "timestamp", "integer", now) + end + end + end + +end