projects/aports·blob v2.14.2 · refs

200 lines · 3.9 KiB

1#!/usr/bin/lua
2
3aports = require("aports")
4lfs = require("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
133subcmd["list-apks"] = {
134 desc = "List all apk files",
135 usage = "",
136 run = function()
137 db:foreach(function(k)
138 db:foreach_pkg(k, function(_, p)
139 print(aports.get_apk_filename(p))
140 end)
141 end)
142 end
143}
144
145function print_usage()
146 io.write("usage: ap -d <DIR> SUBCOMMAND [options]\n\nSubcommands are:\n")
147 local k,v
148 for k in pairs(subcmd) do
149 print(" "..k)
150 end
151end
152
153-- those should be read from some config file
154repodirs = {}
155
156
157-- parse args
158i = 1
159opts = {}
160help = false
161while i <= #arg do
162 if arg[i] == "-d" then
163 i = i + 1
164 repodirs[#repodirs + 1] = arg[i]
165 elseif arg[i] == "-h" then
166 help = true
167 else
168 opts[#opts + 1] = arg[i]
169 end
170 i = i + 1
171end
172
173
174cmd = table.remove(opts, 1)
175
176if cmd == nil then
177 print_usage()
178 -- usage
179 return
180end
181
182if #repodirs == 0 then
183 if lfs.attributes("APKBUILD") then
184 repodirs[1] = string.gsub(lfs.currentdir(), "(.*)/.*", "%1")
185 else
186 repodirs[1] = lfs.currentdir()
187 end
188end
189
190if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
191 db = aports.new(repodirs)
192 loadtime = os.clock()
193 subcmd[cmd].run(opts)
194 runtime = os.clock() - loadtime
195-- io.stderr:write("db load time = "..tostring(loadtime).."\n")
196-- io.stderr:write("cmd run time = "..tostring(runtime).."\n")
197else
198 io.stderr:write(cmd..": invalid subcommand\n")
199end
200