summaryrefslogtreecommitdiff
path: root/ap.in
blob: 3d5925bb2c8604df6097a181c8228f37daa16fdc (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
#!/usr/bin/lua

require("aports")

-- subcommands -----------------------
subcmd = {}
subcmd.revdep = {
	desc = "Print reverse dependencies",
	usage = "PKG...",
	run = function(opts)
		local i
		local apkdb, rev = aports.init_apkdb(repodirs)
		for i = 2, #opts do
			local pkg = opts[i]
			local _,p
			for _,p in pairs(rev[pkg] or {}) do
				print(p.pkgname) 
			end
		end
	end
}

subcmd.list = {
	desc = "Print all packages built from aports tree",
	usage = "",
	run = function()
		local apkdb = aports.init_apkdb(repodirs)
		local k,v
		for k,v in pairs(apkdb) do
			print(k)
		end
	end
}

subcmd.recursdeps = {
	desc = "Recursively print all make dependencies for given packages",
	usage = "PKG...",
	run = function (opts)
		local i
		local visited = {}
		local apkdb, rev = aports.init_apkdb(repodirs)
		function recurs(pn)
			if pn == nil or visited[pn] or apkdb[pn] == nil then
				return
			end
			visited[pn] = true
			local i,d, p
			for i,p in pairs(apkdb[pn]) do
				local _, d
				for _, d in pairs(p.depends) do
					recurs(d)
				end
				for _, d in pairs(p.makedepends) do
					recurs(d)
				end
			end
			print(pn)
		end
		for i = 2, #opts do
			recurs(opts[i])
		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 apkdb, rev = aports.init_apkdb(repodirs)
		local to_print = {}
		function recursdir(pn)
			if pn == nil or visited[pn] or apkdb[pn] == nil then
				return
			end
			visited[pn] = true
			local i, p
			for i,p in pairs(apkdb[pn]) do
				local _, d
				for _, d in pairs(p.depends) do
					recursdir(d)
				end
				for _, d in pairs(p.makedepends) do
					recursdir(d)
				end
				if to_print[p.dir] then
					print(p.dir)
					to_print[p.dir] = nil
				end
			end
		end
		for i = 2, #opts do
			for _,p in pairs(apkdb[opts[i]]) do
				to_print[p.dir] = true
			end
		end
		for i = 2, #opts do
			recursdir(opts[i])
		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 = opts[1]

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

if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
	subcmd[cmd].run(opts)
else
	io.stderr:write(cmd..": invalid subcommand\n")
end