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