From 48884b4eb8fcc8c63b45b72e364cc10ef74086d3 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Wed, 15 Jun 2011 10:24:31 +0200 Subject: aports.lua: make api more object oriented - provide a handle with aports.new(dir) - provide foreach() helper functions --- aports.lua | 97 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 33 deletions(-) (limited to 'aports.lua') diff --git a/aports.lua b/aports.lua index f8a0121..4c57a89 100755 --- a/aports.lua +++ b/aports.lua @@ -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,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 + -- cgit v1.2.3