summaryrefslogtreecommitdiffstats
path: root/lib/package.sh
blob: 7bfec63dad5fa15975d1233224bd055cefc9fa80 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# opkbuild
# lib/package
# Interface for retrieving source and binary package metadata.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

[ -n "${_OB_PACKAGE_SM}" ] && return 0
_OB_PACKAGE_SM='true'

ob_use metadata

_OB_PACKAGE_DIR=
_OB_PACKAGE_FORMAT=
_OB_BINARY_PACKAGES=
_OB_SOURCE_PARAMETERS=
_OB_BINARY_PARAMETERS=

ob_init_package()
{
	_ob_local _obip_dir _obip_orig_text_domain

	if [ ${#} -eq 1 ]; then
		_obip_dir="${1}"
	else
		_ob_return 125
		return ${?}
	fi

	_OB_PACKAGE_DIR="$(cd "${_obip_dir}" && pwd)"

	if [ -r "${_OB_PACKAGE_DIR}/format" ]; then
		case "$(cat "${_OB_PACKAGE_DIR}/format")" in
			2.0)
				_OB_PACKAGE_FORMAT='2'
				;;
		esac
	fi

	if [ -z "${_OB_PACKAGE_FORMAT}" ]; then
		_obip_orig_text_domain="$(ob_get_text_domain)"
		ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}"
		ob_warn "$(ob_get_msg 'unable_to_detect_package_format')"
		ob_set_text_domain "${_obip_orig_text_domain}"
		_ob_return 1
		return ${?}
	fi

	ob_use "package/${_OB_PACKAGE_FORMAT}"

	_OB_BINARY_PACKAGES=

	_ob_return 0
	return ${?}
}

