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 @@
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")
......
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,16 +65,28 @@ 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")
-- return a key list with makedepends and depends
function all_deps(p)
local m = {}
local k,v
if p == nil then
return m
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
function init_apkdb(repodirs)
local function init_apkdb(repodirs)
local pkgdb = {}
local revdeps = {}
local a
......@@ -83,6 +95,7 @@ function init_apkdb(repodirs)
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
......@@ -93,7 +106,7 @@ function init_apkdb(repodirs)
table.insert(pkgdb[v], a)
end
-- 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
revdeps[v] = {}
end
......@@ -103,29 +116,10 @@ function init_apkdb(repodirs)
return pkgdb, revdeps
end
-- return a key list with makedepends and depends
function all_deps(p)
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 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
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