]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- match href='link', allow different style of hrefs in the same document
[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                         } else if (tolower(odp) ~ /href=[ \t]*'[^']+'/) {
180                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
181                                 match(odp,/href='[^']+'/)
182                                 link=substr(odp,RSTART,RLENGTH)
183                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
184                                 link=substr(link,7,length(link)-7)
185                                 retval=(retval " " link)
186                         } else if (tolower(odp) ~ /href=[ \t]*[^ \t>]+/) {
187                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
188                                 match(odp,/href=[^ \t>]+/)
189                                 link=substr(odp,RSTART,RLENGTH)
190                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
191                                 link=substr(link,6,length(link)-5)
192                                 retval=(retval " " link)
193                         } else {
194                                 retval=(retval " INTERNAL_ERROR")
195                                 break
196                         }
197                 }
198         } else {
199                 retval=("WGET ERROR: " errno)
200         }
201         
202         
203         if (DEBUG) print "Zwracane: " retval
204         return retval
205 }
206
207 function subst_defines(var,defs) {
208 # substitute all possible RPM macros
209         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
210                 oldvar=var
211                 for (j in defs) {
212                         gsub("%{" j "}", defs[j], var)
213                         gsub("%" j , defs[j], var)
214                 }
215                 if (var==oldvar) {
216                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
217                         return var
218                 }
219         }
220         return var
221 }
222
223 function find_mirror(url) {
224
225         while (succ = (getline line < "mirrors")) {
226             if (succ==-1) { return url }
227                 nf=split(line,fields,"|")
228                 if (nf>1){
229                         origin=fields[1]
230                         mirror=fields[2]
231                         mname=fields[3]
232                         prefix=substr(url,1,length(origin))
233                         if (prefix==origin){
234                                 if ( DEBUG ) print "Mirror znaleziony na " mname
235                                 close("mirrors")
236                                 return mirror substr(url,length(origin))
237                         }
238                 }
239         }
240
241         return url
242 }
243
244 function process_source(number,lurl,name,version) {
245 # fetches file list, and compares version numbers
246         if ( DEBUG ) print "Przetwarzam " lurl
247
248         if ( index(lurl,version)==0 ) {
249                 if (DEBUG) print "Nie ma numeru wersji."
250                 return 0
251         }
252
253         sub("://",":",lurl)
254         sub("/",":/",lurl)
255         gsub("[^/]*$",":&",lurl)
256         split(lurl,url,":")
257         acc=url[1]
258         host=url[2]
259         dir=url[3]
260         filename=url[4]
261
262         if (index(dir,version)) {
263                 dir=substr(dir,1,index(dir,version)-1)
264                 sub("[^/]*$","",dir)
265                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
266                 if ( DEBUG ) print "Sprawdze katalog: " dir
267                 if ( DEBUG ) print "i plik: " filename
268         }
269
270         filenameexp=filename
271         gsub("\+","\\+",filenameexp)
272         sub(version,"[A-Za-z0-9.]+",filenameexp)
273         gsub("\.","\\.",filenameexp)
274         if ( DEBUG ) print "Wzorzec: " filenameexp
275         match(filename,version)
276         prever=substr(filename,1,RSTART-1)
277         postver=substr(filename,RSTART+RLENGTH)
278         if ( DEBUG ) print "Przed numerkiem: " prever
279         if ( DEBUG ) print "i po: " postver
280         newurl=find_mirror(acc "://" host dir)  
281         #print acc "://" host dir
282         #newurl=url[1]"://"url[2]url[3]url[4]
283         #newurl=acc "://" host dir filename
284         if ( DEBUG ) print "ZaglĀ±dam na " newurl 
285         
286         references=0
287         finished=0
288         oldversion=version
289         odp=get_links(newurl)
290         if( odp ~ "ERROR: ") {
291                 print name "(" number ") " odp
292         } else {
293                 if (DEBUG) print "Sciagnieta strona"
294                 c=split(odp,linki)
295                 for (nr=1; nr<=c; nr++) {
296                         addr=linki[nr]
297                         if (DEBUG) print "Znaleziony link: " addr
298                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
299                                 match(addr,filenameexp)
300                                 newfilename=substr(addr,RSTART,RLENGTH)
301                                 if (DEBUG) print "Hipotetyczny nowy: " newfilename
302                                 newfilename=fixedsub(prever,"",newfilename)
303                                 newfilename=fixedsub(postver,"",newfilename)
304                                 if (DEBUG) print "Wersja: " newfilename
305                                 if (newfilename ~ /\.(pkg|bin|binary)$/) continue
306                                 if (NUMERIC) {
307                                         if ( compare_ver_dec(version, newfilename)==1 ) {
308                                                 if (DEBUG) print "Tak, jest nowa"
309                                                 version=newfilename
310                                                 finished=1
311                                         }
312                                 } else if ( compare_ver(version, newfilename)==1 ) {
313                                         if (DEBUG) print "Tak, jest nowa"
314                                         version=newfilename
315                                         finished=1
316                                 }
317                         }
318                 }
319                 if (finished==0)
320                         print name "(" number ") seems ok: " oldversion
321                 else
322                         print name "(" number ") [OLD] " oldversion " [NEW] " version
323         }
324 }
325         
326 function process_data(name,ver,rel,src) {
327 # this function checks if substitutions were valid, and if true:
328 # processes each URL and tries to get current file list
329         for (i in src) {
330                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
331                         if ( DEBUG ) print "Zrodlo: " src[i]
332                         process_source(i,src[i],name,ver)
333                 } else {
334                         print FNAME ":" i ": niemozliwe podstawienie: " src[i]
335                 }
336         }
337 }
338
339 BEGIN {
340         # if U want to use DEBUG, run script with "-v DEBUG=1"
341         # or uncomment the line below
342         # DEBUG = 1
343
344         errno=system("wget --help > /dev/null 2>&1")
345         if (errno) {
346                 print "No wget installed!"
347                 exit 1
348         }
349         if (ARGC>=3 && ARGV[2]=="-n") {
350                 NUMERIC=1
351                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i] 
352                 ARGC=ARGC-1
353         }
354 }
355
356 FNR==1 {
357         if ( ARGIND != 1 ) {
358                 process_data(NAME,VER,REL,SRC)
359                 NAME="" ; VER="" ; REL=""
360                 for (i in DEFS) delete DEFS[i]
361                 for (i in SRC) delete SRC[i]
362         }
363         FNAME=FILENAME
364 }
365
366 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
367 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
368 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
369 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
370 /^[Ss]ource[0-9]*:/ { if (/(ftp|http):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
371 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
372
373 END {
374         process_data(NAME,VER,REL,SRC)
375 }
This page took 0.47477 seconds and 3 git commands to generate.