aports.lua: make api more object oriented
- provide a handle with aports.new(dir) - provide foreach() helper functions
This commit is contained in:
parent
ac830aeb87
commit
48884b4eb8
32
ap.in
32
ap.in
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
require("aports")
|
require("aports")
|
||||||
|
|
||||||
|
local db
|
||||||
|
|
||||||
-- subcommands -----------------------
|
-- subcommands -----------------------
|
||||||
subcmd = {}
|
subcmd = {}
|
||||||
subcmd.revdep = {
|
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 = {
|
|||||||
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 = {
|
|||||||
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 = {
|
|||||||
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
|
|||||||
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")
|
||||||
|
113
aports.lua
113
aports.lua
@ -1,6 +1,6 @@
|
|||||||
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)
|
|||||||
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)
|
|||||||
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,43 +65,6 @@ function parse_apkbuilds(dirs)
|
|||||||
end
|
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
|
-- return a key list with makedepends and depends
|
||||||
function all_deps(p)
|
function all_deps(p)
|
||||||
@ -123,9 +86,40 @@ function all_deps(p)
|
|||||||
return m
|
return m
|
||||||
end
|
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 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)
|
|||||||
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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user