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