From 2fb3d9b515e013c0902dad4b3da7b5c5e3c30099 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 2 Jul 2024 22:07:12 -0400 Subject: [PATCH 1/3] #514 cleaned up download logic and added retries --- ccmsi.lua | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/ccmsi.lua b/ccmsi.lua index bc91112..a2c6e18 100644 --- a/ccmsi.lua +++ b/ccmsi.lua @@ -18,7 +18,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. local function println(message) print(tostring(message)) end local function print(message) term.write(tostring(message)) end -local CCMSI_VERSION = "v1.15" +local CCMSI_VERSION = "v1.16" local install_dir = "/.install-cache" local manifest_path = "https://mikaylafischler.github.io/cc-mek-scada/manifests/" @@ -120,6 +120,22 @@ local function write_install_manifest(manifest, dependencies) imfile.close() end +-- try at most 3 times to download a file from the repository +local function http_get_file(file) + local dl, err + for i = 1, 3 do + dl, err = http.get(repo_path..file) + if dl then break + else + red();println("HTTP Error "..err) + lgray();println("retrying...") +---@diagnostic disable-next-line: undefined-field + os.sleep(0.25 * i) + end + end + return dl +end + -- recursively build a tree out of the file manifest local function gen_tree(manifest, log) local function _tree_add(tree, split) @@ -420,16 +436,16 @@ elseif mode == "install" or mode == "update" then local files = file_list[dependency] for _, file in pairs(files) do println("GET "..file) - local dl, err = http.get(repo_path..file) + local dl = http_get_file(file) - if dl == nil then - red();println("HTTP Error "..err) - success = false - break - else + if dl then local handle = fs.open(install_dir.."/"..file, "w") handle.write(dl.readAll()) handle.close() + else + red();println("failed to download "..file) + success = false + break end end end @@ -482,16 +498,16 @@ elseif mode == "install" or mode == "update" then local files = file_list[dependency] for _, file in pairs(files) do println("GET "..file) - local dl, err = http.get(repo_path..file) + local dl = http_get_file(file) - if dl == nil then - red();println("HTTP Error "..err) - success = false - break - else + if dl then local handle = fs.open("/"..file, "w") handle.write(dl.readAll()) handle.close() + else + red();println("failed to download "..file) + success = false + break end end end From 1e341af8a52210287b1821a72e8d0d42b223091c Mon Sep 17 00:00:00 2001 From: Mikayla Date: Wed, 3 Jul 2024 15:01:43 +0000 Subject: [PATCH 2/3] #514 optimizations and fixes --- ccmsi.lua | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/ccmsi.lua b/ccmsi.lua index a2c6e18..7f4647f 100644 --- a/ccmsi.lua +++ b/ccmsi.lua @@ -120,20 +120,25 @@ local function write_install_manifest(manifest, dependencies) imfile.close() end --- try at most 3 times to download a file from the repository -local function http_get_file(file) +-- try at most 3 times to download a file from the repository and write into w_path base directory +local function http_get_file(file, w_path) local dl, err for i = 1, 3 do dl, err = http.get(repo_path..file) - if dl then break + if dl then + if i > 1 then green();println("success!");lgray() end + local f = fs.open(w_path..file, "w") + f.write(dl.readAll()) + f.close() + break else red();println("HTTP Error "..err) - lgray();println("retrying...") + if i < 3 then lgray();print("> retrying...") end ---@diagnostic disable-next-line: undefined-field - os.sleep(0.25 * i) + os.sleep(i/3.0) end end - return dl + return dl ~= nil end -- recursively build a tree out of the file manifest @@ -436,13 +441,7 @@ elseif mode == "install" or mode == "update" then local files = file_list[dependency] for _, file in pairs(files) do println("GET "..file) - local dl = http_get_file(file) - - if dl then - local handle = fs.open(install_dir.."/"..file, "w") - handle.write(dl.readAll()) - handle.close() - else + if not http_get_file(file, install_dir.."/") then red();println("failed to download "..file) success = false break @@ -498,13 +497,7 @@ elseif mode == "install" or mode == "update" then local files = file_list[dependency] for _, file in pairs(files) do println("GET "..file) - local dl = http_get_file(file) - - if dl then - local handle = fs.open("/"..file, "w") - handle.write(dl.readAll()) - handle.close() - else + if not http_get_file(file, "/") then red();println("failed to download "..file) success = false break From f2cd98c57aaa6bc83c46f904774873fc42be8515 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 3 Jul 2024 21:14:39 -0400 Subject: [PATCH 3/3] #194 fixes to log file handling, improved failure behavior, skip extra dialogs if nothing can be updated --- ccmsi.lua | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/ccmsi.lua b/ccmsi.lua index 7f4647f..be9c2e8 100644 --- a/ccmsi.lua +++ b/ccmsi.lua @@ -132,7 +132,7 @@ local function http_get_file(file, w_path) f.close() break else - red();println("HTTP Error "..err) + red();println("HTTP Error: "..err) if i < 3 then lgray();print("> retrying...") end ---@diagnostic disable-next-line: undefined-field os.sleep(i/3.0) @@ -193,6 +193,7 @@ local function clean(manifest) local log = nil if fs.exists(app..".settings") and settings.load(app..".settings") then log = settings.get("LogPath") + if log:sub(1, 1) == "/" then log = log:sub(2) end end local tree = gen_tree(manifest, log) @@ -343,7 +344,7 @@ elseif mode == "install" or mode == "update" then local dl, err = http.get(repo_path.."ccmsi.lua") if dl == nil then - red();println("HTTP Error "..err) + red();println("HTTP Error: "..err) println("Installer download failed.");white() else local handle = fs.open(debug.getinfo(1, "S").source:sub(2), "w") -- this file, regardless of name or location @@ -380,9 +381,6 @@ elseif mode == "install" or mode == "update" then ver.graphics.changed = show_pkg_change("graphics", ver.graphics) ver.lockbox.changed = show_pkg_change("lockbox", ver.lockbox) - -- ask for confirmation - if not ask_y_n("Continue", false) then return end - -------------------------- -- START INSTALL/UPDATE -- -------------------------- @@ -397,11 +395,32 @@ elseif mode == "install" or mode == "update" then table.insert(dependencies, app) + -- helper function to check if a dependency is unchanged + local function unchanged(dependency) + if dependency == "system" then return not ver.boot.changed + elseif dependency == "graphics" then return not ver.graphics.changed + elseif dependency == "lockbox" then return not ver.lockbox.changed + elseif dependency == "common" then return not (ver.common.changed or ver.comms.changed) + elseif dependency == app then return not ver.app.changed + else return true end + end + + local any_change = false + for _, dependency in pairs(dependencies) do local size = size_list[dependency] space_required = space_required + size + any_change = any_change or not unchanged(dependency) end + if mode == "update" and not any_change then + yellow();println("Nothing to do, everything is already up-to-date!");white() + return + end + + -- ask for confirmation + if not ask_y_n("Continue", false) then return end + -- check space constraints if space_available < space_required then single_file_mode = true @@ -417,16 +436,6 @@ elseif mode == "install" or mode == "update" then local success = true - -- helper function to check if a dependency is unchanged - local function unchanged(dependency) - if dependency == "system" then return not ver.boot.changed - elseif dependency == "graphics" then return not ver.graphics.changed - elseif dependency == "lockbox" then return not ver.lockbox.changed - elseif dependency == "common" then return not (ver.common.changed or ver.comms.changed) - elseif dependency == app then return not ver.app.changed - else return true end - end - if not single_file_mode then if fs.exists(install_dir) then fs.delete(install_dir);fs.makeDir(install_dir) end @@ -448,6 +457,7 @@ elseif mode == "install" or mode == "update" then end end end + if not success then break end end -- copy in downloaded files (installation) @@ -504,6 +514,7 @@ elseif mode == "install" or mode == "update" then end end end + if not success then break end end if success then