]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- iscsi-initiator-utils -> open-iscsi in deps
[packages/rpm-build-tools.git] / pldnotify.awk
1 #!/bin/awk -f
2 # $Revision$, $Date$
3 #
4 # Copyright (C) 2000-2006 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 get_links(url, errno,link,oneline,retval,odp,wholeodp,lowerodp,tmpfile) {
155 # get all <A HREF=..> tags from specified URL
156         "mktemp /tmp/XXXXXX" | getline tmpfile
157         close("mktemp /tmp/XXXXXX")
158
159         if (url ~ /^http:\/\/(download|dl).(sf|sourceforge).net\//) {
160                 gsub("^http://(download|dl).(sf|sourceforge).net/", "", url)
161                 url = "http://prdownloads.sourceforge.net/" substr(url, 1, 1) "/" substr(url, 1, 2) "/" url
162                 if (DEBUG) print "sf url, mungled url to: " url
163         }
164
165         if (DEBUG) print "Retrieving: " url
166         errno=system("wget -O - \"" url "\" -t 3 -T 300 --passive-ftp > " tmpfile " 2>/dev/null" )
167
168         if (errno==0) {
169                 while (getline oneline < tmpfile)
170                         wholeodp=(wholeodp " " oneline)
171                 if ( DEBUG ) print "Response: " wholeodp
172         }
173
174         close(tmpfile)
175         system("rm -f " tmpfile)
176         urldir=url;
177         sub(/[^\/]+$/,"",urldir)
178
179         if ( errno==0) {
180                 while (match(wholeodp, /<([aA]|[fF][rR][aA][mM][eE])[ \t][^>]*>/) > 0) {
181                         odp=substr(wholeodp,RSTART,RLENGTH);
182                         wholeodp=substr(wholeodp,RSTART+RLENGTH);
183
184                         lowerodp=tolower(odp);
185                         if (lowerodp ~ /<frame[ \t]/) {
186                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",odp);
187                                 match(odp,/src="[^"]+"/)
188                                 newurl=substr(odp,RSTART+5,RLENGTH-6)
189                                 if (DEBUG) print "Frame: " newurl
190                                 if (newurl !~ /\//) {
191                                         newurl=(urldir newurl)
192                                         if (DEBUG) print "Frame->: " newurl
193                                 }
194                                 retval=(retval " " get_links(newurl))
195                         } else if (lowerodp ~ /href=[ \t]*"[^"]*"/) {
196                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
197                                 match(odp,/href="[^"]*"/)
198                                 link=substr(odp,RSTART,RLENGTH)
199                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
200                                 link=substr(link,7,length(link)-7)
201                                 retval=(retval " " link)
202                                 if (DEBUG) print "href(\"\"): " link
203                         } else if (lowerodp ~ /href=[ \t]*'[^']*'/) {
204                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
205                                 match(odp,/href='[^']*'/)
206                                 link=substr(odp,RSTART,RLENGTH)
207                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
208                                 link=substr(link,7,length(link)-7)
209                                 retval=(retval " " link)
210                                 if (DEBUG) print "href(''): " link
211                         } else if (lowerodp ~ /href=[ \t]*[^ \t>]*/) {
212                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
213                                 match(odp,/href=[^ \t>]*/)
214                                 link=substr(odp,RSTART,RLENGTH)
215                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
216                                 link=substr(link,6,length(link)-5)
217                                 retval=(retval " " link)
218                                 if (DEBUG) print "href(): " link
219                         } else {
220                                 # <a ...> but not href - skip
221                                 if (DEBUG) print "skipping <a > without href: " odp
222                         }
223                 }
224         } else {
225                 retval=("WGET ERROR: " errno)
226         }
227
228
229         if (DEBUG) print "Returning: " retval
230         return retval
231 }
232
233 function subst_defines(var,defs) {
234 # substitute all possible RPM macros
235         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
236                 oldvar=var
237                 for (j in defs) {
238                         gsub("%{" j "}", defs[j], var)
239                         gsub("%" j , defs[j], var)
240                 }
241                 if (var==oldvar) {
242                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
243                         return var
244                 }
245         }
246         return var
247 }
248
249 function find_mirror(url) {
250
251         while (succ = (getline line < "mirrors")) {
252             if (succ==-1) { return url }
253                 nf=split(line,fields,"|")
254                 if (nf>1){
255                         origin=fields[1]
256                         mirror=fields[2]
257                         mname=fields[3]
258                         prefix=substr(url,1,length(origin))
259                         if (prefix==origin){
260                                 if ( DEBUG ) print "Mirror fount at " mname
261                                 close("mirrors")
262                                 return mirror substr(url,length(origin)+1)
263                         }
264                 }
265         }
266
267         return url
268 }
269
270 function process_source(number,lurl,name,version) {
271 # fetches file list, and compares version numbers
272         if ( DEBUG ) print "Processing " lurl
273
274         if ( index(lurl,version)==0 ) {
275                 if (DEBUG) print "There is no version number."
276                 return 0
277         }
278
279         sub("://",":",lurl)
280         sub("/",":/",lurl)
281         gsub("[^/]*$",":&",lurl)
282         split(lurl,url,":")
283         acc=url[1]
284         host=url[2]
285         dir=url[3]
286         filename=url[4]
287
288         if (index(dir,version)) {
289                 dir=substr(dir,1,index(dir,version)-1)
290                 sub("[^/]*$","",dir)
291                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
292                 if ( DEBUG ) print "Will check a directory: " dir
293                 if ( DEBUG ) print "and a file: " filename
294         }
295
296         filenameexp=filename
297         gsub("\+","\\+",filenameexp)
298         sub(version,"[A-Za-z0-9.]+",filenameexp)
299         gsub("\.","\\.",filenameexp)
300         if ( DEBUG ) print "Expression: " filenameexp
301         match(filename,version)
302         prever=substr(filename,1,RSTART-1)
303         postver=substr(filename,RSTART+RLENGTH)
304         if ( DEBUG ) print "Before number: " prever
305         if ( DEBUG ) print "and after: " postver
306         newurl=find_mirror(acc "://" host dir)
307         #print acc "://" host dir
308         #newurl=url[1]"://"url[2]url[3]url[4]
309         #newurl=acc "://" host dir filename
310         if ( DEBUG ) print "Looking at " newurl
311
312         references=0
313         finished=0
314         oldversion=version
315         odp=get_links(newurl)
316         if( odp ~ "ERROR: ") {
317                 print name "(" number ") " odp
318         } else {
319                 if (DEBUG) print "WebPage downloaded"
320                 c=split(odp,linki)
321                 for (nr=1; nr<=c; nr++) {
322                         addr=linki[nr]
323                         if (DEBUG) print "Found link: " addr
324                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
325                                 match(addr,filenameexp)
326                                 newfilename=substr(addr,RSTART,RLENGTH)
327                                 if (DEBUG) print "Hypothetical new: " newfilename
328                                 newfilename=fixedsub(prever,"",newfilename)
329                                 newfilename=fixedsub(postver,"",newfilename)
330                                 if (DEBUG) print "Version: " newfilename
331                                 if (newfilename ~ /\.(pkg|bin|binary|built)$/) continue
332                                 if (NUMERIC) {
333                                         if ( compare_ver_dec(version, newfilename)==1 ) {
334                                                 if (DEBUG) print "Yes, there is new one"
335                                                 version=newfilename
336                                                 finished=1
337                                         }
338                                 } else if ( compare_ver(version, newfilename)==1 ) {
339                                         if (DEBUG) print "Yes, there is new one"
340                                         version=newfilename
341                                         finished=1
342                                 }
343                         }
344                 }
345                 if (finished==0)
346                         print name "(" number ") seems ok: " oldversion
347                 else
348                         print name "(" number ") [OLD] " oldversion " [NEW] " version
349         }
350 }
351
352 # upgrade check for pear package using PEAR CLI
353 function pear_upgrade(name, ver) {
354         pname = name;
355         sub(/^php-pear-/, "", pname);
356
357         pearcmd = "pear remote-info " pname " | awk '/^Latest/{print $NF}'"
358         if (DEBUG) {
359                 print "pearcmd: " pearcmd
360         }
361         pearcmd | getline nver
362         close(pearcmd)
363
364         if (compare_ver(ver, nver)) {
365                 print name " [OLD] " ver " [NEW] " nver
366         } else {
367                 print name " seems ok: " ver
368         }
369
370         return
371 }
372
373 function process_data(name,ver,rel,src) {
374         if (name ~ /^php-pear-/) {
375                 return pear_upgrade(name, ver);
376         }
377
378 # this function checks if substitutions were valid, and if true:
379 # processes each URL and tries to get current file list
380         for (i in src) {
381                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
382                         if ( DEBUG ) print "Source: " src[i]
383                         process_source(i,src[i],name,ver)
384                 } else {
385                         print FNAME ":" i ": impossible substitution: " src[i]
386                 }
387         }
388 }
389
390 BEGIN {
391         # if U want to use DEBUG, run script with "-v DEBUG=1"
392         # or uncomment the line below
393         # DEBUG = 1
394
395         errno=system("wget --help > /dev/null 2>&1")
396         if (errno) {
397                 print "No wget installed!"
398                 exit 1
399         }
400         if (ARGC>=3 && ARGV[2]=="-n") {
401                 NUMERIC=1
402                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i]
403                 ARGC=ARGC-1
404         }
405 }
406
407 FNR==1 {
408         if ( ARGIND != 1 ) {
409                 process_data(NAME,VER,REL,SRC)
410                 NAME="" ; VER="" ; REL=""
411                 for (i in DEFS) delete DEFS[i]
412                 for (i in SRC) delete SRC[i]
413         }
414         FNAME=FILENAME
415 }
416
417 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
418 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
419 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
420 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
421 /^[Ss]ource[0-9]*:/ { if (/(ftp|http):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
422 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
423
424 END {
425         process_data(NAME,VER,REL,SRC)
426 }
This page took 0.115975 seconds and 3 git commands to generate.