Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pkgupdate.sh - github.com/openwrt/buildscripts.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 532be3e944189bf8e5f5de00249ee851c41edc32 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/bash


#U="https://downloads.openwrt.org/barrier_breaker/14.07"
U="file:///BB/sync/barrier_breaker/14.07"
R="user@remote:/foo/barrier_breaker/14.07"
T="$(pwd)"

tmp="/home/jow/.relman"

mkdir -p "$tmp"

terminate() {
	local jobs="$(jobs -p)"

	echo "Terminating..."

	pkill -P $$
	exit 1
}

fetch_remote() {
	if [ -f "${1#file:}" ]; then
		cat "${1#file:}"
	else
		wget -qO- "$1"
	fi
}

fetch_remote_dirlist() {
	local entry

	if [ -d "${1#file:}" ]; then
		/bin/ls -1 "${1#file:}" | while read entry; do
			if [ -d "${1#file:}/$entry" ]; then
				echo "$entry"
			fi
		done
	else
		wget -qO- "$1" | sed -ne 's,^<a href="\(.\+\)/".\+$,\1,p'
	fi
}

fetch_remote_filelist() {
	local entry

	if [ -d "${1#file:}" ]; then
		/bin/ls -1 "${1#file:}" | while read entry; do
			if [ -f "${1#file:}/$entry" ]; then
				echo "$entry"
			fi
		done
	else
		wget -qO- "$1" | sed -ne 's,^<a href="\(.\+[^/]\)".\+$,\1,p'
	fi
}

fetch_remote_targets() {
	local target subtarget

	if [ -n "$use_targets" ]; then
		for target in $use_targets; do
			echo "$target"
		done
		return 0
	fi

	if [ ! -s "$tmp/targets.lst" ]; then
		fetch_remote_dirlist "$U" | while read target; do
			[ "$target" = "logs" ] && continue
			fetch_remote_dirlist "$U/$target" | while read subtarget; do
				echo "$target/$subtarget" >> "$tmp/targets.lst"
			done
		done
	fi

	cat "$tmp/targets.lst"
	return 0
}

fetch_remote_feeds() {
	local target

	if [ ! -s "$tmp/feeds.lst" ]; then
		fetch_remote_targets | while read target; do
			fetch_remote_dirlist "$U/$target/packages" > "$tmp/feeds.lst"
			break
		done
	fi

	cat "$tmp/feeds.lst"
	return 0
}

fetch_remote_index() {
	local target feed

	echo "Fetching remote package indizes..."

	fetch_remote_targets | while read target; do
		fetch_remote_feeds | while read feed; do
			if [ ! -s "$tmp/repo-remote/$target/packages/$feed/Packages.gz" ]; then
				echo " * $target $feed"
				mkdir -p "$tmp/repo-remote/$target/packages/$feed"
				wget -qO "$tmp/repo-remote/$target/packages/$feed/Packages.gz" "$U/$target/packages/$feed/Packages.gz"
			fi
		done
	done
}

fetch_remote_sdk() {
	local target sdk

	echo "Fetching remote SDKs..."

	fetch_remote_targets | while read target; do
		if [ ! -s "$tmp/repo-remote/$target/sdk.tar.bz2" ]; then
			fetch_remote_filelist "$U/$target" | while read sdk; do
				case "$sdk" in OpenWrt-SDK-*.tar.bz2)
					echo " * $target $sdk"
					mkdir -p "$tmp/repo-remote/$target"
					fetch_remote "$U/$target/$sdk" > "$tmp/repo-remote/$target/sdk.tar.bz2"
				;; esac
			done
		fi
	done
}

sdk_target() {
	sed -ne 's,^CONFIG_TARGET_BOARD="\(.*\)",\1,p' "$T/.config"
}

