Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.7KB

  1. local serial = require "serialization"
  2. local dl = require "download"
  3. local pkg = {}
  4. pkg.cfgPath = "/boot/cfg/pkg"
  5. pkg.sourcePath = pkg.cfgPath .. "/sources.cfg"
  6. pkg.installedPath = pkg.cfgPath .. "/installed.cfg"
  7. local function getSources()
  8. local f = io.open(pkg.sourcePath,"rb")
  9. if not f then return {} end
  10. local c = f:read("*a")
  11. f:close()
  12. return serial.unserialize(c)
  13. end
  14. local function saveSources(t)
  15. fs.makeDirectory(pkg.cfgPath)
  16. local f = io.open(pkg.sourcePath,"wb")
  17. f:write(serial.serialize(t))
  18. f:close()
  19. end
  20. local function getInstalled()
  21. local f = io.open(pkg.installedPath,"rb")
  22. if not f then return {} end
  23. local c = f:read("*a")
  24. f:close()
  25. return serial.unserialize(c)
  26. end
  27. local function saveInstalled(t)
  28. fs.makeDirectory(pkg.cfgPath)
  29. local f = io.open(pkg.installedPath,"wb")
  30. if not f then return false end
  31. f:write(serial.serialize(t))
  32. f:close()
  33. end
  34. local function getRepoMeta(repo)
  35. if not getSources()[repo].cache or not fs.exists("/boot/cfg/pkg/repo-"..repo..".cfg") then
  36. dl(getSources()[repo].path.."/packages.cfg","/boot/cfg/pkg/repo-"..repo..".cfg")
  37. end
  38. local f = io.open("/boot/cfg/pkg/repo-"..repo..".cfg","rb")
  39. local rt = serial.unserialize(f:read("*a"))
  40. f:close()
  41. if not getSources()[repo].cache then
  42. fs.remove("/boot/cfg/pkg/repo-"..repo..".cfg")
  43. end
  44. return rt
  45. end
  46. local function activatePackage(path,compressed)
  47. require("pkgfs").add(path,compressed)
  48. end
  49. local function deactivatePackage(path)
  50. require("pkgfs").remove(path)
  51. end
  52. function pkg.addRepo(name,path,cache) -- string string boolean -- boolean -- Adds a repository, referred to as *name*, to the list of package sources, with the remote path *path*. If *cache* is set, keep a local copy of the repository index.
  53. local sources = getSources()
  54. sources[name] = {path=path,cache=cache,name=name}
  55. saveSources(sources)
  56. end
  57. function pkg.delRepo(name) -- string -- boolean -- Removes a repository from the list of repositories.
  58. local sources = getSources()
  59. sources[name] = nil
  60. saveSources(sources)
  61. end
  62. function pkg.update() -- Re-download cached repository indexes.
  63. for repo,meta in pairs(getSources()) do
  64. fs.remove("/boot/cfg/pkg/repo-"..repo..".cfg")
  65. if meta.cache then
  66. getRepoMeta(repo)
  67. end
  68. end
  69. end
  70. function pkg.list(filter) -- string -- -- Print a list of available packages matching *filter*.
  71. filter = filter or ""
  72. local pkglist = {}
  73. for repo,_ in pairs(getSources()) do
  74. for pkg,meta in pairs(getRepoMeta(repo)) do
  75. if pkg:find(filter) or (pkg.meta or ""):find(filter) then
  76. meta.repo = repo
  77. pkglist[pkg] = meta
  78. end
  79. end
  80. end
  81. for k,v in pairs(pkglist) do
  82. print(string.format("%s/%s\n %s",v.repo,k,v.description))
  83. end
  84. end
  85. function pkg.getMeta(pkgname) -- string -- table -- Returns the metadata for a the package specified in *pkgname*.
  86. print("Finding package "..pkgname)
  87. for repo,info in pairs(getSources()) do
  88. local pkg = getRepoMeta(repo)[pkgname]
  89. if pkg then
  90. print("Package "..pkgname.." located in repo "..repo.." at "..info.path)
  91. pkg.repository = info
  92. return pkg
  93. end
  94. end
  95. end
  96. function pkg.get(pkgname,auto) -- string boolean -- boolean -- Downloads and mounts a package, identified as *pkgname*, onto the pkgfs.
  97. local pkginfo = pkg.getMeta(pkgname)
  98. if not pkginfo then error("unable to locate package "..pkgname) end
  99. pkginfo.manual = not auto
  100. fs.makeDirectory("/boot/pkg")
  101. for k,v in ipairs(pkginfo.dependencies or {}) do
  102. if not getInstalled()[v] then
  103. pkg.get(v)
  104. end
  105. end
  106. dl(pkginfo.repository.path.."/"..pkginfo.filename,"/boot/pkg/"..pkginfo.filename)
  107. local installed = getInstalled()
  108. installed[pkgname] = pkginfo
  109. saveInstalled(installed)
  110. pcall(activatePackage,"/boot/pkg/"..pkginfo.filename,pkginfo.compressed)
  111. return true
  112. end
  113. function pkg.upgrade(force) -- boolean -- boolean -- Upgrades all packages on the system to the current version stored in the relevant repository. If *force* is set, re-download all packages.
  114. pkg.update()
  115. fs.makeDirectory("/boot/pkg")
  116. local installed = getInstalled()
  117. for repo,info in pairs(getSources()) do
  118. for pkgname,pkg in pairs(getRepoMeta(repo)) do
  119. if pkg.version ~= installed[pkgname].version or force then
  120. pkg.remove(pkgname)
  121. dl(info.path.."/"..pkg.filename,"/boot/pkg/"..pkg.filename)
  122. installed[pkgname] = pkg
  123. pcall(activatePackage,"/boot/pkg/"..pkg.filename,pkg.compressed)
  124. return true
  125. end
  126. end
  127. end
  128. saveInstalled(installed)
  129. end
  130. function pkg.remove(pkgname) -- string -- boolean -- Remove the package *pkgname* from the pkgfs and package directory.
  131. local installed = getInstalled()
  132. local pkginfo = installed[pkgname]
  133. if not pkginfo then error(pkgname .." not installed") end
  134. pcall(deactivatePackage,"/boot/pkg/"..pkginfo.filename)
  135. fs.remove("/boot/pkg/"..pkginfo.filename)
  136. installed[pkgname] = nil
  137. saveInstalled(installed)
  138. return true
  139. end
  140. return pkg