/[dehs]/trunk/dehs_pg.php
ViewVC logotype

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations) (download)
Fri Aug 3 22:37:29 2007 UTC (5 years, 9 months ago) by evaso-guest
File size: 36698 byte(s)
Apply a patch from Thijs Kinkhorst to try to close debian bug #269493 
1 #!/usr/bin/php4 -q
2 <?
3 /*
4
5 Originally written by Stefano Fabri <bluefuture@nospam@email.it>
6 Copyright 2004, Stefano Fabri
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 // stabilire una connessione
23 #ini_set ( "error_reporting", "E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR");
24 ini_set ("html_errors", "off");
25 set_time_limit(0);
26 ini_set ( "memory_limit", "40M");
27 #print_R($_SERVER);
28 require "dehs.config.inc";
29 dir_ctrl($dirs);
30 if (!function_exists('file_get_contents')) {
31 function file_get_contents($filename) {
32 if (!($file = fopen($filename,"r"))) return false;
33 else {
34 $contents = "";
35 do {
36 $data = fread($file, 4096);
37 if (strlen($data) == 0) {
38 break;
39 }
40 $contents .= $data;
41 } while (true);
42 fclose($file);
43 return $contents;
44 }
45 }
46 }
47 if (in_array("-db_add",$argv)) db_add($argv[2]);
48 elseif (in_array("-db_query",$argv)) db_query($argv[2],$argv[3] );
49 elseif (in_array("-keep_diff",$argv)) keep_diff($argv[2]);
50 elseif (in_array("-keep_sources",$argv)) keep_sources();
51 elseif (in_array("-db_popcon",$argv)) db_popcon($argv[2]);
52 elseif (in_array("-keep_popcon",$argv)) keep_popcon($argv[2]);
53 elseif (in_array("-db_upstream",$argv)) db_upstream($argv[2]);
54 elseif (in_array("-update_all",$argv)) update_all();
55 elseif (in_array("-check_db",$argv)) check_db();
56 elseif (in_array("-dehsqa_db",$argv)) dehsqa_db();
57 elseif (in_array("-clear_db",$argv)) clear_db();
58 elseif (in_array("-db_up_error",$argv)) db_up_error();
59 elseif (in_array("-up_changes",$argv)) up_changes($argv[2]);
60 elseif (in_array("-watchf",$argv)) watch_fill($argv[2]);
61 elseif (in_array("-kb_total",$argv)) print "Diff
62 #stat_up_error();
63 #stat_up_error();to download Kb => " . kb_total($argv[2]) . "\n" ;
64 elseif (in_array("-watch_stat",$argv)) watch_stat();
65 else print "Command $argv[1] not found\n";
66 #stat_up_error();
67 function dir_ctrl($dirs) {
68 foreach ($dirs as $dir) {
69 if (!is_dir($dir)) exec("mkdir -m 0740 -p $dir",$output,$res);
70 if ($res!=0) die("Error creating basic dir: $dir\n");
71 }
72 }
73 function ext_watch ($file,$pkg,$version){
74 if (!file_exists($file)) return "";
75 $zp=gzopen($file,"r") or die("Fail to open $file");
76 while (!gzeof($zp)) {
77 $line = gzgets ($zp,4096);
78 if (preg_match("/\+\+\+\s.+\/debian\/watch/",$line)) {
79 $line = gzgets ($zp,4096);
80 preg_match ("/.+\+\d+\,(\d+)\s/",$line,$watch_lenght);
81 for ($i = 1; $i <= $watch_lenght[1]; $i++) {
82 $line=gzgets ($zp,4096);
83 $watch .=substr($line,1);
84 }
85 break;
86 }
87 }
88 if ($watch!=null) return $watch;
89 else return NULL;
90 }
91 function ftp_cn($login,$password,$ftp_server) {
92 $conn_id = @ftp_connect($ftp_server) or DIE("Error: Cannot contact ftp server $ftp_server\n");
93 // login con user name e password
94 ftp_pasv($conn_id,1);
95 $login_result = ftp_login($conn_id, "anonymous","bo@bo.org");
96 // controllo della connessione
97 if ((!$conn_id) || (!$login_result)) {
98 echo "Ftp connection failed!\n";
99 echo "Failed connection to $ftp_server\n";
100 die();
101 } else {
102 return $conn_id;
103 }
104 }
105 function keep_diff($initial) {
106 global $dirs,$dbconn,$mirrors;
107 check_db();
108 $conn_normal=ftp_cn("anonymous","bo@bo.org",$mirrors['unstable']);
109 #$conn_non_us=ftp_cn("anonymous","bo@bo.org",$mirrors["non-US"]);
110 $db = pg_pconnect($dbconn) ;
111 $rsql=pg_exec($db, "SELECT * FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)") OR DIE("Error in diff query\n");
112 $total=pg_numrows($rsql);
113 $btotal=byte_total($db,$initial);
114 print "Total Diff to downlaod Kb => " . number_format ($btotal/1024, 2, ",",".") . "\n";
115 while ($res_array=pg_fetch_array($rsql)) {
116 #if ($res_array['dist']=='non-US') $conn_id = $conn_non_us;
117 #else
118 $conn_id = $conn_normal;
119 if ($localfile=download_diffs($conn_id,$res_array[name],$res_array[version],$res_array['dir'],$res_array[md5_diff],$res_array[md5_atsource],$res_array[bytes],$res_array[dist])) {
120 $watch=ext_watch($localfile,$res_array[name],$res_array[version]);
121 unlink($localfile);
122 if ($watch!=NULL) {
123 $watch=iconv("ISO-8859-1","UTF-8",$watch);
124 $watch= addslashes($watch);
125 pg_exec($db, "UPDATE pkgs SET watch='" . $watch . "', md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or Die("Query error");
126 }
127 else pg_exec($db, "UPDATE pkgs SET watch=NULL, md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or Die("Errore nella query");
128 }
129 $btotal=$btotal-$res_array['bytes'];
130 number_format ($btotal/1024, 2, ",",".");
131 print "\rDiffs left => " . $total-- . " Bytes left => " . number_format ($btotal/1024, 2, ",",".") . " - " . $res_array[name];
132 }
133 ftp_quit($conn_normal);
134 pg_close($db);
135 return "Fatto\n";
136 }
137 function download_diffs(&$conn_id,$pkg,$version,$directory,$md5_diff,$md5_atsource,$diff_kb,$dist) {
138 global $dirs,$mirrors;
139 $localfile=$dirs[diffs_dir] . "/$pkg" . "_" . "$version.diff.gz";
140 #if ($dist=="non-US") $remotefile="/debian-non-US/$directory/$pkg" . "_" . "$version.diff.gz";
141 #else
142 $remotefile="/debian/$directory/$pkg" . "_" . "$version.diff.gz";
143 #print "$localfile - $remotefile\n";
144 if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
145 # print "$pkg Diff.gz => Downloaded\n";
146 if(md5_of_file($localfile)==$md5_atsource) {
147 #print "MD5 => OK\n";
148 #unlink($localfile);
149 return $localfile;
150 }
151 }
152 print "Download Diff.gz of package $pkg => Fallito\n";
153 return false;
154 }
155 function db_add($initial=''){
156 global $dirs,$dists,$dbconn;
157 check_db();
158 keep_source();
159 $db = pg_connect($dbconn) or die(pg_errormessage($db));
160 pg_exec($db, "CREATE TEMP TABLE pkgs_atsrc (name text,dist text)") or die('Error creating temp table pkgs_atsrc');
161 pg_exec($db, "CREATE UNIQUE INDEX idxdis on pkgs_atsrc (name,dist)") or die('Error creating index on temp table');
162 pg_exec($db, "CREATE TEMP TABLE bin_atsrc (name text,bin_name text, dist text)") or die('Error creating temp table pkgs_atsrc');
163 pg_exec($db, "CREATE UNIQUE INDEX idxbis on bin_atsrc (name,bin_name,dist)") or die('Error creating index on temp table bin_atsrc');
164 foreach ($dists as $dist=>$sections) {
165 foreach ($sections as $section) {
166 $filename=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
167 print "\nI'm parsing $dist/$section => Sources.gz\n";
168 # 1 Pacchetto - 2 versione - 3 np - 4 Directory - 5 md5
169 $regexp="/Package:\s($initial.*)[^a]Binary:\s(.+)[^a]Version:\s(\d+:)?(.+)[^a]Priority.+Maintainer:.+<([^>]+)>[^a].+Directory:\s(.+)[^a]Files:.+\s(\S+)\s(\d+)\s" . '\1\S+' . "\.diff\.gz[^a](Uploaders:\s(.+)[^a])?/Ssi";
170 $zp = gzopen($filename, "r") or die("Could not open $filename");
171 $extracted=tempnam("/tmp/", "$dist_$section_Sources");
172 exec("gzip -c -d $filename > $extracted");
173 $sourcesize=filesize($extracted);
174 unlink($extracted);
175 if(filesize($filename)>20) {
176 while (!gzeof($zp)) {
177 $line='';
178 while ($line!="\n") {
179 $line = gzgets ($zp,4096) ;
180 $buff1 .= $line;
181 }
182 if (preg_match($regexp,$buff1,$matches)) {
183 if ($matches[10]) {
184 $matches[10]=iconv("ISO-8859-1","UTF-8",$matches[10]);
185 $matches[10]=addslashes($matches[10]);
186 }
187 @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR DIE("Temp table pkgs_atsrc query error");
188 $rst=@pg_exec($db, "INSERT INTO pkgs (name,version,maint,dir,md5_atsource,bytes,dist,section,uploaders) VALUES ('$matches[1]','$matches[4]','$matches[5]','$matches[6]','$matches[7]','$matches[8]','$dist','$section','$matches[10]')") ;
189 if (!$rst) {
190 $rst=@pg_exec($db, "UPDATE pkgs SET name='$matches[1]',version='$matches[4]',maint='$matches[5]',dir='$matches[6]',md5_atsource='$matches[7]',bytes='$matches[8]',dist='$dist',section='$section',uploaders='$matches[10]' WHERE name='$matches[1]' AND dist='$dist'") OR DIE ("\nDb adding error =>" . pg_errormessage() . "\n");
191 }
192 $bin_names=split(",", $matches[2]);
193 foreach ($bin_names as $bin_name) {
194 pg_exec($db,"INSERT INTO bin_atsrc (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") OR DIE("Temp table pkgs_atsrc query error");
195 $rst=@pg_exec($db, "INSERT INTO binpkgs (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") ;
196 }
197 }
198 else {
199 preg_match("/Package:\s(.+)[^a]Binary/im",$buff1,$matches);
200 if(strncmp($initial,$matches[1],strlen($initial))<0) break;
201 }
202 $left=$sourcesize-gztell($zp);
203 //print "\rSource file Left => $left";
204 $buff1="";
205 unset($matches);
206 }
207 }
208 gzclose($zp) ;
209 }
210 }
211 clear_db($db);
212 pg_close($db);
213 }
214 function db_query($pkg,$dist) {
215 global $dirs,$dbconn;
216 $db = pg_pconnect($dbconn ) or die(pg_errormessage($db));
217 $rsql=pg_exec($db, "SELECT pkgs.* ,mpop_inst FROM (SELECT name,MAX(binpkgs.pop_inst) AS mpop_inst FROM binpkgs GROUP BY name)
218 AS binpkgs INNER JOIN pkgs ON pkgs.name=binpkgs.name
219 WHERE pkgs.name='$pkg' AND dist='$dist';");
220 if (pg_numrows($rsql)==1) {
221 $res_array=pg_fetch_array($rsql);
222 print "Package=> " . $res_array['name'] . "\n";
223 print "Version => " . $res_array['version'] . "\n";
224 print "Diff MD5 Downloaded => " . $res_array['md5_diff'] . "\n";
225 print "Diff MD5 at Sources => " . $res_array['md5_atsource'] . "\n";
226 print "Distribution => " . $res_array['dist'] . "\n";
227 print "Section => " . $res_array['section'] . "\n";
228 print "KBytes => " . number_format ( $res_array['bytes']/1024, 2, ",",".") . "\n";
229 print "Pop Inst=>" . $res_array['mpop_inst'] . "\n";
230 if ($res_array['watch']) {
231 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['watch']);
232 print "Version Upstream => " . $uscan_res[0] . "\n";
233 print "Watch => " . $res_array['watch'];
234 print "Watch Warings => " . $uscan_res[1];
235 }
236 else print "Watch => Not present" . "\n";
237 }
238 else echo "Package=> " . $pkg . "\nNot found\n!!";
239 pg_close($db);
240 }
241 function byte_total(&$db,$initial){
242 global $dirs;
243 $rsql=pg_exec ($db,"Select sum(bytes) as totbytes FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)");
244 $res_array=pg_fetch_array($rsql);
245 return $res_array[totbytes] ;
246 }
247 function keep_source (){
248 global $dirs,$dists,$mirrors;
249 foreach ($dists as $dist=>$sections) {
250 $server=$mirrors[$dist];
251 $conn_id = ftp_cn("anonymous","bo@bo.org",$server);
252 foreach ($sections as $section) {
253 $localfile=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
254 $remotefile=remote_dist_dir($dist) . "/$dist/$section/source/Sources.gz";
255 $sources_info=md5_source($section,$dist);
256 if (!file_exists ($dirs[sources_dir] . "/$dist/")) mkdir ( $dirs[sources_dir] . "/$dist",0740);
257 if (!file_exists ($dirs[sources_dir] . "/$dist/$section")) mkdir ($dirs[sources_dir] . "/$dist/$section",0740);
258 if (!file_exists($localfile) OR md5_of_file($localfile)!=$sources_info[md5]) {
259 print "Downloading $remotefile Bytes $sources_info[bytes]=> ";
260 if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
261 print "Done\n";
262 if(md5_of_file($localfile)==$sources_info[md5]) print "MD5 => OK\n";
263 }
264 else {
265 unlink("$localfile");
266 print "Fail\n";
267 die ("Error in source file");
268 }
269 }
270 }
271 ftp_quit($conn_id);
272 }
273 }
274 function md5_source($section,$dist) {
275 global $mirrors;
276 $relase=file_get_contents ("ftp://$mirrors[$dist]" . remote_dist_dir($dist) . "/$dist/Release") or die("Failed to check MD5 of sources");
277 preg_match ("/\s([^\s]+)\s+(\d+)\s$section\/source\/Sources\.gz/i", $relase,$matches);
278 return array("md5"=>$matches[1],"bytes"=>$matches[2]);
279 # 183dea9ca3c2e35ffd6cf5eee9501c21 2941637 main/binary-i386/Packages.gz
280 }
281
282 function vers_conv($debvers) {
283 /* preg_match("/(.+:)?([^-]+)(ds|dsfg|debian)/",$debvers,$matches);
284 if (!$matches[3]) {
285 unset($matches);
286 preg_match("/(.+:)?([^-]+)/",$debvers,$matches);
287 }
288 if (substr($matches[2],-1)=='.') $matches[2]=substr($matches[2],0,-1);
289 return $matches[2];
290 */
291 // Strip off the epoch
292 $pos = strpos($debvers, ':');
293 if ( $pos !== FALSE) {
294 $debvers = substr($debvers, $pos+1);
295 }
296
297 // strip off the Debian revision (look from the back of the string)
298 $pos = strrpos($debvers, '-');
299 if ( $pos !== FALSE) {
300 $debvers = substr($debvers, 0, $pos);
301 }
302
303 // strip off repacking indicators
304 $debvers = preg_replace("/[-.+~]?(ds|dfsg|debian)(.*)/i", "", $debvers);
305 return $debvers;
306 }
307 function check_db() {
308 global $dirs,$dbconn;
309 $db = @pg_connect($dbconn) or die(pg_last_notice());
310 if (!pg_table_exists($db,"pkgs")){
311 pg_exec($db, "CREATE TABLE pkgs (id serial PRIMARY KEY,name text,version text,maint text, uploaders text, dir text,watch text,md5_diff text,md5_atsource text,bytes numeric,dist text,section text,up_version text,up_changes text, up_url text,watch_warn text,updated bool, keep_changes bool, wwiz text,wwiz_version text, wwiz_type text)") or die('Error creating table pkgs\n');
312 pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die('Errore creating index - ' . pg_last_error($db));
313 $sw=true;
314 }
315 if (!pg_table_exists($db,"binpkgs")){
316 pg_exec($db, "CREATE TABLE binpkgs (id serial PRIMARY KEY, name text,bin_name text,dist text, pop_inst numeric,pop_vote numeric, up_error numeric,avg_error_date numeric)") or die('Error creating table binpkgs\n');
317 pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die('Errore creating index - ' . pg_last_error($db));
318 $sw=true;
319 }
320 #if (!pg_table_exists($db,"upstream")){
321 # pg_exec($db, "CREATE TABLE upstream (name text,dist text,up_version text,watch_warn text,updated bool)") or die('Errore nel creare la tabella');
322 # pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die('Errore nel creare gli indici ' . pg_last_error($db));
323 # $sw=true;
324 #}
325 pg_close($db);
326 if($sw) return $sw;
327 else return false;
328 }
329 function pg_table_exists (&$dblink, $table) {
330 $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";
331 if ($res = pg_exec ($dblink, $sql)) {
332 return pg_numrows($res) > 0;
333 } else {
334 return false; // or throw exception
335 }
336 }
337
338
339 function watch_stat() {
340 global $dirs,$dbconn;
341 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
342 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");
343 $tot_watch=pg_fetch_array($rsql);
344 print "Total packages with Watch =>$tot_watch[0]\n";
345 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");
346 $tot_nowatch=pg_fetch_array($rsql);
347 pg_close($db);
348 print "Totale packages without Watch => $tot_nowatch[0]\n";
349 print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".") . "%\n";
350 }
351 function remote_dist_dir($dist) {
352 #if ($dist=="non-US") return "/debian-non-US/dists/unstable";
353 #else
354 return "/debian/dists";
355 }
356 function clear_db(&$db) {
357 #global $dbconn;
358 #if (check_db()) die("Il Database era vuoto\n");
359 #keep_source();
360 #$db = pg_pconnect($dbconn) or die(pg_result_error($db));
361 $sql1= "Delete from binpkgs where id NOT IN
362 (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;
363 $sql2= "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;
364 $sql3= "UPDATE pkgs SET wwiz=NULL, wwiz_type=NULL, wwiz_version=NULL WHERE watch IS NOT NULL or watch!=''" ;
365 $sql4= "UPDATE pkgs SET up_changes=NULL WHERE wwiz_version=up_version OR up_version=version" ;
366 if(pg_exec($db,$sql1) AND pg_exec($db,$sql2) AND pg_exec($db,$sql3) AND pg_exec($db,$sql4)) print "Db clear complete\n";
367 }
368 function db_popcon($initial='') {
369 global $dirs,$dbconn;
370 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
371 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
372 $regexp="/Package:\s($initial\S*)\s+(\d+)\s+(\d+)/iS";
373 $zp = gzopen($filename, "r") or die("Could not open $filename");
374 $tin=time();
375 if(filesize($filename)>20) {
376 #pg_exec($db, "BEGIN TRANSACTION") OR DIE("Impossibile iniziare la transazione");
377 while (!gzeof($zp)) {
378 $line = gzgets ($zp,4096) ;
379 if (preg_match($regexp,$line,$matches)) {
380 #print "\rName => $matches[1] Install => $matches[2] Vote => $matches[3]";
381 @pg_exec($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
382 $processed+=1;
383 $elapsed_time=time()-$tin;
384 if (($elapsed_time % 20)==0 AND $updtime!=$elapsed_time) {
385 $updtime=$elapsed_time;
386 $wprocessed=$processed-$wprocessed;
387 $avg_time=$wprocessed/20;
388 $wprocessed=$processed;
389 $countdown=(34701- $processed)/$avg_time;
390 $last_time=time();
391 }
392 //print "\rProcessing Popcon: " . date("i:s",$countdown=$countdown-(time()-$last_time)) . " =>" . (34701-$processed) . " Media $avg_time";
393 $last_time=time();
394 }
395 }
396 #pg_exec($db, "COMMIT") OR DIE("Impossibile commsionare la transazione");
397 }
398 @pg_close($db);
399 }
400 function db_popcon_test() {
401 global $dirs,$dbconn;
402 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
403 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
404 $regexp="/Package:\s(\S+)\s+(\d+)\s+(\d+)/";
405 $zp = gzfile($filename, "r") or die("Could not open $filename");
406 if(filesize($filename)>20) {
407 foreach ($zp as $line) {
408 if (preg_match($regexp,$line,$matches)) {
409 # print "Name => $matches[1] \n Install => $matches[2] \n Vote => $matches[3]\n";
410 print "\r" . $i++;
411 #pg_unbuffered_query($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
412 }
413 }
414 }
415 @pg_close($db);
416 }
417 function file_put_contents($filename, $content, $flags = 0) {
418 if (!($file = fopen($filename,$flags))) return false;
419 else {
420 $n = fwrite($file, $content);
421 fclose($file);
422 return true;
423 }
424 }
425 function keep_popcon() {
426 global $dirs,$dbconn;
427 $localfile=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
428 $remotefile="http://popcon.debian.org/all-popcon-results.txt.gz";
429 @file_put_contents($localfile,file_get_contents($remotefile),'w') or die("Errore nello scaricare il file popcon\n");
430 }
431 function is_updated ($up_vers,$debian_vers) {
432 if ($up_vers=='') return 0;
433 passthru("dpkg --compare-versions '$up_vers' eq '" . vers_conv($debian_vers) . "'",$great);
434 if ($great!=0) return 1;
435 else return 0;
436 }
437 function db_upstream($initial='%') {
438 global $dirs,$dbconn;
439 check_db();
440 $db = pg_connect($dbconn) ;
441 $rsql=pg_exec($db, "SELECT id, pkgs.name,pkgs.dist,up_version,version,watch FROM pkgs WHERE watch is not null AND name LIKE '$initial%' ORDER BY name DESC");
442 $tot=pg_numrows($rsql);
443 while ($res_array=pg_fetch_array($rsql)) {
444 $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);
445 if($uscan_res[1] !=null) $uscan_res[1]=addslashes($uscan_res[1]);
446 #print $uscan_res[0] . "\n Name => $res_array[name]\n";
447 $version=$res_array[version];
448 print "Version=>" . $uscan_res[3] . "\n";
449 if ($uscan_res[3]!=null) $version=$uscan_res[3];
450 $updated=is_updated($uscan_res[0],$res_array[version]);
451 if ($uscan_res[0]!=$res_array[up_version]) ($keep_changes=1);
452 else $keep_changes=0;
453 $up_url=iconv("ISO-8859-1","UTF-8",$uscan_res[2]);
454 $up_version=iconv("ISO-8859-1","UTF-8",$uscan_res[0]);
455 $watch_warn=iconv("ISO-8859-1","UTF-8",$uscan_res[1]);
456 pg_exec($db, "UPDATE pkgs SET version='". $version . "', up_version='" . $up_version . "',up_url='" . $up_url . "', watch_warn='" . $watch_warn . "',keep_changes='$keep_changes', wwiz_version=NULL, wwiz_type=NULL, wwiz=NULL, updated='$updated' WHERE id=$res_array[id]") or Die("Error in update pkgs upstream");
457 #print "\rUpdated " . $res_array['name'] . " => $updated Up Version => ". $uscan_res[0];
458 print "\rUpstream left => " . $tot --;
459 # }
460 # else print "Added ". $res_array['name'] ."\n";
461 # if ($great==0) {
462
463 # print "Package=> " . $res_array['name'] . "\n";
464 # print "Version => " . $res_array['version'] . "\n";
465 # print "Upstream Ver=> " . $uscan_res[0] . "\n";
466 #print "Watch warings=> " . $uscan_res[1] . "\n";
467 # }
468 }
469 print "\n";
470 pg_close($db);
471 }
472 function check_url ($url) {
473 $url_p = parse_url ($url);
474 if (isset ($url_p["host"])) $host = $url_p["host"];
475 else return false;
476 preg_match("/(http|ftp)/",$host,$matches);
477 if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 5);
478 elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 5);
479 if (!$fp AND !$fp2) return false;
480 else return true;
481 }
482 function keep_remote ($localfile,$remotefile) {
483 if (!check_url($remotefile)) return false;
484 # $rfp = @fopen ($remotefile, "r");
485 # if (!$rfp) {
486 # return false;
487 # }
488 # fclose($rfp);
489 # $lfp = fopen ($localfile, "w");
490 # stream_set_blocking($rfp,0);
491 # ini_set ( "memory_limit", "100M");
492 # while (!feof($rfp)) {
493 # $data = fread($rfp, 8192);
494 # $bytes+=8192;
495 # print "Downlaoding $remotefile: $bytes\r";
496 # fwrite($lfp, $data);
497 # } while (true);
498 # ini_set ( "memory_limit", "15M");
499
500 if (!copy($remotefile, $localfile)) {
501 echo "Copia di $file non riuscita ...\n";
502 return false;
503 }
504 # fclose($lfp);
505 # fclose($rfp);
506
507 return true;
508 }
509 function up_changes($src_name='%') {
510 global $dbconn;
511 $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");
512 $db = pg_pconnect($dbconn) ;
513 $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!='' AND keep_changes='t' AND name LIKE '$src_name%'");
514 $tot=pg_numrows($rsql);
515 while ($res_array=pg_fetch_array($rsql)) {
516 $localfile=tempnam("/tmp/", "dehs-" . $res_array[id] . ".tar.gz");
517 print "I'm downloading $res_array[up_url]\r";
518 $res=keep_remote($localfile,$res_array['up_url']);
519 print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";
520 if (!$res) {
521 pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
522 $notdown++;
523 $tot--;
524 print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
525 #print "\n Not Downlaoded: $res_array[up_url]\n";
526 unlink($localfile);
527 continue;
528 }
529 foreach($changes_files as $filename) {
530 $command="tar -zxOf " . $localfile . " *$filename 2>/dev/null";
531 exec($command,$up_changes,$cmdres);
532 $up_changes=rtrim(implode("\n",$up_changes));
533 if ($cmdres==0 AND strlen($up_changes)>20) {
534 $sw=true;
535 $up_changes=addslashes($up_changes);
536 $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);
537 if ($conv_res) $up_changes=$conv_res;
538 $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";
539 pg_exec($db,$up_changes_sql);
540 #print "\nFounded $filename on $res_array[up_url]\n";
541 $found++;
542 break;
543 }
544 }
545 if (!$sw) {
546 pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
547 #print "\n Not Founded any on $res_array[up_url]\n";
548 $notfound++;
549 }
550 unlink($localfile);
551 print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
552 }
553 pg_close($db);
554 }
555 function uscan_foo($name,$version,$watch){
556 global $base;
557 $tmpnam=tempnam('/tmp/', $name.'_watch');
558 $fw=fopen($tmpnam,"w");
559
560 fwrite($fw,$watch);
561 fclose($fw);
562 $uscan_res=shell_exec("uscan --dehs --package $name --upstream-version " . vers_conv($version) . " --watchfile $tmpnam 2>/dev/null 2>&1");
563 unlink($tmpnam);
564 $p = xml_parser_create();
565 xml_parse_into_struct($p, $uscan_res, $vals, $index);
566 xml_parser_free($p);
567 $upstream=$vals[$index['UPSTREAM-VERSION'][0]][value];
568 $up_url=$vals[$index['UPSTREAM-URL'][0]][value];
569 $deb_vers=$vals[$index['DEBIAN-UVERSION'][0]][value];
570 $warnings=$vals[$index['WARNINGS'][0]][value];
571 if ($upstream=='') $upstream=null;
572 if ($up_url=='') $up_url=null;
573 if ($deb_vers=='') $deb_vers=null;
574 if ($warnings=='') $warnings=null;
575 return array($upstream,$warnings,$up_url,$deb_vers);
576 }
577 function db_up_error() {
578 global $dbconn;
579 check_db();
580 $db = pg_connect($dbconn) ;
581 $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";
582 print "Downloading bugs list =>";
583 $ferrors=file_get_contents($bugurl) or die ("I can't keep error page from bugs.debian.org");
584 print "Done\n";
585 $ferrors=explode("<li>",$ferrors);
586 $totline=count($ferrors);
587 #$errors[]=array();
588 #array_pop($errors);
589 print "Total line => $totline\n";
590 foreach ($ferrors as $line) {
591 # print $line . "\n";
592 $founded=preg_match("/Package:.+?>([^<]+)<\/a>.+<strong>((\d+)\syear\sand\s|)(\d+)\sdays/sS",$line,$matches);
593 if ($founded) {
594 $errors[$matches[1]][err_tot]++;
595 $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];
596 #if ($matches[3]) print "Anni $matches[3]";
597 #print "Giorni $matches[4]\n";
598 }
599 print "\rLine left => " . $totline--;
600 }
601 #arsort($errors);
602 $total=count($errors);
603 foreach ($errors as $error=>$value) {
604 pg_exec($db, "UPDATE binpkgs SET up_error=" . $value[err_tot] . ",avg_error_date=" . $value[days]/$value[err_tot] . " WHERE bin_name='$error'") or Die("Error in upstream error query");
605 #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot] . "\n";
606 print "\rErrors left => " . $total--;
607 }
608 pg_close($db);
609 }
610 function watch_fill($src_name='%'){
611 global $dirs,$dbconn;
612 $db = pg_pconnect($dbconn) or DIE ("Db error");
613 $checked=$errors=$notfound=$verok=$notmatch=0;
614 $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN
615 (Select max(pop_inst) as mpop_inst,SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist HAVING max(pop_inst) IS NOT NULL) as binpkgs using(name,dist)
616 WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name%' ORDER BY mpop_inst DESC";
617 $rsql=pg_exec($db, $sqlstring);
618 while ($res_array=pg_fetch_array($rsql)) {
619 if (substr($res_array["name"],0,3)=='lib') $initial=substr($res_array["name"],0,4);
620 else $initial=substr($res_array["name"],0,1);
621 $url="http://packages.debian.org/changelogs/pool/" .
622 $res_array["section"] . "/" .
623 $initial . "/" .
624 $res_array["name"] ."/" .
625 $res_array["name"] . "_" .
626 $res_array["version"] . "/copyright";
627 $contents=@file_get_contents($url) or '';
628 $space='';
629 if (!$contents) {
630 $url="http://packages.debian.org/changelogs/pool/" .
631 $res_array["section"] . "/" .
632 $initial . "/" .
633 $res_array["name"] ."/" .
634 $res_array["name"] . "_" .
635 $res_array["version"] . "/" . $res_array["name"] . ".copyright";
636 $contents=@file_get_contents($url) or '';
637 }
638 if (!$contents) {
639 $notfound++;
640 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz_type='no_cright' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
641 }
642 else {
643 $res=preg_match ('/(sf\.net|sourceforge\.net)/',$contents,$matches);
644 if ($res) {
645 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
646 $depured_debian_name=$dmatches[1];
647 $matches[1]="ftp://download.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";
648 }
649 else {
650 unset($matches);
651 $res=preg_match ('/((ftp:\/\/|http:\/\/)\S+(html|htm|\/(?(?=.+(tar|tgz))|[^>\s\)\a]+)))/',$contents,$matches);
652 $final=substr($matches[1],-1);
653 if ($final=='.') {
654 $matches[1]=substr($matches[1],0,-1);
655 $final=substr($matches[1],-1);
656 }
657 if ($matches[2]=="http://") $space=' ' . "(?:.*/)?";
658 elseif ($final!='/') $matches[1].='/';
659 $matches[1]=preg_replace("/((http|ftp):\/\/.+):(.*)/",'$1' . '$3', $matches[1],1);
660 }
661 if (!$res) {
662 $notmatch++;
663 $contents=iconv("ISO-8859-1","UTF-8",$contents);
664 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . addslashes($contents) . "', wwiz_type='notmatch', up_url='', up_changes='',keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
665 }
666 else {
667 if (!preg_match("/$res_array[name]/",$matches[1])) {
668 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
669 $depured_debian_name=$dmatches[1];
670 }
671 else $depured_debian_name=$res_array[name];
672 $watch="version=3\n" .
673 $matches[1] . $space . $depured_debian_name . "-?_?([\d+\.]+|\d+)\.tar.* debian uupdate";
674 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
675 if ($uscan_res[1] AND !$uscan_res[0]) {
676 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . addslashes($uscan_res[1]) . "', wwiz_type='error', up_url='', up_changes='', keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
677 $errors++;
678 }
679 else {
680 $verok++;
681 if ($uscan_res[0]!=$res_array[wwiz_version]) ($keep_changes=1);
682 else $keep_changes=0;
683 pg_exec($db, "UPDATE pkgs SET wwiz='" . addslashes($watch) ."', wwiz_type='watch', wwiz_version='" . $uscan_res[0] . "', up_url='" . $uscan_res[2] . "', keep_changes='$keep_changes' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
684 }
685 }
686 }
687 print "Package checked: " . $checked++ .
688 " OK: $verok Error: $errors Not Matched: $notmatch\r";
689 }
690 print "\nCopyright files not founded: $notfound\n";
691 print "Automatic genereted watch file failure: $errors\n";
692 pg_close($db);
693 }
694 function watch_fill_fre($src_name='%'){
695 global $dirs,$dbconn;
696 $db = pg_pconnect($dbconn) or DIE ("Db error");
697 $checked=$errors=$notfound=$verok=$notmatch=0;
698 $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN
699 (Select max(pop_inst) as mpop_inst,SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist HAVING max(pop_inst) IS NOT NULL) as binpkgs using(name,dist)
700 WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name%' ORDER BY mpop_inst DESC";
701 $rsql=pg_exec($db, $sqlstring);
702 while ($res_array=pg_fetch_array($rsql)) {
703 $url="http://freshmeat.net/projects/" .
704 $res_array["name"] ;
705 $contents=@file_get_contents($url) or '';
706 $space='';
707 if (!$contents) {
708 $notfound++;
709 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz_type='no_cright' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
710 }
711 else {
712 $res=preg_match ('/(tgz|zip).+>(\S+\/).*<\//',$contents,$matches);
713 if ($res) {
714 $durl=$matches[2];
715 $res=preg_match ('/(sf\.net|sourceforge\.net)/',$durl,$matches);
716 if ($res) {
717 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
718 $depured_debian_name=$dmatches[1];
719 $durl="ftp://download.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";
720 }
721 }
722 if (!durl) {
723 $notmatch++;
724 $contents=iconv("ISO-8859-1","UTF-8",$contents);
725 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . addslashes($contents) . "', wwiz_type='notmatch', up_url='', up_changes='',keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
726 }
727 else {
728 if (!preg_match("/$res_array[name]/",$durl)) {
729 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
730 $depured_debian_name=$dmatches[1];
731 }
732 else $depured_debian_name=$res_array[name];
733 $space=" ";
734 $watch="version=3\n" .
735 $durl . $space . $depured_debian_name . "-?_?([\d+\.]+|\d+)\.tar.* debian uupdate";
736 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
737 if ($uscan_res[1] AND !$uscan_res[0]) {
738 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . addslashes($uscan_res[1]) . "', wwiz_type='error', up_url='', up_changes='', keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
739 $errors++;
740 }
741 else {
742 $verok++;
743 if ($uscan_res[0]!=$res_array[wwiz_version]) ($keep_changes=1);
744 else $keep_changes=0;
745 pg_exec($db, "UPDATE pkgs SET wwiz='" . addslashes($watch) ."', wwiz_type='watch', wwiz_version='" . $uscan_res[0] . "', up_url='" . $uscan_res[2] . "', keep_changes='$keep_changes' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
746 }
747 }
748 }
749 #print $watch;
750 print "Package checked: " . $checked++ .
751 " OK: $verok Error: $errors Not Matched: $notmatch\r";
752 }
753 print "\nCopyright files not founded: $notfound\n";
754 print "Automatic genereted watch file failure: $errors\n";
755 pg_close($db);
756 }
757 function dbqa_conn ($dbname,$mode) {
758 $id = dba_open($dbname, $mode, "db4");
759 if (!$id) {
760 echo "dba_open failed\n";
761 exit;
762 }
763 return $id;
764 }
765 function dehsqa_db () {
766 #to complete!!!
767 #print phpinfo();
768 global $dirs,$dbconn;
769 $dir1='/org/alioth.debian.org/chroot/home/groups/dehs/htdocs/';
770 $dir2="/org/qa.debian.org/data/dehs/";
771 $dists=array("unstable","experimental");
772 foreach ($dists as $dist) {
773 $id=dbqa_conn($dir1 . "dehs_qa_" . $dist . ".db",'n');
774 $db = pg_pconnect($dbconn) or DIE ("Db error");
775 $sql="SELECT name, id, up_version, wwiz_version, watch, wwiz_type, watch_warn, updated FROM pkgs WHERE dist='$dist'";
776 $rsql=pg_exec($db, $sql);
777 while($res_array=pg_fetch_array($rsql)) {
778 if (!$res_array[watch]) $version="N/A";
779 elseif (!$res_array[up_version]) $version="Error";
780 else $version=$res_array[up_version];
781 if ($res_array[wwiz_type]=="watch") $wwiz=$res_array[wwiz_version];
782 else $wwiz=$res_array[wwiz_type];
783 $xml="<data><id>$res_array[id]</id>" .
784 "<up_version>$version</up_version>" .
785 "</data>";
786 $value=addslashes($xml);
787 dba_replace($res_array[name],$xml,$id);
788 }
789 dba_optimize($id);
790 dba_close($id);
791 pg_close($db);
792 }
793 }
794 function md5_of_file($inFile) {
795 if (file_exists($inFile)) {
796 return md5_file($inFile);
797 } else {
798 return false;
799 }
800 }
801 function update_all() {
802 global $email;
803 $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
804 db_add();
805 keep_popcon();
806 db_popcon();
807 keep_diff();
808 db_up_error();
809 db_upstream();
810 watch_fill();
811 up_changes();
812 dehsqa_db();
813 $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
814 $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";
815 mail($email, "DEHS report", $messaggio,
816 "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
817 "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
818 "X-Mailer: PHP/" . phpversion());
819 }
820 ?>

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5