aboutsummaryrefslogtreecommitdiffstats
path: root/main/lua-aports/0002-buildrepo-refactor.patch
blob: c140e8d83d0c1e92f6f86e8af0689db9ba978e40 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
From 62478e8dc18cac0ffc3d30917f057b1d1d75f9f0 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Wed, 22 Jul 2015 17:00:31 +0200
Subject: [PATCH 2/2] buildrepo: refactor

- pass logfile directly to build_aport
- run the plugin hooks from loop
- pass progress status to prebuild plugins
---
 buildrepo.lua | 84 +++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 50 insertions(+), 34 deletions(-)

diff --git a/buildrepo.lua b/buildrepo.lua
index 4fb9f12..5c4b931 100755
--- a/buildrepo.lua
+++ b/buildrepo.lua
@@ -97,36 +97,54 @@ local function run_plugins(dirpath, func, ...)
 	end
 end
 
-local function build_aport(aport, repodest, logdir)
+local function plugins_prebuild(...)
+	return run_plugins(pluginsdir, "prebuild", ...)
+end
+
+local function plugins_postbuild(...)
+	return run_plugins(pluginsdir, "postbuild", ...)
+end
+
+local function logfile_path(logdirbase, repo, aport)
+	if logdirbase == nil then
+		return nil
+	end
+	local dir = ("%s/%s/%s"):format(logdirbase, repo, aport.pkgname)
+	if not lfs.attributes(dir) then
+		local path = ""
+		for n in string.gmatch(dir, "[^/]+") do
+			path = path.."/"..n
+			lfs.mkdir(path)
+		end
+	end
+	return ("%s/%s-%s-r%s.log"):format(dir, aport.pkgname, aport.pkgver, aport.pkgrel)
+end
+
+
+local function build_aport(aport, repodest, logfile)
 	local success, errmsg = lfs.chdir(aport.dir)
 	if not success then
 		err("%s", errmsg)
 		return nil
 	end
 	local logredirect = ""
-	if logdir ~= nil then
-		local dir = ("%s/%s"):format(logdir, aport.pkgname)
-		if not lfs.attributes(dir) then
-			assert(lfs.mkdir(dir), dir)
-		end
-		local logfile = ("%s/%s-%s-r%s.log"):format(dir, aport.pkgname, aport.pkgver, aport.pkgrel)
-
+	if logfile ~= nil then
 		logredirect = ("> '%s' 2>&1"):format(logfile)
 	end
 	local cmd = ("REPODEST='%s' abuild -r -m %s"):format(repodest, logredirect)
-	run_plugins(pluginsdir, "prebuild", aport, logfile)
-	if opts.n then
-		success = true
-	else
-		success = os.execute(cmd)
-	end
+	success = os.execute(cmd)
 	if not success then
 		err("%s: Failed to build", aport.pkgname)
 	end
-	run_plugins(pluginsdir, "postbuild", aport, success, logfile)
 	return success
 end
 
+local function log_progress(progress, repo, aport)
+	info("%d/%d %d/%d %s/%s %s-r%s",
+		progress.tried, progress.total,
+		progress.repo_built, progress.repo_total,
+		repo, aport.pkgname, aport.pkgver, aport.pkgrel)
+end
 -----------------------------------------------------------------
 local opthelp = [[
  -a DIR     Set the aports base dir to DIR instead of $HOME/aports
@@ -164,6 +182,10 @@ aportsdir = opts.a or ("%s/aports"):format(homedir)
 repodest = opts.d or abuild.repodest or ("%s/packages"):format(homedir)
 logdirbase = opts.l
 
+if opts.n then
+	build_aport = function() return true end
+end
+
 stats = {}
 for _,repo in pairs(args) do
 	local db = require('aports.db').new(aportsdir, repo)
@@ -199,34 +221,28 @@ for _,repo in pairs(args) do
 		unsorted[aport.pkgname] = true
 	end
 
-	if logdirbase ~= nil then
-		logdir = ("%s/%s"):format(logdirbase, repo)
-		if not lfs.attributes(logdir) then
-			assert(lfs.mkdir(logdir), logdir)
-		end
-	end
-
 	-- build packages
 	local built = 0
 	local tried = 0
 	for aport in db:each_in_build_order(pkgs) do
+		local logfile = logfile_path(logdirbase, repo, aport)
 		tried = tried + 1
-		local totally_built = stats[repo].relevant_aports - #pkgs + built
+		local progress = { tried = tried, total = #pkgs,
+			repo_built = stats[repo].relevant_aports - #pkgs + built,
+			repo_total = stats[repo].relevant_aports,
+		}
 		if not db:known_deps_exists(aport) then
 			warn("%s: Skipped due to missing dependencies", aport.pkgname)
 		elseif not (opts.s and skip_aport(aport)) then
-			info("%d/%d %d/%d %s/%s %s-r%s",
-					tried, #pkgs,
-					totally_built,
-					stats[repo].relevant_aports,
-					repo, aport.pkgname,
-					aport.pkgver, aport.pkgrel)
-			if build_aport(aport, repodest, logdir) then
+			log_progress(progress, repo, aport)
+			plugins_prebuild(aport, progress, logfile)
+			local success = build_aport(aport, repodest, logfile)
+			plugins_postbuild(aport, success, logfile)
+			if success then
 				built = built + 1
-			else
-				if not opts.k then
-					os.exit(1)
-				end
+			end
+			if not success and not opts.k then
+				os.exit(1)
 			end
 		end
 	end
-- 
2.4.6