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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download)
Wed Aug 4 19:31:38 2004 UTC (8 years, 9 months ago) by evaso-guest
File size: 31992 byte(s)
Add data generation for developer.php on qa.debian.org
Various fixes
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", "15M");
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 $conn_id = $conn_normal;
118 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])) {
119 $watch=ext_watch($localfile,$res_array[name],$res_array[version]);
120 unlink($localfile);
121 if ($watch!=NULL) {
122 $watch= addslashes($watch);
123 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");
124 }
125 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");
126 }
127 $btotal=$btotal-$res_array['bytes'];
128 number_format ($btotal/1024, 2, ",",".");
129 print "\rDiffs left => " . $total-- . " Bytes left => " . number_format ($btotal/1024, 2, ",",".") . " - " . $res_array[name];
130 }
131 ftp_quit($conn_normal);
132 ftp_quit($conn_non_us);
133 pg_close($db);
134 return "Fatto\n";
135 }
136 function download_diffs(&$conn_id,$pkg,$version,$directory,$md5_diff,$md5_atsource,$diff_kb,$dist) {
137 global $dirs,$mirrors;
138 $localfile=$dirs[diffs_dir] . "/$pkg" . "_" . "$version.diff.gz";
139 if ($dist=="non-US") $remotefile="/debian-non-US/$directory/$pkg" . "_" . "$version.diff.gz";
140 else $remotefile="/debian/$directory/$pkg" . "_" . "$version.diff.gz";
141 #print "$localfile - $remotefile\n";
142 if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
143 # print "$pkg Diff.gz => Downloaded\n";
144 if(md5_of_file($localfile)==$md5_atsource) {
145 #print "MD5 => OK\n";
146 #unlink($localfile);
147 return $localfile;
148 }
149 }
150 print "Download Diff.gz of package $pkg => Fallito\n";
151 return false;
152 }
153 function db_add($initial=''){
154 global $dirs,$dists,$dbconn;
155 check_db();
156 keep_source();
157 $db = pg_connect($dbconn) or die(pg_errormessage($db));
158 pg_exec($db, "CREATE TEMP TABLE pkgs_atsrc (name text,dist text)") or die('Error creating temp table pkgs_atsrc');
159 pg_exec($db, "CREATE UNIQUE INDEX idxdis on pkgs_atsrc (name,dist)") or die('Error creating index on temp table');
160 pg_exec($db, "CREATE TEMP TABLE bin_atsrc (name text,bin_name text, dist text)") or die('Error creating temp table pkgs_atsrc');
161 pg_exec($db, "CREATE UNIQUE INDEX idxbis on bin_atsrc (name,bin_name,dist)") or die('Error creating index on temp table bin_atsrc');
162 foreach ($dists as $dist=>$sections) {
163 foreach ($sections as $section) {
164 $filename=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
165 print "\nI'm parsing $dist/$section => Sources.gz\n";
166 # 1 Pacchetto - 2 versione - 3 np - 4 Directory - 5 md5
167 $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";
168 $zp = gzopen($filename, "r") or die("Could not open $filename");
169 $extracted="/tmp/$dist_$section_Sources.gz";
170 exec("gzip -c -d $filename > $extracted");
171 $sourcesize=filesize($extracted);
172 unlink($extracted);
173 if(filesize($filename)>20) {
174 while (!gzeof($zp)) {
175 $line='';
176 while ($line!="\n") {
177 $line = gzgets ($zp,4096) ;
178 $buff1 .= $line;
179 }
180 if (preg_match($regexp,$buff1,$matches)) {
181 if ($matches[10]) {
182 $matches[10]=iconv("ISO-8859-1","UTF-8",$matches[10]);
183 $matches[10]=addslashes($matches[10]);
184 }
185 @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR DIE("Temp table pkgs_atsrc query error");
186 $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]')") ;
187 if (!$rst) {
188 $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");
189 }
190 $bin_names=split(",", $matches[2]);
191 foreach ($bin_names as $bin_name) {
192 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");
193 $rst=@pg_exec($db, "INSERT INTO binpkgs (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") ;
194 }
195 }
196 else {
197 preg_match("/Package:\s(.+)[^a]Binary/im",$buff1,$matches);
198 if(strncmp($initial,$matches[1],strlen($initial))<0) break;
199 }
200 $left=$sourcesize-gztell($zp);
201 print "\rSource file Left => $left";
202 $buff1="";
203 unset($matches);
204 }
205 }
206 gzclose($zp) ;
207 }
208 }
209 pg_close($db);
210 clear_db();
211 }
212 function db_query($pkg,$dist) {
213 global $dirs,$dbconn;
214 $db = pg_pconnect($dbconn ) or die(pg_errormessage($db));
215 $rsql=pg_exec($db, "SELECT pkgs.* ,mpop_inst FROM (SELECT name,MAX(binpkgs.pop_inst) AS mpop_inst FROM binpkgs GROUP BY name)
216 AS binpkgs INNER JOIN pkgs ON pkgs.name=binpkgs.name
217 WHERE pkgs.name='$pkg' AND dist='$dist';");
218 if (pg_numrows($rsql)==1) {
219 $res_array=pg_fetch_array($rsql);
220 print "Package=> " . $res_array['name'] . "\n";
221 print "Version => " . $res_array['version'] . "\n";
222 print "Diff MD5 Downloaded => " . $res_array['md5_diff'] . "\n";
223 print "Diff MD5 at Sources => " . $res_array['md5_atsource'] . "\n";
224 print "Distribution => " . $res_array['dist'] . "\n";
225 print "Section => " . $res_array['section'] . "\n";
226 print "KBytes => " . number_format ( $res_array['bytes']/1024, 2, ",",".") . "\n";
227 print "Pop Inst=>" . $res_array['mpop_inst'] . "\n";
228 if ($res_array['watch']) {
229 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['watch']);
230 print "Version Upstream => " . $uscan_res[0] . "\n";
231 print "Watch => " . $res_array['watch'];
232 print "Watch Warings => " . $uscan_res[1];
233 }
234 else print "Watch => Not present" . "\n";
235 }
236 else echo "Package=> " . $pkg . "\nNot found\n!!";
237 pg_close($db);
238 }
239 function byte_total(&$db,$initial){
240 global $dirs;
241 $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)");
242 $res_array=pg_fetch_array($rsql);
243 return $res_array[totbytes] ;
244 }
245 function keep_source (){
246 global $dirs,$dists,$mirrors;
247 foreach ($dists as $dist=>$sections) {
248 $server=$mirrors[$dist];
249 $conn_id = ftp_cn("anonymous","bo@bo.org",$server);
250 foreach ($sections as $section) {
251 $localfile=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
252 $remotefile=remote_dist_dir($dist) . "/$dist/$section/source/Sources.gz";
253 $sources_info=md5_source($section,$dist);
254 if (!file_exists ($dirs[sources_dir] . "/$dist/")) mkdir ( $dirs[sources_dir] . "/$dist",0740);
255 if (!file_exists ($dirs[sources_dir] . "/$dist/$section")) mkdir ($dirs[sources_dir] . "/$dist/$section",0740);
256 if (!file_exists($localfile) OR md5_of_file($localfile)!=$sources_info[md5]) {
257 print "Downloading $remotefile Bytes $sources_info[bytes]=> ";
258 if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
259 print "Done\n";
260 if(md5_of_file($localfile)==$sources_info[md5]) print "MD5 => OK\n";
261 }
262 else {
263 unlink("$localfile");
264 print "Fail\n";
265 die ("Error in source file");
266 }
267 }
268 }
269 ftp_quit($conn_id);
270 }
271 }
272 function md5_source($section,$dist) {
273 global $mirrors;
274 $relase=file_get_contents ("ftp://$mirrors[$dist]" . remote_dist_dir($dist) . "/$dist/Release") or die("Failed to check MD5 of sources");
275 preg_match ("/\s([^\s]+)\s+(\d+)\s$section\/source\/Sources\.gz/i", $relase,$matches);
276 return array("md5"=>$matches[1],"bytes"=>$matches[2]);
277 # 183dea9ca3c2e35ffd6cf5eee9501c21 2941637 main/binary-i386/Packages.gz
278 }
279
280 function vers_conv($debvers) {
281 preg_match("/(.+:)?(.+)(ds-|-)/",$debvers,$matches);
282 return $matches[2];
283 }
284 function check_db() {
285 global $dirs,$dbconn;
286 $db = @pg_connect($dbconn) or die(pg_last_notice());
287 if (!pg_table_exists($db,"pkgs")){
288 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');
289 pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die('Errore creating index - ' . pg_last_error($db));
290 $sw=true;
291 }
292 if (!pg_table_exists($db,"binpkgs")){
293 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');
294 pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die('Errore creating index - ' . pg_last_error($db));
295 $sw=true;
296 }
297 #if (!pg_table_exists($db,"upstream")){
298 # 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');
299 # pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die('Errore nel creare gli indici ' . pg_last_error($db));
300 # $sw=true;
301 #}
302 pg_close($db);
303 if($sw) return $sw;
304 else return false;
305 }
306 function pg_table_exists (&$dblink, $table) {
307 $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";
308 if ($res = pg_exec ($dblink, $sql)) {
309 return pg_numrows($res) > 0;
310 } else {
311 return false; // or throw exception
312 }
313 }
314
315
316 function watch_stat() {
317 global $dirs,$dbconn;
318 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
319 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");
320 $tot_watch=pg_fetch_array($rsql);
321 print "Total packages with Watch =>$tot_watch[0]\n";
322 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");
323 $tot_nowatch=pg_fetch_array($rsql);
324 pg_close($db);
325 print "Totale packages without Watch => $tot_nowatch[0]\n";
326 print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".") . "%\n";
327 }
328 function remote_dist_dir($dist) {
329 if ($dist=="non-US") return "/debian-non-US/dists/unstable";
330 else return "/debian/dists";
331 }
332 function clear_db() {
333 global $dbconn;
334 #if (check_db()) die("Il Database era vuoto\n");
335 #keep_source();
336 $db = pg_pconnect($dbconn) or die(pg_result_error($db));
337 $sql1= "Delete from binpkgs where id NOT IN
338 (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;
339 $sql2= "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;
340 $sql3= "UPDATE pkgs SET wwiz=NULL, wwiz_type=NULL, wwiz_version=NULL WHERE watch IS NOT NULL or watch!=''" ;
341 if(pg_exec($db,$sql1) AND pg_exec($db,$sql2) AND pg_exec($db,$sql3)) print "Db clear complete\n";
342 }
343 function db_popcon($initial='') {
344 global $dirs,$dbconn;
345 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
346 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
347 $regexp="/Package:\s($initial\S*)\s+(\d+)\s+(\d+)/iS";
348 $zp = gzopen($filename, "r") or die("Could not open $filename");
349 $tin=time();
350 if(filesize($filename)>20) {
351 #pg_exec($db, "BEGIN TRANSACTION") OR DIE("Impossibile iniziare la transazione");
352 while (!gzeof($zp)) {
353 $line = gzgets ($zp,4096) ;
354 if (preg_match($regexp,$line,$matches)) {
355 #print "\rName => $matches[1] Install => $matches[2] Vote => $matches[3]";
356 @pg_exec($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
357 $processed+=1;
358 $elapsed_time=time()-$tin;
359 if (($elapsed_time % 20)==0 AND $updtime!=$elapsed_time) {
360 $updtime=$elapsed_time;
361 $wprocessed=$processed-$wprocessed;
362 $avg_time=$wprocessed/20;
363 $wprocessed=$processed;
364 $countdown=(34701- $processed)/$avg_time;
365 $last_time=time();
366 }
367 print "\rProcessing Popcon: " . date("i:s",$countdown=$countdown-(time()-$last_time)) . " =>" . (34701-$processed) . " Media $avg_time";
368 $last_time=time();
369 }
370 }
371 #pg_exec($db, "COMMIT") OR DIE("Impossibile commsionare la transazione");
372 }
373 @pg_close($db);
374 }
375 function db_popcon_test() {
376 global $dirs,$dbconn;
377 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
378 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
379 $regexp="/Package:\s(\S+)\s+(\d+)\s+(\d+)/";
380 $zp = gzfile($filename, "r") or die("Could not open $filename");
381 if(filesize($filename)>20) {
382 foreach ($zp as $line) {
383 if (preg_match($regexp,$line,$matches)) {
384 # print "Name => $matches[1] \n Install => $matches[2] \n Vote => $matches[3]\n";
385 print "\r" . $i++;
386 #pg_unbuffered_query($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
387 }
388 }
389 }
390 @pg_close($db);
391 }
392 function file_put_contents($filename, $content, $flags = 0) {
393 if (!($file = fopen($filename,$flags))) return false;
394 else {
395 $n = fwrite($file, $content);
396 fclose($file);
397 return true;
398 }
399 }
400 function keep_popcon() {
401 global $dirs,$dbconn;
402 $localfile=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
403 $remotefile="http://popcon.debian.org/all-popcon-results.txt.gz";
404 @file_put_contents($localfile,file_get_contents($remotefile),'w') or die("Errore nello scaricare il file popcon\n");
405 }
406 function is_updated ($up_vers,$debian_vers) {
407 if ($up_vers=='') return 0;
408 passthru("dpkg --compare-versions '$up_vers' eq '" . vers_conv($debian_vers) . "'",$great);
409 if ($great!=0) return 1;
410 else return 0;
411 }
412 function db_upstream($initial='%') {
413 global $dirs,$dbconn;
414 check_db();
415 $db = pg_connect($dbconn) ;
416 $rsql=pg_exec($db, "SELECT pkgs.name,pkgs.dist,up_version,version,watch FROM pkgs WHERE watch is not null AND name LIKE '$initial%' ORDER BY name DESC");
417 $tot=pg_numrows($rsql);
418 while ($res_array=pg_fetch_array($rsql)) {
419 $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);
420 if($uscan_res[1] !=null) $uscan_res[1]=addslashes($uscan_res[1]);
421 #print $uscan_res[0] . "\n Name => $res_array[name]\n";
422 $updated=is_updated($uscan_res[0],$res_array[version]);
423 if ($uscan_res[0]!=$res_array[up_version]) ($keep_changes=1);
424 else $keep_changes=0;
425 pg_exec($db, "UPDATE pkgs SET up_version='" . $uscan_res[0] . "',up_url='" . $uscan_res[2] . "', watch_warn='" . $uscan_res[1] . "',keep_changes='$keep_changes', updated='$updated' WHERE name='$res_array[name]' AND dist='$res_array[dist]'") or Die("Error in update pkgs upstream");
426 #print "\rUpdated " . $res_array['name'] . " => $updated Up Version => ". $uscan_res[0];
427 print "\rUpstream left => " . $tot --;
428 # }
429 # else print "Added ". $res_array['name'] ."\n";
430 # if ($great==0) {
431
432 # print "Package=> " . $res_array['name'] . "\n";
433 # print "Version => " . $res_array['version'] . "\n";
434 # print "Upstream Ver=> " . $uscan_res[0] . "\n";
435 #print "Watch warings=> " . $uscan_res[1] . "\n";
436 # }
437 }
438 print "\n";
439 pg_close($db);
440 }
441 function check_url ($url) {
442 $url_p = parse_url ($url);
443 if (isset ($url_p["host"])) $host = $url_p["host"];
444 else return false;
445 preg_match("/(http|ftp)/",$host,$matches);
446 if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 10);
447 elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 10);
448 if (!$fp AND !$fp2) return false;
449 else return true;
450 }
451 function keep_remote ($localfile,$remotefile) {
452 #if (!check_url($remotefile)) return false;
453 $rfp = @fopen ($remotefile, "rb");
454 if (!$rfp) {
455 return false;
456 }
457 $lfp = fopen ($localfile, "w");
458 ini_set ( "memory_limit", "100M");
459 while ($data = fread($rfp, 1024*8)) {
460 $bytes+=1024*8;
461 #print "Downlaoding $remotefile: $bytes\r";
462 fwrite($lfp, $data);
463 }
464 ini_set ( "memory_limit", "15M");
465 fclose($lfp);
466 fclose($rfp);
467
468 return true;
469 }
470 function up_changes($src_name='%') {
471 global $dbconn;
472 $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");
473 $db = pg_connect($dbconn) ;
474 $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!='' AND keep_changes='t' AND name LIKE '$src_name%'");
475 $tot=pg_numrows($rsql);
476 while ($res_array=pg_fetch_array($rsql)) {
477 $localfile="/tmp/dehs-" . $res_array[id] . ".tar.gz";
478 #print "I'm downloading $res_array[up_url]\r";
479 $res=keep_remote($localfile,$res_array['up_url']);
480 #print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";
481 if (!$res) {
482 pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
483 $notdown++;
484 $tot--;
485 print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
486 #print "\n Not Downlaoded: $res_array[up_url]\n";
487 continue;
488 }
489 foreach($changes_files as $filename) {
490 $command="tar -zxOf " . $localfile . " *$filename 2>/dev/null";
491 exec($command,$up_changes,$cmdres);
492 $up_changes=rtrim(implode("\n",$up_changes));
493 if ($cmdres==0 AND strlen($up_changes)>20) {
494 $sw=true;
495 $up_changes=addslashes($up_changes);
496 $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);
497 if ($conv_res) $up_changes=$conv_res;
498 $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";
499 pg_exec($db,$up_changes_sql);
500 #print "\nFounded $filename on $res_array[up_url]\n";
501 $found++;
502 break;
503 }
504 }
505 if (!$sw) {
506 pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
507 #print "\n Not Founded any on $res_array[up_url]\n";
508 $notfound++;
509 }
510 unlink($localfile);
511 print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
512 }
513 pg_close($db);
514 }
515 function uscan_foo($name,$version,$watch){
516 $fw=fopen("/tmp/$name" . "_watch","w");
517 fwrite($fw,$watch);
518 fclose($fw);
519 $uscan_res=shell_exec("perl uscan3.pl $name " . vers_conv($version) . " /tmp/$name" . "_watch 2>/dev/null 2>&1");
520 unlink("/tmp/$name" . "_watch");
521 preg_match("/\+\+(.+)\+\+\s\+-(.+)-\+/",$uscan_res, $matches);
522 if ($matches[1]!='') $upstream=$matches[1];
523 else $upstream=null;
524 if ($matches[2]!='') $up_url=$matches[2];
525 else $up_url=null;
526 $uscan_res=preg_replace("/(\+\+.+-\+)/", "", $uscan_res);
527 if ($uscan_res=='') $uscan_res=null;
528 return array($upstream,$uscan_res,$up_url);
529 }
530 function db_up_error() {
531 global $dbconn;
532 check_db();
533 $db = pg_connect($dbconn) ;
534 $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";
535 print "Downloading bugs list =>";
536 $ferrors=file_get_contents($bugurl) or die ("I can't keep error page from bugs.debian.org");
537 print "Done\n";
538 $ferrors=explode("<li>",$ferrors);
539 $totline=count($ferrors);
540 #$errors[]=array();
541 #array_pop($errors);
542 print "Total line => $totline\n";
543 foreach ($ferrors as $line) {
544 # print $line . "\n";
545 $founded=preg_match("/Package:.+<strong>([^<]+)<\/strong><\/a>.+<strong>((\d+)\syear\sand\s|)(\d+)\sdays/sS",$line,$matches);
546 if ($founded) {
547 $errors[$matches[1]][err_tot]++;
548 $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];
549 #if ($matches[3]) print "Anni $matches[3]";
550 #print "Giorni $matches[4]\n";
551 }
552 print "\rLine left => " . $totline--;
553 }
554 #arsort($errors);
555 $total=count($errors);
556 foreach ($errors as $error=>$value) {
557 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");
558 #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot] . "\n";
559 print "\rErrors left => " . $total--;
560 }
561 pg_close($db);
562 }
563 function watch_fill($src_name='%'){
564 global $dirs,$dbconn;
565 $db = pg_pconnect($dbconn) or DIE ("Db error");
566 $checked=$errors=$notfound=$verok=$notmatch=0;
567 $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN
568 (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)
569 WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name%' ORDER BY mpop_inst DESC";
570 $rsql=pg_exec($db, $sqlstring);
571 while ($res_array=pg_fetch_array($rsql)) {
572 if (substr($res_array["name"],0,3)=='lib') $initial=substr($res_array["name"],0,4);
573 else $initial=substr($res_array["name"],0,1);
574 $url="http://packages.debian.org/changelogs/pool/" .
575 $res_array["section"] . "/" .
576 $initial . "/" .
577 $res_array["name"] ."/" .
578 $res_array["name"] . "_" .
579 $res_array["version"] . "/copyright";
580 $contents=@file_get_contents($url) or '';
581 $space='';
582 if (!$contents) {
583 $url="http://packages.debian.org/changelogs/pool/" .
584 $res_array["section"] . "/" .
585 $initial . "/" .
586 $res_array["name"] ."/" .
587 $res_array["name"] . "_" .
588 $res_array["version"] . "/" . $res_array["name"] . ".copyright";
589 $contents=@file_get_contents($url) or '';
590 }
591 if (!$contents) {
592 $notfound++;
593 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz_type='no_cright' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
594 }
595 else {
596 $res=preg_match ('/(sf\.net|sourceforge\.net)/',$contents,$matches);
597 if ($res) {
598 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
599 $depured_debian_name=$dmatches[1];
600 $matches[1]="ftp://ftp2.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";
601 }
602 else {
603 unset($matches);
604 $res=preg_match ('/((ftp:\/\/|http:\/\/)\S+(html|htm|\/(?(?=.+(tar|tgz))|[^>\s\)\a]+)))/',$contents,$matches);
605 $final=substr($matches[1],-1);
606 if ($final=='.') {
607 $matches[1]=substr($matches[1],0,-1);
608 $final=substr($matches[1],-1);
609 }
610 if ($matches[2]=="http://") $space=' ' . "(?:.*/)?";
611 elseif ($final!='/') $matches[1].='/';
612 $matches[1]=preg_replace("/((http|ftp):\/\/.+):(.*)/",'$1' . '$3', $matches[1],1);
613 }
614 if (!$res) {
615 $notmatch++;
616 $contents=iconv("ISO-8859-1","UTF-8",$contents);
617 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] ."'");
618 }
619 else {
620 if (!preg_match("/$res_array[name]/",$matches[1])) {
621 preg_match("/([^\d]+)/",$res_array[name],$dmatches);
622 $depured_debian_name=$dmatches[1];
623 }
624 else $depured_debian_name=$res_array[name];
625 $watch="version=2\n" .
626 $matches[1] . $space . $depured_debian_name . "-?_?([\d+\.]+|\d+)(\.tar|\.tgz)(\.gz|\.bz2|) debian uupdate";
627 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
628 if ($uscan_res[1] AND !$uscan_res[0]) {
629 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] ."'");
630 $errors++;
631 }
632 else {
633 $verok++;
634 if ($uscan_res[0]!=$res_array[wwiz_version]) ($keep_changes=1);
635 else $keep_changes=0;
636 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] ."'");
637 }
638 }
639 }
640 print "Package checked: " . $checked++ .
641 " OK: $verok Error: $errors Not Matched: $notmatch\r";
642 }
643 print "\nCopyright files not founded: $notfound\n";
644 print "Automatic genereted watch file failure: $errors\n";
645 pg_close($db);
646 }
647 function dbqa_conn ($dbname,$mode) {
648 $id = dba_open($dbname, $mode, "db2");
649 if (!$id) {
650 echo "dba_open failed\n";
651 exit;
652 }
653 return $id;
654 }
655 function dehsqa_db () {
656 #to complete!!!
657 #print phpinfo();
658 global $dirs,$dbconn;
659 $dir1='/org/alioth.debian.org/chroot/home/groups/dehs/htdocs/';
660 $dir2="/org/qa.debian.org/data/dehs/";
661 $dists=array("unstable","experimental");
662 foreach ($dists as $dist) {
663 $id=dbqa_conn($dir1 . "dehs_qa_" . $dist . ".db",'n');
664 $db = pg_pconnect($dbconn) or DIE ("Db error");
665 $sql="SELECT name, id, up_version, wwiz_version, watch, wwiz_type, watch_warn, updated FROM pkgs WHERE dist='$dist'";
666 $rsql=pg_exec($db, $sql);
667 while($res_array=pg_fetch_array($rsql)) {
668 if (!$res_array[watch]) $version="N/A";
669 elseif (!$res_array[up_version]) $version="Error";
670 else $version=$res_array[up_version];
671 if ($res_array[wwiz_type]=="watch") $wwiz=$res_array[wwiz_version];
672 else $wwiz=$res_array[wwiz_type];
673 $xml="<data><id>$res_array[id]</id>" .
674 "<up_version>$version</up_version>" .
675 "<wwiz>$wwiz</wwiz>" .
676 "</data>";
677 $value=addslashes($xml);
678 dba_replace($res_array[name],$xml,$id);
679 }
680 dba_optimize($id);
681 dba_close($id);
682 pg_close($db);
683 }
684 }
685 function md5_of_file($inFile) {
686 if (file_exists($inFile)) {
687 $fd = fopen($inFile, 'r');
688 $fileContents = fread($fd, filesize($inFile));
689 fclose ($fd);
690 return md5($fileContents);
691 } else {
692 return false;
693 }
694 }
695 function update_all() {
696 global $email;
697 $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
698 db_add();
699 keep_popcon();
700 db_popcon();
701 keep_diff();
702 db_up_error();
703 db_upstream();
704 watch_fill();
705 up_changes();
706 dehsqa_db();
707 $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
708 $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";
709 mail($email, "DEHS report", $messaggio,
710 "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
711 "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
712 "X-Mailer: PHP/" . phpversion());
713 }
714 ?>

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5