Advertisement
lincore81

ppm - a rudimentary package manager for cc

Sep 2nd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. -- REPOSITORY
  2.  
  3. function setup_arc_package(name, data)
  4.   shell.run("arc", "x", data.dest)
  5.   fs.delete(data.dest)
  6.   return true
  7. end
  8.  
  9. packages = {
  10.   excav = {url="qeSmMDFM",},
  11.   arc   = {url="VdJMpxkS",},
  12.   utils = {
  13.     url="KuYdxzWZ",
  14.     dest="/utils.arc",
  15.     dependencies={"arc"},
  16.     setup=setup_arc_package,
  17.   },
  18.   turtle = {
  19.     url="XygDHsEi",
  20.     dest="/turtle.arc",
  21.     dependencies={"arc"},
  22.     setup=setup_arc_package,
  23.   },
  24.   libs = {
  25.     url="92pX1nnu",
  26.     dest="/lib.arc",
  27.     dependencies={"arc"},
  28.     setup=setup_arc_package,
  29.   },
  30. }
  31.  
  32.  
  33.  
  34. -- PROGRAM
  35. installed = {}
  36.  
  37.  
  38. function pastebin_load(url)
  39.     url = "http://pastebin.com/raw.php?i="..url
  40.     local resp = http.get(url)
  41.     local code = resp.getResponseCode()
  42.     local ans
  43.     local ok
  44.     if code >= 200 and code < 300 then
  45.         ans = resp.readAll()
  46.         ok = true
  47.     else
  48.         ans = "Response: " .. tostring(code)
  49.         ok = false
  50.     end
  51.     resp.close()
  52.     return ok, ans
  53. end
  54.  
  55. function save_pkg(pkg, dest)
  56.     local h = fs.open(dest, "w")
  57.     h.write(pkg)
  58.     h.close()
  59. end
  60.  
  61.  
  62. function trim(s)
  63.   return s:match "^%s*(.-)%s*$"
  64. end
  65.  
  66. function list(search_str)
  67.     for k,_ in pairs(packages) do
  68.         if search_str == nil or string.match(k, search_str) then
  69.             print(k)
  70.         end
  71.     end
  72. end
  73.  
  74. function resolve_dependencies(deps)
  75.     if deps == nil then return {} end
  76.     local not_installed = {}
  77.     for i,v in ipairs(deps) do
  78.         if not installed[v] then
  79.             table.insert(not_installed, v)
  80.         end
  81.     end
  82.     return not_installed
  83. end
  84.  
  85. function install_package(pkg_name)
  86.     if not packages[pkg_name] then
  87.         error(tostring(pkg_name) .. " does not exist.")
  88.     end
  89.     local data = packages[pkg_name]
  90.     local todo = resolve_dependencies(data.dependencies)
  91.     for _, dep in ipairs(todo) do
  92.         install_package(dep)
  93.     end
  94.  
  95.     term.write("Getting " .. pkg_name .. "... ")
  96.     local ok, pkg = pastebin_load(data.url)
  97.     assert(ok, "Unable to get package (" .. tostring(pkg) .. ")")
  98.     local dest = data.dest or ("/" .. pkg_name)
  99.     save_pkg(pkg, dest)
  100.     if type(data.setup) == "function" then
  101.         data.setup(pkg_name, data)
  102.     end
  103.     term.write("ok.")
  104.     print()
  105.     installed[pkg_name] = true
  106. end
  107.  
  108. function list_installed()
  109.     for k,_ in pairs(installed) do
  110.         print(k)
  111.     end
  112. end
  113.  
  114. function get(...)
  115.     local pkg_names = {...}
  116.     if pkg_names == nil then
  117.         error("Missing arguments: package names")
  118.     end
  119.     for _,v in ipairs(pkg_names) do install_package(v) end
  120.     write_installed_pkgs()
  121.  
  122. end
  123.  
  124.  
  125. function read_installed_pkgs()
  126.     if not fs.exists("/etc/packages") then return {} end
  127.     local h = fs.open("/etc/packages", "r")
  128.     local contents = h.readAll()
  129.     h.close()
  130.     local ans = textutils.unserialize(contents) or {}
  131.     return ans
  132. end
  133.  
  134. function write_installed_pkgs()
  135.     fs.makeDir("/etc")
  136.     local data = textutils.serialize(installed)
  137.     local h = fs.open("/etc/packages", "w")
  138.     h.write(data)
  139.     h.close()
  140. end
  141.  
  142.  
  143. function main(args)
  144.     assert(#args > 0, "Missing arguments: list (pattern) | get package")
  145.     local command = trim(args[1])
  146.  
  147.     local arguments = {
  148.         get = get,
  149.         g = get,
  150.         list = list,
  151.         l = list,
  152.         installed = list_installed,
  153.         i = list_installed,
  154.     }
  155.  
  156.     local func = arguments[command]
  157.     assert(func, "Unknown command: " .. args[1] .. ", try list or get")
  158.     table.remove(args, 1)
  159.     installed = read_installed_pkgs()
  160.     func(unpack(args))
  161. end
  162.  
  163. main{...}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement