From 8979c4c1ee28a449af8f22c14648c3106410ac1d Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 21 Nov 2018 11:55:45 +0800 Subject: [PATCH] Grab v2.0-alpha --- grab.lua | 238 ++++++++++++++++++++++++++++++++++++++++++++------ programs.info | 15 ++-- 2 files changed, 214 insertions(+), 39 deletions(-) diff --git a/grab.lua b/grab.lua index 341e2c7..08a224a 100644 --- a/grab.lua +++ b/grab.lua @@ -2,22 +2,28 @@ -- Created By Kiritow local component=require('component') local shell=require('shell') +local filesystem=require('filesystem') +local serialization=require('serialization') local args,options=shell.parse(...) -if( (#args<1 and (not options["setup"])) or options["help"]) then +local nOptions=0 +for k,v in pairs(options) do nOptions=nOptions+1 end +if( (#args<1 and nOptions<1) or options["help"]) then print("Grab : OpenComputerScripts Installer") - print("Usage:\n\tgrab [options] [files]") + print("Usage:\n\tgrab [] []") print("Options:" .. "\n\t--cn Use mirror site in China. By default grab will download from Github." .. "\n\t--help Display this help page." - .. "\n\t--setup Download some basic files for development." .. "\n\t--version Display version and exit." .. "\n\t--proxy= Given a proxy file which will be loaded and returns a proxy function like: " .. "function(RepoName: string, Branch: string ,FileAddress: string): string" ) + print("Command:" + .. "\n\tinstall, list, update" + ) return end if(options["version"]) then - print("Grab v1.1") + print("Grab v2.0-alpha") return end local function download(url) @@ -68,29 +74,203 @@ else return end end -local files -if(options["setup"]) then - files={"LICENSE","checkarg.lua","libevent.lua","class.lua","util.lua"} -else - files=args -end -print('Downloading...') -for idx,filename in ipairs(files) do - io.write("[" .. idx .. "/" .. #files .. "] " .. filename) - local ok,err=pcall(function() - local flag,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master",filename)) - if(not flag) then - print(" [Download Failed] " .. result) - elseif(code~=200) then - print(" [Download Failed] response code " .. code .. " is not 200.") - else - local f=io.open(filename,"w") - f:write(result) - f:close() - print(" [OK]") - end - end) - if(not ok) then - print(" [Error] " .. err) + +local db_dirs={"/etc/grab",".grab","/tmp/.grab"} +local db_positions={"/etc/grab/programs.info",".grab/programs.info","/tmp/.grab/programs.info"} + +local function CheckAndLoad(raw_content) + local fn,err=load(raw_content) + if(fn) then + local ok,result=pcall(fn) + if(ok) then return result + else return nil,result end end -end \ No newline at end of file + return nil,err +end + +local function ReadDB() + for idx,filename in ipairs(db_positions) do + local f=io.open(filename,"r") + if(f) then + local result=serialization.unserialize(f:read("*a")) + f:close() + return result,filename + end + end + return nil +end + +local function WriteDB(filename,tb,is_raw) -- By default, is_raw=false + local f=io.open(filename,"w") + if(f) then + if(not is_raw) then f:write(serialization.serialize(tb)) + else f:write(tb) end + f:close() + return true + end + return false +end + +local function CreateDB(tb,is_raw) + for idx,dirname in ipairs(db_dirs) do + filesystem.makeDirectory(dirname) -- buggy + end + for idx,filename in ipairs(db_positions) do + if(WriteDB(filename,tb,is_raw)) then + return filename + end + end + return nil +end + +if(args[1]=="update") then + print("Updating programs info....") + io.write("Downloading... ") + local ok,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master","programs.info")) + if(not ok) then + print("[Failed] " .. result) + elseif(code~=200) then + print("[Failed] response code " .. code .. " is not 200.") + else + print("[OK]") + io.write("Validating... ") + local tb_data,validate_err=CheckAndLoad("return " .. result) + result=nil -- release memory + if(tb_data) then + print("[OK]") + io.write("Saving files... ") + local dbfilename=CreateDB(tb_data) + if(dbfilename) then + print("[OK]") + print("Programs info updated and saved to " .. dbfilename) + else + print("[Failed] Unable to save programs info") + end + else + print("[Failed]" .. validate_err) + end + end + + return +end + +local db,dbfilename=ReadDB() + +if(args[1]=="install") then + if(#args<2) then + print("Nothing to install.") + return + else + print("Checking programs info...") + end + + local to_install={} + for i=2,#args,1 do + to_install[args[i]]=true + end + + local newly_added=0 + while true do + local to_add={} + for this_lib in pairs(to_install) do + if(not db[this_lib]) then + print("Library " .. this_lib .. " not found.") + return + else + if(db[this_lib].requires) then + for idx,this_req in ipairs(db[this_lib].requires) do + if(not to_install[this_req] and not to_add[this_req]) then + newly_added=newly_added+1 + to_add[this_req]=true + end + end + end + end + end + for this_lib in pairs(to_add) do + to_install[this_lib]=true + end + if(newly_added==0) then break + else + newly_added=0 + end + end + + print("About to install the following libraries...") + local count_libs=0 + local count_files=0 + for this_lib in pairs(to_install) do + io.write(this_lib .. " ") + count_libs=count_libs+1 + for k in ipairs(db[this_lib].files) do + count_files=count_files+1 + end + end + print("\n" .. count_libs .. " libraries will be installed. " .. count_files .. " will be downloaded.") + + print("Downloading...") + local id_installing=1 + for this_lib in pairs(to_install) do + for k,v in ipairs(db[this_lib].files) do + local toDownload + if(type(k)=="number" and type(v)=="string") then + toDownload=v + elseif(type(k)=="string") then + toDownload=k + else + print("Invalid programs info: key type: " .. type(k) .. ". value type: " .. type(v)) + return + end + + io.write("[" .. id_installing .. "/" .. count_files .. "] Downloading " .. toDownload .. " for " .. this_lib .. "... ") + local ok,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master",filename)) + if(not ok) then + print("[Download Failed] " .. result) + elseif(code~=200) then + print("[Download Failed] response code " .. code .. " is not 200.") + else + if(type(v)=="string") then + local f=io.open(v,"w") + if(f==nil) then + print("[Error] Unable to write to file " .. v) + return + else + f:write(result) + f:close() + print("[OK]") + end + elseif(type(v)=="table") then + local success=false + for idx,value in ipairs(v) do + local f=io.open(value,"w") + if(f) then + success=true + f:write(result) + f:close() + print("[OK]") + end + end + if(not success) then + print("[Error] Unable to write file: " .. toDownload) + return + end + end + end + end + end + + print("Installed " .. count_libs .. " libraies with " .. count_files .. " files.") + return +end + +if(args[1]=="list") then + print("Listing projects...") + for idx,tb in pairs(db) do + print(tb.name) + end + + return +end + +-- reach here? +print("Invalid argument 1: " .. args[1]) \ No newline at end of file diff --git a/programs.info b/programs.info index cf88a4c..2393bc3 100644 --- a/programs.info +++ b/programs.info @@ -1,22 +1,19 @@ { - { - name="grab", + ["grab"]={ title="Grab", info="Grab - Official OpenComputerScripts Installer", files={ "grab.lua" } }, - { - name="checkarg", + ["checkarg"]={ title="Check Arugment", info="For easier argument checking", files={ "checkarg.lua" } }, - { - name="libevent", + ["libevent"]={ title="LibEvent", info="For easier event processing", files={ @@ -26,8 +23,7 @@ "checkarg" } }, - { - name="drone", + ["drone"]={ title="Drone", info="Drone console and bios", author="Kiritow", @@ -40,8 +36,7 @@ "libevent" } }, - { - name="smartstorage", + ["smartstorage"]={ title="Smart Storage", info="Smart Storage", author="Kiritow",