summaryrefslogtreecommitdiffstats
path: root/plugins/url_check/main
blob: 09d437a263a77c1088918ef69f57f53575f44cec (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
#!/bin/bash
# ----------------------------------------------------------
#
# Script: 	PTXdist URL Checker
# Rev: 		1
# Description:
# Written by:	Bjørn Bürger <b.buerger@pengutronix.de>
# Changed:      2006-09-21 bbu
# Docs:		inline
# Manpage:	none
#
# ----------------------------------------------------------
PREFIX="`basename $0` "
# ----------------------------------------------------------
# Short Documentation / Comments
# ----------------------------------------------------------
#
# TODO: 
# build URL_CHECKER as host-tool in ptxdist. 
# Problem -> uses python => big and fat
# OR find a lightweight alternative. 
#
# Please note, that wget has problems w/ ftp links.
#
#

# ----------------------------------------------------------
# generic script settings
# ----------------------------------------------------------
#
# The script domains - chose one or more of:
# - dumb_tool
# - tool
# - development
# - system_management
#
# PTX_SCRIPT_DOMAINS="dumb_tool"

PTX_LIB_VERSION="2"

# ----------------------------------------------------------
# Default Configuration Options
# ----------------------------------------------------------

MKTEMP="mktemp"
WHICH="which"
GREP="grep"
EGREP="egrep"
CAT="cat"
SED="sed"
MKDIR="mkdir"

LINKCHECKER_BIN="linkchecker"

URL_CHECKER="$LINKCHECKER_BIN --no-warnings"
# URL_CHECKER="wget --spider"

PTXCONFIG="selected_ptxconfig"

logdir="`dirname $0`/log"
logfile="$logdir/url_check_log"

# ----------------------------------------------------------
# Load ptx shell library and generic ptx configuration
# ----------------------------------------------------------

PTXLIB=`dirname $0`/ptxlib.bash

if [ -e "$PTXLIB" ] ; then
	. $PTXLIB
else
	echo "ERROR: ptxlib not found"
	exit 1
fi

# ==========================================================
# Temporary files
# ==========================================================

TMPDIR="`$MKTEMP -d /tmp/url_check_plugin.XXXXXXXXXX`" || echo "could not create TMPDIR"

ptx_debug "TMPDIR is $TMPDIR"

# ==========================================================
# Traps
# ==========================================================

[ -e "$TMPDIR/on_exit_reverse.sh" ] || echo "echo" > $TMPDIR/on_exit_reverse.sh

if [ "$DEBUG" = "true" ]; then
	ptx_debug "trap function: deleting temporary files..."
	trap '[ -e "$TMPDIR/on_exit_reverse.sh" ] && sh $TMPDIR/on_exit_reverse.sh ; rm -rvf /tmp/$(basename $TMPDIR)' EXIT
else
	trap '[ -e "$TMPDIR/on_exit_reverse.sh" ] && sh $TMPDIR/on_exit_reverse.sh ; rm -rf /tmp/$(basename $TMPDIR)' EXIT
fi

# ----------------------------------------------------------
# Default Dependency check
# ----------------------------------------------------------

dependency_check_dirs_depends=""
dependency_check_files_depends="$PTXCONFIG"
dependency_check_tools_depends="$LINKCHECKER_BIN $MKTEMP"

ptx_dependency_check

# ==========================================================
# Option Parser
# ==========================================================

Usage() {
cat <<-EOF

Usage: `basename "$0"` OPTIONS

    --help          -h          this help
    --check-all     -a		check ALL packages
    --modules       -m		check module packages
    --builtin       -y		check builtin packages

$0 checks the availability of all needed source packages 
for the current project configuration. By default, packages 
will only be checked, if the corresponding switch in 
ptxdistrc is set to "y" (enabled in 'ptxdist menuconfig')

EOF
}

# Parser
# ------
# option	no argument
# option:	required argument
# option::	optional argument

TEMP=`getopt --options h,a,y,m					\
	--longoptions="help,check-all,modules,builtin"		\
        -n "$0" -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"

while true ; do
        case "$1" in
                -h|--help)
			[ -z "$action" ]
			action="help" ;
			shift
			;;
                -a|--check-all)
			[ -z "$action" ]
			action="check_all" ;
			shift
			;;
		-m|--modules)
			[ -z "$action" ]
                        action="check_modules" ;
                        shift
                        ;;
		-y|--builtin)
			[ -z "$action" ]
                        action="check_builtin" ;
                        shift
                        ;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
done

# ==========================================================
# Script Variables
# ==========================================================

# none

# ==========================================================
# Script Functions
# ==========================================================

init(){
	# choose the right ptxdist version:
	echo "PATH is $PATH"
	PTXDIST_VERSION=$(echo `$GREP PTXCONF_CONFIGFILE_VERSION $PTXCONFIG` \; echo "\$PTXCONF_CONFIGFILE_VERSION" | sh)
	PTXDIST=$(dirname $0)/../../bin/ptxdist
	if [ "$($PTXDIST --version)" != "$PTXDIST_VERSION" ]; then
		PTXDIST=ptxdist-$PTXDIST_VERSION
		which $PTXDIST || PTXDIST=ptxdist
	fi
	PTXDIST_BIN="`$WHICH $PTXDIST`"
	ptx_debug "PTXDIST is: $PTXDIST ($PTXDIST_BIN)"
	if [ "$1" = "all" ]; then
		# get all package labels 
		# (PACKAGES-y contains all activated packages)
		# (PACKAGES- contains all deactivated packages)
		YESPACKAGES=`$PTXDIST print PACKAGES-y`
		MODPACKAGES=`$PTXDIST print PACKAGES-m`
		NOPACKAGES=`$PTXDIST print PACKAGES-`
	elif [ "$1" = "active" ]; then
		# get only configured builtin package labels 
		# (PACKAGES-y contains all active builtin packages)
		YESPACKAGES=`$PTXDIST print PACKAGES-y`
		MODPACKAGES=""
		NOPACKAGES=""
	elif [ "$1" = "modules" ]; then
		# get only configured MODULE package labels 
		# (PACKAGES-m contains all module packages)
		YESPACKAGES=""
		MODPACKAGES=`$PTXDIST print PACKAGES-m`
		NOPACKAGES=""
        else
                # get all configured builtin and module package labels 
                # (PACKAGES-y contains all activated packages)
                YESPACKAGES=`$PTXDIST print PACKAGES-y`
                MODPACKAGES=`$PTXDIST print PACKAGES-m`
                NOPACKAGES=""
	fi
	PACKAGES="$(echo $YESPACKAGES $MODPACKAGES $NOPACKAGES | tr "[a-z-]" "[A-Z_]")"
}

create_url_list(){
	echo "creating list of download URLS for `echo $PACKAGES | wc -w` packages"
	targets=`for target in $PACKAGES; do echo print-${target}_URL; done | sort -u`
	$PTXDIST make -i ${targets} 2>> $TMPDIR/errors > $TMPDIR/urllist
	echo "done"
}

test_urls(){
	egrep -v "^#|^$|^[[:space:]]" $TMPDIR/urllist \
	| while read line; do
		local count_ok=0
		local count_fail=0
		rm -f $TMPDIR/errormsg
		for url in $line; do
			$URL_CHECKER $url >>$TMPDIR/errormsg 2>&1
			case $? in
				0)
				count_ok=$[count_ok+1]
				;;
				*)
				count_fail=$[count_fail+1]
				;;
			esac
		done
		if [ $count_fail -eq 0 ]; then
			echo " [  OK  ] $line"
		elif [ $count_ok  -eq 0 ]; then
			echo " [ FAIL ] $line"
			echo ""
			echo ""
			$CAT $TMPDIR/errormsg | $GREP -A 10 "Start checking at"
			echo ""
		else
			local count=$[count_ok+$count_fail]
			echo " [ $count_ok/$count  ] $line"
		fi
	done
}

runner(){
	$MKDIR -p $logdir
	case $? in
		0)
		test_urls | tee $logfile
		echo "LOGFILE is --> $logfile"
		;;
		*)
		test_urls
		;;
	esac
}

# ==========================================================
# Script Main
# ==========================================================

case "$action" in
       help)
	Usage
       ;;
       check_all)
       	init all
	create_url_list >&2
	runner
       ;;
       check_modules)
       	init modules
	create_url_list >&2
	runner
       ;;
       check_builtin)
       	init active
	create_url_list >&2
	runner
       ;;
       *)
       	init
	create_url_list >&2
	runner
       ;;
esac

# ==========================================================
# Cleanup
# ==========================================================

# unconfigured - done by trap function
# ----------------------------------------------------------
# End
# ----------------------------------------------------------