]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
More debug.
[packages/rpm-build-tools.git] / pldnotify.awk
1 #!/bin/awk -f
2 # $Revision$, $Date$
3 #
4 # Copyright (C) 2000-2010 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 link_seen(link) {
155         for (seenlink in frameseen) {
156                 if (seenlink == link) {
157                         if (DEBUG) print "Link: [" link "] seen already, skipping..."
158                         return 1
159                 }
160         }
161         frameseen[link]=1
162         return 0
163 }
164
165 function mktemp(   _cmd, _tmpfile) {
166         _cmd = "mktemp /tmp/XXXXXX"
167         _cmd | getline _tmpfile
168         close(_cmd)
169         return _tmpfile
170 }
171
172 # get all <A HREF=..> tags from specified URL
173 function get_links(url,filename,   errno,link,oneline,retval,odp,wholeodp,lowerodp,tmpfile,cmd) {
174
175         wholeerr=""
176
177         tmpfile = mktemp()
178         tmpfileerr = mktemp()
179
180         if (url ~ /^http:\/\/(download|dl).(sf|sourceforge).net\//) {
181                 gsub("^http://(download|dl).(sf|sourceforge).net/", "", url)
182                 gsub("/.*", "", url)
183                 url = "http://sourceforge.net/projects/" url "/files/"
184                 if (DEBUG) print "sf url, mungled url to: " url
185         }
186
187         if (url ~ /^http:\/\/(.*)\.googlecode\.com\/files\//) {
188                 gsub("^http://", "", url)
189                 gsub("\..*", "", url)
190                 url = "http://code.google.com/p/" url "/downloads/list"
191                 if (DEBUG) print "googlecode url, mungled url to: " url
192         }
193
194         if (url ~ /^http:\/\/pecl.php.net\/get\//) {
195                 gsub("-.*", "", filename)
196                 url = "http://pecl.php.net/package/" filename
197                 if (DEBUG) print "pecl.php.net url, mungled url to: " url
198         }
199
200         if (url ~ /^(http|ftp):\/\/mysql.*\/Downloads\/MySQL-5.1\//) {
201                 url = "http://dev.mysql.com/downloads/mysql/5.1.html#source"
202                  if (DEBUG) print "mysql 5.1 url, mungled url to: " url
203         }
204
205         if (url ~/^(http|https):\/\/launchpad\.net\/(.*)\//) {
206                 gsub("^(http|https):\/\/launchpad\.net\/", "", url)
207                 gsub("\/.*/", "", url)
208                 url = "https://code.launchpad.net/" url "/+download"
209                 if (DEBUG) print "main launchpad url, mungled url to: " url
210         }
211
212         if (url ~/^(http|https):\/\/edge\.launchpad\.net\/(.*)\//) {
213                 gsub("^(http|https):\/\/edge\.launchpad\.net\/", "", url)
214                 gsub("\/.*/", "", url)
215                 url = "https://edge.launchpad.net/" url "/+download"
216                 if (DEBUG) print "edge launchpad url, mungled url to: " url
217         }
218
219
220         if (DEBUG) print "Retrieving: " url
221         cmd = "wget --user-agent \"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100129 PLD/3.0 (Th) Iceweasel/3.6\" -nv -O - \"" url "\" -t 2 -T 45 --passive-ftp --no-check-certificate > " tmpfile " 2> " tmpfileerr
222         if (DEBUG) print "Execute: " cmd
223         errno = system(cmd)
224         if (DEBUG) print "Execute done"
225
226         if (errno==0) {
227                 wholeodp = ""
228                 if ( DEBUG ) print "Reading succeess response..."
229                 while (getline oneline < tmpfile)
230                         wholeodp=(wholeodp " " oneline)
231                 printf ""
232                 if ( DEBUG ) print "Response: " wholeodp
233         } else {
234                 if ( DEBUG ) print "Reading failure response..."
235                 wholeerr = ""
236                 while (getline oneline < tmpfileerr)
237                         wholeerr=(wholeerr " " oneline)
238                 if ( DEBUG ) print "Error Response: " wholeerr
239         }
240
241         system("rm -f " tmpfile)
242         system("rm -f " tmpfileerr)
243
244         urldir=url;
245         sub(/[^\/]+$/,"",urldir)
246
247         if ( errno==0) {
248                 while (match(wholeodp, /<([aA]|[fF][rR][aA][mM][eE])[ \t][^>]*>/) > 0) {
249                         if (DEBUG) print "Processing links..."
250                         odp=substr(wholeodp,RSTART,RLENGTH);
251                         wholeodp=substr(wholeodp,RSTART+RLENGTH);
252
253                         lowerodp=tolower(odp);
254                         if (lowerodp ~ /<frame[ \t]/) {
255                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",odp);
256                                 match(odp,/src="[^"]+"/)
257                                 newurl=substr(odp,RSTART+5,RLENGTH-6)
258                                 if (DEBUG) print "Frame: " newurl
259                                 if (newurl !~ /\//) {
260                                         newurl=(urldir newurl)
261                                         if (DEBUG) print "Frame->: " newurl
262                                 }
263
264                                 if (link_seen(newurl)) {
265                                         newurl=""
266                                         continue
267                                 }
268
269                                 retval=(retval " " get_links(newurl))
270                         } else if (lowerodp ~ /href=[ \t]*"[^"]*"/) {
271                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
272                                 match(odp,/href="[^"]*"/)
273                                 link=substr(odp,RSTART,RLENGTH)
274                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
275                                 link=substr(link,7,length(link)-7)
276
277                                 if (link_seen(link)) {
278                                         link=""
279                                         continue
280                                 }
281
282                                 retval=(retval " " link)
283                                 if (DEBUG) print "href(\"\"): " link
284                         } else if (lowerodp ~ /href=[ \t]*'[^']*'/) {
285                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
286                                 match(odp,/href='[^']*'/)
287                                 link=substr(odp,RSTART,RLENGTH)
288                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
289                                 link=substr(link,7,length(link)-7)
290
291                                 if (link_seen(link)) {
292                                         link=""
293                                         continue
294                                 }
295
296                                 retval=(retval " " link)
297                                 if (DEBUG) print "href(''): " link
298                         } else if (lowerodp ~ /href=[ \t]*[^ \t>]*/) {
299                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
300                                 match(odp,/href=[^ \t>]*/)
301                                 link=substr(odp,RSTART,RLENGTH)
302                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
303                                 link=substr(link,6,length(link)-5)
304
305                                 if (link_seen(link)) {
306                                         link=""
307                                         continue
308                                 }
309
310                                 retval=(retval " " link)
311                                 if (DEBUG) print "href(): " link
312                         } else {
313                                 # <a ...> but not href - skip
314                                 if (DEBUG) print "skipping <a > without href: " odp
315                         }
316                 }
317         } else {
318                 retval=("WGET ERROR: " errno ": " wholeerr)
319         }
320
321
322         if (DEBUG) print "Returning: " retval
323         return retval
324 }
325
326 function subst_defines(var,defs) {
327 # substitute all possible RPM macros
328         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
329                 oldvar=var
330                 for (j in defs) {
331                         gsub("%{" j "}", defs[j], var)
332                         gsub("%" j , defs[j], var)
333                 }
334                 if (var==oldvar) {
335                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
336                         return var
337                 }
338         }
339         return var
340 }
341
342 function find_mirror(url) {
343
344         while (succ = (getline line < "mirrors")) {
345             if (succ==-1) { return url }
346                 nf=split(line,fields,"|")
347                 if (nf>1){
348                         origin=fields[1]
349                         mirror=fields[2]
350                         mname=fields[3]
351                         prefix=substr(url,1,length(origin))
352                         if (prefix==origin){
353                                 if ( DEBUG ) print "Mirror fount at " mname
354                                 close("mirrors")
355                                 return mirror substr(url,length(origin)+1)
356                         }
357                 }
358         }
359
360         return url
361 }
362
363 function process_source(number,lurl,name,version) {
364 # fetches file list, and compares version numbers
365         if ( DEBUG ) print "Processing " lurl
366
367         if ( index(lurl,version)==0 ) {
368                 if (DEBUG) print "There is no version number."
369                 return 0
370         }
371
372         sub("://",":",lurl)
373         sub("/",":/",lurl)
374         gsub("[^/]*$",":&",lurl)
375         split(lurl,url,":")
376         acc=url[1]
377         host=url[2]
378         dir=url[3]
379         filename=url[4]
380
381         if (index(dir,version)) {
382                 dir=substr(dir,1,index(dir,version)-1)
383                 sub("[^/]*$","",dir)
384                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
385                 if ( DEBUG ) print "Will check a directory: " dir
386                 if ( DEBUG ) print "and a file: " filename
387         }
388
389         filenameexp=filename
390         gsub("\+","\\+",filenameexp)
391         sub(version,"[A-Za-z0-9.]+",filenameexp)
392         gsub("\.","\\.",filenameexp)
393         if ( DEBUG ) print "Expression: " filenameexp
394         match(filename,version)
395         prever=substr(filename,1,RSTART-1)
396         postver=substr(filename,RSTART+RLENGTH)
397         if ( DEBUG ) print "Before number: " prever
398         if ( DEBUG ) print "and after: " postver
399         newurl=find_mirror(acc "://" host dir)
400         #print acc "://" host dir
401         #newurl=url[1]"://"url[2]url[3]url[4]
402         #newurl=acc "://" host dir filename
403         if ( DEBUG ) print "Looking at " newurl
404
405         references=0
406         finished=0
407         oldversion=version
408         odp=get_links(newurl,filename)
409         if( odp ~ "ERROR: ") {
410                 print name "(" number ") " odp
411         } else {
412                 if (DEBUG) print "WebPage downloaded"
413                 c=split(odp,linki)
414                 for (nr=1; nr<=c; nr++) {
415                         addr=linki[nr]
416                         if (DEBUG) print "Found link: " addr
417                         if ((addr ~ filenameexp) && !(addr ~ "[-_.0-9A-Za-z~]" filenameexp)) {
418                                 match(addr,filenameexp)
419                                 newfilename=substr(addr,RSTART,RLENGTH)
420                                 if (DEBUG) print "Hypothetical new: " newfilename
421                                 newfilename=fixedsub(prever,"",newfilename)
422                                 newfilename=fixedsub(postver,"",newfilename)
423                                 if (DEBUG) print "Version: " newfilename
424                                 if (newfilename ~ /\.(asc|sig|pkg|bin|binary|built)$/) continue
425                                 if (NUMERIC) {
426                                         if ( compare_ver_dec(version, newfilename)==1 ) {
427                                                 if (DEBUG) print "Yes, there is new one"
428                                                 version=newfilename
429                                                 finished=1
430                                         }
431                                 } else if ( compare_ver(version, newfilename)==1 ) {
432                                         if (DEBUG) print "Yes, there is new one"
433                                         version=newfilename
434                                         finished=1
435                                 }
436                         }
437                 }
438                 if (finished==0)
439                         print name "(" number ") seems ok: " oldversion
440                 else
441                         print name "(" number ") [OLD] " oldversion " [NEW] " version
442         }
443 }
444
445 # upgrade check for pear package using PEAR CLI
446 function pear_upgrade(name, ver) {
447         pname = name;
448         sub(/^php-pear-/, "", pname);
449
450         pearcmd = "pear remote-info " pname " | awk '/^Latest/{print $NF}'"
451         if (DEBUG) {
452                 print "pearcmd: " pearcmd
453         }
454         pearcmd | getline nver
455         close(pearcmd)
456
457         if (compare_ver(ver, nver)) {
458                 print name " [OLD] " ver " [NEW] " nver
459         } else {
460                 print name " seems ok: " ver
461         }
462
463         return
464 }
465
466 function process_data(name,ver,rel,src) {
467         if (name ~ /^php-pear-/) {
468                 return pear_upgrade(name, ver);
469         }
470
471 # this function checks if substitutions were valid, and if true:
472 # processes each URL and tries to get current file list
473         for (i in src) {
474                 if ( src[i] ~ /%{nil}/ ) {
475                         gsub(/\%\{nil\}/, "", src[i])
476                 }
477                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
478                         if ( DEBUG ) print "Source: " src[i]
479                         process_source(i,src[i],name,ver)
480                 } else {
481                         print FNAME ":" i ": impossible substitution: " src[i]
482                 }
483         }
484 }
485
486 BEGIN {
487         # if U want to use DEBUG, run script with "-v DEBUG=1"
488         # or uncomment the line below
489         # DEBUG = 1
490
491         errno=system("wget --help > /dev/null 2>&1")
492         if (errno) {
493                 print "No wget installed!"
494                 exit 1
495         }
496         if (ARGC>=3 && ARGV[2]=="-n") {
497                 NUMERIC=1
498                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i]
499                 ARGC=ARGC-1
500         }
501 }
502
503 FNR==1 {
504         if ( ARGIND != 1 ) {
505                 # clean frameseen for each ARG
506                 for (i in frameseen) {
507                         delete frameseen[i]
508                 }
509                 frameseen[0] = 1
510
511                 process_data(NAME,VER,REL,SRC)
512                 NAME="" ; VER="" ; REL=""
513                 for (i in DEFS) delete DEFS[i]
514                 for (i in SRC) delete SRC[i]
515         }
516         FNAME=FILENAME
517         DEFS["_alt_kernel"]=""
518         DEFS["20"]="\\ "
519 }
520
521 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
522 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
523 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
524 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
525 /^[Ss]ource[0-9]*:/ { if (/(ftp|http|https):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
526 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
527
528 END {
529         process_data(NAME,VER,REL,SRC)
530 }
This page took 0.107501 seconds and 4 git commands to generate.