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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Wed Oct 13 17:47:11 2004 UTC (8 years, 7 months ago) by evaso-guest
File size: 32625 byte(s)
Some bug fixes
Support debian version in watch file
1 evaso-guest 5 #!/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 evaso-guest 13 #print_R($_SERVER);
28     require "dehs.config.inc";
29 evaso-guest 5 dir_ctrl($dirs);
30 evaso-guest 13 if (!function_exists('file_get_contents')) {
31     function file_get_contents($filename) {
32 evaso-guest 17 if (!($file = fopen($filename,"r"))) return false;
33 evaso-guest 13 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 evaso-guest 5 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 evaso-guest 26 elseif (in_array("-dehsqa_db",$argv)) dehsqa_db();
57 evaso-guest 25 elseif (in_array("-clear_db",$argv)) clear_db();
58 evaso-guest 5 elseif (in_array("-db_up_error",$argv)) db_up_error();
59 evaso-guest 17 elseif (in_array("-up_changes",$argv)) up_changes($argv[2]);
60     elseif (in_array("-watchf",$argv)) watch_fill($argv[2]);
61 evaso-guest 5 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 evaso-guest 20 if (!is_dir($dir)) exec("mkdir -m 0740 -p $dir",$output,$res);
70     if ($res!=0) die("Error creating basic dir: $dir\n");
71 evaso-guest 5 }
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 evaso-guest 17 $conn_id = @ftp_connect($ftp_server) or DIE("Error: Cannot contact ftp server $ftp_server\n");
93 evaso-guest 5 // 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 evaso-guest 17 $db = pg_connect($dbconn) or die(pg_errormessage($db));
158 evaso-guest 5 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 evaso-guest 20 $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 evaso-guest 5 $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 evaso-guest 20 unlink($extracted);
173 evaso-guest 5 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 evaso-guest 20 if ($matches[10]) {
182     $matches[10]=iconv("ISO-8859-1","UTF-8",$matches[10]);
183     $matches[10]=addslashes($matches[10]);
184     }
185 evaso-guest 5 @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR DIE("Temp table pkgs_atsrc query error");
186 evaso-guest 20 $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 evaso-guest 5 if (!$rst) {
188 evaso-guest 20 $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 evaso-guest 5 $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 evaso-guest 31 clear_db($db);
210     pg_close($db);
211 evaso-guest 5 }
212     function db_query($pkg,$dist) {
213     global $dirs,$dbconn;
214 evaso-guest 17 $db = pg_pconnect($dbconn ) or die(pg_errormessage($db));
215 evaso-guest 5 $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 evaso-guest 20 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 evaso-guest 5 }
269 evaso-guest 20 ftp_quit($conn_id);
270 evaso-guest 5 }
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 evaso-guest 29 preg_match("/(.+:)?([^-]+)(ds|dsfg|debian)/",$debvers,$matches);
282     if (!$matches[3]) {
283     unset($matches);
284     preg_match("/(.+:)?([^-]+)/",$debvers,$matches);
285     }
286     if (substr($matches[2],-1)=='.') $matches[2]=substr($matches[2],0,-1);
287     return $matches[2];
288 evaso-guest 5 }
289     function check_db() {
290     global $dirs,$dbconn;
291 evaso-guest 17 $db = @pg_connect($dbconn) or die(pg_last_notice());
292 evaso-guest 5 if (!pg_table_exists($db,"pkgs")){
293 evaso-guest 20 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');
294 evaso-guest 5 pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die('Errore creating index - ' . pg_last_error($db));
295     $sw=true;
296     }
297     if (!pg_table_exists($db,"binpkgs")){
298     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');
299     pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die('Errore creating index - ' . pg_last_error($db));
300     $sw=true;
301     }
302     #if (!pg_table_exists($db,"upstream")){
303     # 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');
304     # pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die('Errore nel creare gli indici ' . pg_last_error($db));
305     # $sw=true;
306     #}
307     pg_close($db);
308     if($sw) return $sw;
309     else return false;
310     }
311     function pg_table_exists (&$dblink, $table) {
312     $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";
313     if ($res = pg_exec ($dblink, $sql)) {
314     return pg_numrows($res) > 0;
315     } else {
316     return false; // or throw exception
317     }
318     }
319    
320    
321     function watch_stat() {
322     global $dirs,$dbconn;
323 evaso-guest 17 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
324 evaso-guest 5 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");
325     $tot_watch=pg_fetch_array($rsql);
326     print "Total packages with Watch =>$tot_watch[0]\n";
327     $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");
328     $tot_nowatch=pg_fetch_array($rsql);
329     pg_close($db);
330     print "Totale packages without Watch => $tot_nowatch[0]\n";
331     print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".") . "%\n";
332     }
333     function remote_dist_dir($dist) {
334 evaso-guest 20 if ($dist=="non-US") return "/debian-non-US/dists/unstable";
335 evaso-guest 5 else return "/debian/dists";
336     }
337 evaso-guest 31 function clear_db(&$db) {
338     #global $dbconn;
339 evaso-guest 25 #if (check_db()) die("Il Database era vuoto\n");
340     #keep_source();
341 evaso-guest 31 #$db = pg_pconnect($dbconn) or die(pg_result_error($db));
342 evaso-guest 25 $sql1= "Delete from binpkgs where id NOT IN
343 evaso-guest 5 (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;
344 evaso-guest 25 $sql2= "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;
345     $sql3= "UPDATE pkgs SET wwiz=NULL, wwiz_type=NULL, wwiz_version=NULL WHERE watch IS NOT NULL or watch!=''" ;
346 evaso-guest 30 $sql4= "UPDATE pkgs SET up_changes=NULL WHERE wwiz_version=up_version OR up_version=version" ;
347     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";
348 evaso-guest 5 }
349     function db_popcon($initial='') {
350     global $dirs,$dbconn;
351 evaso-guest 17 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
352 evaso-guest 5 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
353     $regexp="/Package:\s($initial\S*)\s+(\d+)\s+(\d+)/iS";
354     $zp = gzopen($filename, "r") or die("Could not open $filename");
355     $tin=time();
356     if(filesize($filename)>20) {
357     #pg_exec($db, "BEGIN TRANSACTION") OR DIE("Impossibile iniziare la transazione");
358     while (!gzeof($zp)) {
359     $line = gzgets ($zp,4096) ;
360     if (preg_match($regexp,$line,$matches)) {
361     #print "\rName => $matches[1] Install => $matches[2] Vote => $matches[3]";
362     @pg_exec($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
363     $processed+=1;
364     $elapsed_time=time()-$tin;
365     if (($elapsed_time % 20)==0 AND $updtime!=$elapsed_time) {
366     $updtime=$elapsed_time;
367     $wprocessed=$processed-$wprocessed;
368     $avg_time=$wprocessed/20;
369     $wprocessed=$processed;
370     $countdown=(34701- $processed)/$avg_time;
371     $last_time=time();
372     }
373     print "\rProcessing Popcon: " . date("i:s",$countdown=$countdown-(time()-$last_time)) . " =>" . (34701-$processed) . " Media $avg_time";
374     $last_time=time();
375     }
376     }
377     #pg_exec($db, "COMMIT") OR DIE("Impossibile commsionare la transazione");
378     }
379     @pg_close($db);
380     }
381     function db_popcon_test() {
382     global $dirs,$dbconn;
383 evaso-guest 17 $db = pg_pconnect($dbconn) or die(pg_errormessage($db));
384 evaso-guest 5 $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
385     $regexp="/Package:\s(\S+)\s+(\d+)\s+(\d+)/";
386     $zp = gzfile($filename, "r") or die("Could not open $filename");
387     if(filesize($filename)>20) {
388     foreach ($zp as $line) {
389     if (preg_match($regexp,$line,$matches)) {
390     # print "Name => $matches[1] \n Install => $matches[2] \n Vote => $matches[3]\n";
391     print "\r" . $i++;
392     #pg_unbuffered_query($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");
393     }
394     }
395     }
396     @pg_close($db);
397     }
398     function file_put_contents($filename, $content, $flags = 0) {
399     if (!($file = fopen($filename,$flags))) return false;
400     else {
401     $n = fwrite($file, $content);
402     fclose($file);
403     return true;
404     }
405     }
406     function keep_popcon() {
407     global $dirs,$dbconn;
408     $localfile=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
409     $remotefile="http://popcon.debian.org/all-popcon-results.txt.gz";
410     @file_put_contents($localfile,file_get_contents($remotefile),'w') or die("Errore nello scaricare il file popcon\n");
411     }
412 evaso-guest 17 function is_updated ($up_vers,$debian_vers) {
413     if ($up_vers=='') return 0;
414 evaso-guest 26 passthru("dpkg --compare-versions '$up_vers' eq '" . vers_conv($debian_vers) . "'",$great);
415     if ($great!=0) return 1;
416 evaso-guest 17 else return 0;
417     }
418     function db_upstream($initial='%') {
419 evaso-guest 5 global $dirs,$dbconn;
420     check_db();
421     $db = pg_connect($dbconn) ;
422 evaso-guest 31 $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");
423 evaso-guest 5 $tot=pg_numrows($rsql);
424     while ($res_array=pg_fetch_array($rsql)) {
425     $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);
426     if($uscan_res[1] !=null) $uscan_res[1]=addslashes($uscan_res[1]);
427     #print $uscan_res[0] . "\n Name => $res_array[name]\n";
428 evaso-guest 31 $version=$res_array[version];
429     print "Version=>" . $uscan_res[3] . "\n";
430     if ($uscan_res[3]!=null) $version=$uscan_res[3];
431     $updated=is_updated($uscan_res[0],$res_array[version]);
432 evaso-guest 17 if ($uscan_res[0]!=$res_array[up_version]) ($keep_changes=1);
433     else $keep_changes=0;
434 evaso-guest 31 pg_exec($db, "UPDATE pkgs SET version='". $version . "', up_version='" . $uscan_res[0] . "',up_url='" . $uscan_res[2] . "', watch_warn='" . $uscan_res[1] . "',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");
435 evaso-guest 17 #print "\rUpdated " . $res_array['name'] . " => $updated Up Version => ". $uscan_res[0];
436     print "\rUpstream left => " . $tot --;
437     # }
438 evaso-guest 5 # else print "Added ". $res_array['name'] ."\n";
439     # if ($great==0) {
440    
441     # print "Package=> " . $res_array['name'] . "\n";
442     # print "Version => " . $res_array['version'] . "\n";
443     # print "Upstream Ver=> " . $uscan_res[0] . "\n";
444     #print "Watch warings=> " . $uscan_res[1] . "\n";
445     # }
446     }
447     print "\n";
448     pg_close($db);
449     }
450 evaso-guest 20 function check_url ($url) {
451     $url_p = parse_url ($url);
452     if (isset ($url_p["host"])) $host = $url_p["host"];
453     else return false;
454     preg_match("/(http|ftp)/",$host,$matches);
455     if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 10);
456     elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 10);
457     if (!$fp AND !$fp2) return false;
458     else return true;
459     }
460 evaso-guest 17 function keep_remote ($localfile,$remotefile) {
461 evaso-guest 20 #if (!check_url($remotefile)) return false;
462 evaso-guest 17 $rfp = @fopen ($remotefile, "rb");
463     if (!$rfp) {
464     return false;
465     }
466     $lfp = fopen ($localfile, "w");
467     ini_set ( "memory_limit", "100M");
468     while ($data = fread($rfp, 1024*8)) {
469     $bytes+=1024*8;
470 evaso-guest 20 #print "Downlaoding $remotefile: $bytes\r";
471 evaso-guest 17 fwrite($lfp, $data);
472     }
473     ini_set ( "memory_limit", "15M");
474     fclose($lfp);
475     fclose($rfp);
476    
477     return true;
478     }
479     function up_changes($src_name='%') {
480     global $dbconn;
481     $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");
482     $db = pg_connect($dbconn) ;
483     $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!='' AND keep_changes='t' AND name LIKE '$src_name%'");
484     $tot=pg_numrows($rsql);
485     while ($res_array=pg_fetch_array($rsql)) {
486     $localfile="/tmp/dehs-" . $res_array[id] . ".tar.gz";
487     #print "I'm downloading $res_array[up_url]\r";
488     $res=keep_remote($localfile,$res_array['up_url']);
489     #print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";
490     if (!$res) {
491     pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
492     $notdown++;
493     $tot--;
494     print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
495     #print "\n Not Downlaoded: $res_array[up_url]\n";
496     continue;
497     }
498     foreach($changes_files as $filename) {
499     $command="tar -zxOf " . $localfile . " *$filename 2>/dev/null";
500     exec($command,$up_changes,$cmdres);
501     $up_changes=rtrim(implode("\n",$up_changes));
502     if ($cmdres==0 AND strlen($up_changes)>20) {
503     $sw=true;
504     $up_changes=addslashes($up_changes);
505     $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);
506     if ($conv_res) $up_changes=$conv_res;
507     $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";
508     pg_exec($db,$up_changes_sql);
509     #print "\nFounded $filename on $res_array[up_url]\n";
510     $found++;
511     break;
512     }
513     }
514     if (!$sw) {
515     pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
516     #print "\n Not Founded any on $res_array[up_url]\n";
517     $notfound++;
518     }
519     unlink($localfile);
520     print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
521     }
522     pg_close($db);
523     }
524 evaso-guest 5 function uscan_foo($name,$version,$watch){
525     $fw=fopen("/tmp/$name" . "_watch","w");
526     fwrite($fw,$watch);
527     fclose($fw);
528 evaso-guest 17 $uscan_res=shell_exec("perl uscan3.pl $name " . vers_conv($version) . " /tmp/$name" . "_watch 2>/dev/null 2>&1");
529 evaso-guest 5 unlink("/tmp/$name" . "_watch");
530 evaso-guest 31 preg_match("/\+\+(.+)\+\+\s\+-(.+)-\+\s<debian_v>(.+)<\/debian_v>/",$uscan_res, $matches);
531 evaso-guest 5 if ($matches[1]!='') $upstream=$matches[1];
532     else $upstream=null;
533 evaso-guest 17 if ($matches[2]!='') $up_url=$matches[2];
534     else $up_url=null;
535 evaso-guest 31 if ($matches[3]!='' OR $matches[3]!='debian') $deb_vers=$matches[3];
536     else $deb_vers=null;
537     $uscan_res=preg_replace("/(.+<\/debian_v>)/", "", $uscan_res);
538 evaso-guest 5 if ($uscan_res=='') $uscan_res=null;
539 evaso-guest 31 return array($upstream,$uscan_res,$up_url,$deb_vers);
540 evaso-guest 5 }
541     function db_up_error() {
542     global $dbconn;
543     check_db();
544     $db = pg_connect($dbconn) ;
545     $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";
546     print "Downloading bugs list =>";
547     $ferrors=file_get_contents($bugurl) or die ("I can't keep error page from bugs.debian.org");
548     print "Done\n";
549     $ferrors=explode("<li>",$ferrors);
550     $totline=count($ferrors);
551     #$errors[]=array();
552     #array_pop($errors);
553     print "Total line => $totline\n";
554     foreach ($ferrors as $line) {
555     # print $line . "\n";
556     $founded=preg_match("/Package:.+<strong>([^<]+)<\/strong><\/a>.+<strong>((\d+)\syear\sand\s|)(\d+)\sdays/sS",$line,$matches);
557     if ($founded) {
558     $errors[$matches[1]][err_tot]++;
559     $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];
560     #if ($matches[3]) print "Anni $matches[3]";
561     #print "Giorni $matches[4]\n";
562     }
563     print "\rLine left => " . $totline--;
564     }
565     #arsort($errors);
566     $total=count($errors);
567     foreach ($errors as $error=>$value) {
568     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");
569     #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot] . "\n";
570     print "\rErrors left => " . $total--;
571     }
572     pg_close($db);
573     }
574 evaso-guest 17 function watch_fill($src_name='%'){
575 evaso-guest 13 global $dirs,$dbconn;
576 evaso-guest 17 $db = pg_pconnect($dbconn) or DIE ("Db error");
577 evaso-guest 13 $checked=$errors=$notfound=$verok=$notmatch=0;
578 evaso-guest 17 $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN
579 evaso-guest 13 (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)
580 evaso-guest 17 WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name%' ORDER BY mpop_inst DESC";
581 evaso-guest 13 $rsql=pg_exec($db, $sqlstring);
582     while ($res_array=pg_fetch_array($rsql)) {
583     if (substr($res_array["name"],0,3)=='lib') $initial=substr($res_array["name"],0,4);
584     else $initial=substr($res_array["name"],0,1);
585     $url="http://packages.debian.org/changelogs/pool/" .
586     $res_array["section"] . "/" .
587     $initial . "/" .
588     $res_array["name"] ."/" .
589     $res_array["name"] . "_" .
590     $res_array["version"] . "/copyright";
591     $contents=@file_get_contents($url) or '';
592 evaso-guest 17 $space='';
593 evaso-guest 13 if (!$contents) {
594 evaso-guest 26 $url="http://packages.debian.org/changelogs/pool/" .
595     $res_array["section"] . "/" .
596     $initial . "/" .
597     $res_array["name"] ."/" .
598     $res_array["name"] . "_" .
599     $res_array["version"] . "/" . $res_array["name"] . ".copyright";
600     $contents=@file_get_contents($url) or '';
601     }
602     if (!$contents) {
603 evaso-guest 13 $notfound++;
604 evaso-guest 26 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz_type='no_cright' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");
605 evaso-guest 13 }
606     else {
607 evaso-guest 17 $res=preg_match ('/(sf\.net|sourceforge\.net)/',$contents,$matches);
608 evaso-guest 23 if ($res) {
609     preg_match("/([^\d]+)/",$res_array[name],$dmatches);
610     $depured_debian_name=$dmatches[1];
611     $matches[1]="ftp://ftp2.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";
612     }
613 evaso-guest 17 else {
614     unset($matches);
615 evaso-guest 21 $res=preg_match ('/((ftp:\/\/|http:\/\/)\S+(html|htm|\/(?(?=.+(tar|tgz))|[^>\s\)\a]+)))/',$contents,$matches);
616 evaso-guest 17 $final=substr($matches[1],-1);
617 evaso-guest 23 if ($final=='.') {
618     $matches[1]=substr($matches[1],0,-1);
619     $final=substr($matches[1],-1);
620     }
621     if ($matches[2]=="http://") $space=' ' . "(?:.*/)?";
622     elseif ($final!='/') $matches[1].='/';
623     $matches[1]=preg_replace("/((http|ftp):\/\/.+):(.*)/",'$1' . '$3', $matches[1],1);
624 evaso-guest 17 }
625 evaso-guest 13 if (!$res) {
626     $notmatch++;
627 evaso-guest 23 $contents=iconv("ISO-8859-1","UTF-8",$contents);
628 evaso-guest 26 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] ."'");
629 evaso-guest 13 }
630     else {
631 evaso-guest 23 if (!preg_match("/$res_array[name]/",$matches[1])) {
632     preg_match("/([^\d]+)/",$res_array[name],$dmatches);
633     $depured_debian_name=$dmatches[1];
634     }
635     else $depured_debian_name=$res_array[name];
636 evaso-guest 13 $watch="version=2\n" .
637 evaso-guest 29 $matches[1] . $space . $depured_debian_name . "-?_?([\w+\d+\.]+|\d+)(\.tar|\.tgz)(\.gz|\.bz2|) debian uupdate";
638 evaso-guest 13 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
639 evaso-guest 15 if ($uscan_res[1] AND !$uscan_res[0]) {
640 evaso-guest 26 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] ."'");
641 evaso-guest 13 $errors++;
642     }
643     else {
644     $verok++;
645 evaso-guest 17 if ($uscan_res[0]!=$res_array[wwiz_version]) ($keep_changes=1);
646     else $keep_changes=0;
647     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] ."'");
648 evaso-guest 13 }
649     }
650     }
651     print "Package checked: " . $checked++ .
652     " OK: $verok Error: $errors Not Matched: $notmatch\r";
653     }
654 evaso-guest 17 print "\nCopyright files not founded: $notfound\n";
655 evaso-guest 13 print "Automatic genereted watch file failure: $errors\n";
656     pg_close($db);
657     }
658 evaso-guest 26 function dbqa_conn ($dbname,$mode) {
659     $id = dba_open($dbname, $mode, "db2");
660     if (!$id) {
661     echo "dba_open failed\n";
662     exit;
663     }
664     return $id;
665     }
666     function dehsqa_db () {
667     #to complete!!!
668     #print phpinfo();
669     global $dirs,$dbconn;
670     $dir1='/org/alioth.debian.org/chroot/home/groups/dehs/htdocs/';
671     $dir2="/org/qa.debian.org/data/dehs/";
672     $dists=array("unstable","experimental");
673     foreach ($dists as $dist) {
674     $id=dbqa_conn($dir1 . "dehs_qa_" . $dist . ".db",'n');
675     $db = pg_pconnect($dbconn) or DIE ("Db error");
676     $sql="SELECT name, id, up_version, wwiz_version, watch, wwiz_type, watch_warn, updated FROM pkgs WHERE dist='$dist'";
677     $rsql=pg_exec($db, $sql);
678     while($res_array=pg_fetch_array($rsql)) {
679     if (!$res_array[watch]) $version="N/A";
680     elseif (!$res_array[up_version]) $version="Error";
681     else $version=$res_array[up_version];
682     if ($res_array[wwiz_type]=="watch") $wwiz=$res_array[wwiz_version];
683     else $wwiz=$res_array[wwiz_type];
684     $xml="<data><id>$res_array[id]</id>" .
685     "<up_version>$version</up_version>" .
686     "<wwiz>$wwiz</wwiz>" .
687     "</data>";
688     $value=addslashes($xml);
689     dba_replace($res_array[name],$xml,$id);
690     }
691     dba_optimize($id);
692     dba_close($id);
693     pg_close($db);
694     }
695     }
696     function md5_of_file($inFile) {
697     if (file_exists($inFile)) {
698     $fd = fopen($inFile, 'r');
699     $fileContents = fread($fd, filesize($inFile));
700     fclose ($fd);
701     return md5($fileContents);
702     } else {
703     return false;
704     }
705     }
706 evaso-guest 5 function update_all() {
707     global $email;
708     $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
709     db_add();
710     keep_popcon();
711     db_popcon();
712     keep_diff();
713     db_up_error();
714     db_upstream();
715 evaso-guest 13 watch_fill();
716 evaso-guest 17 up_changes();
717 evaso-guest 26 dehsqa_db();
718 evaso-guest 5 $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
719     $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";
720     mail($email, "DEHS report", $messaggio,
721     "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
722     "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
723     "X-Mailer: PHP/" . phpversion());
724     }
725     ?>

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5