summaryrefslogtreecommitdiffstats
path: root/aports/pkg.lua
blob: f21f352a3881636a1d403876e7e0d97850eb82d0 (plain)
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

local M = {}
local abuild = require('aports.abuild')
local lfs = require('lfs')

function M.is_remote(url)
	local _,pref
	for _,pref in pairs{ "^http://", "^ftp://", "^https://", ".*::.*" } do
		if string.match(url, pref) then
			return true
		end
	end
	return false
end

-- iterator for all remote sources of given pkg/aport
function M.remote_sources(p)
	if p == nil or type(p.source) ~= "table" then
		return nil
	end
	return coroutine.wrap(function()
		for _,url in pairs(p.source) do
			if M.is_remote(url) then
				coroutine.yield(url)
			end
		end
	end)
end

function M.get_maintainer(pkg)
	if pkg == nil or pkg.dir == nil then
		return nil
	end
	local f = io.open(pkg.dir.."/APKBUILD")
	if f == nil then
		return nil
	end
	local line
	for line in f:lines() do
		local maintainer = line:match("^%s*#%s*Maintainer:%s*(.*)")
		if maintainer then
			f:close()
			return maintainer
		end
	end
	f:close()
	return nil
end

function M.get_repo_name(pkg)
	if pkg == nil or pkg.dir == nil then
		return nil
	end
	return string.match(pkg.dir, ".*/(.*)/.*")
end

function M.get_apk_file_name(pkg, name)
	return (name or pkg.pkgname).."-"..pkg.pkgver.."-r"..pkg.pkgrel..".apk"
end

function M.get_apk_file_path(pkg, name)
	local repodest = pkg.repodest or abuild.repodest
	if pkg.repodest == nil and abuild.pkgdest ~= nil and abuild.pkgdest ~= "" then
		return abuild.pkgdest.."/"..M.get_apk_file_name(pkg, name)
	end
	if repodest ~= nil and repodest ~= "" then
		return repodest.."/"..M.get_repo_name(pkg).."/"..abuild.arch.."/"..M.get_apk_file_name(pkg, name)
	end
	return pkg.dir.."/"..M.get_apk_file_name(pkg, name)
end

function M.apk_file_exists(pkg, name)
	-- technically we check if it is readable...
	local filepath = M.get_apk_file_path(pkg, name)
        if lfs.attributes(filepath) == nil then
		io.stderr:write(("DEBUG: path=%s\n"):format(filepath))
	end
	return lfs.attributes(filepath) ~= nil
end

function M.all_apks_exists(pkg)
	if not pkg:apk_file_exists() then
		return false
	end
	for _, subpkgname in pairs(pkg.subpackages) do
		if not pkg:apk_file_exists(subpkgname) then
			return false
		end
	end
	return true
end

function M.arch_enabled(pkg)
	return not pkg.arch["!"..abuild.arch] and (pkg.arch.all or pkg.arch.noarch or pkg.arch[abuild.arch])
end

function M.libc_enabled(pkg)
	return not pkg.options["!libc_"..abuild.libc]
end

function M.relevant(pkg)
	return pkg:arch_enabled() and pkg:libc_enabled()
end

function M.each_dependency(pkg)
	return coroutine.wrap(function()
		for _,dep in pairs(pkg.depends or {}) do
			coroutine.yield(dep)
		end
		for _,dep in pairs(pkg.makedepends or {}) do
			coroutine.yield(dep)
		end
	end)
end


function M.init(pkg, repodest)
	for k,v in pairs(M) do
		pkg[k] = v
	end
	pkg.repodest = repodest
end

return M