From 9253f00eef12437c4088c02ebd33f5079cf58c5e Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 26 Feb 2024 02:25:25 +0100 Subject: [PATCH] funktioniert nimmer aber heut is gut --- PowerManager.lua | 5 +- updater.lua => git-tool.lua | 164 ++++++++++++++++++++++++++---------- 2 files changed, 123 insertions(+), 46 deletions(-) rename updater.lua => git-tool.lua (57%) diff --git a/PowerManager.lua b/PowerManager.lua index 4419f0e..d5749dc 100644 --- a/PowerManager.lua +++ b/PowerManager.lua @@ -6,20 +6,19 @@ local event = require( "event" ) local splashText = "Stromanzeige - Ultimate ROG RGB LED Edition" local frameTitle = "Power Monitor - Klicken zum Beenden" - + local oldW, oldH = gpu.getResolution() local newW = 160 local newH = 50 local numberOfPanels = 2 local panelWidth = (newW / numberOfPanels) gpu.setResolution(newW, newH) - + function clearScreen() local w,h = gpu.getResolution() drawLine(1, 1, w, h, 0x000000) end - function drawLine(startX, startY, stopX, stopY, colorOfLine) local oldColor = gpu.getBackground(false) gpu.setBackground(colorOfLine, false) diff --git a/updater.lua b/git-tool.lua similarity index 57% rename from updater.lua rename to git-tool.lua index 8b6edd9..5d17b2c 100644 --- a/updater.lua +++ b/git-tool.lua @@ -9,6 +9,7 @@ NO = {"n","no","N","No","NO"} SupportedRemotes = { Github = { BaseUrl = "https://github.com/", + RawApiUrl = "https://raw.githubusercontent.com/", Implemented = true }, Gitea = { @@ -17,7 +18,15 @@ SupportedRemotes = { } } -Repository = {} +EmptyRepository = { + Owner = nil, + Name = nil, + ShortName = nil, + RepoIdentifier = nil, + Remote = nil, + CurrentBranch = nil, + CurrentLocalPath = nil +} ---default repo to update DefaultRepository = { @@ -67,8 +76,17 @@ local function askTextQuestion(question, defaultAnswerOnEnter, allowOnly) if userInput == "" then return defaultAnswerOnEnter else return userInput end end +local function makeDirIfNotExists(target) + if filesystem.exists(target) then + if not filesystem.isDirectory(target) then error("target directory already exists and is not a directory.") end + if filesystem.get(target).isReadOnly() then error("target directory is read-only.") end + else + if not filesystem.makeDirectory(target) then error("directory creation failed") end + end +end + --- todo: pcall and catch errors -local function downloadRepo(repository, remoteType, autoOverride) +local function downloadRepo(repository, remote, autoOverride) local function validateRepositoryIdentifier(repository) if not repository.RepoIdentifier:match("^[%w-.]*/[%w-.]*$") then @@ -79,24 +97,15 @@ local function downloadRepo(repository, remoteType, autoOverride) validateRepositoryIdentifier(repository) - local function makeDirIfNotExists(target) - if filesystem.exists(target) then - if not filesystem.isDirectory(target) then error("target directory already exists and is not a directory.") end - if filesystem.get(target).isReadOnly() then error("target directory is read-only.") end - else - if not filesystem.makeDirectory(target) then error("directory creation failed") end - end - end - --- FIXME: If download only mode is enabled, set this to /home. still todo local targetDownloadPath = DefaultTemporaryDownloadPath..repository.RepoIdentifier - repository.CurrentLocalPath = targetDownloadPath - local success, err = pcall(makeDirIfNotExists, targetDownloadPath) + repository.CurrentLocalPath = targetDownloadPath.."/" + local success, res = pcall(makeDirIfNotExists, targetDownloadPath) if not success then error("the download failed because of filesystem errors.") end - local function fetchFilesAndSubdirs(repository, remoteType, dir) + local function fetchFilesAndSubdirs(repository, remote, dir) dir = dir or "" -- default value, start at root dir - if remoteType == SupportedRemotes.Github then + if remote == SupportedRemotes.Github then print("fetching contents for "..repository.RepoIdentifier..dir) local githubApiUrl="https://api.github.com/repos/"..repository.RepoIdentifier.."/contents"..dir local success,chunks=pcall(internet.request,githubApiUrl) @@ -116,7 +125,7 @@ local function downloadRepo(repository, remoteType, autoOverride) if t[i].type=="dir" then table.insert(directories,dir.."/"..t[i].name) - local subfiles,subdirs=fetchFilesAndSubdirs(repository,SupportedRemotes.Github,dir.."/"..t[i].name) + local subfiles,subdirs=fetchFilesAndSubdirs(repository,remote,dir.."/"..t[i].name) for i=1,#subfiles do table.insert(files,subfiles[i]) end @@ -133,7 +142,7 @@ local function downloadRepo(repository, remoteType, autoOverride) end --- fetch and make dirs in the target download path recursively - local files,dirs=fetchFilesAndSubdirs(repository.RepoIdentifier, remoteType, "") + local files,dirs=fetchFilesAndSubdirs(repository, repository.Remote, "") for i=1,#dirs do local success, err = pcall(makeDirIfNotExists, targetDownloadPath..dirs[i]) if not success then error(("the download failed because of filesystem errors. %x"):format(err)) end @@ -143,80 +152,141 @@ local function downloadRepo(repository, remoteType, autoOverride) if autoOverride == true then replaceMode = "always" end local function downloadFiles(files, targetDownloadPath, replaceMode) + local replace=nil + local downloadedFileTargets = {} for i=1,#files do - local replace=nil - if filesystem.exists(targetDownloadPath..files[i]) then + if replaceMode == "always" then replace = true end + local fileExists = filesystem.exists(targetDownloadPath..files[i]) + if fileExists then --- FIXME dir löschen statt error if filesystem.isDirectory(targetDownloadPath..files[i]) then error("file "..targetDownloadPath..files[i].." blocked by directory with same name!") end - if not replaceMode == "ask" then - replace = (replaceMode=="always") - replace = not (replaceMode=="never") - end - else print("\nFile "..targetDownloadPath..files[i].." already exists.\nReplace with new version?") end - local response="" + if replace == nil then - --- FIXME: ggf falsch und buggy local userInput = askTextQuestion("\nFile "..targetDownloadPath..files[i].." already exists.\nReplace with new version?","n",{"y","n","A","S"}) + --- FIXME: replaceMode ist broken. A wird immer wieder gefragt. if userInput=="A" then replaceMode="always" userInput="y" elseif userInput=="S" then replaceMode="never" userInput="n" - elseif userInput == "y" then replace=true - elseif userInput == "n" then replace=false end + if userInput == "y" then replace=true end + if userInput == "n" then replace=false end end - if replace then filesystem.remove(targetDownloadPath..files[i]) end - if replace or replace == nil then + end + if fileExists and replace then filesystem.remove(targetDownloadPath..files[i]) end + if replace or (replace == nil) then --- TODO @Freddy: Coole Animation hinzufügen print("downloading file "..files[i]) - local url=repository.Remote.BaseUrl..repository.Name.."/"..repository.CurrentBranch..files[i] + --- HACK: RawApiUrl is for using Github Raw API. may have to be patched for Gitea Support + local url=repository.Remote.RawApiUrl..repository.RepoIdentifier.."/"..repository.CurrentBranch..files[i] local success,response=pcall(internet.request,url) if success then local raw="" for chunk in response do raw=raw..chunk end - print("writing to "..targetDownloadPath..files[i]) - local file=io.open(targetDownloadPath..files[i],"w") + local absoluteDownloadFileTargetPath = targetDownloadPath..files[i] + print("writing to "..absoluteDownloadFileTargetPath) + local file=io.open(absoluteDownloadFileTargetPath,"w") if file then -- might be nil under wierd circumstances file:write(raw) file:close() end + table.insert(downloadedFileTargets, files[i]) else error("a file did not download correctly. aborting") end - end + else error("file not removed, but installation was cancelled - This might result in a broken install.") end end + --- return list of all abolute paths the files were downloaded to + return downloadedFileTargets end - success, err = pcall(downloadFiles, files, targetDownloadPath, replaceMode) - if success then print("All files downloaded successfully.") else error(err) end + success, res = pcall(downloadFiles, files, targetDownloadPath, replaceMode) + if success then print("All files downloaded successfully.") return res else error(res) end + end -local function installShortcut(currentRepoPath, shortcutName) +local function installFiles(downloadedFiles, downloadTargetDir, installTargetDir) + local installedFiles = {} + + --- FIXME: ask user about replacing files. + local replace = true + makeDirIfNotExists(installTargetDir) + for idx, file in pairs(downloadedFiles) do + print(idx..": Installing File "..downloadTargetDir..file:sub(2,-1).." to target directory "..installTargetDir..file) + local fileExists = filesystem.exists(installTargetDir..file) + if fileExists and replace then filesystem.remove(installTargetDir..file) end + if replace or (replace == nil) then + --- TODO @Freddy: Coole Animation hinzufügen + os.execute("cp "..downloadTargetDir..file:sub(2,-1).." "..installTargetDir..file) + table.insert(installedFiles, installTargetDir..file) + else error("file not removed, but installation was cancelled - This might result in a broken install.") end + end + return installedFiles +end + +local function installShortcut(currentRepoPath, shortcutName, targetDir) print("installing shortcut...") - os.execute("mv "..currentRepoPath.."shortcut.lua /usr/bin/"..shortcutName..".lua") + makeDirIfNotExists(targetDir) + os.execute("mv "..currentRepoPath.."shortcut.lua "..targetDir..shortcutName..".lua") end --- todo: automatic read of dependency list (txt file containing lines with or somethink like it) --- removeme local function legacyInstallDependencies() + print("Legacy Mode: Installing hardcoded dependencies...") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/shapes_default.lua /lib/shapes_default.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/GUI.lua /lib/GUI.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/term_mod.lua /lib/term_mod.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/tech_demo.lua /home/GUI_tech_demo.lua") end +local function removeDownloads(downloadTargetDir, downloadedFiles) + for idx, file in pairs(downloadedFiles) do + file = file:sub(2,-1) --- get "file" from previous string "/file" + print(idx..": Removing temporary file "..downloadTargetDir..file) + local fileExists = filesystem.exists(downloadTargetDir..file) + if fileExists then filesystem.remove(downloadTargetDir..file) end + end + print("cleaned temporary files.") +end + +local function createManifest(installedFiles, installedShortcuts, manifestTarget, repoName) + makeDirIfNotExists(manifestTarget) + local manifest = "" + for idx, file in pairs(installedFiles) do + manifest = manifest..file.."\n" + end + for idy, shortcut in pairs(installedShortcuts) do + manifest = manifest..shortcut.."\n" + end + print("writing manifest to "..manifestTarget.."/"..repoName) + local file=io.open(manifestTarget.."/"..repoName,"w") + if file then -- might be nil under wierd circumstances + file:write(manifest) + file:close() + end +end + --- fix legacy shit -local function runFullInstallTask(repository) +local function runFullInstallTask(repository, shortcutName) --- first, download the actual repo. --- then, find dependencies - if then exist, download and install them --- then, install the actual program + local installTargetDir = "/usr/"..repository.Name + local shortcutTargetDir = "/usr/bin/" + local downloadTargetDir = DefaultTemporaryDownloadPath + local manifestTarget = "/etc/manifest/" + legacyInstallDependencies() --only for testing while new version not implemented yet print("downloading "..repository.RepoIdentifier) - downloadRepo(repository, repository.Remote, false) --enable auto-overwrite in other situations still todo - installShortcut(repository.CurrentLocalPath) + local downloadedFiles = downloadRepo(repository, repository.Remote, false) --enable auto-overwrite in other situations still todo + local installedFiles = installFiles(downloadedFiles, downloadTargetDir, installTargetDir) + local installedShortcuts = installShortcut(repository.CurrentLocalPath, shortcutName, shortcutTargetDir) + removeDownloads(downloadTargetDir, downloadedFiles) + createManifest(installedFiles, installedShortcuts, manifestTarget, repository.Name) end local function printHelpText() @@ -229,6 +299,9 @@ local function printHelpText() end local function run(cliArgs) + local repo = EmptyRepository + local shortcutName = nil + if #cliArgs<1 then print("No Arguments given. For help, please check -h or --help") return @@ -238,8 +311,13 @@ local function run(cliArgs) return elseif cliArgs[1] == "-d" then --- ask user about repo - Repository = DefaultRepository - if askYesOrNoQuestion("Use default config? (github::seesberger/PowerManager)?",YES,NO,true) == true then runFullInstallTask(DefaultRepository) end + if askYesOrNoQuestion("Use default config? (github::seesberger/PowerManager)?",YES,NO,true) == true then + repo = DefaultRepository + --- ask user about shortcutname + if askYesOrNoQuestion("Use default shortcut name \"powerman\"?",YES,NO,true) then shortcutName = "powerman" end + runFullInstallTask(repo, shortcutName) + --- TODO non-default config + else print("other things not implemented yet") end else print('"'..cliArgs[1]..'" - Bad argument. Try --help') print("Program exited.")