]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- more debug, don't hang on empty hrefs
[packages/rpm-build-tools.git] / pldnotify.awk
1 #!/bin/awk -f
2 # $Revision$, $Date$
3 function fixedsub(s1,s2,t,      ind) {
4 # substitutes fixed strings (not regexps)
5         if (ind = index(t,s1)) {
6                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
7         }
8         return t
9 }
10
11 function ispre(s) {
12         if ((s~"pre")||(s~"PRE")||(s~"beta")||(s~"BETA")||(s~"alpha")||(s~"ALPHA")||(s~"rc")||(s~"RC")) {
13                 if (DEBUG) print "pre-version"
14                 return 1
15         } else {
16                 return 0
17         }
18 }
19         
20 function compare_ver(v1,v2) {
21 # compares version numbers
22         while (match(v1,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
23                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
24         while (match(v2,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
25                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
26         sub("^0*","",v1)
27         sub("^0*","",v2)
28         gsub("\.0*",".",v1)
29         gsub("\.0*",".",v2)
30         if (DEBUG) print "v1 == " v1
31         if (DEBUG) print "v2 == " v2
32         count=split(v1,v1a,"\.")
33         count2=split(v2,v2a,"\.")
34         
35         if (count<count2) mincount=count 
36         else mincount=count2
37         
38         for (i=1; i<=mincount; i++) {
39                 if (v1a[i]=="") v1a[i]=0
40                 if (v2a[i]=="") v2a[i]=0
41                 if (DEBUG) print "i == " i
42                 if (DEBUG) print "v1[i] == " v1a[i]
43                 if (DEBUG) print "v2[i] == " v2a[i]
44                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
45                         if (length(v2a[i])>length(v1a[i]))
46                                 return 1
47                         else if (v2a[i]>v1a[i])
48                                 return 1
49                         else if (length(v1a[i])>length(v2a[i]))
50                                 return 0
51                         else if (v1a[i]>v2a[i])
52                                 return 0
53                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
54                         if (v2a[i]>v1a[i])
55                                 return 1
56                         else if (v1a[i]>v2a[i])
57                                 return 0
58                 } else if (ispre(v1a[i]) == 1)
59                         return 1
60                 else
61                         return 0
62         }
63         if ((count2==mincount)&&(count!=count2)) {
64                 for (i=count2+1; i<=count; i++)
65                         if (ispre(v1a[i]) == 1)
66                                 return 1
67                 return 0
68         } else if (count!=count2) {
69                 for (i=count+1; i<=count2; i++)
70                         if (ispre(v2a[i]) == 1)
71                                 return 0
72                 return 1
73         }
74         return 0
75 }
76
77 function compare_ver_dec(v1,v2) {
78 # compares version numbers as decimal floats
79         while (match(v1,/[0-9][a-zA-Z]/))
80                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
81         while (match(v2,/[0-9][a-zA-Z]/))
82                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
83         sub("^0*","",v1)
84         sub("^0*","",v2)
85         if (DEBUG) print "v1 == " v1
86         if (DEBUG) print "v2 == " v2
87         count=split(v1,v1a,"\.")
88         count2=split(v2,v2a,"\.")
89         
90         if (count<count2) mincount=count 
91         else mincount=count2
92         
93         for (i=1; i<=mincount; i++) {
94                 if (v1a[i]=="") v1a[i]=0
95                 if (v2a[i]=="") v2a[i]=0
96                 if (DEBUG) print "i == " i
97                 if (DEBUG) print "v1[i] == " v1a[i]
98                 if (DEBUG) print "v2[i] == " v2a[i]
99                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
100                         if (i==2) {
101                                 if (0+("." v2a[i])>0+("." v1a[i]))
102                                         return 1
103                                 else if (0+("." v1a[i])>0+("." v2a[i]))
104                                         return 0
105                         } else {
106                                 if (length(v2a[i])>length(v1a[i]))
107                                         return 1
108                                 else if (v2a[i]>v1a[i])
109                                         return 1
110                                 else if (length(v1a[i])>length(v2a[i]))
111                                         return 0
112                                 else if (v1a[i]>v2a[i])
113                                         return 0
114                         }
115                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
116                         if (v2a[i]>v1a[i])
117                                 return 1
118                         else if (v1a[i]>v2a[i])
119                                 return 0
120                 } else if (ispre(v1a[i]) == 1)
121                         return 1
122                 else
123                         return 0
124         }
125         if ((count2==mincount)&&(count!=count2)) {
126                 for (i=count2+1; i<=count; i++)
127                         if (ispre(v1a[i]) == 1)
128                                 return 1
129                 return 0
130         } else if (count!=count2) {
131                 for (i=count+1; i<=count2; i++)
132                         if (ispre(v2a[i]) == 1)
133                                 return 0
134                 return 1
135         }
136         return 0
137 }
138
139 function get_links(url, errno,link,oneline,retval,odp,tmpfile) {
140 # get all <A HREF=..> tags from specified URL
141         "mktemp /tmp/XXXXXX" | getline tmpfile
142         close("mktemp /tmp/XXXXXX")
143         
144         if (DEBUG) print "Retrieving: " url
145         errno=system("wget -O - \"" url "\" -t 3 -T 300 --passive-ftp > " tmpfile " 2>/dev/null" )
146         
147         if (errno==0) {
148                 while (getline oneline < tmpfile)
149                         odp=(odp " " oneline)
150                 if ( DEBUG ) print "Odpowiedz: " odp
151         }
152         
153         close(tmpfile)
154         system("rm -f " tmpfile)
155         urldir=url;
156         sub(/[^\/]+$/,"",urldir)
157         if ( errno==0) {
158                 while ((tolower(odp) ~ /<frame[ \t]/)||(tolower(odp) ~ /href=/)) {
159                         if (tolower(odp) ~ /<frame[ \t]/) {
160                                 match(tolower(odp),/<frame[ \t][^>]*>/)
161                                 ramka=substr(odp,RSTART,RLENGTH)
162                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
163                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",ramka);
164                                 match(ramka,/src="[^"]+"/)
165                                 newurl=substr(ramka,RSTART+5,RLENGTH-6)
166                                 if (DEBUG) print "Ramka: " newurl
167                                 if (newurl !~ /\//) {
168                                         newurl=(urldir newurl)
169                                         if (DEBUG) print "Ramka->: " newurl
170                                 }
171                                 retval=(retval " " get_links(newurl))
172                         } else if (tolower(odp) ~ /href=[ \t]*"[^"]*"/) {
173                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
174                                 match(odp,/href="[^"]*"/)
175                                 link=substr(odp,RSTART,RLENGTH)
176                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
177                                 link=substr(link,7,length(link)-7)
178                                 retval=(retval " " link)
179                                 if (DEBUG) print "href(\"\"): " link
180                         } else if (tolower(odp) ~ /href=[ \t]*'[^']*'/) {
181                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
182                                 match(odp,/href='[^']*'/)
183                                 link=substr(odp,RSTART,RLENGTH)
184                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
185                                 link=substr(link,7,length(link)-7)
186                                 retval=(retval " " link)
187                                 if (DEBUG) print "href(''): " link
188                         } else if (tolower(odp) ~ /href=[ \t]*[^ \t>]*/) {
189                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
190                                 match(odp,/href=[^ \t>]*/)
191                                 link=substr(odp,RSTART,RLENGTH)
192                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
193                                 link=substr(link,6,length(link)-5)
194                                 retval=(retval " " link)
195                                 if (DEBUG) print "href(): " link
196                         } else {
197                                 retval=(retval " INTERNAL_ERROR")
198                                 break
199                         }
200                 }
201         } else {
202                 retval=("WGET ERROR: " errno)
203         }
204         
205         
206         if (DEBUG) print "Zwracane: " retval
207         return retval
208 }
209
210 function subst_defines(var,defs) {
211 # substitute all possible RPM macros
212         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
213                 oldvar=var
214                 for (j in defs) {
215                         gsub("%{" j "}", defs[j], var)
216                         gsub("%" j , defs[j], var)
217                 }
218                 if (var==oldvar) {
219                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
220                         return var
221                 }
222         }
223         return var
224 }
225
226 function find_mirror(url) {
227
228         while (succ = (getline line < "mirrors")) {
229             if (succ==-1) { return url }
230                 nf=split(line,fields,"|")
231                 if (nf>1){
232                         origin=fields[1]
233                         mirror=fields[2]
234                         mname=fields[3]
235                         prefix=substr(url,1,length(origin))
236                         if (prefix==origin){
237                                 if ( DEBUG ) print "Mirror znaleziony na " mname
238                                 close("mirrors")
239                                 return mirror substr(url,length(origin))
240                         }
241                 }
242         }
243
244         return url
245 }
246
247 function process_source(number,lurl,name,version) {
248 # fetches file list, and compares version numbers
249         if ( DEBUG ) print "Przetwarzam " lurl
250
251         if ( index(lurl,version)==0 ) {
252                 if (DEBUG) print "Nie ma numeru wersji."
253                 return 0
254         }
255
256         sub("://",":",lurl)
257         sub("/",":/",lurl)
258         gsub("[^/]*$",":&",lurl)
259         split(lurl,url,":")
260         acc=url[1]
261         host=url[2]
262         dir=url[3]
263         filename=url[4]
264
265         if (index(dir,version)) {
266                 dir=substr(dir,1,index(dir,version)-1)
267                 sub("[^/]*$","",dir)
268                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
269                 if ( DEBUG ) print "Sprawdze katalog: " dir
270                 if ( DEBUG ) print "i plik: " filename
271         }
272
273         filenameexp=filename
274         gsub("\+","\\+",filenameexp)
275         sub(version,"[A-Za-z0-9.]+",filenameexp)
276         gsub("\.","\\.",filenameexp)
277         if ( DEBUG ) print "Wzorzec: " filenameexp
278         match(filename,version)
279         prever=substr(filename,1,RSTART-1)
280         postver=substr(filename,RSTART+RLENGTH)
281         if ( DEBUG ) print "Przed numerkiem: " prever
282         if ( DEBUG ) print "i po: " postver
283         newurl=find_mirror(acc "://" host dir)  
284         #print acc "://" host dir
285         #newurl=url[1]"://"url[2]url[3]url[4]
286         #newurl=acc "://" host dir filename
287         if ( DEBUG ) print "ZaglĀ±dam na " newurl 
288         
289         references=0
290         finished=0
291         oldversion=version
292         odp=get_links(newurl)
293         if( odp ~ "ERROR: ") {
294                 print name "(" number ") " odp
295         } else {
296                 if (DEBUG) print "Sciagnieta strona"
297                 c=split(odp,linki)
298                 for (nr=1; nr<=c; nr++) {
299                         addr=linki[nr]
300                         if (DEBUG) print "Znaleziony link: " addr
301                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
302                                 match(addr,filenameexp)
303                                 newfilename=substr(addr,RSTART,RLENGTH)
304                                 if (DEBUG) print "Hipotetyczny nowy: " newfilename
305                                 newfilename=fixedsub(prever,"",newfilename)
306                                 newfilename=fixedsub(postver,"",newfilename)
307                                 if (DEBUG) print "Wersja: " newfilename
308                                 if (newfilename ~ /\.(pkg|bin|binary)$/) continue
309                                 if (NUMERIC) {
310                                         if ( compare_ver_dec(version, newfilename)==1 ) {
311                                                 if (DEBUG) print "Tak, jest nowa"
312                                                 version=newfilename
313                                                 finished=1
314                                         }
315                                 } else if ( compare_ver(version, newfilename)==1 ) {
316                                         if (DEBUG) print "Tak, jest nowa"
317                                         version=newfilename
318                                         finished=1
319                                 }
320                         }
321                 }
322                 if (finished==0)
323                         print name "(" number ") seems ok: " oldversion
324                 else
325                         print name "(" number ") [OLD] " oldversion " [NEW] " version
326         }
327 }
328         
329 function process_data(name,ver,rel,src) {
330 # this function checks if substitutions were valid, and if true:
331 # processes each URL and tries to get current file list
332         for (i in src) {
333                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
334                         if ( DEBUG ) print "Zrodlo: " src[i]
335                         process_source(i,src[i],name,ver)
336                 } else {
337                         print FNAME ":" i ": niemozliwe podstawienie: " src[i]
338                 }
339         }
340 }
341
342 BEGIN {
343         # if U want to use DEBUG, run script with "-v DEBUG=1"
344         # or uncomment the line below
345         # DEBUG = 1
346
347         errno=system("wget --help > /dev/null 2>&1")
348         if (errno) {
349                 print "No wget installed!"
350                 exit 1
351         }
352         if (ARGC>=3 && ARGV[2]=="-n") {
353                 NUMERIC=1
354                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i] 
355                 ARGC=ARGC-1
356         }
357 }
358
359 FNR==1 {
360         if ( ARGIND != 1 ) {
361                 process_data(NAME,VER,REL,SRC)
362                 NAME="" ; VER="" ; REL=""
363                 for (i in DEFS) delete DEFS[i]
364                 for (i in SRC) delete SRC[i]
365         }
366         FNAME=FILENAME
367 }
368
369 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
370 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
371 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
372 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
373 /^[Ss]ource[0-9]*:/ { if (/(ftp|http):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
374 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
375
376 END {
377         process_data(NAME,VER,REL,SRC)
378 }
This page took 0.280657 seconds and 3 git commands to generate.