neuroticfox

dc_installer repost

Jul 9th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. local component = require("component");
  2. local fs = require("filesystem");
  3. local serialization = require("serialization");
  4. local term = require("term");
  5. local internet = nil;
  6.  
  7. --[[
  8.  
  9. --]]
  10. local packages = {"oop", "libGUI", "draconic_control", "dc_gui"};
  11. local repositoryURL = "http://xypm.tenyx.de/";
  12.  
  13. local gpu = component.gpu;
  14.  
  15. function fetchPackage(pack)
  16.     local url = repositoryURL .. pack .. ".xypm";
  17.    
  18.     local request = internet.request(url);
  19.     if request == nil then
  20.         return nil;
  21.     end
  22.    
  23.     local data = "";
  24.    
  25.     for chunk in request do
  26.         data = data .. chunk;
  27.     end
  28.    
  29.     return serialization.unserialize(data);
  30. end
  31.  
  32. local function verifyPackage(package)
  33.     if not component.isAvailable("data") then
  34.         -- If we don't have a data card we can't verify anything.
  35.         -- However in order to make data cards optional we just return true
  36.         return true;
  37.     end
  38.    
  39.     for fileName, fileChecksum in pairs(package.checksums or {}) do
  40.         if package.files[fileName] and component.data.sha256(package.files[fileName]) ~= fileChecksum then
  41.             return false;
  42.         end
  43.     end
  44.  
  45.     return true;
  46. end
  47.  
  48. function installPackage(package_data)
  49.     for fileName, fileContents in pairs(package_data.files) do
  50.         local filePath = fs.path(fileName);
  51.         if fs.exists(fileName) then
  52.             fs.remove(fileName);
  53.         end
  54.        
  55.         if filePath ~= nil then
  56.             local pathSegments = fs.segments(filePath);
  57.             local pathChecked = "/";
  58.             for _, segment in pairs(pathSegments) do
  59.                 pathChecked = fs.concat(pathChecked, segment);
  60.                 if not fs.exists(pathChecked) then
  61.                     fs.makeDirectory(pathChecked);
  62.                 end
  63.             end
  64.         end
  65.        
  66.         local file = io.open(fileName, "wb");
  67.         if file == nil then
  68.             return false;
  69.         end
  70.         file:write(fileContents);
  71.         file:close();
  72.     end
  73.    
  74.     for linkName, linkTarget in pairs(package_data.links) do
  75.         local filePath = fs.path(fileName);
  76.         if fs.exists(fileName) then
  77.             fs.remove(fileName);
  78.         end
  79.        
  80.         if filePath ~= nil then
  81.             local pathSegments = fs.segments(filePath);
  82.             local pathChecked = "/";
  83.             for _, segment in pairs(pathSegments) do
  84.                 pathChecked = fs.concat(pathChecked, segment);
  85.                 if not fs.exists(pathChecked) then
  86.                     fs.makeDirectory(pathChecked);
  87.                 end
  88.             end
  89.         end
  90.        
  91.         if not fs.link(linkTarget, linkname) then
  92.             return false;
  93.         end
  94.     end
  95.    
  96.     return true;
  97. end
  98.  
  99. print("XyFreaks' Package Deployment Tool");
  100. print("The following packages are scheduled for deployment:");
  101. local packageNameMaxLen = 0;
  102.  
  103. if component.isAvailable("internet") then
  104.     internet = component.internet;
  105. else
  106.     print("You need an Internet Card for this to work.");
  107.     return 1;
  108. end
  109.  
  110. internet = require("internet");
  111.  
  112. for _, p in pairs(packages) do
  113.     print(" - " .. p);
  114.     packageNameMaxLen = math.max(packageNameMaxLen, string.len(p));
  115. end
  116. print("");
  117.  
  118. local termWidth = gpu.getResolution();
  119. local _, termY = term.getCursor();
  120. local barWidthMax = termWidth - packageNameMaxLen - 11;
  121.  
  122. for i, p in pairs(packages) do
  123.     local percent = (2 * (i-1) / #packages / 2);
  124.     local barRep = math.floor(barWidthMax * percent + 0.5);
  125.     term.setCursor(1, termY);
  126.     local pName = string.sub(p .. string.rep(" ", packageNameMaxLen), 1, packageNameMaxLen);
  127.    
  128.     term.write(pName .. " |" .. string.rep("=", barRep) .. ">" .. string.rep(" ", barWidthMax - barRep) .. "|" .. string.format("%6.2f%%", percent * 100), false); 
  129.     local packageData = fetchPackage(p);
  130.     if packageData == nil then
  131.         print("");
  132.         print("Failed to download " .. p);
  133.         return;
  134.     end
  135.    
  136.     term.setCursor(1, termY);
  137.     percent = ((2 * (i-1) + 1) / #packages / 2);
  138.     barRep = math.floor(barWidthMax * percent + 0.5);
  139.     term.write(pName .. " |" .. string.rep("=", barRep) .. ">" .. string.rep(" ", barWidthMax - barRep) .. "|" .. string.format("%6.2f%%", percent * 100), false);
  140.     if not verifyPackage(packageData) then
  141.         print("");
  142.         print("Integrity check for " .. p .. " failed.");
  143.     end
  144.    
  145.     if not installPackage(packageData) then
  146.         print("");
  147.         print("Failed to install " .. p);
  148.     end
  149. end
  150. term.setCursor(1, termY);
  151. local pName = string.sub("Done" .. string.rep(" ", packageNameMaxLen), 1, packageNameMaxLen);
  152. term.write(pName .. " |" .. string.rep("=", barWidthMax) .. ">" .. "|" .. "100.00%", false);
Add Comment
Please, Sign In to add comment