]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- display wget error message
[packages/rpm-build-tools.git] / pldnotify.awk
1 #!/bin/awk -f
2 # $Revision$, $Date$
3 #
4 # Copyright (C) 2000-2008 PLD-Team <feedback@pld-linux.org>
5 # Authors:
6 #       Sebastian Zagrodzki <zagrodzki@pld-linux.org>
7 #       Jacek Konieczny <jajcus@pld-linux.org>
8 #       Andrzej Krzysztofowicz <ankry@pld-linux.org>
9 #       Jakub Bogusz <qboosh@pld-linux.org>
10 #       Elan Ruusamäe <glen@pld-linux.org>
11 #
12 # See cvs log pldnotify.awk for list of contributors
13 #
14 # TODO:
15 # - "SourceXDownload" support (use given URLs if present instead of cut-down SourceX URLs)
16 # - "SourceXActiveFTP" support
17
18 function fixedsub(s1,s2,t,      ind) {
19 # substitutes fixed strings (not regexps)
20         if (ind = index(t,s1)) {
21                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
22         }
23         return t
24 }
25
26 function ispre(s) {
27         if ((s~"pre")||(s~"PRE")||(s~"beta")||(s~"BETA")||(s~"alpha")||(s~"ALPHA")||(s~"rc")||(s~"RC")) {
28                 if (DEBUG) print "pre-version"
29                 return 1
30         } else {
31                 return 0
32         }
33 }
34
35 function compare_ver(v1,v2) {
36 # compares version numbers
37         while (match(v1,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
38                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
39         while (match(v2,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
40                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
41         sub("^0*","",v1)
42         sub("^0*","",v2)
43         gsub("\.0*",".",v1)
44         gsub("\.0*",".",v2)
45         if (DEBUG) print "v1 == " v1
46         if (DEBUG) print "v2 == " v2
47         count=split(v1,v1a,"\.")
48         count2=split(v2,v2a,"\.")
49
50         if (count<count2) mincount=count
51         else mincount=count2
52
53         for (i=1; i<=mincount; i++) {
54                 if (v1a[i]=="") v1a[i]=0
55                 if (v2a[i]=="") v2a[i]=0
56                 if (DEBUG) print "i == " i
57                 if (DEBUG) print "v1[i] == " v1a[i]
58                 if (DEBUG) print "v2[i] == " v2a[i]
59                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
60                         if (length(v2a[i])>length(v1a[i]))
61                                 return 1
62                         else if (v2a[i]>v1a[i])
63                                 return 1
64                         else if (length(v1a[i])>length(v2a[i]))
65                                 return 0
66                         else if (v1a[i]>v2a[i])
67                                 return 0
68                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
69                         if (v2a[i]>v1a[i])
70                                 return 1
71                         else if (v1a[i]>v2a[i])
72                                 return 0
73                 } else if (ispre(v1a[i]) == 1)
74                         return 1
75                 else
76                         return 0
77         }
78         if ((count2==mincount)&&(count!=count2)) {
79                 for (i=count2+1; i<=count; i++)
80                         if (ispre(v1a[i]) == 1)
81                                 return 1
82                 return 0
83         } else if (count!=count2) {
84                 for (i=count+1; i<=count2; i++)
85                         if (ispre(v2a[i]) == 1)
86                                 return 0
87                 return 1
88         }
89         return 0
90 }
91
92 function compare_ver_dec(v1,v2) {
93 # compares version numbers as decimal floats
94         while (match(v1,/[0-9][a-zA-Z]/))
95                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
96         while (match(v2,/[0-9][a-zA-Z]/))
97                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
98         sub("^0*","",v1)
99         sub("^0*","",v2)
100         if (DEBUG) print "v1 == " v1
101         if (DEBUG) print "v2 == " v2
102         count=split(v1,v1a,"\.")
103         count2=split(v2,v2a,"\.")
104
105         if (count<count2) mincount=count
106         else mincount=count2
107
108         for (i=1; i<=mincount; i++) {
109                 if (v1a[i]=="") v1a[i]=0
110                 if (v2a[i]=="") v2a[i]=0
111                 if (DEBUG) print "i == " i
112                 if (DEBUG) print "v1[i] == " v1a[i]
113                 if (DEBUG) print "v2[i] == " v2a[i]
114                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
115                         if (i==2) {
116                                 if (0+("." v2a[i])>0+("." v1a[i]))
117                                         return 1
118                                 else if (0+("." v1a[i])>0+("." v2a[i]))
119                                         return 0
120                         } else {
121                                 if (length(v2a[i])>length(v1a[i]))
122                                         return 1
123                                 else if (v2a[i]>v1a[i])
124                                         return 1
125                                 else if (length(v1a[i])>length(v2a[i]))
126                                         return 0
127                                 else if (v1a[i]>v2a[i])
128                                         return 0
129                         }
130                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
131                         if (v2a[i]>v1a[i])
132                                 return 1
133                         else if (v1a[i]>v2a[i])
134                                 return 0
135                 } else if (ispre(v1a[i]) == 1)
136                         return 1
137                 else
138                         return 0
139         }
140         if ((count2==mincount)&&(count!=count2)) {
141                 for (i=count2+1; i<=count; i++)
142                         if (ispre(v1a[i]) == 1)
143                                 return 1
144                 return 0
145         } else if (count!=count2) {
146                 for (i=count+1; i<=count2; i++)
147                         if (ispre(v2a[i]) == 1)
148                                 return 0
149                 return 1
150         }
151         return 0
152 }
153
154 function link_seen(link) {
155         for (seenlink in frameseen) {
156                 if (seenlink == link) {
157                         if (DEBUG) print "Link: [" link "] seen already, skipping..."
158                         return 1
159                 }
160         }
161         frameseen[link]=1
162         return 0
163 }
164
165 function get_links(url,filename,errno,link,oneline,retval,odp,wholeodp,lowerodp,tmpfile) {
166 # get all <A HREF=..> tags from specified URL
167
168         wholeerr=""
169
170         "mktemp /tmp/XXXXXX" | getline tmpfile
171         close(tmpfile)
172
173         "mktemp /tmp/errXXXXXX" | getline tmpfileerr
174         close(tmpfileerr)
175
176         if (url ~ /^http:\/\/(download|dl).(sf|sourceforge).net\//) {
177                 gsub("^http://(download|dl).(sf|sourceforge).net/", "", url)
178                 url = "http://prdownloads.sourceforge.net/" substr(url, 1, 1) "/" substr(url, 1, 2) "/" url
179                 if (DEBUG) print "sf url, mungled url to: " url
180         }
181
182         if (url ~ /^http:\/\/(.*)\.googlecode\.com\/files\//) {
183                 gsub("^http://", "", url)
184                 gsub("\..*", "", url)
185                 url = "http://code.google.com/p/" url "/downloads/list"
186                 if (DEBUG) print "googlecode url, mungled url to: " url
187         }
188
189         if (url ~ /^http:\/\/pecl.php.net\/get\//) {
190                 gsub("-.*", "", filename)
191                 url = "http://pecl.php.net/package/" filename
192                 if (DEBUG) print "pecl.php.net url, mungled url to: " url
193         }
194
195         if (url ~ /^(http|ftp):\/\/mysql.*\/Downloads\/MySQL-5.1\//) {
196                 url = "http://dev.mysql.com/downloads/mysql/5.1.html#source"
197                  if (DEBUG) print "mysql 5.1 url, mungled url to: " url
198         }
199
200
201         if (DEBUG) print "Retrieving: " url
202         errno=system("wget -nv -O - \"" url "\" -t 2 -T 45 --passive-ftp > " tmpfile " 2> " tmpfileerr )
203
204         if (errno==0) {
205                 while (getline oneline < tmpfile)
206                         wholeodp=(wholeodp " " oneline)
207                 if ( DEBUG ) print "Response: " wholeodp
208         } else {
209                 wholeerr = ""
210                 while (getline oneline < tmpfileerr)
211                         wholeerr=(wholeerr " " oneline)
212                 if ( DEBUG ) print "Error Response: " wholeerr
213         }
214
215         close(tmpfile)
216         system("rm -f " tmpfile)
217
218         close(tmpfileerr)
219         system("rm -f " tmpfileerr)
220
221         urldir=url;
222         sub(/[^\/]+$/,"",urldir)
223
224         if ( errno==0) {
225                 while (match(wholeodp, /<([aA]|[fF][rR][aA][mM][eE])[ \t][^>]*>/) > 0) {
226                         odp=substr(wholeodp,RSTART,RLENGTH);
227                         wholeodp=substr(wholeodp,RSTART+RLENGTH);
228
229                         lowerodp=tolower(odp);
230                         if (lowerodp ~ /<frame[ \t]/) {
231                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",odp);
232                                 match(odp,/src="[^"]+"/)
233                                 newurl=substr(odp,RSTART+5,RLENGTH-6)
234                                 if (DEBUG) print "Frame: " newurl
235                                 if (newurl !~ /\//) {
236                                         newurl=(urldir newurl)
237                                         if (DEBUG) print "Frame->: " newurl
238                                 }
239
240                                 if (link_seen(newurl)) {
241                                         newurl=""
242                                         continue
243                                 }
244
245                                 retval=(retval " " get_links(newurl))
246                         } else if (lowerodp ~ /href=[ \t]*"[^"]*"/) {
247                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
248                                 match(odp,/href="[^"]*"/)
249                                 link=substr(odp,RSTART,RLENGTH)
250                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
251                                 link=substr(link,7,length(link)-7)
252
253                                 if (link_seen(link)) {
254                                         link=""
255                                         continue
256                                 }
257
258                                 retval=(retval " " link)
259                                 if (DEBUG) print "href(\"\"): " link
260                         } else if (lowerodp ~ /href=[ \t]*'[^']*'/) {
261                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
262                                 match(odp,/href='[^']*'/)
263                                 link=substr(odp,RSTART,RLENGTH)
264                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
265                                 link=substr(link,7,length(link)-7)
266
267                                 if (link_seen(link)) {
268                                         link=""
269                                         continue
270                                 }
271
272                                 retval=(retval " " link)
273                                 if (DEBUG) print "href(''): " link
274                         } else if (lowerodp ~ /href=[ \t]*[^ \t>]*/) {
275                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
276                                 match(odp,/href=[^ \t>]*/)
277                                 link=substr(odp,RSTART,RLENGTH)
278                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
279                                 link=substr(link,6,length(link)-5)
280
281                                 if (link_seen(link)) {
282                                         link=""
283                                         continue
284                                 }
285
286                                 retval=(retval " " link)
287                                 if (DEBUG) print "href(): " link
288                         } else {
289                                 # <a ...> but not href - skip
290                                 if (DEBUG) print "skipping <a > without href: " odp
291                         }
292                 }
293         } else {
294                 retval=("WGET ERROR: " errno ": " wholeerr)
295         }
296
297
298         if (DEBUG) print "Returning: " retval
299         return retval
300 }
301
302 function subst_defines(var,defs) {
303 # substitute all possible RPM macros
304         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
305                 oldvar=var
306                 for (j in defs) {
307                         gsub("%{" j "}", defs[j], var)
308                         gsub("%" j , defs[j], var)
309                 }
310                 if (var==oldvar) {
311                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
312                         return var
313                 }
314         }
315         return var
316 }
317
318 function find_mirror(url) {
319
320         while (succ = (getline line < "mirrors")) {
321             if (succ==-1) { return url }
322                 nf=split(line,fields,"|")
323                 if (nf>1){
324                         origin=fields[1]
325                         mirror=fields[2]
326                         mname=fields[3]
327                         prefix=substr(url,1,length(origin))
328                         if (prefix==origin){
329                                 if ( DEBUG ) print "Mirror fount at " mname
330                                 close("mirrors")
331                                 return mirror substr(url,length(origin)+1)
332                         }
333                 }
334         }
335
336         return url
337 }
338
339 function process_source(number,lurl,name,version) {
340 # fetches file list, and compares version numbers
341         if ( DEBUG ) print "Processing " lurl
342
343         if ( index(lurl,version)==0 ) {
344                 if (DEBUG) print "There is no version number."
345                 return 0
346         }
347
348         sub("://",":",lurl)
349         sub("/",":/",lurl)
350         gsub("[^/]*$",":&",lurl)
351         split(lurl,url,":")
352         acc=url[1]
353         host=url[2]
354         dir=url[3]
355         filename=url[4]
356
357         if (index(dir,version)) {
358                 dir=substr(dir,1,index(dir,version)-1)
359                 sub("[^/]*$","",dir)
360                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
361                 if ( DEBUG ) print "Will check a directory: " dir
362                 if ( DEBUG ) print "and a file: " filename
363         }
364
365         filenameexp=filename
366         gsub("\+","\\+",filenameexp)
367         sub(version,"[A-Za-z0-9.]+",filenameexp)
368         gsub("\.","\\.",filenameexp)
369         if ( DEBUG ) print "Expression: " filenameexp
370         match(filename,version)
371         prever=substr(filename,1,RSTART-1)
372         postver=substr(filename,RSTART+RLENGTH)
373         if ( DEBUG ) print "Before number: " prever
374         if ( DEBUG ) print "and after: " postver
375         newurl=find_mirror(acc "://" host dir)
376         #print acc "://" host dir
377         #newurl=url[1]"://"url[2]url[3]url[4]
378         #newurl=acc "://" host dir filename
379         if ( DEBUG ) print "Looking at " newurl
380
381         references=0
382         finished=0
383         oldversion=version
384         odp=get_links(newurl,filename)
385         if( odp ~ "ERROR: ") {
386                 print name "(" number ") " odp
387         } else {
388                 if (DEBUG) print "WebPage downloaded"
389                 c=split(odp,linki)
390                 for (nr=1; nr<=c; nr++) {
391                         addr=linki[nr]
392                         if (DEBUG) print "Found link: " addr
393                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
394                                 match(addr,filenameexp)
395                                 newfilename=substr(addr,RSTART,RLENGTH)
396                                 if (DEBUG) print "Hypothetical new: " newfilename
397                                 newfilename=fixedsub(prever,"",newfilename)
398                                 newfilename=fixedsub(postver,"",newfilename)
399                                 if (DEBUG) print "Version: " newfilename
400                                 if (newfilename ~ /\.(pkg|bin|binary|built)$/) continue
401                                 if (NUMERIC) {
402                                         if ( compare_ver_dec(version, newfilename)==1 ) {
403                                                 if (DEBUG) print "Yes, there is new one"
404                                                 version=newfilename
405                                                 finished=1
406                                         }
407                                 } else if ( compare_ver(version, newfilename)==1 ) {
408                                         if (DEBUG) print "Yes, there is new one"
409                                         version=newfilename
410                                         finished=1
411                                 }
412                         }
413                 }
414                 if (finished==0)
415                         print name "(" number ") seems ok: " oldversion
416                 else
417                         print name "(" number ") [OLD] " oldversion " [NEW] " version
418         }
419 }
420
421 # upgrade check for pear package using PEAR CLI
422 function pear_upgrade(name, ver) {
423         pname = name;
424         sub(/^php-pear-/, "", pname);
425
426         pearcmd = "pear remote-info " pname " | awk '/^Latest/{print $NF}'"
427         if (DEBUG) {
428                 print "pearcmd: " pearcmd
429         }
430         pearcmd | getline nver
431         close(pearcmd)
432
433         if (compare_ver(ver, nver)) {
434                 print name " [OLD] " ver " [NEW] " nver
435         } else {
436                 print name " seems ok: " ver
437         }
438
439         return
440 }
441
442 function process_data(name,ver,rel,src) {
443         if (name ~ /^php-pear-/) {
444                 return pear_upgrade(name, ver);
445         }
446
447 # this function checks if substitutions were valid, and if true:
448 # processes each URL and tries to get current file list
449         for (i in src) {
450                 if ( src[i] ~ /%{nil}/ ) {
451                         gsub(/\%\{nil\}/, "", src[i])
452                 }
453                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
454                         if ( DEBUG ) print "Source: " src[i]
455                         process_source(i,src[i],name,ver)
456                 } else {
457                         print FNAME ":" i ": impossible substitution: " src[i]
458                 }
459         }
460 }
461
462 BEGIN {
463         # if U want to use DEBUG, run script with "-v DEBUG=1"
464         # or uncomment the line below
465         # DEBUG = 1
466
467         errno=system("wget --help > /dev/null 2>&1")
468         if (errno) {
469                 print "No wget installed!"
470                 exit 1
471         }
472         if (ARGC>=3 && ARGV[2]=="-n") {
473                 NUMERIC=1
474                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i]
475                 ARGC=ARGC-1
476         }
477         frameseen[0] = 1
478 }
479
480 FNR==1 {
481         if ( ARGIND != 1 ) {
482                 process_data(NAME,VER,REL,SRC)
483                 NAME="" ; VER="" ; REL=""
484                 for (i in DEFS) delete DEFS[i]
485                 for (i in SRC) delete SRC[i]
486         }
487         FNAME=FILENAME
488         DEFS["_alt_kernel"]=""
489         DEFS["20"]="\\ "
490 }
491
492 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
493 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
494 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
495 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
496 /^[Ss]ource[0-9]*:/ { if (/(ftp|http|https):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
497 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
498
499 END {
500         process_data(NAME,VER,REL,SRC)
501 }
This page took 0.063992 seconds and 4 git commands to generate.