]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pldnotify.awk
- use wget to retrieve HTTP source locations (solves all problems with
[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"))
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")) 
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"))
53                                 return 0
54                 return 1
55         }
56         return 0
57 }
58
59 function get_http_links(host,dir,port,  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         if (DEBUG) print "Retrieving HTML : http://" host ":" port dir
64         errno=system("wget -O - \"http://" host ":" port dir "\" > " tmpfile " 2>/dev/null" )
65         
66         if (errno==0) {
67                 while (getline oneline < tmpfile)
68                         odp=(odp " " oneline)
69                 if ( DEBUG ) print "Odpowiedz: " odp
70         }
71                 
72         close(tmpfile)
73         if ( errno==0) {
74                 while (tolower(odp) ~ /href=/) {
75                         match(tolower(odp),/href="[^"]+"/)
76                         link=substr(odp,RSTART,RLENGTH)
77                         odp=substr(odp,RSTART+RLENGTH)
78                         link=substr(link,7,length(link)-7)
79                         retval=(retval " " link)
80                 }
81         } else {
82                 retval=("HTTP ERROR: " errno)
83         }
84         
85         system("rm -f " tmpfile)
86         
87         if (DEBUG) print "Zwracane: " retval
88         return retval
89 }
90
91 function get_ftp_links(host,dir,port,   tmpfile,link,retval) {
92         "mktemp /tmp/XXXXXX" | getline tmpfile
93         close("mktemp /tmp/XXXXXX")
94         
95         errno=system("export PLIKTMP=\"" tmpfile "\" FTP_DIR=\"" dir "\" FTP_PASS=\"pldnotifier@pld.org.pl\" FTP_USERNAME=\"anonymous\" FTP_HOST=\"" host "\" DEBUG=\"" DEBUG "\" ; nc -e \"./ftplinks.sh\" " host " " port)
96         
97         if (DEBUG) print "Skonczylem sciagac"
98         if (errno==0) {
99                 while (getline link < tmpfile)
100                         retval=(retval " " link)
101                 close(tmpfile)
102         } else {
103                 retval=("FTP ERROR: " errno)
104         }
105
106         system("rm -f " tmpfile)
107         
108         if (DEBUG) print "Zwracane: " retval
109         return retval
110 }
111
112 function subst_defines(var,defs) {
113 # substitute all possible RPM macros
114         while ((var ~ /%{.*}/) || (var ~ /%[A-Za-z0-9_]+/)) {
115                 oldvar=var
116                 for (j in defs) {
117                         gsub("%{" j "}", defs[j], var)
118                         gsub("%" j , defs[j], var)
119                 }
120                 if (var==oldvar) {
121                         if ( DEBUG ) for (i in defs) print i " == " defs[i]
122                         return var
123                 }
124         }
125         return var
126 }
127
128 function process_source(number,lurl,name,version) {
129 # fetches file list, and compares version numbers
130         if ( DEBUG ) print "Przetwarzam " lurl
131
132         if ( index(lurl,version)==0 ) {
133                 if (DEBUG) print "Nie ma numeru wersji."
134                 return 0
135         }
136
137         sub("://",":",lurl)
138         sub("/",":/",lurl)
139         gsub("[^/]*$",":&",lurl)
140         split(lurl,url,":")
141         acc=url[1]
142         host=url[2]
143         dir=url[3]
144         filename=url[4]
145
146         if (index(dir,version)) {
147                 dir=substr(dir,1,index(dir,version)-1)
148                 sub("[^/]*$","",dir)
149                 sub("(\.tar\.(bz|bz2|gz)|zip)$","",filename)
150                 if ( DEBUG ) print "Sprawdze katalog: " dir
151                 if ( DEBUG ) print "i plik: " filename
152         }
153
154         filenameexp=filename
155         gsub("\+","\\+",filenameexp)
156         sub(version,"[A-Za-z0-9\\.]+",filenameexp)
157         if ( DEBUG ) print "Wzorzec: " filenameexp
158         match(filename,version)
159         prever=substr(filename,1,RSTART-1)
160         postver=substr(filename,RSTART+RLENGTH)
161         if ( DEBUG ) print "Przed numerkiem: " prever
162         if ( DEBUG ) print "i po: " postver
163         
164         if ( DEBUG ) print "ZaglĀ±dam na " acc "://" host dir 
165         
166         references=0
167         finished=0
168         oldversion=version
169         if (acc=="http") 
170                 odp=get_http_links(host,dir,80)
171         else {
172                 odp=get_ftp_links(host,dir,21)
173         }
174         if( odp ~ "ERROR: ") {
175                 print name "(" number ") " odp
176         } else {
177                 c=split(odp,linki)
178                 for (nr=1; nr<=c; nr++) {
179                         addr=linki[nr]
180                         if (DEBUG) print "Znaleziony link: " addr
181                         if (addr ~ filenameexp) {
182                                 match(addr,filenameexp)
183                                 newfilename=substr(addr,RSTART,RLENGTH)
184                                 if (DEBUG) print "Hipotetyczny nowy: " newfilename
185                                 sub(prever,"",newfilename)
186                                 sub(postver,"",newfilename)
187                                 if (DEBUG) print "Wersja: " newfilename
188                                 if ( compare_ver(version, newfilename)==1 ) {
189                                         if (DEBUG) print "Tak, jest nowa"
190                                         version=newfilename
191                                         finished=1
192                                 }
193                         }
194                 }
195                 if (finished==0)
196                         print name "(" number ") seems ok"
197                 else
198                         print name "(" number ") [OLD] " oldversion " [NEW] " version
199         }
200 }
201         
202 function process_data(name,ver,rel,src) {
203 # this function checks if substitutions were valid, and if true:
204 # processes each URL and tries to get current file list
205         for (i in src) {
206                 if ( src[i] !~ /%{.*}/ && src[i] !~ /%[A-Za-z0-9_]/ )  {
207                         if ( DEBUG ) print "Zrodlo: " src[i]
208                         process_source(i,src[i],name,ver)
209                 } else {
210                         print FNAME ":" i ": niemozliwe podstawienie: " src[i]
211                 }
212         }
213 }
214
215 BEGIN {
216         # if U want to use DEBUG, run script with "-v DEBUG=1"
217         # or uncomment the line below
218         # DEBUG = 1
219 }
220
221 FNR==1 {
222         if ( ARGIND != 1 ) {
223                 process_data(NAME,VER,REL,SRC)
224                 NAME="" ; VER="" ; REL=""
225                 for (i in DEFS) delete DEFS[i]
226                 for (i in SRC) delete SRC[i]
227         }
228         FNAME=FILENAME
229 }
230
231 /^[Uu][Rr][Ll]:/&&(URL=="") { URL=subst_defines($2,DEFS) ; DEFS["url"]=URL }
232 /^[Nn]ame:/&&(NAME=="") { NAME=subst_defines($2,DEFS) ; DEFS["name"]=NAME }
233 /^[Vv]ersion:/&&(VER=="") { VER=subst_defines($2,DEFS) ; DEFS["version"]=VER }
234 /^[Rr]elease:/&&(REL=="") { REL=subst_defines($2,DEFS) ; DEFS["release"]=REL }
235 /^[Ss]ource[0-9]*:/ { if (/(ftp|http):\/\//) SRC[FNR]=subst_defines($2,DEFS) }
236 /%define/ { DEFS[$2]=subst_defines($3,DEFS) }
237
238 END {
239         process_data(NAME,VER,REL,SRC)
240 }
This page took 0.090714 seconds and 4 git commands to generate.