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