ob_parse_package_metadata()
{
	_ob_local _obppm_opt _obppm_cache_file _obppm_pkg _obppm_pkg_clean \
		_obppm_param_name _obppm_param_value

	while getopts 'c:' _obppm_opt; do
		case "${_obppm_opt}" in
			c)
				_obppm_cache_file="${OPTARG}"
				;;
			?)
				_ob_return 125
				return ${?}
				;;
		esac
	done
	shift $(($OPTIND - 1))

	if [ ${#} -gt 0 ]; then
		_ob_return 125
		return ${?}
	fi

	# Load a previously saved cache file, if any.
	if [ -n "${_obppm_cache_file}" -a -r "${_obppm_cache_file}" ]; then
		if [ "${_obppm_cache_file#*/}" != "${_obppm_cache_file}" ]; then
			. "${_obppm_cache_file}"
		else
			. "./${_obppm_cache_file}"
		fi
		_ob_return 0
		return ${?}
	fi

	_ob_package_do 'parse_package_metadata'

	if [ -n "${_obppm_cache_file}" ]; then
		>"${_obppm_cache_file}"
		for _obppm_param_name in ${_OB_SOURCE_PARAMETERS}; do
			_obppm_param_value="$(ob_get_source_parameter \
				"${_obppm_param_name}" | sed "s/'/'\\\\''/g")"
			printf "%s='%s'\n" \
				"_OB_SRCFIELD_${_obppm_param_name}" "${_obppm_param_value}" \
				>>"${_obppm_cache_file}"
		done
		for _obppm_pkg in $(ob_get_binary_packages); do
			_obppm_pkg_clean="$(echo "${_obppm_pkg}" | tr 'a-z' 'A-Z' | \
				tr -dC 'A-Z0-9')"
			for _obppm_param_name in ${_OB_BINARY_PARAMETERS}; do
				_obppm_param_value="$(ob_get_binary_parameter "${_obppm_pkg}" \
					"${_obppm_param_name}" | sed "s/'/'\\\\''/g")"
				printf "%s='%s'\n" \
					"_OB_BINFIELD_${_obppm_pkg_clean}_${_obppm_param_name}" \
					"${_obppm_param_value}" \
					>>"${_obppm_cache_file}"
			done
		done
	fi

	_ob_return 0
	return ${?}
}

ob_get_binary_packages()
{
	_ob_local _obgbpk_opt _obgbpk_host_arch _obgbpk_pkgs _obgbpk_pkg \
		_obgbpk_pkgs_clean _obgbpk_pkg_clean _obgbpk_orig_text_domain

	while getopts 'a:' _obgbpk_opt; do
		case "${_obgbpk_opt}" in
			a)
				_obgbpk_host_arch="${OPTARG}"
				;;
			?)
				_ob_return 125
				return ${?}
				;;
		esac
	done
	shift $(($OPTIND - 1))

	if [ ${#} -gt 0 ]; then
		_ob_return 125
		return ${?}
	fi

	# NB: If a source package lists no binary packages, this will be true each
	# call.
	if [ -z "${_OB_BINARY_PACKAGES}" ]; then

		_ob_package_do 'get_binary_packages'

		_obgbpk_orig_text_domain="$(ob_get_text_domain)"
		ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}"

		_obgbpk_pkgs_clean=' '

		for _obgbpk_pkg in ${_OB_BINARY_PACKAGES}; do

			# Validate the name.
			if ! ob_validate_binary_name "${_obgbpk_pkg}"; then
				ob_warn 'bad_binary_name'
				continue
			fi

			# Make sure the "clean" name is unique.
			_obgbpk_pkg_clean=" $(echo "${_obgbpk_pkg}" | tr 'a-z' 'A-Z' | \
				tr -dC 'A-Z0-9') "
			if echo "${_obgbpk_pkgs_clean}" | grep "${_obgbpk_pkg_clean}" \
					>/dev/null 2>&1; then
				ob_warn 'duplicate_clean_binary_name'
				continue
			else
				_obgbpk_pkgs_clean="${_obgbpk_pkgs_clean}${_obgbpk_pkg_clean} "
			fi

			_obgbpk_pkgs="${_obgbpk_pkgs} ${_obgbpk_pkg}"

		done

		_OB_BINARY_PACKAGES="$(IFS=' ' echo ${_obgbpk_pkgs})"

		ob_set_text_domain "${_obgbpk_orig_text_domain}"

	fi

	if [ -n "${_obgbpk_host_arch}" ]; then
		for _obgbpk_pkg in ${_OB_BINARY_PACKAGES}; do
			if ob_match_arch "${_obgbpk_host_arch}" "$(ob_get_binary_parameter \
					"${_obgbpk_pkg}" 'Architecture')"; then
				_obgbpk_pkgs="${_obgbpk_pkgs} ${_obgbpk_pkg}"
			fi
		done
	else
		_obgbpk_pkgs="${_OB_BINARY_PACKAGES}"
	fi

	IFS=' ' echo ${_obgbpk_pkgs}

	_ob_return 0
	return ${?}
}

