Advertisement
magik6000

Untitled

Nov 22nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 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. ---MAIN CODE
  157.  
  158. local argv = {...}
  159. if     argv[1] == "init"   then
  160.     print("Installing directories")
  161.     fs.makeDir("/etc/")
  162.     fs.makeDir("/etc/ccpt")
  163.     fs.makeDir("/bin/")
  164.     fs.makeDir("/usr/")
  165.     fs.makeDir("/usr/bin")
  166.     fs.makeDir("/usr/lib")
  167.     fs.makeDir("/usr/share")
  168.     print("Downloading default config")
  169.     download("/etc/ccpt/config","http://cc.nativehttp.org/fresh/config")
  170.     print("Updating package list")
  171.     update_list()
  172. elseif argv[1] == "update" then
  173.     print("Updating package list")
  174.     update_list()
  175. elseif argv[1] == "install" then
  176.     if argv[2] == nil then
  177.         print("Usage: ccpk install [name]")
  178.     else
  179.         print("Reading Database")
  180.         local entry = base_find(argv[2])
  181.         if entry then
  182.             print("Downloading package header")
  183.             local header = download(nil, entry.url..entry.name.."/index")
  184.             print("Checking for conflicts")
  185.             local vres = validate(entry.name,header)
  186.             if vres == validator.ok then
  187.                 print("Downloading files")
  188.             elseif vres == validator.installed then
  189.                 print("Package already installed")
  190.             else
  191.                 print("File conflict detected!")
  192.             end
  193.         else
  194.             print("Package not found!")
  195.         end
  196.     end
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement