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