]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- handle cgit snapshot urls (libva.spec as example)
[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 # - support debian/watch http://wiki.debian.org/debian/watch/
18
19
20 function d(s) {
21         if (!DEBUG) {
22                 return
23         }
24         print s >> "/dev/stderr"
25 }
26
27 function fixedsub(s1,s2,t,      ind) {
28 # substitutes fixed strings (not regexps)
29         if (ind = index(t,s1)) {
30                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
31         }
32         return t
33 }
34
35 function ispre(s) {
36         if ((s~"pre")||(s~"PRE")||(s~"beta")||(s~"BETA")||(s~"alpha")||(s~"ALPHA")||(s~"rc")||(s~"RC")) {
37                 d("pre-version")
38                 return 1
39         } else {
40                 return 0
41         }
42 }
43
44 function compare_ver(v1,v2) {
45 # compares version numbers
46         while (match(v1,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
47                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
48         while (match(v2,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
49                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
50         sub("^0*","",v1)
51         sub("^0*","",v2)
52         gsub("\.0*",".",v1)
53         gsub("\.0*",".",v2)
54         d("v1 == " v1)
55         d("v2 == " v2)
56         count=split(v1,v1a,"\.")
57         count2=split(v2,v2a,"\.")
58
59         if (count<count2) mincount=count
60         else mincount=count2
61
62         for (i=1; i<=mincount; i++) {
63                 if (v1a[i]=="") v1a[i]=0
64                 if (v2a[i]=="") v2a[i]=0
65                 d("i == " i)
66                 d("v1[i] == " v1a[i])
67                 d("v2[i] == " v2a[i])
68                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
69                         if (length(v2a[i])>length(v1a[i]))
70                                 return 1
71                         else if (v2a[i]>v1a[i])
72                                 return 1
73                         else if (length(v1a[i])>length(v2a[i]))
74                                 return 0
75                         else if (v1a[i]>v2a[i])
76                                 return 0
77                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
78                         if (v2a[i]>v1a[i])
79                                 return 1
80                         else if (v1a[i]>v2a[i])
81                                 return 0
82                 } else if (ispre(v1a[i]) == 1)
83                         return 1
84                 else
85                         return 0
86         }
87         if ((count2==mincount)&&(count!=count2)) {
88                 for (i=count2+1; i<=count; i++)
89                         if (ispre(v1a[i]) == 1)
90                                 return 1
91                 return 0
92         } else if (count!=count2) {
93                 for (i=count+1; i<=count2; i++)
94                         if (ispre(v2a[i]) == 1)
95                                 return 0
96                 return 1
97         }
98         return 0
99 }
100
101 function compare_ver_dec(v1,v2) {
102 # compares version numbers as decimal floats
103         while (match(v1,/[0-9][a-zA-Z]/))
104                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
105         while (match(v2,/[0-9][a-zA-Z]/))
106                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
107         sub("^0*","",v1)
108         sub("^0*","",v2)
109         d("v1 == " v1)
110         d("v2 == " v2)
111         count=split(v1,v1a,"\.")
112         count2=split(v2,v2a,"\.")
113
114         if (count<count2) mincount=count
115         else mincount=count2
116
117         for (i=1; i<=mincount; i++) {
118                 if (v1a[i]=="") v1a[i]=0
119                 if (v2a[i]=="") v2a[i]=0
120                 d("i == " i)
121                 d("v1[i] == " v1a[i])
122                 d("v2[i] == " v2a[i])
123                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
124                         if (i==2) {
125                                 if (0+("." v2a[i])>0+("." v1a[i]))
126                                         return 1
127                                 else if (0+("." v1a[i])>0+("." v2a[i]))
128                                         return 0
129                         } else {
130                                 if (length(v2a[i])>length(v1a[i]))
131                                         return 1
132                                 else if (v2a[i]>v1a[i])
133                                         return 1
134                                 else if (length(v1a[i])>length(v2a[i]))
135                                         return 0
136                                 else if (v1a[i]>v2a[i])
137                                         return 0
138                         }
139                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
140                         if (v2a[i]>v1a[i])
141                                 return 1
142                         else if (v1a[i]>v2a[i])
143                                 return 0
144                 } else if (ispre(v1a[i]) == 1)
145                         return 1
146                 else
147                         return 0
148         }
149         if ((count2==mincount)&&(count!=count2)) {
150                 for (i=count2+1; i<=count; i++)
151                         if (ispre(v1a[i]) == 1)
152                                 return 1
153                 return 0
154         } else if (count!=count2) {
155                 for (i=count+1; i<=count2; i++)
156                         if (ispre(v2a[i]) == 1)
157                                 return 0
158                 return 1
159         }
160         return 0
161 }
162
163 function link_seen(link) {
164         for (seenlink in frameseen) {
165                 if (seenlink == link) {
166                         d("Link: [" link "] seen already, skipping...")
167                         return 1
168                 }
169         }
170         frameseen[link]=1
171         return 0
172 }
173
174 function mktemp(   _cmd, _tmpfile) {
175         _cmd = "mktemp /tmp/XXXXXX"
176         _cmd | getline _tmpfile
177         close(_cmd)
178         return _tmpfile
179 }
180
181 # fix link to artificial one that will be recognized rest of this script
182 function postfix_link(url, link) {
183         oldlink = link
184         if ((url ~/^(http|https):\/\/github.com\//) && (link ~ /.*\/tarball\//)) {
185                 gsub(".*\/tarball\/", "", link)
186                 link = link ".tar.gz"
187         }
188         d("POST FIXING URL [ " oldlink " ] to [ " link " ]")
189         return link
190 }
191
192 # get all <A HREF=..> tags from specified URL
193 function get_links(url,filename,   errno,link,oneline,retval,odp,wholeodp,lowerodp,tmpfile,cmd) {
194
195         wholeerr=""
196
197         tmpfile = mktemp()
198         tmpfileerr = mktemp()
199
200         if (url ~ /^http:\/\/(download|dl).(sf|sourceforge).net\//) {
201                 gsub("^http://(download|dl).(sf|sourceforge).net/", "", url)
202                 gsub("/.*", "", url)
203                 url = "http://sourceforge.net/projects/" url "/files/"
204                 d("sf url, mungled url to: " url)
205         }
206
207         if (url ~ /^http:\/\/(.*)\.googlecode\.com\/files\//) {
208                 gsub("^http://", "", url)
209                 gsub("\..*", "", url)
210                 url = "http://code.google.com/p/" url "/downloads/list"
211                 d("googlecode url, mungled url to: " url)
212         }
213
214         if (url ~ /^http:\/\/pecl.php.net\/get\//) {
215                 gsub("-.*", "", filename)
216                 url = "http://pecl.php.net/package/" filename
217                 d("pecl.php.net url, mungled url to: " url)
218         }
219
220         if (url ~ /^(http|ftp):\/\/mysql.*\/Downloads\/MySQL-5.1\//) {
221                 url = "http://dev.mysql.com/downloads/mysql/5.1.html#source"
222                  d("mysql 5.1 url, mungled url to: " url)
223         }
224
225         if (url ~/^(http|https):\/\/launchpad\.net\/(.*)\//) {
226                 gsub("^(http|https):\/\/launchpad\.net\/", "", url)
227                 gsub("\/.*/", "", url)
228                 url = "https://code.launchpad.net/" url "/+download"
229                 d("main launchpad url, mungled url to: " url)
230         }
231
232         if (url ~/^(http|https):\/\/edge\.launchpad\.net\/(.*)\//) {
233                 gsub("^(http|https):\/\/edge\.launchpad\.net\/", "", url)
234                 gsub("\/.*/", "", url)
235                 url = "https://edge.launchpad.net/" url "/+download"
236                 d("edge launchpad url, mungled url to: " url)
237         }
238
239         if (url ~/^(http|https):\/\/github.com\/.*\/(.*)\/tarball\//) {
240                 gsub("\/tarball\/.*", "/downloads", url)
241                 d("github tarball url, mungled url to: " url)
242         }
243
244         if (url ~/^(http|https):\/\/cgit\..*\/(.*)\/snapshot\//) {
245                 gsub("\/snapshot\/.*", "/", url)
246                 d("cgit snapshot tarball url, mungled url to: " url)
247         }
248
249         d("Retrieving: " url)
250         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
251         d("Execute: " cmd)
252         errno = system(cmd)
253         d("Execute done")
254
255         if (errno==0) {
256                 wholeodp = ""
257                 d("Reading succeess response...")
258                 while (getline oneline < tmpfile)
259                         wholeodp=(wholeodp " " oneline)
260 #                       d("Response: " wholeodp)
261         } else {
262                 d("Reading failure response...")
263                 wholeerr = ""
264                 while (getline oneline < tmpfileerr)
265                         wholeerr=(wholeerr " " oneline)
266                 d("Error Response: " wholeerr)
267         }
268
269         system("rm -f " tmpfile)
270         system("rm -f " tmpfileerr)
271
272         urldir=url;
273         sub(/[^\/]+$/,"",urldir)
274
275         if ( errno==0) {
276                 while (match(wholeodp, /<([aA]|[fF][rR][aA][mM][eE])[ \t][^>]*>/) > 0) {
277                         d("Processing links...")
278                         odp=substr(wholeodp,RSTART,RLENGTH);
279                         wholeodp=substr(wholeodp,RSTART+RLENGTH);
280
281                         lowerodp=tolower(odp);
282                         if (lowerodp ~ /<frame[ \t]/) {
283                                 sub(/[sS][rR][cC]=[ \t]*/,"src=",odp);
284                                 match(odp,/src="[^"]+"/)
285                                 newurl=substr(odp,RSTART+5,RLENGTH-6)
286                                 d("Frame: " newurl)
287                                 if (newurl !~ /\//) {
288                                         newurl=(urldir newurl)
289                                         d("Frame->: " newurl)
290                                 }
291
292                                 if (link_seen(newurl)) {
293                                         newurl=""
294                                         continue
295                                 }
296
297                                 retval=(retval " " get_links(newurl))
298                         } else if (lowerodp ~ /href=[ \t]*"[^"]*"/) {
299                                 sub(/[hH][rR][eE][fF]=[ \t]*"/,"href=\"",odp)
300                                 match(odp,/href="[^"]*"/)
301                                 link=substr(odp,RSTART,RLENGTH)
302                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
303                                 link=substr(link,7,length(link)-7)
304                                 link=postfix_link(url, link)
305
306                                 if (link_seen(link)) {
307                                         link=""
308                                         continue
309                                 }
310
311                                 retval=(retval " " link)
312                                 d("href(\"\"): " link)
313                         } else if (lowerodp ~ /href=[ \t]*'[^']*'/) {
314                                 sub(/[hH][rR][eE][fF]=[ \t]*'/,"href='",odp)
315                                 match(odp,/href='[^']*'/)
316                                 link=substr(odp,RSTART,RLENGTH)
317                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
318                                 link=substr(link,7,length(link)-7)
319                                 link=postfix_link(url, link)
320
321                                 if (link_seen(link)) {
322                                         link=""
323                                         continue
324                                 }
325
326                                 retval=(retval " " link)
327                                 d("href(''): " link)
328                         } else if (lowerodp ~ /href=[ \t]*[^ \t>]*/) {
329                                 sub(/[hH][rR][eE][fF]=[ \t]*/,"href=",odp)
330                                 match(odp,/href=[^ \t>]*/)
331                                 link=substr(odp,RSTART,RLENGTH)
332                                 odp=substr(odp,1,RSTART) substr(odp,RSTART+RLENGTH)
333                                 link=substr(link,6,length(link)-5)
334
335                                 if (link_seen(link)) {
336                                         link=""
337                                         continue
338                                 }
339
340                                 retval=(retval " " link)
341                                 d("href(): " link)
342                         } else {
343                                 # <a ...> but not href - skip
344                                 d("skipping <a > without href: " odp)
345                         }
346                 }
347         } else {
348                 retval=("WGET ERROR: " errno ": " wholeerr)
349         }
350
351
352         d("Returning: " retval)
353         return retval
354 }
355
356 function subst_defines(var,defs) {
357 # substitute all possible RPM macros
358         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
359                 oldvar=var
360                 for (j in defs) {
361                         gsub("%{" j "}", defs[j], var)
362                         gsub("%" j , defs[j], var)
363                 }
364                 if (var==oldvar) {
365                         if (DEBUG) {
366                                 for (i in defs) {
367                                         d(i " == " defs[i])
368                                 }
369                         }
370                         return var
371                 }
372         }
373         return var
374 }
375
376 function find_mirror(url) {
377
378         while (succ = (getline line < "mirrors")) {
379             if (succ==-1) { return url }
380                 nf=split(line,fields,"|")
381                 if (nf>1){
382                         origin=fields[1]
383                         mirror=fields[2]
384                         mname=fields[3]
385                         prefix=substr(url,1,length(origin))
386                         if (prefix==origin){
387                                 d("Mirror fount at " mname)
388                                 close("mirrors")
389                                 return mirror substr(url,length(origin)+1)
390                         }
391                 }
392         }
393
394         return url
395 }
396
397 function process_source(number,lurl,name,version) {
398 # fetches file list, and compares version numbers
399         d("Processing " lurl)
400
401         if ( index(lurl,version)==0 ) {
402                 d("There is no version number.")
403                 return 0
404         }
405
406         sub("://",":",lurl)
407         sub("/",":/",lurl)
408         gsub("[^/]*$",":&",lurl)
409         split(lurl,url,":")
410         acc=url[1]
411         host=url[2]
412         dir=url[3]
413         filename=url[4]
414
415         if (index(dir,version)) {
416                 # directory name as version maching mode:
417                 # if /something/version/name-version.tarball then check
418                 # in /something/ looking for newer directory
419                 dir=substr(dir,1,index(dir,version)-1)
420                 sub("[^/]*$","",dir)
421                 sub("(\.tar\.(bz|bz2|gz|lzma|xz)|zip)$","",filename)
422         }
423
424         d("Will check a directory: " dir)
425         d("and a file: " filename)
426
427         filenameexp=filename
428         gsub("\+","\\+",filenameexp)
429         sub(version,"[A-Za-z0-9.]+",filenameexp)
430         gsub("\.","\\.",filenameexp)
431         d("Expression: " filenameexp)
432         match(filename,version)
433         prever=substr(filename,1,RSTART-1)
434         postver=substr(filename,RSTART+RLENGTH)
435         d("Before number: " prever)
436         d("and after: " postver)
437         newurl=find_mirror(acc "://" host dir)
438         #print acc "://" host dir
439         #newurl=url[1]"://"url[2]url[3]url[4]
440         #newurl=acc "://" host dir filename
441         d("Looking at " newurl)
442
443         references=0
444         finished=0
445         oldversion=version
446         odp=get_links(newurl,filename)
447         if( odp ~ "ERROR: ") {
448                 print name "(" number ") " odp
449         } else {
450                 d("WebPage downloaded")
451                 c=split(odp,linki)
452                 for (nr=1; nr<=c; nr++) {
453                         addr=linki[nr]
454
455                         d("Found link: " addr)
456
457                         # github has very different tarball links that clash with this safe check
458                         if (!(newurl ~/^(http|https):\/\/github.com\/.*\/tarball/)) {
459                                 if (addr ~ "[-_.0-9A-Za-z~]" filenameexp) {
460                                         continue
461                                 }
462                         }
463
464                         if (addr ~ filenameexp) {
465                                 match(addr,filenameexp)
466                                 newfilename=substr(addr,RSTART,RLENGTH)
467                                 d("Hypothetical new: " newfilename)
468                                 newfilename=fixedsub(prever,"",newfilename)
469                                 newfilename=fixedsub(postver,"",newfilename)
470                                 d("Version: " newfilename)
471                                 if (newfilename ~ /\.(asc|sig|pkg|bin|binary|built)$/) continue
472                                 # strip ending (happens when in directiory name as version matching mode)
473                                 sub("(\.tar\.(bz|bz2|gz|lzma|xz)|zip)$","",newfilename)
474                                 if (NUMERIC) {
475                                         if ( compare_ver_dec(version, newfilename)==1 ) {
476                                                 d("Yes, there is new one")
477                                                 version=newfilename
478                                                 finished=1
479                                         }
480                                 } else if ( compare_ver(version, newfilename)==1 ) {
481                                         d("Yes, there is new one")
482                                         version=newfilename
483                                         finished=1
484                                 }
485                         }
486                 }
487                 if (finished==0)
488                         print name "(" number ") seems ok: " oldversion
489                 else
490                         print name "(" number ") [OLD] " oldversion " [NEW] " version
491         }
492 }
493
494 function rss_upgrade(name, ver, url, regex, cmd, nver) {
495         regex = "s/.*<title>" regex "<\/title>.*/\\1/p"
496         cmd = "wget -q -O - " url " | sed -nre '" regex "' | head -n1"
497
498         d("rss_upgrade_cmd: " cmd)
499         cmd | getline nver
500         close(cmd)
501
502         if (compare_ver(ver, nver)) {
503                 print name " [OLD] " ver " [NEW] " nver
504         } else {
505                 print name " seems ok: " ver
506         }
507 }
508
509 # check for ZF upgrade from rss
510 function zf_upgrade(name, ver) {
511         rss_upgrade(name, ver, \
512                 "http://devzone.zend.com/tag/Zend_Framework_Management/format/rss2.0", \
513                 "Zend Framework ([^\\s]+) Released" \
514         );
515 }
516
517 function hudson_upgrade(name, ver) {
518         rss_upgrade(name, ver, \
519                 "https://hudson.dev.java.net/servlets/ProjectRSS?type=news", \
520                 "Hudson ([0-9.]+) released" \
521         );
522 }
523
524 # upgrade check for pear package using PEAR CLI
525 function pear_upgrade(name, ver,    pname, pearcmd, nver) {
526         pname = name;
527         sub(/^php-pear-/, "", pname);
528
529         pearcmd = "pear remote-info " pname " | awk '/^Latest/{print $NF}'"
530         d("pearcmd: " pearcmd)
531         pearcmd | getline nver
532         close(pearcmd)
533
534         if (compare_ver(ver, nver)) {
535                 print name " [OLD] " ver " [NEW] " nver
536         } else {
537                 print name " seems ok: " ver
538         }
539
540         return
541 }
542
543 function vim_upgrade(name, ver,     mver, nver, vimcmd) {
544         # %patchset_source -f ftp://ftp.vim.org/pub/editors/vim/patches/7.2/7.2.%03g 1 %{patchlevel}
545         mver = substr(ver, 0, 4)
546         vimcmd = "wget -q -O - ftp://ftp.vim.org/pub/editors/vim/patches/"mver"/MD5SUMS|grep -vF .gz|tail -n1|awk '{print $2}'"
547         d("vimcmd: " vimcmd)
548         vimcmd | getline nver
549         close(vimcmd)
550
551         if (compare_ver(ver, nver)) {
552                 print name " [OLD] " ver " [NEW] " nver
553         } else {
554                 print name " seems ok: " ver
555         }
556 }
557
558 function process_data(name,ver,rel,src) {
559         if (name ~ /^php-pear-/) {
560                 return pear_upgrade(name, ver);
561         }
562         if (name == "ZendFramework") {
563                 return zf_upgrade(name, ver);
564         }
565         if (name == "hudson") {
566                 return hudson_upgrade(name, ver);
567         }
568         if (name == "vim") {
569                 return vim_upgrade(name, ver);
570         }
571
572 # this function checks if substitutions were valid, and if true:
573 # processes each URL and tries to get current file list
574         for (i in src) {
575                 if ( src[i] ~ /%{nil}/ ) {
576                         gsub(/\%\{nil\}/, "", src[i])
577                 }
578                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
579                         d("Source: " src[i])
580                         process_source(i,src[i],name,ver)
581                 } else {
582                         print FNAME ":" i ": impossible substitution: " src[i]
583                 }
584         }
585 }
586
587 BEGIN {
588         # if U want to use DEBUG, run script with "-v DEBUG=1"
589         # or uncomment the line below
590         # DEBUG = 1
591
592         errno=system("wget --help > /dev/null 2>&1")
593         if (errno) {
594                 print "No wget installed!"
595                 exit 1
596         }
597         if (ARGC>=3 && ARGV[2]=="-n") {
598                 NUMERIC=1
599                 for (i=3; i<ARGC; i++) ARGV[i-1]=ARGV[i]
600                 ARGC=ARGC-1
601         }
602 }
603
604 FNR==1 {
605         if ( ARGIND != 1 ) {
606                 # clean frameseen for each ARG
607                 for (i in frameseen) {
608                         delete frameseen[i]
609                 }
610                 frameseen[0] = 1
611
612                 process_data(NAME,VER,REL,SRC)
613                 NAME="" ; VER="" ; REL=""
614                 for (i in DEFS) delete DEFS[i]
615                 for (i in SRC) delete SRC[i]
616         }
617         FNAME=FILENAME
618         DEFS["_alt_kernel"]=""
619         DEFS["20"]="\\ "
620 }
621
622 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
623 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
624 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
625 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
626 /^[Ss]ource[0-9]*:/ { if (/(ftp|http|https):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
627 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
628
629 END {
630         process_data(NAME,VER,REL,SRC)
631 }
This page took 0.105546 seconds and 4 git commands to generate.