| 1 | #!/usr/bin/lua |
| 2 | |
| 3 | require("aports") |
| 4 | require("lfs") |
| 5 | |
| 6 | local db |
| 7 | |
| 8 | local function build_is_outdated(pkg) |
| 9 | local apk_attr = lfs.attributes(aports.get_apk_file_path(pkg)) |
| 10 | local apkbuild_attr = lfs.attributes(pkg.dir.."/APKBUILD") |
| 11 | if apk_attr == nil then |
| 12 | return true |
| 13 | end |
| 14 | return os.difftime(apk_attr.modification, apkbuild_attr.modification) < 0 |
| 15 | end |
| 16 | |
| 17 | local function build_is_missing(pkg) |
| 18 | return lfs.attributes(aports.get_apk_file_path(pkg)) == nil |
| 19 | end |
| 20 | |
| 21 | -- subcommands ----------------------- |
| 22 | subcmd = {} |
| 23 | subcmd.revdep = { |
| 24 | desc = "Print reverse dependencies", |
| 25 | usage = "PKG...", |
| 26 | run = function(opts) |
| 27 | local i |
| 28 | for i = 1, #opts do |
| 29 | db:foreach_revdep(opts[i], function (k,p) |
| 30 | print(p.pkgname) |
| 31 | end) |
| 32 | end |
| 33 | end |
| 34 | } |
| 35 | |
| 36 | subcmd.list = { |
| 37 | desc = "Print all packages built from aports tree", |
| 38 | usage = "", |
| 39 | run = function() |
| 40 | db:foreach(function (k) |
| 41 | print(k) |
| 42 | end) |
| 43 | end |
| 44 | } |
| 45 | |
| 46 | subcmd.recursdeps = { |
| 47 | desc = "Recursively print all make dependencies for given packages", |
| 48 | usage = "PKG...", |
| 49 | run = function (opts) |
| 50 | for i = 1, #opts do |
| 51 | db:recurs_until(opts[i], function(pn) |
| 52 | print(pn) |
| 53 | end) |
| 54 | end |
| 55 | end |
| 56 | } |
| 57 | |
| 58 | subcmd.builddirs = { |
| 59 | desc = "Print the build dirs for given packages in build order", |
| 60 | usage = "PKG...", |
| 61 | run = function(opts) |
| 62 | local i, p, _ |
| 63 | local visited = {} |
| 64 | local to_print = {} |
| 65 | for i = 1, #opts do |
| 66 | db:foreach_pkg(opts[i], function(_, p) |
| 67 | to_print[p.dir] = true |
| 68 | end) |
| 69 | end |
| 70 | for i = 1, #opts do |
| 71 | db:recurs_until(opts[i], function(pn) |
| 72 | local j,p |
| 73 | db:foreach_pkg(pn, function(j, p) |
| 74 | if to_print[p.dir] then |
| 75 | print(p.dir) |
| 76 | to_print[p.dir] = nil |
| 77 | end |
| 78 | end) |
| 79 | end) |
| 80 | end |
| 81 | end |
| 82 | } |
| 83 | |
| 84 | subcmd.sources = { |
| 85 | desc = "List sources", |
| 86 | usage = "PKG...", |
| 87 | run = function(opts) |
| 88 | local i, p, _ |
| 89 | for i = 1, #opts do |
| 90 | db:foreach_pkg(opts[i], function(_, p) |
| 91 | aports.foreach_remote_source(p, function(url) |
| 92 | print(p.pkgname, p.pkgver, string.gsub(url, p.pkgver, "$VERSION")) |
| 93 | end) |
| 94 | end) |
| 95 | end |
| 96 | end |
| 97 | } |
| 98 | |
| 99 | subcmd["rebuild-list"] = { |
| 100 | desc = "List packages that can/should be rebuilt", |
| 101 | usage = "", |
| 102 | run = function() |
| 103 | local outdated = {} |
| 104 | db:foreach(function(k) |
| 105 | db:foreach_pkg(k, function(_, p) |
| 106 | if build_is_outdated(p) then |
| 107 | table.insert(outdated, p.pkgname) |
| 108 | end |
| 109 | end) |
| 110 | end) |
| 111 | -- print build dirs in build sort order |
| 112 | subcmd.builddirs.run(outdated) |
| 113 | end |
| 114 | } |
| 115 | |
| 116 | subcmd["build-list"] = { |
| 117 | desc = "List packages that is not built", |
| 118 | usage = "", |
| 119 | run = function() |
| 120 | local missing = {} |
| 121 | db:foreach(function(k) |
| 122 | db:foreach_pkg(k, function(_, p) |
| 123 | if build_is_missing(p) then |
| 124 | table.insert(missing, p.pkgname) |
| 125 | end |
| 126 | end) |
| 127 | end) |
| 128 | -- print build dirs in build sort order |
| 129 | subcmd.builddirs.run(missing) |
| 130 | end |
| 131 | } |
| 132 | |
| 133 | function print_usage() |
| 134 | io.write("usage: ap -d <DIR> SUBCOMMAND [options]\n\nSubcommands are:\n") |
| 135 | local k,v |
| 136 | for k in pairs(subcmd) do |
| 137 | print(" "..k) |
| 138 | end |
| 139 | end |
| 140 | |
| 141 | -- those should be read from some config file |
| 142 | repodirs = {} |
| 143 | |
| 144 | |
| 145 | -- parse args |
| 146 | i = 1 |
| 147 | opts = {} |
| 148 | help = false |
| 149 | while i <= #arg do |
| 150 | if arg[i] == "-d" then |
| 151 | i = i + 1 |
| 152 | repodirs[#repodirs + 1] = arg[i] |
| 153 | elseif arg[i] == "-h" then |
| 154 | help = true |
| 155 | else |
| 156 | opts[#opts + 1] = arg[i] |
| 157 | end |
| 158 | i = i + 1 |
| 159 | end |
| 160 | |
| 161 | |
| 162 | cmd = table.remove(opts, 1) |
| 163 | |
| 164 | if cmd == nil then |
| 165 | print_usage() |
| 166 | -- usage |
| 167 | return |
| 168 | end |
| 169 | |
| 170 | if #repodirs == 0 then |
| 171 | if lfs.attributes("APKBUILD") then |
| 172 | repodirs[1] = string.gsub(lfs.currentdir(), "(.*)/.*", "%1") |
| 173 | else |
| 174 | repodirs[1] = lfs.currentdir() |
| 175 | end |
| 176 | end |
| 177 | |
| 178 | if subcmd[cmd] and type(subcmd[cmd].run) == "function" then |
| 179 | db = aports.new(repodirs) |
| 180 | loadtime = os.clock() |
| 181 | subcmd[cmd].run(opts) |
| 182 | runtime = os.clock() - loadtime |
| 183 | -- io.stderr:write("db load time = "..tostring(loadtime).."\n") |
| 184 | -- io.stderr:write("cmd run time = "..tostring(runtime).."\n") |
| 185 | else |
| 186 | io.stderr:write(cmd..": invalid subcommand\n") |
| 187 | end |
| 188 | |