]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- added --inet option to wget call
[packages/rpm-build-tools.git] / pldnotify.awk
1 #!/bin/awk -f
2 # $Revision$, $Date$
3
4 function compare_ver(v1,v2) {
5 # compares version numbers
6         while (match(v1,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
7                 v1=(substr(v1,1,RSTART) "." substr(v1,RSTART+RLENGTH-1))
8         while (match(v2,/[a-zA-Z][0-9]|[0-9][a-zA-Z]/))
9                 v2=(substr(v2,1,RSTART) "." substr(v2,RSTART+RLENGTH-1))
10         sub("^0*","",v1)
11         sub("^0*","",v2)
12         if (DEBUG) print "v1 == " v1
13         if (DEBUG) print "v2 == " v2
14         count=split(v1,v1a,"\.")
15         count2=split(v2,v2a,"\.")
16         
17         if (count<count2) mincount=count 
18         else mincount=count2
19         
20         for (i=1; i<=mincount; i++) {
21                 if (v1a[i]=="") v1a[i]=0
22                 if (v2a[i]=="") v2a[i]=0
23                 if (DEBUG) print "i == " i
24                 if (DEBUG) print "v1[i] == " v1a[i]
25                 if (DEBUG) print "v2[i] == " v2a[i]
26                 if ((v1a[i]~/[0-9]/)&&(v2a[i]~/[0-9]/)) {
27                         if (length(v2a[i])>length(v1a[i]))
28                                 return 1
29                         else if (v2a[i]>v1a[i])
30                                 return 1
31                         else if (length(v1a[i])>length(v2a[i]))
32                                 return 0
33                         else if (v1a[i]>v2a[i])
34                                 return 0
35                 } else if ((v1a[i]~/[A-Za-z]/)&&(v2a[i]~/[A-Za-z]/)) {
36                         if (v2a[i]>v1a[i])
37                                 return 1
38                         else if (v1a[i]>v2a[i])
39                                 return 0
40                 } else if ((v1a[i]~"pre")||(v1a[i]~"beta")||(v1a[i]~"alpha"))
41                         return 1
42                 else
43                         return 0
44         }
45         if ((count2==mincount)&&(count!=count2)) {
46                 for (i=count2+1; i<=count; i++)
47                         if ((v1a[i]~"pre")||(v1a[i]~"beta")||(v1a[i]~"alpha")) 
48                                 return 1
49                 return 0
50         } else if (count!=count2) {
51                 for (i=count+1; i<=count2; i++)
52                         if ((v2a[i]~"pre")||(v2a[i]~"beta")||(v2a[i]~"alpha"))
53                                 return 0
54                 return 1
55         }
56         return 0
57 }
58
59 function get_links(url, errno,link,oneline,retval,odp,tmpfile) {
60 # get all <A HREF=..> tags from specified URL
61         "mktemp /tmp/XXXXXX" | getline tmpfile
62         close("mktemp /tmp/XXXXXX")
63         
64         if (DEBUG) print "Retrieving: " url
65         errno=system("wget --inet -O - \"" url "\" > " tmpfile " 2>/dev/null" )
66         
67         if (errno==0) {
68                 while (getline oneline < tmpfile)
69                         odp=(odp " " oneline)
70                 if ( DEBUG ) print "Odpowiedz: " odp
71         }
72                 
73         close(tmpfile)
74         system("rm -f " tmpfile)
75         if ( errno==0) {
76                 while ((tolower(odp) ~ /<frame[ \t]/)||(tolower(odp) ~ /href=/)) {
77                         if (tolower(odp) ~ /<frame[ \t]/) {
78                                 match(tolower(odp),/<frame[ \t][^>]*>/)
79                                 ramka=substr(odp,RSTART,RLENGTH)
80                                 odp=substr(odp,RSTART+RLENGTH)
81                                 match(tolower(ramka),/src="[^"]+"/)
82                                 newurl=substr(ramka,RSTART+5,RLENGTH-6)
83                                 if (DEBUG) print "Ramka: " newurl
84                                 retval=(retval " " get_links(newurl))
85                         } else {
86                                 match(tolower(odp),/href="[^"]+"/)
87                                 link=substr(odp,RSTART,RLENGTH)
88                                 odp=substr(odp,RSTART+RLENGTH)
89                                 link=substr(link,7,length(link)-7)
90                                 retval=(retval " " link)
91                         }
92                 }
93         } else {
94                 retval=("WGET ERROR: " errno)
95         }
96         
97         
98         if (DEBUG) print "Zwracane: " retval
99         return retval
100 }
101
102 function subst_defines(var,defs) {
103 # substitute all possible RPM macros
104         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
105                 oldvar=var
106                 for (j in defs) {
107                         gsub("%{" j "}", defs[j], var)
108                         gsub("%" j , defs[j], var)
109                 }
110                 if (var==oldvar) {
111                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
112                         return var
113                 }
114         }
115         return var
116 }
117
118 function process_source(number,lurl,name,version) {
119 # fetches file list, and compares version numbers
120         if ( DEBUG ) print "Przetwarzam " lurl
121
122         if ( index(lurl,version)==0 ) {
123                 if (DEBUG) print "Nie ma numeru wersji."
124                 return 0
125         }
126
127         sub("://",":",lurl)
128         sub("/",":/",lurl)
129         gsub("[^/]*$",":&",lurl)
130         split(lurl,url,":")
131         acc=url[1]
132         host=url[2]
133         dir=url[3]
134         filename=url[4]
135
136         if (index(dir,version)) {
137                 dir=substr(dir,1,index(dir,version)-1)
138                 sub("[^/]*$","",dir)
139                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
140                 if ( DEBUG ) print "Sprawdze katalog: " dir
141                 if ( DEBUG ) print "i plik: " filename
142         }
143
144         filenameexp=filename
145         gsub("\+","\\+",filenameexp)
146         sub(version,"[A-Za-z0-9\\.]+",filenameexp)
147         if ( DEBUG ) print "Wzorzec: " filenameexp
148         match(filename,version)
149         prever=substr(filename,1,RSTART-1)
150         postver=substr(filename,RSTART+RLENGTH)
151         if ( DEBUG ) print "Przed numerkiem: " prever
152         if ( DEBUG ) print "i po: " postver
153         
154         if ( DEBUG ) print "ZaglĀ±dam na " acc "://" host dir 
155         
156         references=0
157         finished=0
158         oldversion=version
159         odp=get_links(acc "://" host dir)
160         if( odp ~ "ERROR: ") {
161                 print name "(" number ") " odp
162         } else {
163                 c=split(odp,linki)
164                 for (nr=1; nr<=c; nr++) {
165                         addr=linki[nr]
166                         if (DEBUG) print "Znaleziony link: " addr
167                         if (addr ~ filenameexp) {
168                                 match(addr,filenameexp)
169                                 newfilename=substr(addr,RSTART,RLENGTH)
170                                 if (DEBUG) print "Hipotetyczny nowy: " newfilename
171                                 sub(prever,"",newfilename)
172                                 sub(postver,"",newfilename)
173                                 if (DEBUG) print "Wersja: " newfilename
174                                 if ( compare_ver(version, newfilename)==1 ) {
175                                         if (DEBUG) print "Tak, jest nowa"
176                                         version=newfilename
177                                         finished=1
178                                 }
179                         }
180                 }
181                 if (finished==0)
182                         print name "(" number ") seems ok: " oldversion
183                 else
184                         print name "(" number ") [OLD] " oldversion " [NEW] " version
185         }
186 }
187         
188 function process_data(name,ver,rel,src) {
189 # this function checks if substitutions were valid, and if true:
190 # processes each URL and tries to get current file list
191         for (i in src) {
192                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
193                         if ( DEBUG ) print "Zrodlo: " src[i]
194                         process_source(i,src[i],name,ver)
195                 } else {
196                         print FNAME ":" i ": niemozliwe podstawienie: " src[i]
197                 }
198         }
199 }
200
201 BEGIN {
202         # if U want to use DEBUG, run script with "-v DEBUG=1"
203         # or uncomment the line below
204         # DEBUG = 1
205
206         errno=system("wget --help > /dev/null 2>&1")
207         if (errno) {
208                 print "No wget installed!"
209                 exit 1
210         }
211 }
212
213 FNR==1 {
214         if ( ARGIND != 1 ) {
215                 process_data(NAME,VER,REL,SRC)
216                 NAME="" ; VER="" ; REL=""
217                 for (i in DEFS) delete DEFS[i]
218                 for (i in SRC) delete SRC[i]
219         }
220         FNAME=FILENAME
221 }
222
223 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
224 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
225 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
226 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
227 /^[Ss]ource[0-9]*:/ { if (/(ftp|http):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
228 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
229
230 END {
231         process_data(NAME,VER,REL,SRC)
232 }
This page took 0.053578 seconds and 4 git commands to generate.