summaryrefslogtreecommitdiffstats
path: root/plugins/url_check/main
blob: de48b0cb16364d839a8a1ee36fc3e30d6af268a5 (plain) (blame)
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
#!/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="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

$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			\
	--longoptions="help,check-all"		\
        -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
			;;
                --) 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=$(echo `$GREP PTXCONF_CONFIGFILE_VERSION $PTXCONFIG` \; echo "ptxdist-\$PTXCONF_CONFIGFILE_VERSION" | sh)
	which $PTXDIST || PTXDIST=ptxdist
	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 | grep "^PACKAGES" | sed -e s/'PACKAGES-y is "'//g -e s/\"//g | tr "a-z-" "A-Z_"`
		NOPACKAGES=`$PTXDIST print PACKAGES- | grep "^PACKAGES" | sed -e s/'PACKAGES- is "'//g -e s/\"//g | tr "a-z-" "A-Z_"`
	else
		# get configured package labels 
		# (PACKAGES-y contains all activated packages)
		YESPACKAGES=`$PTXDIST print PACKAGES-y | grep "^PACKAGES" | sed -e s/'PACKAGES-y is "'//g -e s/\"//g | tr "a-z-" "A-Z_"`
		NOPACKAGES=""
	fi
	PACKAGES="$YESPACKAGES $NOPACKAGES"
}

create_url_list(){
	echo "creating list of download URLS for `echo $PACKAGES | wc -w` packages (This may take a while!)"
	for target in $PACKAGES; do
		$PTXDIST print ${target}_URL | $SED -e s/.*is\ \"//g -e s/\"//g >> $TMPDIR/urllist
		echo -n "."
	done
	echo "done"
}

test_urls(){
	egrep -v "^#|^$|^[[:space:]]" $TMPDIR/urllist \
	| while read line; do
		$URL_CHECKER $line >$TMPDIR/errormsg 2>&1
		case $? in
			0)
			echo " [  OK  ] $line"
			;;
			*)
			echo ""
			echo " [ FAIL ] $line"
			echo ""
			$CAT $TMPDIR/errormsg | $GREP -A 10 "Start checking at"
			echo ""
			;;
		esac
	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
       ;;
       *)
       	init
	create_url_list >&2
	runner
       ;;
esac

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

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