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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5