projects/aports·blob v2.11.2 · refs

188 lines · 3.6 KiB

1#!/usr/bin/lua
2
3require("aports")
4require("lfs")
5
6local db
7
8local 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
15end
16
17local function build_is_missing(pkg)
18 return lfs.attributes(aports.get_apk_file_path(pkg)) == nil
19end
20
21-- subcommands -----------------------
22subcmd = {}
23subcmd.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
36subcmd.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
46subcmd.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
58subcmd.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
84subcmd.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
99subcmd["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
116subcmd["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
133function 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
139end
140
141-- those should be read from some config file
142repodirs = {}
143
144
145-- parse args
146i = 1
147opts = {}
148help = false
149while 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
159end
160
161
162cmd = table.remove(opts, 1)
163
164if cmd == nil then
165 print_usage()
166 -- usage
167 return
168end
169
170if #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
176end
177
178if 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")
185else
186 io.stderr:write(cmd..": invalid subcommand\n")
187end
188