Commit 48884b4e authored by Natanael Copa's avatar Natanael Copa

aports.lua: make api more object oriented

- provide a handle with aports.new(dir)
- provide foreach() helper functions
parent ac830aeb
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
require("aports") require("aports")
local db
-- subcommands ----------------------- -- subcommands -----------------------
subcmd = {} subcmd = {}
subcmd.revdep = { subcmd.revdep = {
...@@ -9,13 +11,10 @@ subcmd.revdep = { ...@@ -9,13 +11,10 @@ subcmd.revdep = {
usage = "PKG...", usage = "PKG...",
run = function(opts) run = function(opts)
local i local i
local apkdb, rev = aports.init_apkdb(repodirs)
for i = 2, #opts do for i = 2, #opts do
local pkg = opts[i] db:foreach_revdep(opts[i], function (k,p)
local _,p print(p.pkgname)
for _,p in pairs(rev[pkg] or {}) do end)
print(p.pkgname)
end
end end
end end
} }
...@@ -24,11 +23,9 @@ subcmd.list = { ...@@ -24,11 +23,9 @@ subcmd.list = {
desc = "Print all packages built from aports tree", desc = "Print all packages built from aports tree",
usage = "", usage = "",
run = function() run = function()
local apkdb = aports.init_apkdb(repodirs) db:foreach(function (k)
local k,v
for k,v in pairs(apkdb) do
print(k) print(k)
end end)
end end
} }
...@@ -36,9 +33,8 @@ subcmd.recursdeps = { ...@@ -36,9 +33,8 @@ subcmd.recursdeps = {
desc = "Recursively print all make dependencies for given packages", desc = "Recursively print all make dependencies for given packages",
usage = "PKG...", usage = "PKG...",
run = function (opts) run = function (opts)
local db, rev = aports.init_apkdb(repodirs)
for i = 2, #opts do for i = 2, #opts do
aports.recurs_until(db, opts[i], function(pn) db:recurs_until(opts[i], function(pn)
print(pn) print(pn)
end) end)
end end
...@@ -51,22 +47,21 @@ subcmd.builddirs = { ...@@ -51,22 +47,21 @@ subcmd.builddirs = {
run = function(opts) run = function(opts)
local i, p, _ local i, p, _
local visited = {} local visited = {}
local db, rev = aports.init_apkdb(repodirs)
local to_print = {} local to_print = {}
for i = 2, #opts do for i = 2, #opts do
for _,p in pairs(db[opts[i]]) do db:foreach_pkg(opts[i], function(_, p)
to_print[p.dir] = true to_print[p.dir] = true
end end)
end end
for i = 2, #opts do for i = 2, #opts do
aports.recurs_until(db, opts[i], function(pn) db:recurs_until(opts[i], function(pn)
local j,p local j,p
for j, p in pairs(db[pn]) do db:foreach_pkg(pn, function(j, p)
if to_print[p.dir] then if to_print[p.dir] then
print(p.dir) print(p.dir)
to_print[p.dir] = nil to_print[p.dir] = nil
end end
end end)
end) end)
end end
end end
...@@ -111,6 +106,7 @@ if cmd == nil then ...@@ -111,6 +106,7 @@ if cmd == nil then
end end
if subcmd[cmd] and type(subcmd[cmd].run) == "function" then if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
db = aports.new(repodirs)
subcmd[cmd].run(opts) subcmd[cmd].run(opts)
else else
io.stderr:write(cmd..": invalid subcommand\n") io.stderr:write(cmd..": invalid subcommand\n")
......
module(..., package.seeall) module(..., package.seeall)
function split(str) local function split(str)
local t = {} local t = {}
local e local e
if (str == nil) then if (str == nil) then
...@@ -12,7 +12,7 @@ function split(str) ...@@ -12,7 +12,7 @@ function split(str)
return t return t
end end
function split_apkbuild(line) local function split_apkbuild(line)
local r = {} local r = {}
local dir,pkgname, pkgver, pkgrel, arch, depends, makedepends, subpackages, source = string.match(line, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)") local dir,pkgname, pkgver, pkgrel, arch, depends, makedepends, subpackages, source = string.match(line, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)")
r.dir = dir r.dir = dir
...@@ -27,7 +27,7 @@ function split_apkbuild(line) ...@@ -27,7 +27,7 @@ function split_apkbuild(line)
end end
-- parse the APKBUILDs and return an iterator -- parse the APKBUILDs and return an iterator
function parse_apkbuilds(dirs) local function parse_apkbuilds(dirs)
local i,v, p local i,v, p
local str="" local str=""
if dirs == nil then if dirs == nil then
...@@ -65,16 +65,28 @@ function parse_apkbuilds(dirs) ...@@ -65,16 +65,28 @@ function parse_apkbuilds(dirs)
end end
function target_packages(pkgdb, pkgname)
local i,v -- return a key list with makedepends and depends
local t = {} function all_deps(p)
for i,v in ipairs(pkgdb[pkgname]) do local m = {}
table.insert(t, pkgname.."-"..v.pkgver.."-r"..v.pkgrel..".apk") local k,v
if p == nil then
return m
end end
return t if type(p.depends) == "table" then
for k,v in pairs(p.depends) do
m[v] = true
end
end
if type(p.makedepends) == "table" then
for k,v in pairs(p.makedepends) do
m[v] = true
end
end
return m
end end
function init_apkdb(repodirs) local function init_apkdb(repodirs)
local pkgdb = {} local pkgdb = {}
local revdeps = {} local revdeps = {}
local a local a
...@@ -83,6 +95,7 @@ function init_apkdb(repodirs) ...@@ -83,6 +95,7 @@ function init_apkdb(repodirs)
if pkgdb[a.pkgname] == nil then if pkgdb[a.pkgname] == nil then
pkgdb[a.pkgname] = {} pkgdb[a.pkgname] = {}
end end
a.all_deps = all_deps
table.insert(pkgdb[a.pkgname], a) table.insert(pkgdb[a.pkgname], a)
-- add subpackages to package db -- add subpackages to package db
local k,v local k,v
...@@ -93,7 +106,7 @@ function init_apkdb(repodirs) ...@@ -93,7 +106,7 @@ function init_apkdb(repodirs)
table.insert(pkgdb[v], a) table.insert(pkgdb[v], a)
end end
-- add to reverse dependencies -- add to reverse dependencies
for k,v in pairs(a.makedepends) do for v in pairs(all_deps(a)) do
if revdeps[v] == nil then if revdeps[v] == nil then
revdeps[v] = {} revdeps[v] = {}
end end
...@@ -103,29 +116,10 @@ function init_apkdb(repodirs) ...@@ -103,29 +116,10 @@ function init_apkdb(repodirs)
return pkgdb, revdeps return pkgdb, revdeps
end end
-- return a key list with makedepends and depends local Aports = {}
function all_deps(p) function Aports:recurs_until(pn, func)
local m = {}
local k,v
if p == nil then
return m
end
if type(p.depends) == "table" then
for k,v in pairs(p.depends) do
m[v] = true
end
end
if type(p.makedepends) == "table" then
for k,v in pairs(p.makedepends) do
m[v] = true
end
end
return m
end
function recurs_until(apkdb, pn, func)
local visited={} local visited={}
local apkdb = self.apks
function recurs(pn) function recurs(pn)
if pn == nil or visited[pn] or apkdb[pn] == nil then if pn == nil or visited[pn] or apkdb[pn] == nil then
return false return false
...@@ -145,3 +139,40 @@ function recurs_until(apkdb, pn, func) ...@@ -145,3 +139,40 @@ function recurs_until(apkdb, pn, func)
return recurs(pn) return recurs(pn)
end end
function Aports:target_packages(pkgname)
local i,v
local t = {}
for k,v in pairs(self.apks[pkgname]) do
table.insert(t, pkgname.."-"..v.pkgver.."-r"..v.pkgrel..".apk")
end
return t
end
function Aports:foreach(f)
local k,v
for k,v in pairs(self.apks) do
f(k,v)
end
end
function Aports:foreach_revdep(pkg, f)
local k,v
for k,v in pairs(self.revdeps[pkg] or {}) do
f(k,v)
end
end
function Aports:foreach_pkg(pkg, f)
local k,v
for k,v in pairs(self.apks[pkg]) do
f(k,v)
end
end
function new(repodirs)
local h = Aports
h.repodirs = repodirs
h.apks, h.revdeps = init_apkdb(repodirs)
return h
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment