summaryrefslogtreecommitdiffstats
path: root/scripts/lib/ptxd_make_get.sh
blob: 24d0cc75acb1b632bdbe657b6f00e62fbce7bfc1 (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
395
396
397
#!/bin/bash
#
# Copyright (C) 2008, 2009 by Marc Kleine-Budde <mkl@pengutronix.de>
#
# See CREDITS for details about who has contributed to this project.
#
# For further information about the PTXdist project and license conditions
# see the README file.
#

#
# in env:
#
# ${path}	: local file name
# ${url}	: the url to download
# ${opts[]}	: an array of options
#
ptxd_make_get_http() {
	set -- "${opts[@]}"
	unset opts

	#
	# scan for valid options
	#
	while [ ${#} -ne 0 ]; do
		local opt="${1}"
		shift

		case "${opt}" in
			no-check-certificate|no-proxy)
				opts[${#opts[@]}]="--${opt}"
				;;
			cookie:*)
				opts[${#opts[@]}]="--no-cookies"
				opts[${#opts[@]}]="--header"
				opts[${#opts[@]}]="Cookie: ${opt#cookie:}"
				;;
			*)
				ptxd_bailout "invalid option '${opt}' to ${FUNCNAME}"
				;;
		esac
	done
	unset opt

	#
	# download to temporary file first, move it to correct
	# file name after successfull download
	#
	local file="${url##*/}"

	# remove any pending or half downloaded files
	rm -f -- "${path}."*

	local temp_file="$(mktemp "${path}.XXXXXXXXXX")" || ptxd_bailout "failed to create tempfile"
	ptxd_make_serialize_take
	wget \
	    --passive-ftp \
	    --progress=bar:force \
	    --timeout=30 \
	    --tries=5 \
	    --user-agent="PTXdist ${PTXDIST_VERSION_FULL}" \
	    ${PTXDIST_QUIET:+--quiet} \
	    "${opts[@]}" \
	    -O "${temp_file}" \
	    "${url}" && {
		chmod 644 -- "${temp_file}" &&
		file "${temp_file}" | grep -vq " HTML " &&
		touch -- "${temp_file}" &&
		mv -- "${temp_file}" "${path}"
		ptxd_make_serialize_put
		return
	}
	ptxd_make_serialize_put

	rm -f -- "${temp_file}"

	# return with failure, we didn't manage to download the file
	return 1
}
export -f ptxd_make_get_http


#
# in env:
#
# ${path}	: local file name
# ${url}	: the url to download
# ${opts[]}	: an array of options
#
ptxd_make_get_git() {
	set -- "${opts[@]}"
	unset opts
	local tag
	local mirror="${url#[a-z]*//}"
	mirror="$(dirname "${path}")/${mirror//\//.}"
	local prefix="$(basename "${path}")"
	prefix="${prefix%.tar.*}/"

	case "${path}" in
	*.tar.gz|*.tar.bz2|*.tar.xz|*.zip)
		;;
	*)
		ptxd_bailout "Only .tar.gz, .tar.bz2, .tar.xz and .zip archives are supported for git downloads."
		;;
	esac

	#
	# scan for valid options
	#
	while [ ${#} -ne 0 ]; do
		local opt="${1}"
		shift

		case "${opt}" in
			tag=*)
				tag="${opt#tag=}"
				;;
			*)
				ptxd_bailout "invalid option '${opt}' to ${FUNCNAME}"
				;;
		esac
	done
	unset opt

	if [ -z "${tag}" ]; then
		ptxd_bailout "git url '${url}' has no 'tag' option"
	fi

	ptxd_make_serialize_take
	echo "${PROMPT}git: fetching '${url} into '${mirror}'..."
	if [ ! -d "${mirror}" ]; then
		git init --bare --shared "${mirror}"
	else
		git --git-dir="${mirror}" remote rm origin
	fi
	# overwrite everything so the git repository is in a defined state
	git --git-dir="${mirror}" config transfer.fsckObjects true &&
	git --git-dir="${mirror}" config tar.tar.bz2.command "bzip2 -c" &&
	git --git-dir="${mirror}" config tar.tar.xz.command "xz -c"
	git --git-dir="${mirror}" remote add origin "${url}" &&
	git --git-dir="${mirror}" fetch --progress -pf origin "+refs/*:refs/*"  &&
	# at least for some git versions this is not group writeable for shared repos
	chmod g+w "${mirror}/FETCH_HEAD"

	if ! git --git-dir="${mirror}" rev-parse --verify -q "${tag}" > /dev/null; then
		ptxd_make_serialize_put
		ptxd_bailout "git: tag '${tag}' not found in '${url}'"
	fi &&

	git --git-dir="${mirror}" archive --prefix="${prefix}" -o "${path}" "${tag}"
	ptxd_make_serialize_put
}
export -f ptxd_make_get_git


#
# in env:
#
# ${path}	: local file name
# ${url}	: the url to download
# ${opts[]}	: an array of options
#
ptxd_make_get_svn() {
	set -- "${opts[@]}"
	unset opts
	local rev
	local tarcomp
	local mirror="${url#[a-z]*//}"
	mirror="$(dirname "${path}")/${mirror//\//.}"
	local prefix="$(basename "${path}")"
	prefix="${prefix%.tar.*}"

	case "${path}" in
	*.tar.gz)
		tarcomp="--gzip"
		;;
	*.tar.bz2)
		tarcomp="--bzip2"
		;;
	*.tar.xz)
		tarcomp="--xz"
		;;
	*)
		ptxd_bailout "Only .tar.gz, .tar.bz2, .tar.xz and archives are supported for svn downloads."
		;;
	esac

	#
	# scan for valid options
	#
	while [ ${#} -ne 0 ]; do
		local opt="${1}"
		shift

		case "${opt}" in
			rev=*)
				rev="${opt#rev=}"
				;;
			*)
				ptxd_bailout "invalid option '${opt}' to ${FUNCNAME}"
				;;
		esac
	done
	unset opt

	if [ -z "${rev}" ]; then
		ptxd_bailout "svn url '${url}' has no 'rev' option"
	fi

	ptxd_make_serialize_take
	echo "${PROMPT}svn: fetching '${url} into '${mirror}'..."
	if [ ! -d "${mirror}" ]; then
		svn checkout -r ${rev} "${url}" "${mirror}"
	else
		svn update -r ${rev} "${mirror}"
	fi &&
	lmtime=$(svn info -r ${rev} "${mirror}" | \
		awk '/^Last Changed Date:/ {print $4 " " $5 " " $6}') &&
	echo "${PROMPT}svn: last modification time '${lmtime}'" &&
	tar --exclude-vcs --show-stored-names ${tarcomp} \
		--mtime="${lmtime}" --transform "s|^\.|${prefix}|g" \
		--create --file "${path}" -C "${mirror}" .
	ptxd_make_serialize_put
}
export -f ptxd_make_get_svn


#
# check if download is disabled
#
# in env:
#
# ${url}	: the url to download
#
ptxd_make_get_download_permitted() {
	if [ -n "${PTXCONF_SETUP_NO_DOWNLOAD}" -a -z "${PTXDIST_FORCE_DOWNLOAD}" ]; then {
		cat <<EOF

error: automatic download prohibited

Please download '${url}'
manually into '$(dirname ${path})'

EOF
		set -- ${orig_argv[@]}
		if [ $# -ne 1 ]; then
			echo "If this URL doesn't work, you may try these ones:"
			while [ ${#} -ne 0 ]; do
				[ "${1}" != "${url}" ] && echo "'${1}'"
				shift
			done
			echo
		fi
		exit 1; } >&2
	fi
}
export -f ptxd_make_get_download_permitted


#
# $1: target source path (including file name)
# $@: possible download URLs, seperated by space
#
# options seperated from URLs by ";"
#
# valid options:
# - no-check-certificate	don't check server certificate (https only)
# - no-proxy			don't use proxy even if configured
#
ptxd_make_get() {
	local -a argv
	local ptxmirror_url

	local path="${1}"
	shift

	local -a orig_argv
	orig_argv=( "${@}" )

	if [ -z "${1}" ]; then
		echo
		echo "${PROMPT}error: empty parameter to '${FUNCNAME}'"
		echo
		exit 1
	fi

	ptxmirror_url="${path/#\/*\//${ptxd_make_get_mirror}/}"

	#
	# split by spaces, etc
	#
	set -- ${@}

	while [ ${#} -gt 0 ]; do
		local add=true
		local url="${1}"
		shift

		if [[ "${url}" =~ "file://" ]]; then
			# keep original URL
			argv[${#argv[@]}]="${url}"
			# assume, that local URLs are always available
			ptxmirror_url=
			continue
		fi
		# restrict donwload only to white-listed URLs
		if [ -n "${PTXCONF_SETUP_PTXMIRROR_ONLY}" ]; then
			local pattern
			add=false
			for pattern in "${ptxd_make_get_mirror}" \
					${PTXCONF_SETUP_URL_WHITELIST}; do
				if [[ "${url}" =~ "${pattern}" ]]; then
					add=true
					break
				fi
			done
		fi
		if ${add}; then
			argv[${#argv[@]}]="${url}"
			if [ "${url}" = "${ptxmirror_url}" ]; then
				# avoid duplicates
				ptxmirror_url=
			fi
		fi
	done
	if [ -n "${ptxmirror_url}" ]; then
		argv[${#argv[@]}]="${ptxmirror_url}"
	fi

	set -- "${argv[@]}"

	while [ ${#} -ne 0 ]; do
		#
		# strip options which are seperated by ";" form the
		# URL, store in "opts" array
		#
		local orig_ifs="${IFS}"
		IFS=";"
		local -a opts
		opts=( ${1} )
		IFS="${orig_ifs}"
		unset orig_ifs

		local url="${opts[0]}"
		unset opts[0]

		shift

		case "${url}" in
		git://*|http://*.git|https://*.git|ssh://*.git)
			ptxd_make_get_download_permitted &&
			ptxd_make_get_git && return
			;;
		svn://*)
			ptxd_make_get_download_permitted &&
			ptxd_make_get_svn && return
			;;
		http://*|https://*|ftp://*)
			ptxd_make_get_download_permitted &&
			ptxd_make_get_http && return
			;;
		file*)
			local thing="${url/file:\/\///}"

			if [ -f "$thing" ]; then
				echo "local archive, skiping get"
				return
			elif [ -d "${thing}" ]; then
				echo "local directory instead of tar file, skipping get"
				return
			else
				thing="${url/file:\/\//./}"
				if [ -d "${thing}" ]; then
					echo "local project directory instead of tar file, skipping get"
					return
				fi
			fi
			;;
		*)
			echo
			echo "Unknown URL Type!"
			echo "URL: $url"
			echo
			exit 1
			;;
		esac
	done

	if [ "${ptxd_make_get_nofail}" != "y" ]; then
		echo
		echo "Could not download package"
		echo "URL: ${orig_argv[@]}"
		echo
		exit 1
	fi
}
export -f ptxd_make_get