urielsalis

uosatp

Dec 15th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. local tArgs = {...}
  2. local databaseURL = "http://pastebin.com/raw.php?i=eV8GcT3Q"
  3. local databaseSoruce = {}
  4. local appDatabase = {}
  5.  
  6. --Functions----------
  7. function split(str, pat)
  8.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  9.     local fpat = "(.-)" .. pat
  10.     local last_end = 1
  11.     local s, e, cap = str:find(fpat, 1)
  12.     while s do
  13.         if s ~= 1 or cap ~= "" then
  14.             table.insert(t,cap)
  15.         end
  16.         last_end = e+1
  17.         s, e, cap = str:find(fpat, last_end)
  18.     end
  19.     if last_end <= #str then
  20.         cap = str:sub(last_end)
  21.         table.insert(t, cap)
  22.     end
  23.     return t
  24. end
  25. local function getHttp(url, save)
  26.     local request = http.get(url)
  27.     if type(request) == "table" then
  28.         f = fs.open(save, "w")
  29.         f.write(request.readAll())
  30.         f.close()
  31.         return true
  32.     else
  33.         f = fs.open(}"programs/"..save, "w")
  34.         f.write(request.readAll())
  35.         f.close()
  36.         return true
  37.     else
  38.         return false
  39.     end
  40. end
  41.  
  42. local function printError(text)
  43.     if term.isColor() then
  44.         term.setTextColor(colors.red)
  45.         print(text)
  46.         term.setTextColor(colors.white)
  47.     end
  48. end
  49.  
  50. local function printSucsess(text)
  51.     if term.isColor() then
  52.         term.setTextColor(colors.lime)
  53.         print(text)
  54.         term.setTextColor(colors.white)
  55.     end
  56. end
  57.  
  58. local function databaseUpdate(silentMode)
  59.     f = fs.open("/.uosatp/source", "r")
  60.     databaseSoruce = textutils.unserialize(f.readAll())
  61.     f.close()
  62.  
  63.     local sucsessfulRequests = 0
  64.     local failedRequests = 0
  65.  
  66.     for i, v in ipairs(databaseSoruce) do
  67.         if not silentMode then
  68.             print("Updating " ..v)
  69.         end
  70.         local request = http.get(v)
  71.         local tempSource = nil
  72.         if type(request) == "table" then
  73.             local tempSource = textutils.unserialize(request.readAll())
  74.             for k,v in pairs(tempSource) do
  75.                 appDatabase[k] = v
  76.             end
  77.             sucsessfulRequests = sucsessfulRequests + 1
  78.         else
  79.             failedRequests = failedRequests + 1
  80.         end
  81.     end
  82.  
  83.     if not silentMode then
  84.         print(sucsessfulRequests .." sources updated, ".. failedRequests .." sources failed")
  85.     end
  86.  
  87.     f = fs.open("/.uosatp/database", "w")
  88.     f.write(textutils.serialize(appDatabase))
  89.     f.close()
  90.     return true
  91. end
  92.  
  93. local function install(appInstall)
  94.     isLocated = false
  95.     for k,v in pairs(appDatabase) do
  96.         if string.lower(k) == string.lower(appInstall) then
  97.             isLocated = true
  98.             write("Do you want to continue [Y/n]? ")
  99.             local downloadChoice = read()
  100.             if downloadChoice == "" or string.lower(downloadChoice) == "y" then
  101.                 print("Downloading . . .")
  102.                 if not getHttp(v,  "/.uosatp/cache/" ..k) then
  103.                     printError("Download failed!")
  104.                     break
  105.                 else
  106.                     f = fs.open("/.uosatp/cache/" ..k, "r")
  107.                     packageData = f.readAll()
  108.                     f.close()
  109.                     local state, error = pcall(loadfile("/.uosatp/cache/" ..k))
  110.                     if error ~= "" then
  111.                         error(error)
  112.                     end
  113.                     saveLocation = appInstall
  114.                     print("Initializing installer")
  115.                     print("Reading file")
  116.                     print("Inserting passwords . . .")
  117.                     print("Reading dependencies")
  118.                     print("Installing "..saveLocation.." . . .")
  119.                     fs.move("/.uosatp/cache/" ..k, saveLocation)
  120.                     file = fs.open(saveLocation, "r")
  121.                     depend = file.readLine()
  122.                     depend = textutils.unserialize(depend)
  123.                     names = file.readLine()
  124.                     names = textutils.unserialize(names)
  125.                     if type(depend) == "table" then
  126.                         if type(names) == "table" then
  127.                             for i=1,#depend do
  128.                                 print("         Downloading "..names[i])
  129.                                 getHttp(names[i], depend[i])
  130.                                 print("             Installing")
  131.                                 print("         End")
  132.                             end
  133.                         end
  134.                     end
  135.                     printSucsess("Installed!")
  136.                 end
  137.             end
  138.         else
  139.             printError("Abort.")
  140.         end
  141.     end
  142.  
  143.  
  144.     if not isLocated then
  145.         printError("Unable to locate package")
  146.     end
  147. end
  148.  
  149. local function appSearch(sApp)
  150.     local found = false
  151.     for k,v in pairs(appDatabase) do
  152.         if string.lower(sApp) == string.lower(k) then
  153.             found = true
  154.         end
  155.     end
  156.     return found
  157. end
  158.  
  159. local function fileSystemSetup()
  160.     if not fs.exists("/.uosatp/") then
  161.         fs.makeDir("/.uosatp/cache")
  162.         fWrite = {databaseURL}
  163.         f = fs.open("/.uosatp/source", "w")
  164.         f.write(textutils.serialize(fWrite))
  165.         f.close()
  166.         databaseUpdate(true)
  167.     end
  168. end
  169. --Main----------
  170. fileSystemSetup()
  171.  
  172. f = fs.open("/.uosatp/database", "r")
  173. appDatabase = textutils.unserialize(f.readAll())
  174. f.close()
  175.  
  176. if tArgs[1] == "update" then
  177.     databaseUpdate(false)
  178. elseif tArgs[1] == "install" and tArgs[2] then
  179.     if string.lower(tArgs[2]) == "apt-get" then
  180.         printSucsess("uosatpCeption!")
  181.     else
  182.         install(tArgs[2])
  183.     end
  184. elseif tArgs[1] == "add" and tArgs[2] then
  185.     f = fs.open("/.uosatp/source", "r")
  186.     databaseSource = textutils.unserialize(f.readAll())
  187.     f.close()
  188.     table.insert(databaseSource, tArgs[2])
  189.     f = fs.open("/.uosatp/source", "w")
  190.     f.write(textutils.serialize(databaseSource))
  191.     f.close()
  192.     printSucsess("Source added")
  193. elseif tArgs[1] == "search" and tArgs[2] then
  194.     if appSearch(tArgs[2]) then
  195.         printSucsess("Package found")
  196.     else
  197.         printError("Unable to locate package")
  198.     end
  199. elseif tArgs[1] == "purge" then
  200.     print("Do you want to continue [Y/n]? ")
  201.     local purgeChoice = read()
  202.     if string.lower(purgeChoice) == "y" or purgeChoice == "" then
  203.         fs.delete("/.uosatp")
  204.         fileSystemSetup()
  205.         printSucsess("Purge complete")
  206.     else
  207.         printError("Abort")
  208.     end
  209. elseif tArgs[1] == "help" or tArgs[1] == "--help" or tArgs[1] == nil then
  210.     print([[
  211.     Commands:
  212.         Update - Retrieve new database list
  213.         Install - Install new packages
  214.         Search - Check if package exists
  215.         Add - Add a software source
  216.         Purge - Purge all user sources
  217.         uosatp has super cow powers
  218.        
  219.     API functions
  220.         Start - Iniialize UOSATP so the other functions work
  221.         apiInstall - Install a package
  222.         apiAdd - Add a software Source
  223.         apiUpdate - Retrieve new database list
  224.         apiSearch - Check if package exist
  225.        
  226.         Returns true or false]])
  227. elseif tArgs[1] == "moo" then
  228.     print([[
  229.          (__)
  230.          (oo)
  231.    /------\/
  232.   / |    ||  
  233.  *  /\---/\
  234.     ~~   ~~  
  235. ...."Have you mooed today?"...]])
  236. else
  237.     printError("Invalid operation")
  238.     printSucsess("Usage: uosatp help")
  239. end
  240. --APIFUNCTIONS
  241. function Start()
  242.     databaseURL = "http://pastebin.com/raw.php?i=eV8GcT3Q"
  243.     databaseSoruce = {}
  244.     appDatabase = {}
  245.     fileSystemSetup()
  246.     f = fs.open("/.uosatp/database", "r")
  247.     appDatabase = textutils.unserialize(f.readAll())
  248.     f.close()
  249. end
  250. function apiInstall(appInstall)
  251.     isLocated = false
  252.     for k,v in pairs(appDatabase) do
  253.         if string.lower(k) == string.lower(appInstall) then
  254.             isLocated = true
  255.             local downloadChoice = "y"
  256.             if downloadChoice == "" or string.lower(downloadChoice) == "y" then
  257.                 if not getHttp(v,  "/.uosatp/cache/" ..k) then
  258.                     return false
  259.                     break
  260.                 else
  261.                     f = fs.open("/.uosatp/cache/" ..k, "r")
  262.                     packageData = f.readAll()
  263.                     f.close()
  264.                     local state, error = pcall(loadfile("/.uosatp/cache/" ..k))
  265.                     if error ~= "" then
  266.                         return false
  267.                     end
  268.                     saveLocation = appInstall
  269.                     fs.move("/.uosatp/cache/" ..k, saveLocation)
  270.                     file = fs.open(saveLocation, "r")
  271.                     depend = file.readLine()
  272.                     depend = textutils.unserialize(depend)
  273.                     names = file.readLine()
  274.                     names = textutils.unserialize(names)
  275.                     if type(depend) == "table" then
  276.                         if type(names) == "table" then
  277.                             for i=1,#depend do
  278.                                 getHttp(names[i], depend[i])
  279.                             end
  280.                         end
  281.                     end
  282.                     return true
  283.                 end
  284.             end
  285.         else
  286.             return false
  287.         end
  288.     end
  289.  
  290.  
  291.     if not isLocated then
  292.         return false
  293.     end
  294. end
  295. function apiAdd()
  296.     f = fs.open("/.uosatp/source", "r")
  297.     databaseSource = textutils.unserialize(f.readAll())
  298.     f.close()
  299.     table.insert(databaseSource, tArgs[2])
  300.     f = fs.open("/.uosatp/source", "w")
  301.     f.write(textutils.serialize(databaseSource))
  302.     f.close()
  303.     return true
  304. end
  305. function apiUpdate()
  306.     databaseUpdate(true)
  307. end
  308. function apiSearch()
  309.     if appSearch(tArgs[2]) then
  310.         return true
  311.     else
  312.         return false
  313.     end
  314. end
Advertisement
Add Comment
Please, Sign In to add comment