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