Advertisement
mypal125

CWCOS Installer - /usr/bin/pkg

May 24th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. local tArgs = { ... }
  2. theapi = {}
  3. function theapi.createDirectory(_dir)
  4.   fs.makeDir(_dir)
  5. end
  6. function theapi.deleteFile(_file)
  7.   fs.delete(_file)
  8. end
  9. function theapi.download(_url, _to)
  10.   if not fs.exists(_to) then
  11.     fs.makeDir(_to)
  12.     fs.delete(_to)
  13.   end
  14.   local httphandle = http.get(_url)
  15.   local httptext = httphandle.readAll()
  16.   httphandle.close()
  17.   local filehandle = fs.open(_to, "w")
  18.   filehandle.write(httptext)
  19.   filehandle.close()
  20. end
  21.  
  22. toinstaller = {
  23.   ["install"] = theapi
  24. }
  25.  
  26. if fs.exists("/pkg/.lock") then
  27.   term.setTextColor(colors.red)
  28.   error("ERROR: Could not obtain lock. Is another pkg process running? (/pkg/.lock exists)")
  29. end
  30.  
  31. if not http then
  32.   term.setTextColor(colors.red)
  33.   print("ERROR: package requires the HTTP API enabled and firewall disabled. Please enable HTTP and set the firewall to \"*\" in the ComputerCraft config.")
  34. end
  35.  
  36. if not fs.isDir("/pkg") then
  37.   print("First run stuff happening")
  38.   print("Creating /pkg")
  39.   fs.makeDir("/pkg")
  40. end
  41.  
  42. if not fs.exists("/pkg/repo") then
  43.   print("Creating /pkg/repo")
  44.   local repofile = fs.open("/pkg/repo", "w")
  45.   repofile.write("CodingWithClass/CWCOS")
  46.   repofile.close()
  47. end
  48.  
  49. local lock = fs.open("/pkg/.lock", "w")
  50. lock.write(" ")
  51. lock.close()
  52.  
  53. print("Loading repo...")
  54. local repofile = fs.open("/pkg/repo", "r")
  55. ghrepo = repofile:readAll()
  56. local packageListHTTP = http.get("https://raw.githubusercontent.com/"..ghrepo.."/packagerepo/packages")
  57. if not packageListHTTP and not fs.exists("/pkg/repocache") then
  58.   term.setTextColor(colors.red)
  59.   fs.delete("/pkg/.lock")
  60.   error("ERROR: Package list failed to load from repository and no cache exists. Please check you can get a connection to GitHub.")
  61. elseif not packageListHTTP and fs.exists("/pkg/repocache") then
  62.   term.setTextColor(colors.orange)
  63.   print("WARNING: Package list failed to load from repository. Using cache.")
  64.   local packageListFile = fs.open("/pkg/cache", "r")
  65.   local textPackageList = packageListFile.readAll()
  66.   packageListFile.close()
  67.   packageList = textutils.unserialize(textPackageList)
  68. else
  69.   local textPackageList = packageListHTTP.readAll()
  70.   packageListHTTP.close()
  71.   packageList = textutils.unserialize(textPackageList)
  72.   print("Downloaded")
  73. end
  74. print("Caching...")
  75. packageListCache = fs.open("/pkg/cache", "w")
  76. packageListCache.write(textutils.serialize(packageList))
  77. packageListCache.close()
  78. print(textutils.serialize(tArgs))
  79. print(textutils.serialize({ ... }))
  80. if tArgs[1] == "find" then
  81.   print("Packages:")
  82.   for _,pname in pairs(packageList) do
  83.     print(pname)
  84.   end
  85. elseif tArgs[1] == "install" then
  86.   print("Finding package "..tArgs[2])
  87.   local found = false
  88.   for _,pname in pairs(packageList) do
  89.     if pname == tArgs[2] then
  90.       print("Found")
  91.       found = true
  92.       break
  93.     end
  94.   end
  95.   if not found then
  96.     term.setTextColor(colors.red)
  97.     fs.delete("/pkg/.lock")
  98.     error("ERROR: Package not found in list")
  99.   end
  100.   print("Downloading")
  101.   local packageHTTP = http.get("https://raw.githubusercontent.com/"..ghrepo.."/packagerepo/"..tArgs[2])
  102.   if not packageHTTP then
  103.     error("ERROR: Download failed. Please try again later.")
  104.   end
  105.   local packageinstaller = packageHTTP.readAll()
  106.   local packagetempfile = fs.open("/pkg/tmp", "w")
  107.   packagetempfile.write(packageinstaller)
  108.   packagetempfile.close()
  109.   os.run({toinstaller}, "/pkg/tmp")
  110.   print("Installed")
  111. end
  112.  
  113. fs.delete("/pkg/tmp")
  114. fs.delete("/pkg/.lock")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement