Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LeadLined-OS Install Utility v0.2 (as of last update)
- -- Development just keeps dragging on, doesn't it?
- local args = {...}
- local dirs = { -- Directories to make
- "home",
- "sys",
- "bin",
- "sys/apis",
- "sys/svcs",
- "sys/users",
- "sys/drivers",
- "home/root",
- "old_files",
- }
- local ignoreList = {
- ".gitattributes",
- ".gitignore",
- "README.md",
- "install",
- "unused",
- }
- local install_temp_dir = "LEADLINED_INSTALL_TEMP_DIR"
- function github_getRequestsLeft()
- local h = http.get("https://api.github.com/rate_limit")
- if h then
- local data = h.readAll()
- h.close()
- local s = string.match(data, "\"remaining\":(%d+)")
- if s then
- return tonumber(s) or 0
- end
- else
- return 0
- end
- end
- function github_getRequestLimit()
- local h = http.get("https://api.github.com/rate_limit")
- if h then
- local data = h.readAll()
- h.close()
- local s = string.match(data, "\"limit\":(%d+)")
- if s then
- return tonumber(s) or 0
- end
- else
- return 0
- end
- end
- function parse_contentsRequest(response)
- local tables = {}
- local id = 0
- for ident, data in string.gmatch(response, "\"(%a+)\":\"([^%\"]+)\",") do
- --local line = string.match(lines[i], "%s+(%.+)")
- --if line ~= "{" and line ~= "}" and line ~= "}," then
- -- local ident, data = string.match(line, "\"(%a+)\": \"([^%\"]+)\"")
- --print("["..ident.."] = "..data)
- if ident == "name" then
- id = id + 1
- tables[id] = {}
- tables[id]["name"] = data
- tables[id]["links"] = {}
- elseif ident == "self" or ident == "git" or ident == "html" then
- tables[id]["links"][ident] = data
- elseif ident ~= "_links" then
- tables[id][ident] = data
- end
- end
- return tables
- end
- function github_getContents(user, repo, path, verbose, fileNameColor, normalTextColor)
- if verbose then
- fileNameColor = fileNameColor or colors.orange
- normalTextColor = normalTextColor or colors.white
- write("Listing: ")
- term.setTextColor(fileNameColor)
- write(path)
- term.setTextColor(normalTextColor)
- write("...")
- end
- path = path or ""
- local url = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..path
- --print("HTTP GET: "..url)
- if github_getRequestsLeft() == 0 then
- return false, {}
- end
- local handle = http.get(url)
- if handle then
- local data = handle.readAll()
- --print("Retrieved "..#data.." bytes.")
- handle.close()
- --print(data)
- if verbose then
- print("Success.")
- end
- return true, parse_contentsRequest(data)
- else
- if verbose then
- print("Failed.")
- end
- return false, {}
- end
- end
- function github_getFile(user, repo, file, branch, verbose, fileNameColor, normalTextColor)
- if verbose then
- fileNameColor = fileNameColor or colors.orange
- normalTextColor = normalTextColor or colors.white
- write("Retrieving: ")
- term.setTextColor(fileNameColor)
- write(file)
- term.setTextColor(normalTextColor)
- write("...")
- end
- local url = "https://raw.github.com/"..user.."/"..repo.."/"..branch.."/"..file
- --print("HTTP GET: "..url)
- local handle = http.get(url)
- if handle then
- local data = handle.readAll()
- handle.close()
- if verbose then
- print("Success.")
- print("Retrieved "..#data.." bytes.")
- end
- return true, data
- else
- if verbose then
- print("Failed.")
- end
- return false
- end
- end
- function github_getAllFilesRecursive(user, repo, dir, branch, verbose, fileNameColor, normalTextColor)
- dir = dir or ""
- local files = {}
- local status, contents = github_getContents(user, repo, dir, verbose, fileNameColor, normalTextColor)
- if not status then
- return false, {}, 1, 0, 1, 0
- end
- local totalFiles = 0
- local totalDirs = 0
- local failedFiles = 0
- local failedDirs = 0
- for i,v in ipairs(contents) do
- local skip = false
- for x=1, #ignoreList do
- if ignoreList[x] == v["path"] then
- skip = true
- break
- end
- end
- if not skip then
- if v["type"] == "file" then
- totalFiles = totalFiles+1
- local status, data = github_getFile(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
- if status then
- files[v["path"]] = data
- else
- failedFiles = failedFiles+1
- end
- elseif v["type"] == "dir" then
- totalDirs = totalDirs+1
- local status, files2, fDirs, fFiles, tDirs, tFiles = github_getAllFilesRecursive(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
- if status then
- for i,v in pairs(files2) do
- files[i] = v
- end
- failedDirs = failedDirs+fDirs
- failedFiles = failedFiles+fFiles
- totalFiles = totalFiles + tFiles
- totalDirs = totalDirs + tDirs
- else
- failedDirs = failedDirs+1
- end
- end
- end
- end
- return true, files, failedDirs, failedFiles, totalDirs, totalFiles
- end
- term.clear()
- term.setCursorPos(1,1)
- local maxX, maxY = term.getSize()
- local inColor = term.isColor()
- print("LeadLined-OS Install Utility")
- print(string.rep("=", maxX))
- print("Welcome to the LeadLined-OS Install Program.")
- if args[1] ~= "--auth-files-only" then
- write("You have ")
- if inColor then term.setTextColor(colors.yellow) end
- write(github_getRequestsLeft())
- term.setTextColor(colors.white)
- write(" of ")
- if inColor then term.setTextColor(colors.yellow) end
- write(github_getRequestLimit())
- term.setTextColor(colors.white)
- print(" requests left.")
- if github_getRequestsLeft() == 0 then
- if inColor then term.setTextColor(colors.red) end
- write("ERROR: ")
- term.setTextColor(colors.white)
- print("We can't make anymore requests to Github!")
- print("Try installing again in an hour.")
- return
- end
- if github_getRequestsLeft() < 5 then
- if inColor then term.setTextColor(colors.orange) end
- write("WARNING: ")
- term.setTextColor(colors.white)
- print("We might not be able to download the entire OS!")
- local x,y = term.getCursorPos()
- while true do
- term.setCursorPos(x, y)
- term.clearLine()
- write("Install anyways? Y/N> ")
- term.setTextColor(colors.yellow)
- local a = string.lower(string.sub(read(), 1, 1))
- term.setTextColor(colors.white)
- if a == "y" then
- break
- elseif a == "n" then
- print("Try installing again in an hour.")
- return
- end
- end
- else
- print("Press ENTER to continue, and any other key to exit.")
- local event, key = os.pullEvent("key")
- if key ~= keys.enter then
- return
- end
- end
- print("Downloading files...")
- local status, files, fDirs, fFiles, tDirs, tFiles = github_getAllFilesRecursive("Tatantyler", "LeadLined-OS", "", "master", true, colors.yellow, colors.white)
- if status and ((fDirs == 0) and (fFiles == 0)) then
- print("Download completed successfully.")
- write("Downloaded ")
- if inColor then term.setTextColor(colors.yellow) end
- write(tFiles)
- term.setTextColor(colors.white)
- write(" files and ")
- if inColor then term.setTextColor(colors.yellow) end
- write(tDirs)
- term.setTextColor(colors.white)
- print(" directories.")
- if inColor then term.setTextColor(colors.lime) end
- print("No failures reported.")
- term.setTextColor(colors.white)
- elseif status and (not((fDirs == 0) and (fFiles == 0))) then
- print("Download was partially successful.")
- write("Downloaded ")
- if inColor then term.setTextColor(colors.yellow) end
- write(tFiles)
- term.setTextColor(colors.white)
- write(" files and ")
- if inColor then term.setTextColor(colors.yellow) end
- write(tDirs)
- term.setTextColor(colors.white)
- print(" directories.")
- write("Failed: ")
- if inColor then term.setTextColor(colors.red) end
- write(fFiles)
- term.setTextColor(colors.white)
- write(" files and ")
- if inColor then term.setTextColor(colors.red) end
- write(fDirs)
- term.setTextColor(colors.white)
- print(" directories.")
- elseif not status then
- if inColor then term.setTextColor(colors.red) end
- print("Download failed; could not get base directory listing.")
- term.setTextColor(colors.white)
- return
- end
- print("Moving old files to temp dir...")
- if fs.exists(install_temp_dir) then
- fs.delete(install_temp_dir)
- end
- fs.makeDir(install_temp_dir)
- local userFiles = fs.list("")
- for i=1, #userFiles do
- if (not fs.isReadOnly(userFiles[i])) and userFiles[i] ~= install_temp_dir then
- fs.move(userFiles[i], fs.combine(install_temp_dir, userFiles[i])) -- We'll be moving these files to the new user's home directory soon
- print("Moved: "..userFiles[i])
- end
- end
- print("Creating directory structure...")
- for i=1, #dirs do
- if fs.exists(dirs[i]) then
- fs.delete(dirs[i])
- end
- fs.makeDir(dirs[i])
- end
- print("Writing OS files to disk...")
- for path, data in pairs(files) do
- write("Writing: "..path)
- local base = string.sub(path, 1, #path-#fs.getName(path))
- if not fs.exists(base) then
- fs.makeDir(base)
- end
- local handle = fs.open(path, "w")
- handle.write(data)
- handle.close()
- print(" ("..#data.." bytes)")
- end
- else
- for i=1, #dirs do
- if not fs.exists(dirs[i]) then
- fs.makeDir(dirs[i])
- end
- end
- print("Authentication File Only mode enabled.")
- os.pullEvent("key")
- end
- if args[1] ~= "--auth-files-only" then
- local tempFiles = fs.list(install_temp_dir)
- for i=1, #tempFiles do
- fs.move(fs.combine(install_temp_dir, tempFiles[i]), fs.combine("old_files", tempFiles[i]))
- end
- fs.delete(install_temp_dir)
- end
- print("Installation complete!")
- print("Your computer will now restart.")
- os.sleep(1.5)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement