Advertisement
Guest User

apt-get

a guest
Feb 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. local server="https://www-user.tu-chemnitz.de/"
  2. local spath ="~jorot/cclib/"
  3. local headerpath="package/apt-get/header/"
  4. local libpath="package/"
  5. local arg = {...}
  6. os.loadAPI("package/log/log")
  7. if (log == nil) then
  8.     log = {}
  9.     log.i = print
  10.     log.e = print
  11.     log.w = print
  12. end
  13. function wget(name,path)
  14.   local url = server..spath..name
  15.   log.i("Downloading "..url.." ...")
  16.   if(not http.checkURL(server)) then
  17.     log.i("404 Server not reachable:"..server)
  18.     return -1
  19.   end
  20.   h = http.get(url)
  21.   if(h==nil) then
  22.     log.i("404 couldn't open connection")
  23.     return -1
  24.   end
  25.   local s = h.readAll()
  26.   f =fs.open(path,"w")
  27.   f.write(s)
  28.   f.close()
  29.   h.close()
  30.   log.i("saved as "..path)
  31. end
  32. local function install()
  33.   if #arg<2 then
  34.     log.i("Usage: apt-get install <package>")
  35.     return
  36.   end
  37.   local ln = arg[2]
  38.   if(fs.exists(headerpath..ln)) then
  39.     runheader(headerpath..ln)
  40.   else
  41.     log.i("Packageheaders not found! Try apt-get update")
  42.   end
  43. end
  44. local function update() --downloads header files
  45.     log.i("Update headerfiles ...")    
  46.     wget("package-list",".apt-temp")
  47.     local file = fs.open(".apt-temp","r")
  48.     local line = file.readLine()
  49.     while(line) do
  50.         log.i("Downloading header for "..line)
  51.         wget(line.."/meta.phf",headerpath..line)                
  52.         line=file.readLine()    
  53.     end
  54.     file.close()    
  55.     fs.delete(".apt-temp")
  56.     log.i("apt-get: compleate")
  57. end
  58. local function runheader(ln)
  59.     local file = fs.open(ln,"r")
  60.     local pn
  61.     local line=file.readLine()
  62.     while line~=nil do
  63.         if(str_starts(line,"PACKAGE")) then
  64.             if pn~=nil then
  65.                 log.i("ERROR multible packages")
  66.                 return -1            
  67.             end            
  68.             pn=string.sub(line,9)
  69.             log.i("package:"..string.sub(line,9))
  70.             fs.makeDir(libpath..pn)            
  71.         elseif(str_starts(line,"VERSION")) then log.i("version:"..string.sub(line,9,9).."."..string.sub(line,10,11).."."..string.sub(line,12))
  72.         elseif(str_starts(line,"AUTHOR")) then log.i("author :"..string.sub(line,8))
  73.         elseif false then
  74.         elseif str_starts(line,"GET")  then
  75.             local p = string.find(line," AS ")
  76.             local sp = string.sub(line,5,p)
  77.             local lp = string.sub(line,p+4)
  78.             wget(pn.."/"..sp,libpath..pn.."/"..lp)
  79.         elseif str_starts(line, "RUN") then
  80.             log.i("running command")
  81.             local sp = string.sub(line,5)
  82.             shell.run(sp)
  83.         elseif str_starts(line, "REQUIRE") then
  84.             local sp = string.sub(line,9)
  85.             log.i(string.sub(line,9))
  86.             if str_starts(sp,"HARDWARE") then
  87.                 log.i("RHW")
  88.                 if(peripheral.find(string.sub(sp,10))==nil) then
  89.                     log.w("required Hardware (" .. string.sub(sp,10) .. ") not found")
  90.                     if(choice("ignore and continue")) then break else return end
  91.                 else
  92.                     log.i("required Hardware (" .. string.sub(sp,10) .. ") found")
  93.                 end
  94.             end
  95.         elseif str_starts(line,"STARTUP") then
  96.             startup_add(string.sub(line,9))
  97.         elseif str_starts(line,"--") then
  98.         else log.i("error:"..line)        
  99.         end
  100.         line=file.readLine()    
  101.     end
  102.     file.close()
  103. end
  104. local function str_starts(str,pre)
  105.     return string.sub(str,0,string.len(pre))==pre
  106. end
  107. local function startup_add(com)
  108.     log.i("register startup:" .. com)
  109.     sf = fs.open("startup","a")
  110.     sf.write("shell.run(\""..com.."\") --apt-get")
  111.     sf.close()
  112. end
  113. local function choice(str)
  114.     write(str .." [Y/N]? ")
  115.     c = read()
  116.     if(c=="Y" or c == "y") then return true end
  117.     return false
  118. end
  119.  
  120. if(arg[1]=="install") then install()
  121. elseif(arg[1]=="update") then update()
  122. elseif(arg[1]=="upgrade") then upgrade()
  123. else print("Usage: apt-get update | install <name>")
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement