]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- fix close (I hope it's correct now)
[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("mktemp /tmp/XXXXXX")
172
173         "mktemp /tmp/errXXXXXX" | getline tmpfileerr
174         close("mktemp /tmp/errXXXXXX")
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         system("rm -f " tmpfile)
216         system("rm -f " tmpfileerr)
217
218         urldir=url;
219         sub(/[^\/]+$/,"",urldir)
220
221         if ( errno==0) {
222                 while (match(wholeodp, /<([aA]|[fF][rR][aA][mM][eE])[ \t][^>]*>/) > 0) {
223                         odp=substr(wholeodp,RSTART,RLENGTH);
224                         wholeodp=substr(wholeodp,RSTART+RLENGTH);
225
226                         lowerodp=tolower(odp);
227                         if (lowerodp ~ /<frame[ \t]/) {
228                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",odp);
229                                 match(odp,/src="[^"]+"/)
230                                 newurl=substr(odp,RSTART+5,RLENGTH-6)
231                                 if (DEBUG) print "Frame: " newurl
232                                 if (newurl !~ /\//) {
233                                         newurl=(urldir newurl)
234                                         if (DEBUG) print "Frame->: " newurl
235                                 }
236
237                                 if (link_seen(newurl)) {
238                                         newurl=""
239                                         continue
240                                 }
241
242                                 retval=(retval " " get_links(newurl))
243                         } else if (lowerodp ~ /href=[ \t]*"[^"]*"/) {
244                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
245                                 match(odp,/href="[^"]*"/)
246                                 link=substr(odp,RSTART,RLENGTH)
247                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
248                                 link=substr(link,7,length(link)-7)
249
250                                 if (link_seen(link)) {
251                                         link=""
252                                         continue
253                                 }
254
255                                 retval=(retval " " link)
256                                 if (DEBUG) print "href(\"\"): " link
257                         } else if (lowerodp ~ /href=[ \t]*'[^']*'/) {
258                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
259                                 match(odp,/href='[^']*'/)
260                                 link=substr(odp,RSTART,RLENGTH)
261                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
262                                 link=substr(link,7,length(link)-7)
263
264                                 if (link_seen(link)) {
265                                         link=""
266                                         continue
267                                 }
268
269                                 retval=(retval " " link)
270                                 if (DEBUG) print "href(''): " link
271                         } else if (lowerodp ~ /href=[ \t]*[^ \t>]*/) {
272                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
273                                 match(odp,/href=[^ \t>]*/)
274                                 link=substr(odp,RSTART,RLENGTH)
275                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
276                                 link=substr(link,6,length(link)-5)
277
278                                 if (link_seen(link)) {
279                                         link=""
280                                         continue
281                                 }
282
283                                 retval=(retval " " link)
284                                 if (DEBUG) print "href(): " link
285                         } else {
286                                 # <a ...> but not href - skip
287                                 if (DEBUG) print "skipping <a > without href: " odp
288                         }
289                 }
290         } else {
291                 retval=("WGET ERROR: " errno ": " wholeerr)
292         }
293
294
295         if (DEBUG) print "Returning: " retval
296         return retval
297 }
298
299 function subst_defines(var,defs) {
300 # substitute all possible RPM macros
301         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
302                 oldvar=var
303                 for (j in defs) {
304                         gsub("%{" j "}", defs[j], var)
305                         gsub("%" j , defs[j], var)
306                 }
307                 if (var==oldvar) {
308                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
309                         return var
310                 }
311         }
312         return var
313 }
314
315 function find_mirror(url) {
316
317         while (succ = (getline line < "mirrors")) {
318             if (succ==-1) { return url }
319                 nf=split(line,fields,"|")
320                 if (nf>1){
321                         origin=fields[1]
322                         mirror=fields[2]
323                         mname=fields[3]
324                         prefix=substr(url,1,length(origin))
325                         if (prefix==origin){
326                                 if ( DEBUG ) print "Mirror fount at " mname
327                                 close("mirrors")
328                                 return mirror substr(url,length(origin)+1)
329                         }
330                 }
331         }
332
333         return url
334 }
335
336 function process_source(number,lurl,name,version) {
337 # fetches file list, and compares version numbers
338         if ( DEBUG ) print "Processing " lurl
339
340         if ( index(lurl,version)==0 ) {
341                 if (DEBUG) print "There is no version number."
342                 return 0
343         }
344
345         sub("://",":",lurl)
346         sub("/",":/",lurl)
347         gsub("[^/]*$",":&",lurl)
348         split(lurl,url,":")
349         acc=url[1]
350         host=url[2]
351         dir=url[3]
352         filename=url[4]
353
354         if (index(dir,version)) {
355                 dir=substr(dir,1,index(dir,version)-1)
356                 sub("[^/]*$","",dir)
357                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
358                 if ( DEBUG ) print "Will check a directory: " dir
359                 if ( DEBUG ) print "and a file: " filename
360         }
361
362         filenameexp=filename
363         gsub("\+","\\+",filenameexp)
364         sub(version,"[A-Za-z0-9.]+",filenameexp)
365         gsub("\.","\\.",filenameexp)
366         if ( DEBUG ) print "Expression: " filenameexp
367         match(filename,version)
368         prever=substr(filename,1,RSTART-1)
369         postver=substr(filename,RSTART+RLENGTH)
370         if ( DEBUG ) print "Before number: " prever
371         if ( DEBUG ) print "and after: " postver
372         newurl=find_mirror(acc "://" host dir)
373         #print acc "://" host dir
374         #newurl=url[1]"://"url[2]url[3]url[4]
375         #newurl=acc "://" host dir filename
376         if ( DEBUG ) print "Looking at " newurl
377
378         references=0
379         finished=0
380         oldversion=version
381         odp=get_links(newurl,filename)
382         if( odp ~ "ERROR: ") {
383                 print name "(" number ") " odp
384         } else {
385                 if (DEBUG) print "WebPage downloaded"
386                 c=split(odp,linki)
387                 for (nr=1; nr<=c; nr++) {
388                         addr=linki[nr]
389                         if (DEBUG) print "Found link: " addr
390                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
391                                 match(addr,filenameexp)
392                                 newfilename=substr(addr,RSTART,RLENGTH)
393                                 if (DEBUG) print "Hypothetical new: " newfilename
394                                 newfilename=fixedsub(prever,"",newfilename)
395                                 newfilename=fixedsub(postver,"",newfilename)
396                                 if (DEBUG) print "Version: " newfilename
397                                 if (newfilename ~ /\.(pkg|bin|binary|built)$/) continue
398                                 if (NUMERIC) {
399                                         if ( compare_ver_dec(version, newfilename)==1 ) {
400                                                 if (DEBUG) print "Yes, there is new one"
401                                                 version=newfilename
402                                                 finished=1
403                                         }
404                                 } else if ( compare_ver(version, newfilename)==1 ) {
405                                         if (DEBUG) print "Yes, there is new one"
406                                         version=newfilename
407                                         finished=1
408                                 }
409                         }
410                 }
411                 if (finished==0)
412                         print name "(" number ") seems ok: " oldversion
413                 else
414                         print name "(" number ") [OLD] " oldversion " [NEW] " version
415         }
416 }
417
418 # upgrade check for pear package using PEAR CLI
419 function pear_upgrade(name, ver) {
420         pname = name;
421         sub(/^php-pear-/, "", pname);
422
423         pearcmd = "pear remote-info " pname " | awk '/^Latest/{print $NF}'"
424         if (DEBUG) {
425                 print "pearcmd: " pearcmd
426         }
427         pearcmd | getline nver
428         close(pearcmd)
429
430         if (compare_ver(ver, nver)) {
431                 print name " [OLD] " ver " [NEW] " nver
432         } else {
433                 print name " seems ok: " ver
434         }
435
436         return
437 }
438
439 function process_data(name,ver,rel,src) {
440         if (name ~ /^php-pear-/) {
441                 return pear_upgrade(name, ver);
442         }
443
444 # this function checks if substitutions were valid, and if true:
445 # processes each URL and tries to get current file list
446         for (i in src) {
447                 if ( src[i] ~ /%{nil}/ ) {
448                         gsub(/\%\{nil\}/, "", src[i])
449                 }
450                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
451                         if ( DEBUG ) print "Source: " src[i]
452                         process_source(i,src[i],name,ver)
453                 } else {
454                         print FNAME ":" i ": impossible substitution: " src[i]
455                 }
456         }
457 }
458
459 BEGIN {
460         # if U want to use DEBUG, run script with "-v DEBUG=1"
461         # or uncomment the line below
462         # DEBUG = 1
463
464         errno=system("wget --help > /dev/null 2>&1")
465         if (errno) {
466                 print "No wget installed!"
467                 exit 1
468         }
469         if (ARGC>=3 && ARGV[2]=="-n") {
470                 NUMERIC=1
471                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i]
472                 ARGC=ARGC-1
473         }
474         frameseen[0] = 1
475 }
476
477 FNR==1 {
478         if ( ARGIND != 1 ) {
479                 process_data(NAME,VER,REL,SRC)
480                 NAME="" ; VER="" ; REL=""
481                 for (i in DEFS) delete DEFS[i]
482                 for (i in SRC) delete SRC[i]
483         }
484         FNAME=FILENAME
485         DEFS["_alt_kernel"]=""
486         DEFS["20"]="\\ "
487 }
488
489 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
490 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
491 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
492 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
493 /^[Ss]ource[0-9]*:/ { if (/(ftp|http|https):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
494 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
495
496 END {
497         process_data(NAME,VER,REL,SRC)
498 }
This page took 0.129985 seconds and 4 git commands to generate.