aports.lua: make api more object oriented

- provide a handle with aports.new(dir)
- provide foreach() helper functions
This commit is contained in:
Natanael Copa 2011-06-15 10:24:31 +02:00
parent ac830aeb87
commit 48884b4eb8
2 changed files with 86 additions and 59 deletions

32
ap.in
View File

@ -2,6 +2,8 @@
require("aports")
local db
-- subcommands -----------------------
subcmd = {}
subcmd.revdep = {
@ -9,13 +11,10 @@ subcmd.revdep = {
usage = "PKG...",
run = function(opts)
local i
local apkdb, rev = aports.init_apkdb(repodirs)
for i = 2, #opts do
local pkg = opts[i]
local _,p
for _,p in pairs(rev[pkg] or {}) do
print(p.pkgname)
end
db:foreach_revdep(opts[i], function (k,p)
print(p.pkgname)
end)
end
end
}
@ -24,11 +23,9 @@ subcmd.list = {
desc = "Print all packages built from aports tree",
usage = "",
run = function()
local apkdb = aports.init_apkdb(repodirs)
local k,v
for k,v in pairs(apkdb) do
db:foreach(function (k)
print(k)
end
end)
end
}
@ -36,9 +33,8 @@ subcmd.recursdeps = {
desc = "Recursively print all make dependencies for given packages",
usage = "PKG...",
run = function (opts)
local db, rev = aports.init_apkdb(repodirs)
for i = 2, #opts do
aports.recurs_until(db, opts[i], function(pn)
db:recurs_until(opts[i], function(pn)
print(pn)
end)
end
@ -51,22 +47,21 @@ subcmd.builddirs = {
run = function(opts)
local i, p, _
local visited = {}
local db, rev = aports.init_apkdb(repodirs)
local to_print = {}
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
end
end)
end
for i = 2, #opts do
aports.recurs_until(db, opts[i], function(pn)
db:recurs_until(opts[i], function(pn)
local j,p
for j, p in pairs(db[pn]) do
db:foreach_pkg(pn, function(j, p)
if to_print[p.dir] then
print(p.dir)
to_print[p.dir] = nil
end
end
end)
end)
end
end
@ -111,6 +106,7 @@ if cmd == nil then
end
if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
db = aports.new(repodirs)
subcmd[cmd].run(opts)
else
io.stderr:write(cmd..": invalid subcommand\n")

View File

@ -1,6 +1,6 @@
module(..., package.seeall)
function split(str)
local function split(str)
local t = {}
local e
if (str == nil) then
@ -12,7 +12,7 @@ function split(str)
return t
end
function split_apkbuild(line)
local function split_apkbuild(line)
local r = {}
local dir,pkgname, pkgver, pkgrel, arch, depends, makedepends, subpackages, source = string.match(line, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)")
r.dir = dir
@ -27,7 +27,7 @@ function split_apkbuild(line)
end
-- parse the APKBUILDs and return an iterator
function parse_apkbuilds(dirs)
local function parse_apkbuilds(dirs)
local i,v, p
local str=""
if dirs == nil then
@ -65,43 +65,6 @@ function parse_apkbuilds(dirs)
end
function target_packages(pkgdb, pkgname)
local i,v
local t = {}
for i,v in ipairs(pkgdb[pkgname]) do
table.insert(t, pkgname.."-"..v.pkgver.."-r"..v.pkgrel..".apk")
end
return t
end
function init_apkdb(repodirs)
local pkgdb = {}
local revdeps = {}
local a
for a in parse_apkbuilds(repodirs) do
-- io.write(a.pkgname.." "..a.pkgver.."\t"..a.dir.."\n")
if pkgdb[a.pkgname] == nil then
pkgdb[a.pkgname] = {}
end
table.insert(pkgdb[a.pkgname], a)
-- add subpackages to package db
local k,v
for k,v in pairs(a.subpackages) do
if pkgdb[v] == nil then
pkgdb[v] = {}
end
table.insert(pkgdb[v], a)
end
-- add to reverse dependencies
for k,v in pairs(a.makedepends) do
if revdeps[v] == nil then
revdeps[v] = {}
end
table.insert(revdeps[v], a)
end
end
return pkgdb, revdeps
end
-- return a key list with makedepends and depends
function all_deps(p)
@ -123,9 +86,40 @@ function all_deps(p)
return m
end
local function init_apkdb(repodirs)
local pkgdb = {}
local revdeps = {}
local a
for a in parse_apkbuilds(repodirs) do
-- io.write(a.pkgname.." "..a.pkgver.."\t"..a.dir.."\n")
if pkgdb[a.pkgname] == nil then
pkgdb[a.pkgname] = {}
end
a.all_deps = all_deps
table.insert(pkgdb[a.pkgname], a)
-- add subpackages to package db
local k,v
for k,v in pairs(a.subpackages) do
if pkgdb[v] == nil then
pkgdb[v] = {}
end
table.insert(pkgdb[v], a)
end
-- add to reverse dependencies
for v in pairs(all_deps(a)) do
if revdeps[v] == nil then
revdeps[v] = {}
end
table.insert(revdeps[v], a)
end
end
return pkgdb, revdeps
end
function recurs_until(apkdb, pn, func)
local Aports = {}
function Aports:recurs_until(pn, func)
local visited={}
local apkdb = self.apks
function recurs(pn)
if pn == nil or visited[pn] or apkdb[pn] == nil then
return false
@ -145,3 +139,40 @@ function recurs_until(apkdb, pn, func)
return recurs(pn)
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