sdk_subtarget() {
	local mk target subtarget
	for mk in "$T/target/linux"/*/*/target.mk; do
		mk="${mk#*/target/linux/}"
		target="${mk%/*/target.mk}"
		subtarget="${mk#$target/}"
		subtarget="${subtarget%/target.mk}"

		if grep -qE "^CONFIG_TARGET_${target}_${subtarget}=y\$" "$T/.config"; then
			echo "$subtarget"
			return 0
		fi
	done
	return 1
}

sdk_prepare() {
	local target="$1"

	if [ ! -d "$tmp/sdk/$target/.git" ]; then
		echo " * [$slot:$target] Initializing SDK"

		rm -rf "$tmp/sdk/$target"
		mkdir -p "$tmp/sdk/$target"
		tar --strip-components=1 -C "$tmp/sdk/$target" -xjf "$tmp/repo-remote/$target/sdk.tar.bz2"

		mkdir -p "$tmp/dl"
		rm -rf "$tmp/sdk/$target/dl"
		ln -sf "$tmp/dl" "$tmp/sdk/$target/dl"

		mkdir -p "$tmp/feeds"
		rm -rf "$tmp/sdk/$target/feeds"
		ln -sf "$tmp/feeds" "$tmp/sdk/$target/feeds"

		(
			cd "$tmp/sdk/$target"
			git init .
			find . -maxdepth 1 | xargs git add
			git commit -m "Snapshot"
		) >/dev/null
	else
		echo " * [$slot:$target] Resetting SDK"

		(
			cd "$tmp/sdk/$target"
			git reset --hard HEAD
			git clean -f -d
		) >/dev/null
	fi
}

sdk_feed_install() {
	local pkg feed target="$1"; shift

	echo " * [$slot:$target] Installing packages"

	(
		flock -x 9

		cd "$tmp/sdk/$target"

		./scripts/feeds update

		for pkg in "$@"; do
			case "$pkg" in
				*:*) feed="${pkg#*:}"; pkg="${pkg%%:*}" ;;
				*) feed="" ;;
			esac

			./scripts/feeds install -d m${feed:+ -p "$feed"} "$pkg"
		done
	) 9>"$tmp/feeds.lock" 2>/dev/null >/dev/null
}