ob_get_source_parameter()
{
	_ob_local _obgsp_name

	if [ ${#} -eq 1 ]; then
		_obgsp_name="${1}"
	else
		_ob_return 125
		return ${?}
	fi

	# Convert field name to uppercase and validate.
	_obgsp_name="$(echo "${_obgsp_name}" | tr 'a-z-' 'A-Z_')"
	case "${_obgsp_name:- }" in
		*[!A-Z0-9_]*)
			_ob_return 125
			return ${?}
			;;
	esac

	eval echo \"\$\{"_OB_SRCFIELD_${_obgsp_name}"\}\"

	_ob_return 0
	return ${?}
}

ob_get_binary_parameter()
{
	_ob_local _obgbp_package _obgbp_name

	if [ ${#} -eq 2 ]; then
		_obgbp_package="${1}"
		_obgbp_name="${2}"
	else
		_ob_return 125
		return ${?}
	fi

	if ! ob_validate_binary_name "${_obgbp_package}"; then
		_ob_return 125
		return ${?}
	fi

	# Convert package name to its uppercase "clean" form.
	_obgbp_package="$(echo "${_obgbp_package}" | tr 'a-z' 'A-Z' | \
		tr -dC 'A-Z0-9')"

	# Convert field name to uppercase and validate.
	_obgbp_name="$(echo "${_obgbp_name}" | tr 'a-z-' 'A-Z_')"
	case "${_obgbp_name:- }" in
		*[!A-Z0-9_]*)
			_ob_return 125
			return ${?}
			;;
	esac

	eval echo \"\$\{"_OB_BINFIELD_${_obgbp_package}_${_obgbp_name}"\}\"

	_ob_return 0
	return ${?}
}

ob_summarize_package_arch()
{
	_ob_local

	if [ ${#} -gt 0 ]; then
		_ob_return 125
		return ${?}
	fi

	# XXX: Stub.

	_ob_return 0
	return ${?}
}

ob_summarize_package_plat()
{
	_ob_local

	if [ ${#} -gt 0 ]; then
		_ob_return 125
		return ${?}
	fi

	# XXX: Stub.

	_ob_return 0
	return ${?}
}

_ob_package_do()
{
	_ob_local _obpd_func

	_obpd_func="${1}"
	shift 1

	"_ob_${_obpd_func}_${_OB_PACKAGE_FORMAT}" "${@}"

	_ob_return ${?}
	return ${?}
}

_ob_set_binary_packages()
{
	_ob_local _obsbpk_packages

	_obsbpk_packages="${1}"

	_OB_BINARY_PACKAGES="${_obsbpk_packages}"

	_ob_return 0
	return ${?}
}

_ob_set_source_parameter()
{
	_ob_local _obssp_name _obssp_value

	_obssp_name="${1}"
	_obssp_value="${2}"

	# Convert field name to uppercase and validate.
	_obssp_name="$(echo "${_obssp_name}" | tr 'a-z-' 'A-Z_')"
	case "${_obssp_name:- }" in
		*[!A-Z0-9_]*)
			_ob_return 125
			return ${?}
			;;
	esac

	_OB_SOURCE_PARAMETERS="${_OB_SOURCE_PARAMETERS} ${_obssp_name}"

	# Escape the value.
	_obssp_value="$(echo "${_obssp_value}" | sed "s/'/'\\\\''/g")"

	eval "_OB_SRCFIELD_${_obssp_name}='${_obssp_value}'"

	_ob_return 0
	return ${?}
}

_ob_set_binary_parameter()
{
	_ob_local _obsbp_package _obsbp_name _obsbp_value

	_obsbp_package="${1}"
	_obsbp_name="${2}"
	_obsbp_value="${3}"

	if ! ob_validate_binary_name "${_obsbp_package}"; then
		_ob_return 125
		return ${?}
	fi

	# Convert package name to its uppercase "clean" form.
	_obsbp_package="$(echo "${_obsbp_package}" | tr 'a-z' 'A-Z' | \
		tr -dC 'A-Z0-9')"

	# Convert field name to uppercase and validate.
	_obsbp_name="$(echo "${_obsbp_name}" | tr 'a-z-' 'A-Z_')"
	case "${_obsbp_name:- }" in
		*[!A-Z0-9_]*)
			_ob_return 125
			return ${?}
			;;
	esac

	if ! echo " ${_OB_BINARY_PARAMETERS} " | grep " ${_obsbp_name} " \
			>/dev/null 2>&1; then
		_OB_BINARY_PARAMETERS="${_OB_BINARY_PARAMETERS} ${_obsbp_name}"
	fi

	# Escape the value.
	_obsbp_value="$(echo "${_obsbp_value}" | sed "s/'/'\\\\''/g")"

	eval "_OB_BINFIELD_${_obsbp_package}_${_obsbp_name}='${_obsbp_value}'"

	_ob_return 0
	return ${?}
}