summaryrefslogtreecommitdiff
path: root/ap.in
blob: 2152ecaf5b79ad248d40719fa91616f6251d81dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!@LUA_SHEBANG@

aports = require("aports")
lfs = require("lfs")

local db

local function build_is_outdated(pkg)
	local apk_attr = lfs.attributes(aports.get_apk_file_path(pkg))
	local apkbuild_attr = lfs.attributes(pkg.dir.."/APKBUILD")
	if apk_attr == nil then
		return true
	end
	return os.difftime(apk_attr.modification, apkbuild_attr.modification) < 0
end

local function build_is_missing(pkg)
	return lfs.attributes(aports.get_apk_file_path(pkg)) == nil
end
	
-- subcommands -----------------------
subcmd = {}
subcmd.revdep = {
	desc = "Print reverse dependencies",
	usage = "PKG...",
	run = function(opts)
		local i
		for i = 1, #opts do
			db:foreach_revdep(opts[i], function (k,p)
				print(p.pkgname)
			end)
		end
	end
}

subcmd.list = {
	desc = "Print all packages built from aports tree",
	usage = "",
	run = function()
		db:foreach(function (k)
			print(k)
		end)
	end
}

subcmd.recursdeps = {
	desc = "Recursively print all make dependencies for given packages",
	usage = "PKG...",
	run = function (opts)
		for i = 1, #opts do
			db:recurs_until(opts[i], function(pn)
				print(pn)
			end)
		end
	end
}

subcmd.builddirs = {
	desc = "Print the build dirs for given packages in build order",
	usage = "PKG...",
	run = function(opts)
		local i, p, _
		local visited = {}
		local to_print = {}
		for i = 1, #opts do
			db:foreach_pkg(opts[i], function(_, p)
				to_print[p.dir] = true
			end)
		end
		for i = 1, #opts do
			db:recurs_until(opts[i], function(pn)
				local j,p
				db:foreach_pkg(pn, function(j, p)
					if to_print[p.dir] then
						print(p.dir)
						to_print[p.dir] = nil
					end
				end)	
			end)
		end
	end
}

subcmd.sources = {
	desc = "List sources",
	usage = "PKG...",
	run = function(opts)
		local i, p, _
		for i = 1, #opts do
			db:foreach_pkg(opts[i], function(_, p)
				aports.foreach_remote_source(p, function(url)
					print(p.pkgname, p.pkgver, string.gsub(url, p.pkgver, "$VERSION"))
				end)
			end)
		end
	end
}

subcmd["rebuild-list"] = {
	desc = "List packages that can/should be rebuilt",
	usage = "",
	run = function()
		local outdated = {}
		db:foreach(function(k)
			db:foreach_pkg(k, function(_, p)
				if build_is_outdated(p) then
					table.insert(outdated, p.pkgname)
				end
			end)
		end)
		-- print build dirs in build sort order
		subcmd.builddirs.run(outdated)
	end
}

subcmd["build-list"] = {
	desc = "List packages that is not built",
	usage = "",
	run = function()
		local missing = {}
		db:foreach(function(k)
			db:foreach_pkg(k, function(_, p)
				if build_is_missing(p) then
					table.insert(missing, p.pkgname)
				end
			end)
		end)
		-- print build dirs in build sort order
		subcmd.builddirs.run(missing)
	end
}

subcmd["list-apks"] = {
	desc = "List all apk files",
	usage = "",
	run = function()
		db:foreach(function(k)
			db:foreach_pkg(k, function(_, p)
				print(aports.get_apk_filename(p))
			end)
		end)
	end
}

function print_usage()
	io.write("usage: ap -d <DIR> SUBCOMMAND [options]\n\nSubcommands are:\n")
	local k,v
	for k in pairs(subcmd) do
		print("  "..k)
	end
end

-- those should be read from some config file
repodirs = {} 


-- parse args
i = 1
opts = {}
help = false
while i <= #arg do
	if arg[i] == "-d" then
		i = i + 1
		repodirs[#repodirs + 1] = arg[i]
	elseif arg[i] == "-h" then
		help = true
	else
		opts[#opts + 1] = arg[i]
	end
	i = i + 1
end


cmd = table.remove(opts, 1)

if cmd == nil then
	print_usage()
	-- usage
	return
end

if #repodirs == 0 then
	if lfs.attributes("APKBUILD") then
		repodirs[1] = string.gsub(lfs.currentdir(), "(.*)/.*", "%1")
	else
		repodirs[1] = lfs.currentdir()
	end
end

if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
	db = aports.new(repodirs)
	loadtime = os.clock()
	subcmd[cmd].run(opts)
	runtime = os.clock() - loadtime
--	io.stderr:write("db load time = "..tostring(loadtime).."\n")
--	io.stderr:write("cmd run time = "..tostring(runtime).."\n")
else
	io.stderr:write(cmd..": invalid subcommand\n")
end