sdk_pkg_compile() {
	local pkg target="$1"; shift

	echo " * [$slot:$target] Compiling packages"

	for pkg in "$@"; do
		(cd "$tmp/sdk/$target"; make "package/${pkg%%:*}/compile") >/dev/null

		mkdir -p "$tmp/repo-local/$target/packages"
		cp -a "$tmp/sdk/$target/bin"/*/packages/* "$tmp/repo-local/$target/packages/"
	done
}

find_remote_pkg_name() {
	local feed name target="$1" pkg="$2"

	while read feed; do
		name="$(zcat "$tmp/repo-remote/$target/packages/$feed/Packages.gz" | \
			sed -ne "s/Filename: \\(${pkg%%:*}_.\\+\\.ipk\\)\$/\1/p")"

		if [ -n "$name" ]; then
			echo "$target/packages/$feed/$name"
			return 0
		fi
	done < "$tmp/feeds.lst"

	return 1
}

find_remote_pkg_feed() {
	local feed target="$1" pkg="$2"

	while read feed; do
		if zcat "$tmp/repo-remote/$target/packages/$feed/Packages.gz" | grep -qE "^Package: ${pkg%%:*}\$"; then
			echo "$feed"
			return 0
		fi
	done < "$tmp/feeds.lst"

	return 1
}

find_local_pkg_feed() {
	local feed file target="$1" pkg="$2"

	while read feed; do
		for file in "$tmp/repo-local/$target/packages/$feed/${pkg%%:*}"_[^_]*_[^_]*.ipk; do
			if [ -s "$file" ]; then
				echo "$feed"
				return 0
			fi
		done
	done < "$tmp/feeds.lst"

	return 1
}

index_patch_cmd() {
	local target="$1" feed="$2"; shift; shift
	local idir="$tmp/repo-remote/$target/packages/$feed"
	local odir="$tmp/repo-local/$target/packages/$feed"

	if [ ! -s "$odir/Packages" ]; then
		mkdir -p "$odir"
		zcat "$idir/Packages.gz" > "$odir/Packages"
	fi

	./bin/patch-index.pl --index "$odir/Packages" "$@" > "$odir/Packages.$$"

	mv "$odir/Packages.$$" "$odir/Packages"
}

index_patch() {
	local target="$1" feed pkg dir; shift

	echo " * [$slot:$target] Patching repository index"

	for pkg in "$@"; do
		feed="$(find_remote_pkg_feed "$target" "$pkg")"
		[ -n "$feed" ] && index_patch_cmd "$target" "$feed" \
			--remove "${pkg%%:*}"

		feed="$(find_local_pkg_feed "$target" "$pkg")"
		[ -n "$feed" ] && index_patch_cmd "$target" "$feed" \
			--add "$tmp/repo-local/$target/packages/$feed/${pkg%%:*}"_*.ipk
	done

	while read feed; do
		dir="$tmp/repo-local/$target/packages/$feed"
		if [ -s "$dir/Packages" ]; then
			gzip -c -9 "$dir/Packages" > "$dir/Packages.gz"
		fi
	done < "$tmp/feeds.lst"
}

file_rsync() {
	local target="$1" pkg path; shift

	echo " * [$slot:$target] Syncing files"

	mkdir -p "$tmp/empty"

	for pkg in "$@"; do
		path="$(find_remote_pkg_name "$target" "$pkg")"

		[ -n "$path" ] && echo rsync --dry-run -rv \
			--delete="${path##*/}" --exclude="*" "$tmp/empty/" "$R/${path%/*}/"

		echo rsync --dry-run -rv "$tmp/repo-local/$target/packages/" "$R/$target/packages/"
	done

}

run_jobs() {
	local targets=$(fetch_remote_targets)
	local target slot count

	#echo "* Compiling packages"

	for slot in $(seq 0 $((num_jobs-1))); do (
		count=1; for target in $targets; do
			if [ $((count++ % $num_jobs)) -eq $slot ]; then
				if [ $do_compile -gt 0 ]; then
					sdk_prepare "$target"
					sdk_feed_install "$target" "$@"
					sdk_pkg_compile "$target" "$@"
				fi

				if [ $do_index -gt 0 ]; then
					index_patch "$target" "$@"
				fi

				if [ $do_rsync -gt 0 ]; then
					file_rsync "$target" "$@"
				fi
			fi
		done
	) & done
}

trap terminate INT TERM

do_compile=0
do_index=0
do_rsync=0

num_jobs=$(grep processor /proc/cpuinfo | wc -l)

use_targets=""
use_packages=""

usage() {
	cat <<-EOT
$0 {-a|-c|-i|-r} [-j jobs] -p package [-p package ...]

  -a
     Perform all build steps (compile, rebuild, rsync)

  -c
     Compile packages for all architectures.

  -i
     Rebuild indexes for all architectures.

  -r
     Rsync files for all architecures.

  -j jobs
     Use the given number of jobs when compiling packages.
     Default is $num_jobs jobs.

  -p package[:feed]
     Add given package to the queue. If a feed suffix is
     given then prefer this feed name when installing the
     package into the SDK.


  -t target
     Restrict operation to given targets.

	EOT
	exit 1
}

while getopts ":acirp:t:" opt; do
	case "$opt" in
		a)
			do_compile=1
			do_index=1
			do_rsync=1
		;;
		c) do_compile=1 ;;
		i) do_index=1 ;;
		r) do_rsync=1 ;;
		j) num_jobs="$OPTARG" ;;
		p) use_packages="${use_packages:+$use_packages }$OPTARG" ;;
		t) use_targets="${use_targets:+$use_targets }$OPTARG" ;;
	esac
done

[ -z "$use_packages" ] && usage

echo "* Preparing metadata"
fetch_remote_targets >/dev/null
fetch_remote_feeds >/dev/null

run_jobs "$use_packages"