Advertisement
magik6000

Untitled

Nov 22nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. --CCPT - ComputerCraft Packaging Tool
  2.  
  3. if not http then
  4. error("HTTP API MUST be enabled to use this program")
  5. end
  6.  
  7. --Pseudo enums
  8.  
  9. local validator = {ok = 0, installed = 1, file_conflict = 2}
  10.  
  11. ---UTILITIES
  12.  
  13. local function CGetS(file,name)
  14.     local _cfg = fs.open(file,"r")
  15.    
  16.     if not _cfg then
  17.         error("Could not open configuration file: "..file)
  18.     end
  19.    
  20.     local x = true;
  21.      
  22.     while x do
  23.         local line = _cfg.readLine()
  24.         if line == nil then
  25.             x = false;
  26.         else
  27.          
  28.             local side = false
  29.             local prop = ""
  30.             local val = ""
  31.             for a=1,#line do
  32.                 if line:sub(a,a) == '=' then
  33.                     side = true
  34.                 elseif side then
  35.                     val =  val .. line:sub(a,a)
  36.                 else
  37.                     prop = prop .. line:sub(a,a)
  38.                 end
  39.             end
  40.            
  41.             if prop == name then
  42.                 _cfg.close()
  43.                 return val
  44.             end        
  45.         end  
  46.     end
  47.     _cfg.close()   
  48. end
  49.  
  50. local function CGetN(file,name)
  51.     return tonumber(CGetS(file,name))
  52. end
  53.  
  54. local function download(file, url)
  55.     local res = http.get(url)
  56.     if res then
  57.         if file ~= nil then
  58.             local fhnd = fs.open(file, "w");
  59.             if fhnd then
  60.                 fhnd.write(res.readAll())
  61.                 fhnd.close()
  62.                 return res.readAll()
  63.             else
  64.                 res.close()
  65.                 error("Could not open "..file.." for writing")
  66.             end
  67.         else
  68.             return res.readAll()
  69.         end
  70.     else
  71.         error("Download failed for: "..url)
  72.     end
  73.     res.close()
  74. end
  75.  
  76. function split(text,splitter)
  77.     local rt = {}
  78.     local act = ""
  79.     for x=1,#text do
  80.         if text:sub(x,x+#splitter-1) == splitter then
  81.             rt[#rt+1]=act
  82.             act=""
  83.         else
  84.             act = act .. text:sub(x,x)
  85.         end
  86.     end
  87.     if act ~= "" then
  88.         rt[#rt+1] = act
  89.     end
  90.     return rt;
  91. end
  92.  
  93. ---Intarnal functions
  94.  
  95. local function update_list()
  96.     local sync = CGetS("/etc/ccpt/config","master")
  97.     if sync then
  98.         download("/etc/ccpt/list",sync)
  99.     else
  100.         error("Update failed: master server not set!")
  101.     end
  102. end
  103.  
  104. local function base_find(name)
  105.     local base = fs.open("/etc/ccpt/list","r")
  106.    
  107.     if not base then
  108.         error("Could not open base file: /etc/ccpt/list")
  109.     end
  110.    
  111.     local x = true;
  112.     while x do
  113.         local line = base.readLine()
  114.         if line == nil then
  115.             x = false;
  116.         else
  117.             local entry = split(line,";")
  118.             if entry[1] == "p" then
  119.                 if entry[2] == name then
  120.                     local ret = {name=entry[2],url=entry[4],version=tonumber(entry[3])}
  121.                     return ret
  122.                 end
  123.             end
  124.         end
  125.     end
  126. end
  127.  
  128. local function validate(pname,header)
  129.     local instbase = fs.open("/etc/ccpt/installed","r")
  130.     if instbase then
  131.         local x = true
  132.         while x do
  133.             local tline = instbase.readLine()
  134.             if tline == nil then
  135.                 x = false
  136.             else
  137.                 if pname == split(tline,";")[1] then
  138.                     return validator.installed
  139.                 end
  140.             end
  141.         end
  142.     end
  143.     --local filebase = fs.open("/etc/ccpt/files","r")
  144.     lhead = split(header,"\n")
  145.     local x = 1
  146.     for x = 1, #lhead do
  147.         if split(lhead[x],";")[1] == "f" then
  148.             if fs.exists(split(lhead[x],";")[2]) then
  149.                 return validator.file_conflict
  150.             end
  151.         end
  152.     end
  153.    
  154.     return validator.ok
  155. end
  156.  
  157. local function download_files(url,header)
  158. lhead = split(header,"\n")
  159.     local x = 1
  160.     for x = 1, #lhead do
  161.         if split(lhead[x],";")[1] == "f" then
  162.             download(split(lhead[x],";")[2],url..split(lhead[x],";")[2])
  163.         end
  164.     end
  165. end
  166. ---MAIN CODE
  167.  
  168. local argv = {...}
  169. if     argv[1] == "init"   then
  170.     print("Installing directories")
  171.     fs.makeDir("/etc/")
  172.     fs.makeDir("/etc/ccpt")
  173.     fs.makeDir("/bin/")
  174.     fs.makeDir("/usr/")
  175.     fs.makeDir("/usr/bin")
  176.     fs.makeDir("/usr/lib")
  177.     fs.makeDir("/usr/share")
  178.     print("Downloading default config")
  179.     download("/etc/ccpt/config","http://cc.nativehttp.org/fresh/config")
  180.     print("Updating package list")
  181.     update_list()
  182. elseif argv[1] == "update" then
  183.     print("Updating package list")
  184.     update_list()
  185. elseif argv[1] == "install" then
  186.     if argv[2] == nil then
  187.         print("Usage: ccpk install [name]")
  188.     else
  189.         print("Reading Database")
  190.         local entry = base_find(argv[2])
  191.         if entry then
  192.             print("Downloading package header")
  193.             local header = download(nil, entry.url..entry.name.."/index")
  194.             print("Checking for conflicts")
  195.             local vres = validate(entry.name,header)
  196.             if vres == validator.ok then
  197.                 print("Downloading files")
  198.                 download_files(split(header,";")[4]..split(header,";")[2],header)
  199.             elseif vres == validator.installed then
  200.                 print("Package already installed")
  201.             else
  202.                 print("File conflict detected!")
  203.             end
  204.         else
  205.             print("Package not found!")
  206.         end
  207.     end
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement