summaryrefslogtreecommitdiffhomepage
path: root/subr.ex/ex_pkg_restart.subr
blob: a0cf220e46310cc4f260f640af0779625283e7e8 (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

#
# exp_pkg_check_restart_at() - check restart (-r) specification for unknown build steps
# @_rstatus:		out reference to status string
# @_rspec_at:		in reference to restart build step list
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_check_restart_at() {
	local	_epcra_rstatus="${1#\$}" _epcra_rspec_at="${2#\$}"	\
		_epcra_len=0 _epcra_rc=0 _epcra_spec_at="";

	if ! rtl_llift2 "${_epcra_rspec_at}" \$_epcra_spec_at "," " "\
	|| ! rtl_lfilter \$_epcra_spec_at "${DEFAULT_BUILD_STEPS} ALL LAST"\
	|| ! rtl_llength \$_epcra_spec_at \$_epcra_len; then
		_epcra_rc=1;
	elif [ "${_epcra_len}" -gt 0 ]; then
		_epcra_rc=1;
		rtl_setrstatus "${_epcra_rstatus}" 'unknown build step(s) \`'"${_epcra_spec_at}"''\''';
	fi;

	return "${_epcra_rc}";
};

#
# exp_pkg_expand_restart_at_spec() - expand list of virtual build steps and list of build steps into list of build steps w/ operator subspecifications and virtual build steps expanded
# @_rstatus:		out reference to status string
# @_rset:		in reference to restart virtual build step set
# @_rspec_at:		inout reference to restart build step list
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_expand_restart_at_spec() {
	local	_eperas_rstatus="${1#\$}" _eperas_rset="${2#\$}" _eperas_rspec_at="${3#\$}"	\
		_eperas_at="" _eperas_rc=0 _eperas_spec_at="" _eperas_spec_at_="";
	eval _eperas_spec_at='${'"${_eperas_rspec_at}"'}';

	case "${_eperas_spec_at}" in
	ALL|LAST|"")
		;;

	^*)
		_eperas_spec_at="${_eperas_spec_at#^}";
		if exp_pkg_expand_restart_at_virtual			\
				"${_eperas_rstatus}"			\
				"${_eperas_spec_at}" \$_eperas_spec_at	\
				"${_eperas_rset}"			\
		&& exp_pkg_check_restart_at "${_eperas_rstatus}" \$_eperas_spec_at;
		then
			rtl_llift2 \$DEFAULT_BUILD_STEPS "${_eperas_rspec_at}" " 	" ",";
			rtl_llift2 \$_eperas_spec_at \$_eperas_spec_at_ "," " ";
			for _eperas_at in ${_eperas_spec_at_}; do
				rtl_lfilter "${_eperas_rspec_at}" "${_eperas_at}" ",";
			done; rtl_lfilter "${_eperas_rspec_at}" "finish" ",";
		else
			_eperas_rc=1;
		fi; ;;

	\<=*|\<*|\>=*|\>*)
		exp_pkg_expand_restart_at_spec_cmp	\
			"${_eperas_rstatus}"		\
			"${_eperas_rset}" "${_eperas_rspec_at}";
		_eperas_rc="${?}"; ;;

	*)
		if ! exp_pkg_expand_restart_at_virtual				\
				"${_eperas_rstatus}"				\
				"${_eperas_spec_at}" "${_eperas_rspec_at}"	\
				"${_eperas_rset}"; then
			_eperas_rc=1;
		fi; ;;
	esac;

	if [ "${_eperas_rc}" -eq 0 ]; then
		if ! exp_pkg_check_restart_at "${_eperas_rstatus}" "${_eperas_rspec_at}"; then
			_epprs_rc=1;
		elif eval [ '"${'"${_eperas_rspec_at}"':+1}"' != 1 ]; then
			_epprs_rc=1;
			rtl_setrstatus "${_eperas_rstatus}" 'zero-length build step list';
		fi;
	fi;

	return "${_eperas_rc}";
};

#
# exp_pkg_expand_restart_at_spec_cmp() - expand list of virtual build steps and list of build steps into list of build steps w/ operator subspecifications expanded
# @_rstatus:		out reference to status string
# @_rset:		in reference to restart virtual build step set
# @_rspec_at:		inout reference to restart build step list
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_expand_restart_at_spec_cmp() {
	local	_eperasc_rstatus="${1#\$}" _eperasc_rset="${2#\$}" _eperasc_rspec_at="${3#\$}"		\
		_eperasc_at="" _eperasc_eqfl="" _eperasc_foundfl="" _eperasc_ltfl="" _eperasc_rc=0	\
		_eperasc_spec_at="" _eperasc_spec_at0="";
	eval _eperasc_spec_at0='${'"${_eperasc_rspec_at}"'}';

	[ "${_eperasc_spec_at0#<}" = "${_eperasc_spec_at0}" ]; _eperasc_ltfl="${?}";
	if [ "${_eperasc_spec_at0#[<>]=}" != "${_eperasc_spec_at0}" ]; then
		_eperasc_spec_at0="${_eperasc_spec_at0#[<>]=}"; _eperasc_eqfl=1;
	else
		_eperasc_spec_at0="${_eperasc_spec_at0#[<>]}"; _eperasc_eqfl=0;
	fi; _eperasc_spec_at="";

	if exp_pkg_expand_restart_at_virtual				\
			"${_eperasc_rstatus}"				\
			"${_eperasc_spec_at0%%,*}" \$_eperasc_spec_at0	\
			"${_eperasc_rset}"				\
	&& exp_pkg_check_restart_at "${_eperasc_rstatus}" \$_eperasc_spec_at0; then
		if [ "${_eperasc_eqfl}" -eq 1 -a "${_eperasc_ltfl}" -eq 1 ]\
		|| [ "${_eperasc_eqfl}" -eq 0 -a "${_eperasc_ltfl}" -eq 0 ]; then
			_eperasc_spec_at0="${_eperasc_spec_at0##*,}";
		elif [ "${_eperasc_eqfl}" -eq 1 -a "${_eperasc_ltfl}" -eq 0 ]\
		||   [ "${_eperasc_eqfl}" -eq 0 -a "${_eperasc_ltfl}" -eq 1 ]; then
			_eperasc_spec_at0="${_eperasc_spec_at0%%,*}";
		fi;

		_eperasc_foundfl=0; for _eperasc_at in ${DEFAULT_BUILD_STEPS}; do
			if [ "${_eperasc_ltfl}" -eq 1 ]; then
				if [ "${_eperasc_at}" = "${_eperasc_spec_at0%%,*}" ]; then
					if [ "${_eperasc_eqfl}" -eq 1 ]; then
						_eperasc_spec_at="${_eperasc_spec_at:+${_eperasc_spec_at},}${_eperasc_at}";
					fi; break;
				fi;
			else
				if [ "${_eperasc_at}" = "${_eperasc_spec_at0%%,*}" ]; then
					_eperasc_foundfl=1; [ "${_eperasc_eqfl}" -eq 0 ] && continue;
				fi; [ "${_eperasc_foundfl}" -eq 0 ] && continue;
			fi;
			_eperasc_spec_at="${_eperasc_spec_at:+${_eperasc_spec_at},}${_eperasc_at}";
		done;
	else
		_eperasc_rc=1;
	fi;

	eval ${_eperasc_rspec_at}='${_eperasc_spec_at}';
	return "${_eperasc_rc}";
};

#
# exp_pkg_expand_restart_at_virtual() - expand list of build steps into list of build steps w/ expansion of virtual build steps
# @_rstatus:		out reference to status string
# @_spec_at:		restart build step list
# @_rspec_at_new:	out reference to new restart build step list
# @_rset:		in reference to restart virtual build step set
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_expand_restart_at_virtual() {
	local	_epera_rstatus="${1#\$}" _epera_spec_at="${2}" _epera_rspec_at_new="${3#\$}"	\
		_epera_rset="${4#\$}"								\
		_epera_at="" _epera_IFS0="${IFS:- 	}" _epera_rc=0 _epera_spec_at_new=""	\
		IFS;

	eval ${_epera_rspec_at_new}=;
	IFS=","; set -- ${_epera_spec_at}; IFS="${_epera_IFS0}";
	while [ "${#}" -gt 0 ]; do
		_epera_at="${1}"; shift;
		if [ "${_epera_at#@}" != "${_epera_at}" ]; then
			_epera_at="${_epera_at#@}";
			if [ "${_epera_at%[!0-9a-zA-Z_]*}" != "${_epera_at}" ]; then
				_epera_rc=1;
				rtl_setrstatus "${_epera_rstatus}" 'invalid virtual build step \`'"${_epera_at}"''\''';
			elif eval [ '"${'"${_epera_rset}${_epera_at}"':+1}"' = 1 ]; then
				eval _epera_at='${'"${_epera_rset}${_epera_at}"'}';
			else
				_epera_rc=1;
				rtl_setrstatus "${_epera_rstatus}" 'unknown virtual build step \`'"${_epera_at}"''\''';
			fi;
		fi;
		eval ${_epera_rspec_at_new}='${'"${_epera_rspec_at_new}"':+${'"${_epera_rspec_at_new}"'},}${_epera_at}';
	done;

	return "${_epera_rc}";
};

#
# exp_pkg_expand_restart_recursive() - expand restart (-r) specification into recursion flag
# @_rspec:		inout reference to restart {specification,package name list}
# @_rrecursive:		out reference to recursion flag
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_expand_restart_recursive() {
	local _eperr_rspec="${1#\$}" _eperr_rrecursive="${2#\$}" _eperr_spec="";
	eval _eperr_spec='${'"${_eperr_rspec}"'}';

	case "${_eperr_spec}" in

	\*\*\*[a-zA-Z]*)
			eval ${_eperr_rspec}='${_eperr_spec#\*\*\*}' ${_eperr_rrecursive}=3; ;;
	\*\*[a-zA-Z]*)	eval ${_eperr_rspec}='${_eperr_spec#\*\*}' ${_eperr_rrecursive}=2; ;;
	\*[a-zA-Z]*)	eval ${_eperr_rspec}='${_eperr_spec#\*}' ${_eperr_rrecursive}=1; ;;
	ALL)		eval ${_eperr_rrecursive}=2; ;;
	LAST)		eval ${_eperr_rrecursive}=0; ;;

	esac;

	return 0;
};

#
# exp_pkg_expand_restart_spec() - expand restart (-r) specification into list of build steps
# @_rstatus:		out reference to status string
# @_rspec:		inout reference to restart {specification,package name list}
# @_rspec_at:		out reference to restart build step list
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_expand_restart_spec() {
	local	_epers_rstatus="${1#\$}" _epers_rspec="${2#\$}" _epers_rspec_at="${3#\$}"	\
		_epers_last_pkg="" _epers_rc=0 _epers_spec="" _epers_spec_at="" _epers_spec_at0="";
	eval _epers_spec='${'"${_epers_rspec}"'}';

	case "${_epers_spec}" in
	"")	eval ${_epers_rspec_at}=; ;;
	ALL)	eval ${_epers_rspec_at}=ALL; ;;
	LAST|LAST:*)
		case "${_epers_spec}" in
		LAST)	eval ${_epers_rspec_at}=LAST; ;;
		LAST:*)	eval ${_epers_rspec_at}='${_epers_spec#LAST:}'; ;;
		esac;
		if [ "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME:+1}" = 1 ]\
		&& [ -e "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]; then
			if read -r _epers_last_pkg <"${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}"\
			&& rtl_fileop rm "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}"; then
				eval ${_epers_rspec}='${_epers_last_pkg}';
			else
				_epers_rc=1;
				rtl_setrstatus "${_epers_rstatus}" 'failed to read or clear status of last failed package \`'"${_epers_last_pkg}"''\''';
			fi;
		else
			_epers_rc=1;
			rtl_setrstatus "${_epers_rstatus}" 'cannot rebuild last failed package';
		fi; ;;

	*:*)	eval ${_epers_rspec}='${_epers_spec%:*}' ${_epers_rspec_at}='${_epers_spec#*:}'; ;;
	*)	eval ${_epers_rspec_at}= ${_epers_rspec_at}=ALL; ;;
	esac;

	return "${_epers_rc}";
};

#
# exp_pkg_init_restart_at_virtual() - produce list of virtual build step set in default build steps
# @_rset:		out reference to restart virtual build step set
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_init_restart_at_virtual() {
	local _eperav_rset="${1#\$}" _eperav_step="" _eperav_step_virtual="" _epera_steps="";

	rtl_lfilter2 \$DEFAULT_BUILD_STEPS \$_epera_steps "finish";
	for _eperav_step in ${_epera_steps}; do
		_eperav_step_virtual="${_eperav_step%%_*}";
		if eval [ '"${'"${_eperav_rset}${_eperav_step_virtual}"':+1}"' != 1 ]; then
			eval ${_eperav_rset}='"${'"${_eperav_rset}"':+${'"${_eperav_rset}"'},}${_eperav_step_virtual}"';
		fi;
		eval ${_eperav_rset}${_eperav_step_virtual}='"${'"${_eperav_rset}${_eperav_step_virtual}"':+${'"${_eperav_rset}${_eperav_step_virtual}"'},}${_eperav_step}"';
	done;

	return 0;
};

#
# ex_pkg_process_restart_spec() - process restart (-r) specification into list of build steps and recursion flag
# @_rstatus:		out reference to status string
# @_rspec:		inout reference to restart {specification,package name list}
# @_rspec_at:		out reference to restart build step list
# @_rrecursive:		out reference to restart recursion flag
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
ex_pkg_process_restart_spec() {
	local	_epprs_rstatus="${1#\$}" _epprs_rspec="${2#\$}" _epprs_rspec_at="${3#\$}"	\
		_epprs_rrecursive="${4#\$}"							\
		_epprs_at="" _epprs_rc=0 _epprs_spec_at_new="" _epprs_step="" _epprs_step1=""	\
		_epprs_virtual_set="";

	if eval [ '"${'"${_epprs_rspec}"':+1}"' = 1 ]; then
		if exp_pkg_init_restart_at_virtual \$_epprs_virtual_set						\
		&& exp_pkg_expand_restart_spec "${_epprs_rstatus}" "${_epprs_rspec}" \$_epprs_spec_at_new	\
		&& exp_pkg_expand_restart_recursive "${_epprs_rspec}" "${_epprs_rrecursive}"			\
		&& exp_pkg_expand_restart_at_spec "${_epprs_rstatus}" \$_epprs_virtual_set \$_epprs_spec_at_new;
		then

			eval ${_epprs_rspec_at}=;
			case "${_epprs_spec_at_new}" in

			ALL|LAST)
				eval ${_epprs_rspec_at}='${_epprs_spec_at_new}'; ;;

			*)
				for _epprs_at in ${DEFAULT_BUILD_STEPS}; do
					if rtl_lmatch \$_epprs_at "${_epprs_spec_at_new}" ","; then
						eval ${_epprs_rspec_at}='${'"${_epprs_rspec_at}"':+${'"${_epprs_rspec_at}"'},}${_epprs_at}';
					fi;
				done;
				if eval [ '"${'"${_epprs_rspec_at}"'##*,}"' != "finish" ]; then
					rtl_lfilter2 "${_epprs_rspec_at}" \$_epprs_step "clean,finish" ","; _epprs_step="${_epprs_step##*,}";
					rtl_lfilter2 \$DEFAULT_BUILD_STEPS \$_epprs_step1 "clean finish"; _epprs_step1="${_epprs_step1##* }";
					if [ "${_epprs_step}" = "${_epprs_step1}" ]; then
						eval ${_epprs_rspec_at}='${'"${_epprs_rspec_at}"':+${'"${_epprs_rspec_at}"'},}finish';
					fi;
				fi; ;;

			esac;
			rtl_llift "${_epprs_rspec}" "," " " || _epprs_rc=1;
		else
			_epprs_rc=1;
		fi;
		rtl_sunset \$_epprs_virtual_set;
	fi;

	return "${_epprs_rc}";
};

# vim:filetype=sh textwidth=0