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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 182 - (hide annotations) (download)
Thu Feb 4 01:24:48 2010 UTC (3 years, 4 months ago) by geissert
File size: 53523 byte(s)
Add support for bzip2-compressed 3.0 source packages
1 atomo64-guest 168 #!/usr/bin/php -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 atomo64-guest 105 Copyright 2007, 2008 by Raphael Geissert <atomo64@gmail.com>
8 atomo64-guest 54
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13    
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18    
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 evaso-guest 5 */
23 atomo64-guest 54
24 evaso-guest 5 // stabilire una connessione
25     #ini_set ( "error_reporting", "E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR");
26     ini_set ("html_errors", "off");
27     set_time_limit(0);
28 geissert 177 ini_set ( "memory_limit", "80M");
29 evaso-guest 13 #print_R($_SERVER);
30     require "dehs.config.inc";
31 atomo64-guest 122 require "events.class.php";
32 geissert 180 # Use PEAR: Archive_Tar to uncompress 3.0 debian.tar.gz files
33 geissert 181 require "PEAR.php";
34 geissert 180 require "Archive/Tar.php";
35 evaso-guest 5 dir_ctrl($dirs);
36 evaso-guest 13 if (!function_exists('file_get_contents')) {
37 atomo64-guest 54 function file_get_contents($filename) {
38     if (!($file = fopen($filename,"rb"))) return false;
39     else {
40     $contents = "";
41     do {
42     $data = fread($file, 4096);
43     if (strlen($data) == 0) {
44     break;
45     }
46     $contents .= $data;
47     } while (true);
48     fclose($file);
49     return $contents;
50     }
51     }
52     }
53    
54     if (!function_exists('file_put_contents')) {
55     function file_put_contents($filename, $content, $flags = 0) {
56     if (!($file = fopen($filename,$flags))) return false;
57     else {
58     $n = fwrite($file, $content);
59     fclose($file);
60     return true;
61     }
62     }
63     }
64    
65 atomo64-guest 76 if (in_array("-db_add",$argv)) db_add((!empty($argv[2])?$argv[2]:''), false);
66     elseif (in_array("-db_query",$argv)) db_query($argv[2],(!empty($argv[3])?$argv[3]:'%') );
67 atomo64-guest 59 elseif (in_array("-dl_diffs",$argv) || in_array("-keep_diff",$argv)) dl_diffs($argv[2]);
68 atomo64-guest 64 elseif (in_array("-keep_sources",$argv) || in_array("-download_sources",$argv)) download_sources();
69 atomo64-guest 135 elseif (in_array("-db_popcon",$argv)) db_popcon();
70 atomo64-guest 122 elseif (in_array("-dl_popcon",$argv) || in_array("-keep_popcon",$argv)) dl_popcon();
71 atomo64-guest 85 elseif (in_array("-db_upstream",$argv)) db_upstream((!empty($argv[2])?$argv[2]:'%'));
72 atomo64-guest 149 elseif (in_array("-dl_from_vcs",$argv)) dl_from_vcs((!empty($argv[2])?$argv[2]:'%'),(!empty($argv[3])?$argv[3]:'%'));
73 atomo64-guest 54 elseif (in_array("-update_all",$argv)) update_all();
74 atomo64-guest 64 elseif (in_array("-update_new",$argv)) update_new();
75 atomo64-guest 65 elseif (in_array("-update_bogus",$argv)) update_bogus();
76 atomo64-guest 54 elseif (in_array("-check_db",$argv)) check_db();
77     elseif (in_array("-dehsqa_db",$argv)) dehsqa_db();
78     elseif (in_array("-clear_db",$argv)) clear_db();
79     elseif (in_array("-db_up_error",$argv)) db_up_error();
80 atomo64-guest 85 elseif (in_array("-up_changes",$argv)) up_changes((!empty($argv[2])?$argv[2]:'%'), true);
81     elseif (in_array("-watchf",$argv) || in_array("-wwiz",$argv)) watch_wizard((!empty($argv[2])?$argv[2]:'%'));
82 atomo64-guest 54 elseif (in_array("-kb_total",$argv)) print "Diff
83 evaso-guest 5 #stat_up_error();
84     #stat_up_error();to download Kb => " . kb_total($argv[2]) . "\n" ;
85 atomo64-guest 54 elseif (in_array("-watch_stat",$argv)) watch_stat();
86     else print "Command $argv[1] not found\n";
87 evaso-guest 5 #stat_up_error();
88     function dir_ctrl($dirs) {
89 atomo64-guest 132 if (!is_array($dirs)) {
90     $dirs = array($dirs);
91     }
92 atomo64-guest 54 foreach ($dirs as $dir) {
93     if (!is_dir($dir)) exec("mkdir -m 0740 -p " . escapeshellarg($dir), $output, $res);
94     if ($res != 0) die_status("Error creating basic dir: $dir\n");
95     }
96 evaso-guest 5 }
97 atomo64-guest 54 function ext_watch ($file,$pkg,$version) {
98     if (!file_exists($file)) return "";
99 geissert 180 if (substr($file,-8) == ".diff.gz") {
100     // Format: 1.0
101     $zp=gzopen($file,"r") or die_status("Fail to open $file");
102     while (!gzeof($zp)) {
103 atomo64-guest 54 $line = gzgets ($zp,4096);
104 geissert 180 if (preg_match('/\+\+\+\s.+\/debian\/watch$/',$line)) {
105     $line = gzgets ($zp,4096);
106     preg_match ("/.+\+\d+\,(\d+)\s/",$line,$watch_lenght);
107     for ($i = 1; $i <= $watch_lenght[1]; $i++) {
108     $line=gzgets ($zp,4096);
109     $watch .=substr($line,1);
110     }
111     break;
112 evaso-guest 5 }
113     }
114 atomo64-guest 54 }
115 geissert 182 elseif (preg_match('/\.debian\.tar\.(gz|bz2)$/', $file, $matches)) {
116 geissert 180 // Format 3.0 (*)
117 geissert 182 $AT = new Archive_Tar($file,$matches[1]);
118 geissert 180 $watch = $AT->extractInString('debian/watch');
119     }
120 atomo64-guest 54 if ($watch!=null) return $watch;
121     else return NULL;
122 evaso-guest 5 }
123     function ftp_cn($login,$password,$ftp_server) {
124 atomo64-guest 54 $conn_id = @ftp_connect($ftp_server) or die_status("Error: Cannot contact ftp server $ftp_server\n");
125     // login con user name e password
126     ftp_pasv($conn_id,1);
127     $login_result = ftp_login($conn_id, "anonymous","bo@bo.org");
128     // controllo della connessione
129     if ((!$conn_id) || (!$login_result)) {
130 evaso-guest 5 echo "Ftp connection failed!\n";
131     echo "Failed connection to $ftp_server\n";
132 atomo64-guest 49 die_status();
133 evaso-guest 5 } else {
134     return $conn_id;
135     }
136     }
137 atomo64-guest 59 function dl_diffs($initial = '') {
138 atomo64-guest 54 global $dirs,$dbconn,$mirrors;
139     check_db();
140     $conn_normal=ftp_cn("anonymous","bo@bo.org",$mirrors['unstable']);
141     #$conn_non_us=ftp_cn("anonymous","bo@bo.org",$mirrors["non-US"]);
142     $db = pg_pconnect($dbconn) ;
143     $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");
144     $total=pg_numrows($rsql);
145     $btotal=byte_total($db,$initial);
146 geissert 180 print "Total Diff to download Kb => " . number_format ($btotal/1024, 2, ",",".") . "\n";
147 atomo64-guest 54 while ($res_array=pg_fetch_array($rsql)) {
148     #if ($res_array['dist']=='non-US') $conn_id = $conn_non_us;
149     #else
150     $conn_id = $conn_normal;
151 atomo64-guest 95 if ($localfile=download_diff($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])) {
152 atomo64-guest 54 $watch=ext_watch($localfile,$res_array[name],$res_array[version]);
153     unlink($localfile);
154     if ($watch!=NULL) {
155     $watch=iconv("ISO-8859-1","UTF-8",$watch);
156 atomo64-guest 66 $watch= pg_escape_string($watch);
157 atomo64-guest 54 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");
158     }
159     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");
160     }
161     $btotal=$btotal-$res_array['bytes'];
162     number_format ($btotal/1024, 2, ",",".");
163     print "\rDiffs left => " . $total-- . " Bytes left => " . number_format ($btotal/1024, 2, ",",".") . " - " . $res_array[name];
164     }
165     ftp_quit($conn_normal);
166     pg_close($db);
167     return "Fatto\n";
168     }
169 atomo64-guest 95 function download_diff(&$conn_id,$pkg,$version,$directory,$md5_diff,$md5_atsource,$diff_kb,$dist) {
170 atomo64-guest 54 global $dirs,$mirrors;
171 geissert 182 $diffTypes = array(".diff.gz",".debian.tar.gz",".debian.tar.bz2");
172 geissert 180 foreach($diffTypes as $diffType) {
173     $localfile=$dirs[diffs_dir] . "/$pkg" . "_" . $version . $diffType;
174     $remotefile="/debian/$directory/$pkg" . "_" . "$version" . $diffType;
175     if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
176     # print "$pkg Diff.gz => Downloaded\n";
177     if(md5_of_file($localfile)==$md5_atsource) {
178     #print "MD5 => OK\n";
179     #unlink($localfile);
180     return $localfile;
181     }
182 atomo64-guest 54 }
183     }
184     print "Download Diff.gz of package $pkg => Fallito\n";
185     return false;
186 evaso-guest 5 }
187 atomo64-guest 102 function db_add($initial='') {
188 atomo64-guest 54 global $dirs,$dists,$dbconn;
189     check_db();
190 atomo64-guest 64 download_sources();
191 atomo64-guest 54
192 atomo64-guest 65 $db = pg_pconnect($dbconn) or die_status(pg_last_error($db));
193 atomo64-guest 54 pg_exec($db, "CREATE TEMP TABLE pkgs_atsrc (name text,dist text)") or die_status('Error creating temp table pkgs_atsrc');
194     pg_exec($db, "CREATE UNIQUE INDEX idxdis on pkgs_atsrc (name,dist)") or die_status('Error creating index on temp table');
195     pg_exec($db, "CREATE TEMP TABLE bin_atsrc (name text,bin_name text, dist text)") or die_status('Error creating temp table pkgs_atsrc');
196     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');
197    
198     foreach ($dists as $dist=>$sections) {
199 geissert 175 $pkgs = array();
200 geissert 176 $pkgsections = array();
201 evaso-guest 5 foreach ($sections as $section) {
202 atomo64-guest 54 $filename=$dirs['sources_dir'] . "/$dist/$section/Sources.gz";
203     print "\nI'm parsing $dist/$section => Sources.gz\n";
204 geissert 180
205     // Big regexp to parse a package entry
206    
207     // [1] Source package name
208     $regexp = "/Package:\s($initial.*)[^a]";
209     // [2] : Binary package name
210     $regexp .= "Binary:\s(.+)[^a]";
211     // [3] : Epoch
212     // [4] : Version
213     $regexp .= "Version:\s(\d+:)?(.+)[^a]";
214     $regexp .= "Priority.+";
215     // [5] : Maintainer email address
216     $regexp .= "Maintainer:.+<([^>]+)>[^a]";
217     $regexp .= ".+";
218     // [6] : Source package format
219     $regexp .= "Format:\s(.+)[^a]";
220     // [7] : Package directory
221     $regexp .= "Directory:\s(.+)[^a]";
222 geissert 182 // [8] : md5sum of the .diff.gz (or .debian.tar.(gz|bz2))
223 geissert 180 // [9] : Size of the file in bytes
224     // [10] : Extension (.diff.gz or .debian.tar.gz)
225 geissert 182 $regexp .= "Files:.+\s(\S{32})\s(\d+)\s" . '\1\S+' . "(\.diff\.gz|\.debian\.tar\.(?:gz|bz2))[^a]";
226 geissert 180 // [11] : Uploaders field
227     // [12] : Uploaders value
228     $regexp .= "(Uploaders:\s(.+)[^a])?";
229     // [13] : DM-Upload-Allowed field
230     // [14] : DM-Upload-Allowed value
231     $regexp .= "(Dm-Upload-Allowed:\s(.+)[^a])?";
232     // [15] : Homepage field
233     // [16] : Homepage value
234     $regexp .= "(Homepage:\s(.+)[^a])?";
235     // [17] : Vcs-Browser field
236     // [18] : Vcs-Browser value
237     $regexp .= "(Vcs-Browser:\s(.+)[^a])?";
238     // [19] : Vcs-* field
239     // [20] : vcs type
240     // [21] : Vcs-* value
241     $regexp .= "(Vcs-([A-Z][a-z]+):\s(.+)[^a])?";
242     // [21] : Checksums-* field
243     // [22] : Checksums-* value
244     $regexp .= "(Checksums-\S+:\s(.+)[^a])?";
245     $regexp .= "/Ssi";
246 atomo64-guest 54 $zp = gzopen($filename, "r") or die_status("Could not open $filename");
247     $extracted=tempnam("/tmp/", "$dist_$section_Sources");
248     exec("gzip -c -d $filename > $extracted");
249     $sourcesize=filesize($extracted);
250     unlink($extracted);
251     if(filesize($filename)>20) {
252     while (!gzeof($zp)) {
253     $line='';
254     while ($line!="\n") {
255     $line = gzgets ($zp,4096) ;
256     $buff1 .= $line;
257     }
258     if (preg_match($regexp,$buff1,$matches)) {
259 geissert 176 if (!isset($pkgs[$matches[1]]) || is_updated($pkgs[$matches[1]][4],$matches[4],true)) {
260 geissert 177 unset($matches[0]);
261 geissert 176 $pkgs[$matches[1]] = $matches;
262     $pkgsections[$matches[1]] = $section;
263     }
264 atomo64-guest 54 }
265     else {
266     preg_match("/Package:\s(.+)[^a]Binary/im",$buff1,$matches);
267     if(strncmp($initial,$matches[1],strlen($initial))<0) break;
268     }
269     $left=$sourcesize-gztell($zp);
270     print "\rSource file Left => $left";
271     $buff1="";
272     unset($matches);
273     }
274 evaso-guest 5 }
275     gzclose($zp) ;
276     }
277 geissert 178 foreach ($pkgs as $package=>$matches) {
278 geissert 176 $section = $pkgsections[$matches[1]];
279 geissert 180 // Maintainer email address
280 geissert 176 if ($matches[5]) {
281     $matches[5]=iconv("ISO-8859-1","UTF-8",$matches[5]);
282     $matches[5]=pg_escape_string($matches[5]);
283     }
284 geissert 180 // Uploaders
285     if ($matches[12]) {
286     $matches[12]=iconv("ISO-8859-1","UTF-8",$matches[12]);
287     $matches[12]=pg_escape_string($matches[12]);
288 geissert 176 }
289     @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR die_status("Temp table pkgs_atsrc query error");
290 geissert 180 $rst=@pg_exec($db, "INSERT INTO pkgs (name,version,dversionmangled,maint,dir,md5_atsource,bytes,dist,section,uploaders,homepage,vcs_browser,vcs_type,vcs,vcsoversource,lastpkgsourcesupdate) VALUES ('$matches[1]','$matches[4]','$matches[4]','$matches[5]','$matches[7]','$matches[8]','$matches[9]','$dist','$section','$matches[12]','$matches[16]','$matches[18]','$matches[20]','$matches[21]','0',now())");
291 geissert 176 if (!$rst) {
292     $rsql=pg_exec($db, "SELECT pkgs.version FROM pkgs WHERE name='$matches[1]' AND dist='$dist';");
293     $version = $matches[4]; // just for safety
294     while ($res_array=pg_fetch_array($rsql)) {
295     $version = $res_array['version'];
296     }
297     $extra = '';
298     if ($version != $matches[4]) {
299     // only update the dversionmangled field if the versions differ
300     $extra = ",dversionmangled='$matches[4]'";
301     // also reset wwiz_type
302     $extra = ",wwiz_type=NULL";
303     // and update the lastpkgsourcesupdate field
304     $extra = ",lastpkgsourcesupdate=now()";
305     }
306 geissert 180 $rst=@pg_exec($db, "UPDATE pkgs SET name='$matches[1]',version='$matches[4]'$extra,maint='$matches[5]',dir='$matches[7]',md5_atsource='$matches[8]',bytes='$matches[9]',dist='$dist',section='$section',uploaders='$matches[12]',homepage='$matches[16]',vcs_browser='$matches[18]',vcs_type='$matches[20]',vcs='$matches[21]' WHERE name='$matches[1]' AND dist='$dist'") OR die_status("\nDb adding error =>" . pg_last_error() . "\n");
307 geissert 176 }
308     $bin_names=split(",", $matches[2]);
309     foreach ($bin_names as $bin_name) {
310     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");
311     $rst=@pg_exec($db, "INSERT INTO binpkgs (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") ;
312     }
313     }
314 atomo64-guest 54 }
315 atomo64-guest 102 clear_db($db);
316 atomo64-guest 54 pg_close($db);
317 evaso-guest 5 }
318 atomo64-guest 72 function db_query($pkg,$dist='%') {
319 atomo64-guest 54 global $dirs,$dbconn;
320 atomo64-guest 65 $db = pg_pconnect($dbconn ) or die_status(pg_last_error($db));
321 atomo64-guest 122 $rsql=pg_exec($db, "SELECT * FROM pkgs
322     WHERE name='$pkg' AND dist LIKE '$dist';");
323 atomo64-guest 72 while ($res_array=pg_fetch_array($rsql)) {
324 atomo64-guest 54 print "Package=> " . $res_array['name'] . "\n";
325     print "Version => " . $res_array['version'] . "\n";
326 atomo64-guest 73 print "Mangled version => " . $res_array['dversionmangled'] . "\n";
327 atomo64-guest 54 print "Diff MD5 Downloaded => " . $res_array['md5_diff'] . "\n";
328     print "Diff MD5 at Sources => " . $res_array['md5_atsource'] . "\n";
329     print "Distribution => " . $res_array['dist'] . "\n";
330     print "Section => " . $res_array['section'] . "\n";
331     print "KBytes => " . number_format ( $res_array['bytes']/1024, 2, ",",".") . "\n";
332 atomo64-guest 122 print "Pop Inst=>" . $res_array['pop_inst'] . "\n";
333 evaso-guest 5 if ($res_array['watch']) {
334 atomo64-guest 54 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['watch']);
335 atomo64-guest 121 print "Version Upstream => " . $uscan_res['uversion'] . "\n";
336 atomo64-guest 72 print "Watch => " . trim($res_array['watch'])."\n";
337 atomo64-guest 121 print "Watch Warnings => " . $uscan_res['warnings'];
338 evaso-guest 5 }
339     else print "Watch => Not present" . "\n";
340 atomo64-guest 58 if ($res_array['wwiz_type']) {
341     $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['wwiz']);
342 atomo64-guest 121 print "WWiz Upstream => " . $uscan_res['uversion'] . "\n";
343 atomo64-guest 72 print "WWiz => " . $res_array['wwiz']."\n";
344 atomo64-guest 121 print "WWiz Warings => " . $uscan_res['warnings'];
345 atomo64-guest 58 }
346     print "\n";
347 atomo64-guest 54 }
348 atomo64-guest 72 if (pg_numrows($rsql)==0) echo "Package=> " . $pkg . "\nNot found\n!!";
349 atomo64-guest 54 pg_close($db);
350 evaso-guest 5 }
351 atomo64-guest 54 function byte_total(&$db,$initial) {
352     global $dirs;
353     $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)");
354     $res_array=pg_fetch_array($rsql);
355     return $res_array[totbytes] ;
356 evaso-guest 5 }
357 atomo64-guest 64 function download_sources () {
358 atomo64-guest 54 global $dirs,$dists,$mirrors;
359     foreach ($dists as $dist=>$sections) {
360     $server=$mirrors[$dist];
361     $conn_id = ftp_cn("anonymous","bo@bo.org",$server);
362     foreach ($sections as $section) {
363     $localfile=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
364     $remotefile=remote_dist_dir($dist) . "/$dist/$section/source/Sources.gz";
365     $sources_info=md5_source($section,$dist);
366     if (!file_exists ($dirs[sources_dir] . "/$dist/")) mkdir ( $dirs[sources_dir] . "/$dist",0740);
367     if (!file_exists ($dirs[sources_dir] . "/$dist/$section")) mkdir ($dirs[sources_dir] . "/$dist/$section",0740);
368     if (!file_exists($localfile) OR md5_of_file($localfile)!=$sources_info[md5]) {
369     print "Downloading $remotefile Bytes $sources_info[bytes]=> ";
370     if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
371     print "Done\n";
372     if(md5_of_file($localfile)==$sources_info[md5]) print "MD5 => OK\n";
373 atomo64-guest 59 else print "MD5 => no match\n";
374 atomo64-guest 54 }
375     else {
376 atomo64-guest 59 unlink($localfile);
377 atomo64-guest 54 print "Fail\n";
378     die_status ("Error in source file");
379     }
380     }
381 evaso-guest 5 }
382 atomo64-guest 54 ftp_quit($conn_id);
383     }
384 evaso-guest 5 }
385     function md5_source($section,$dist) {
386 atomo64-guest 54 global $mirrors;
387 atomo64-guest 144 $release=file_get_contents ("ftp://$mirrors[$dist]" . remote_dist_dir($dist) . "/$dist/Release") or die_status("Failed to check MD5 of sources");
388     preg_match ("/\s([^\s]+)\s+(\d+)\s$section\/source\/Sources\.gz/i", $release,$matches);
389 atomo64-guest 54 return array("md5"=>$matches[1],"bytes"=>$matches[2]);
390     # 183dea9ca3c2e35ffd6cf5eee9501c21 2941637 main/binary-i386/Packages.gz
391 evaso-guest 5 }
392 atomo64-guest 54
393 atomo64-guest 118 function vers_conv($debvers, $extendedConv = true) {
394 atomo64-guest 54 // Strip off the epoch
395     $pos = strpos($debvers, ':');
396     if ( $pos !== FALSE) {
397     $debvers = substr($debvers, $pos+1);
398     }
399 evaso-guest 45
400 atomo64-guest 54 // strip off the Debian revision (look from the back of the string)
401     $pos = strrpos($debvers, '-');
402     if ( $pos !== FALSE) {
403     $debvers = substr($debvers, 0, $pos);
404     }
405 evaso-guest 45
406 atomo64-guest 116 if ($extendedConv) {
407     // strip off repacking indicators
408     $debvers = preg_replace("/[-.+~]?(ds|dfsg|debian)(.*)/i", "", $debvers);
409     }
410    
411 atomo64-guest 54 return $debvers;
412 evaso-guest 5 }
413     function check_db() {
414 atomo64-guest 54 global $dirs,$dbconn;
415 atomo64-guest 64 $db = pg_pconnect($dbconn) or die_status(1/*pg_last_notice($db)*/);
416 atomo64-guest 95 if (!pg_table_exists($db, "pkgs")) {
417 atomo64-guest 149 pg_exec($db, "CREATE TABLE pkgs (id serial PRIMARY KEY,name text,version text, dversionmangled 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, lastcheck timestamp, homepage text, vcs text, vcs_type text, vcs_browser text,vcs_watch text, vcsoversource bool,vcs_up_version text,vcs_dversionmangled text,vcs_up_url text, vcs_watch_warn text, vcs_updated bool, lastvcsfetch timestamp, lastupvsdebsync timestamp, lastpkgsourcesupdate timestamp, failures_count numeric, pop_inst numeric,pop_vote numeric)") or die_status('Error creating table pkgs\n');
418 atomo64-guest 54 pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
419 atomo64-guest 95 pg_exec($db, "CREATE INDEX checkx on pkgs (lastcheck)") or die_status('Errore creating index - ' . pg_last_error($db));
420 atomo64-guest 133 pg_exec($db, "CREATE INDEX updatex on pkgs (lastcheck,lastpkgsourcesupdate);") or die_status('Errore creating index - ' . pg_last_error($db));
421 atomo64-guest 122 pg_exec($db, "CREATE INDEX maintx on pkgs (maint)") or die_status('Errore creating index - ' . pg_last_error($db));
422 atomo64-guest 54 $sw=true;
423     }
424 atomo64-guest 95 if (!pg_table_exists($db, "binpkgs")) {
425 atomo64-guest 54 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');
426     pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
427     $sw=true;
428     }
429 atomo64-guest 95 if (!pg_table_exists($db, "events")) {
430 atomo64-guest 122 pg_exec($db, "CREATE TABLE events (id serial PRIMARY KEY, name text,type text, dist text, stamp timestamp, content text)") or die_status('Error creating table events\n');
431     pg_exec($db, "CREATE UNIQUE INDEX idxevents on events (name,dist,content)") or die_status('Error creating index - ' . pg_last_error($db));
432     pg_exec($db, "CREATE INDEX pkgxeventtype on events (name,type,dist)") or die_status('Error creating index - ' . pg_last_error($db));
433 atomo64-guest 78 $sw=true;
434 atomo64-guest 95 }
435 atomo64-guest 122 if (!pg_table_exists($db, "stamps")) {
436     pg_exec($db, "CREATE TABLE stamps (type text PRIMARY KEY, stamp timestamp, pid text)") or die_status('Error creating table stamps\n');
437     $sw=true;
438     }
439 atomo64-guest 54 #if (!pg_table_exists($db,"upstream")){
440     # 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');
441     # pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die_status('Errore nel creare gli indici ' . pg_last_error($db));
442     # $sw=true;
443     #}
444     pg_close($db);
445     if($sw) return $sw;
446     else return false;
447 evaso-guest 5 }
448     function pg_table_exists (&$dblink, $table) {
449 atomo64-guest 54 $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";
450     if ($res = pg_exec ($dblink, $sql)) {
451     return pg_numrows($res) > 0;
452     } else {
453     return false; // or throw exception
454     }
455     }
456 evaso-guest 5
457    
458     function watch_stat() {
459 atomo64-guest 54 global $dirs,$dbconn;
460 atomo64-guest 65 $db = pg_pconnect($dbconn) or die_status(pg_last_error($db));
461 atomo64-guest 54 $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");
462     $tot_watch=pg_fetch_array($rsql);
463     print "Total packages with Watch =>$tot_watch[0]\n";
464     $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");
465     $tot_nowatch=pg_fetch_array($rsql);
466     pg_close($db);
467     print "Totale packages without Watch => $tot_nowatch[0]\n";
468     print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".") . "%\n";
469 evaso-guest 5 }
470     function remote_dist_dir($dist) {
471 atomo64-guest 54 #if ($dist=="non-US") return "/debian-non-US/dists/unstable";
472     #else
473     return "/debian/dists";
474 evaso-guest 5 }
475 atomo64-guest 102 function clear_db(&$db) {
476 atomo64-guest 54 #global $dbconn;
477     #if (check_db()) die_status("Il Database era vuoto\n");
478 atomo64-guest 64 #download_sources();
479 atomo64-guest 54 #$db = pg_pconnect($dbconn) or die_status(pg_result_error($db));
480     $sql1= "Delete from binpkgs where id NOT IN
481 evaso-guest 5 (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;
482 atomo64-guest 54 $sql2= "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;
483 atomo64-guest 102 $sql3= "UPDATE pkgs SET wwiz=NULL, wwiz_type=NULL, wwiz_version=NULL WHERE watch IS NOT NULL and watch!=''" ;
484     $sql4= "UPDATE pkgs SET up_changes=NULL WHERE updated='1'";
485     if(pg_exec($db,$sql1) && pg_exec($db,$sql2) && pg_exec($db,$sql3) && pg_exec($db,$sql4)) {
486 atomo64-guest 85 print "Db clear complete\n";
487     }
488 evaso-guest 5 }
489 atomo64-guest 135 function db_popcon() {
490 atomo64-guest 54 global $dirs,$dbconn;
491 atomo64-guest 65 $db = pg_pconnect($dbconn) or die_status(pg_last_error($db));
492 atomo64-guest 135 $filename=$dirs['popcon_dir'] . "/popcon.db";
493     $popcon_db = dba_open($filename, 'r-', 'db4');
494 atomo64-guest 122
495 atomo64-guest 124 $rsql = pg_exec($db, "SELECT name FROM pkgs");
496 atomo64-guest 122
497     while ($res_array=pg_fetch_array($rsql)) {
498     $package = $res_array['name'];
499    
500 atomo64-guest 126 if (!dba_exists('si:' . $package, $popcon_db)) {
501 atomo64-guest 122 continue;
502 atomo64-guest 54 }
503 atomo64-guest 122
504 atomo64-guest 126 $inst = dba_fetch("si:$package", $popcon_db);
505     $vote = dba_fetch("sv:$package", $popcon_db);
506 atomo64-guest 122
507 atomo64-guest 135 // when one of the values is 'missing' set to -1 so the other value
508     // _is_ UPDATEd, but use -1 to catch those bugs
509 atomo64-guest 136 $inst = (is_numeric($inst))? $inst : -1;
510     $vote = (is_numeric($vote))? $vote : -1;
511 atomo64-guest 135
512 atomo64-guest 122 @pg_exec($db, "UPDATE pkgs SET pop_inst='$inst',pop_vote='$vote' WHERE name='$package'");
513 atomo64-guest 54 }
514 atomo64-guest 122
515     dba_close($popcon_db);
516 atomo64-guest 54 @pg_close($db);
517 evaso-guest 5 }
518 atomo64-guest 59 function dl_popcon() {
519 atomo64-guest 54 global $dirs,$dbconn;
520 atomo64-guest 122
521 atomo64-guest 135 $localtempdir=$dirs['popcon_dir'] . "/new";
522 atomo64-guest 131 dir_ctrl($localtempdir);
523    
524 atomo64-guest 135 $localfile=$dirs['popcon_dir'] . "/popcon.db";
525 atomo64-guest 122 $remotefile='http://qa.debian.org/data/popcon/popcon.db';
526    
527     $return = 0;
528 atomo64-guest 123 $output = null;
529 atomo64-guest 131 exec('cd ' . escapeshellarg($localtempdir) . ' && ' .
530 atomo64-guest 135 '/usr/bin/wget -qN ' . escapeshellarg($remotefile), $output, $return);
531 atomo64-guest 122
532     if ($return != 0) {
533     echo __FUNCTION__ . "wget exited with status $return!\n";
534     return false;
535     }
536    
537 atomo64-guest 131 return copy($localtempdir . '/popcon.db', $localfile);
538 evaso-guest 5 }
539 atomo64-guest 102 function is_updated ($up_vers,$debian_vers, $dversionAsIs = false) {
540 atomo64-guest 97 if ($up_vers=='') return 1;
541 atomo64-guest 102
542 atomo64-guest 116 $extendedConv = true;
543    
544 atomo64-guest 102 if ($up_vers === $debian_vers) $dversionAsIs = true;
545    
546 atomo64-guest 116 if (strpos(strtolower($up_vers), 'dfsg') !== false && strpos(strtolower($debian_vers), 'dfsg') !== false) {
547     $extendedConv = false;
548     }
549 atomo64-guest 102
550 atomo64-guest 119 passthru("dpkg --compare-versions ".escapeshellarg($up_vers)." le " . escapeshellarg($dversionAsIs?$debian_vers:vers_conv($debian_vers, $extendedConv)),$great);
551 atomo64-guest 116
552 atomo64-guest 97 return (int)!($great);
553 evaso-guest 17 }
554 atomo64-guest 99 function db_upstream($initial='%', $checkStamps = false, $limit = null) {
555     global $dirs, $dbconn, $minimumAge;
556    
557 atomo64-guest 54 check_db();
558 atomo64-guest 64 $db = pg_pconnect($dbconn) ;
559 atomo64-guest 95 events::DB($db);
560 atomo64-guest 99
561     $postWHERE = '';
562     if ($checkStamps) {
563 atomo64-guest 167 $postWHERE = "AND (lastcheck<='".sqldate(time() - ($minimumAge*24*3600))."' OR lastcheck IS NULL OR (lastpkgsourcesupdate>lastcheck))";
564 atomo64-guest 99 }
565    
566     $postORDER = '';
567     if ($limit !== null && is_int($limit)) {
568     $postORDER = 'LIMIT ' . $limit;
569     }
570    
571 atomo64-guest 149 $rsql=pg_exec($db, "SELECT id, name,dist,up_version,version,watch,lastupvsdebsync,failures_count FROM pkgs WHERE watch is not null AND name LIKE '$initial' $postWHERE ORDER BY lastcheck DESC $postORDER");
572 atomo64-guest 54 $tot=pg_numrows($rsql);
573 atomo64-guest 99
574 atomo64-guest 54 while ($res_array=pg_fetch_array($rsql)) {
575 atomo64-guest 95
576 atomo64-guest 54 $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);
577 atomo64-guest 95
578 atomo64-guest 121 #print $uscan_res['uversion'] . "\n Name => $res_array[name]\n";
579 atomo64-guest 95 $version = $res_array['version'];
580     $dversionmangled = $res_array['version'];
581     $lastupvsdebsync = $res_array['lastupvsdebsync'];
582 atomo64-guest 110 $mangle = true;
583 atomo64-guest 95
584 atomo64-guest 149 /* Do not increase the number of failures if uscan didn't throw any warning
585     because it might be that the watch file is "empty"
586     */
587     if (!$uscan_res['uversion'] && $uscan_res['warnings']) {
588     $res_array['failures_count']++;
589     } else {
590     $res_array['failures_count'] = 0;
591     }
592    
593 atomo64-guest 121 print "\rVersion=>" . $uscan_res['dversion'];
594     if ($uscan_res['dversion']!=null) $version=$uscan_res['dversion'];
595     if ($uscan_res['dversionmangled']!=null && $uscan_res['dversionmangled'] != vers_conv($res_array['version'])) {
596     $dversionmangled=$uscan_res['dversionmangled'];
597 atomo64-guest 110 $mangle = false;
598     }
599 atomo64-guest 95
600 atomo64-guest 121 $up_url=iconv("ISO-8859-1","UTF-8",$uscan_res['uurl']);
601     $up_version=iconv("ISO-8859-1","UTF-8",$uscan_res['uversion']);
602 atomo64-guest 116
603 atomo64-guest 121 $updated = $uscan_res['updated'];
604 atomo64-guest 95
605     /*if (!$updated)*/ {
606 atomo64-guest 121 $updated=is_updated($uscan_res['uversion'], $dversionmangled, !$mangle);
607 atomo64-guest 76 }
608    
609 atomo64-guest 70 if (!$updated) {
610 atomo64-guest 149 events::newVersion($res_array['name'], $res_array['version'], $up_version, $res_array['dist'], $up_url);
611 atomo64-guest 95 } else {
612     $lastupvsdebsync = sqldate();
613 atomo64-guest 70 }
614    
615 atomo64-guest 107 if ($lastupvsdebsync === null) {
616     $lastupvsdebsync = sqldate(0);
617     }
618    
619 atomo64-guest 149 $keep_changes = (int) ($up_version != $res_array['up_version']);
620 atomo64-guest 95
621 atomo64-guest 66 $watch_warn='';
622 atomo64-guest 121 if($uscan_res['warnings'] != null) {
623     $watch_warn=pg_escape_string(iconv("ISO-8859-1","UTF-8", $uscan_res['warnings']));
624 atomo64-guest 66 }
625 atomo64-guest 95
626 atomo64-guest 149 pg_exec($db, "UPDATE pkgs SET
627     up_version='" . pg_escape_string($up_version) . "',
628     dversionmangled='".pg_escape_string($dversionmangled)."',
629     up_url='" . pg_escape_string($up_url) . "',
630     watch_warn='" . $watch_warn . "',
631     keep_changes='$keep_changes',
632     wwiz_version=NULL,
633     wwiz_type=NULL,
634     wwiz=NULL,
635     failures_count=$res_array[failures_count],
636     updated='$updated',
637     lastcheck=now(),
638     lastupvsdebsync='$lastupvsdebsync'
639     WHERE id=$res_array[id]") or die_status("Error in update pkgs upstream");
640    
641 atomo64-guest 121 #print "\rUpdated " . $res_array['name'] . " => $updated Up Version => ". $uscan_res['uversion'];
642 atomo64-guest 78 print "\t Upstream left => " . $tot --;
643 atomo64-guest 54 # }
644     # else print "Added ". $res_array['name'] ."\n";
645     # if ($great==0) {
646    
647     # print "Package=> " . $res_array['name'] . "\n";
648     # print "Version => " . $res_array['version'] . "\n";
649 atomo64-guest 121 # print "Upstream Ver=> " . $uscan_res['uversion'] . "\n";
650     #print "Watch warings=> " . $uscan_res['warnings'] . "\n";
651 atomo64-guest 54 # }
652     }
653     print "\n";
654     pg_close($db);
655 evaso-guest 5 }
656 evaso-guest 20 function check_url ($url) {
657 atomo64-guest 54 $url_p = parse_url ($url);
658     if (isset ($url_p["host"])) $host = $url_p["host"];
659     else return false;
660     preg_match("/(http|ftp)/",$host,$matches);
661     if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 5);
662     elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 5);
663     if (!$fp AND !$fp2) return false;
664     else return true;
665 evaso-guest 20 }
666 evaso-guest 17 function keep_remote ($localfile,$remotefile) {
667 atomo64-guest 54 if (!check_url($remotefile)) return false;
668     # $rfp = @fopen ($remotefile, "r");
669     # if (!$rfp) {
670     # return false;
671     # }
672     # fclose($rfp);
673     # $lfp = fopen ($localfile, "w");
674     # stream_set_blocking($rfp,0);
675     # ini_set ( "memory_limit", "100M");
676     # while (!feof($rfp)) {
677     # $data = fread($rfp, 8192);
678     # $bytes+=8192;
679     # print "Downlaoding $remotefile: $bytes\r";
680     # fwrite($lfp, $data);
681     # } while (true);
682     # ini_set ( "memory_limit", "15M");
683    
684     if (!copy($remotefile, $localfile)) {
685     echo "Copia di $file non riuscita ...\n";
686     return false;
687     }
688     # fclose($lfp);
689     # fclose($rfp);
690    
691     return true;
692 evaso-guest 44 }
693 atomo64-guest 59 function up_changes($src_name='%', $force = false) {
694     global $dbconn, $fetchUpstreamChanges;
695    
696     if (!$fetchUpstreamChanges && !$force) {
697 atomo64-guest 95 //print "\nSkipping download of upstream sources to grab changelogs\n";
698 atomo64-guest 59 return;
699     }
700    
701 atomo64-guest 54 $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");
702 evaso-guest 44 $db = pg_pconnect($dbconn) ;
703 atomo64-guest 85 $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!='' AND keep_changes='t' AND name LIKE '$src_name'");
704 atomo64-guest 54 $tot=pg_numrows($rsql);
705     while ($res_array=pg_fetch_array($rsql)) {
706     $localfile=tempnam("/tmp/", "dehs-" . $res_array[id] . ".tar.gz");
707     print "I'm downloading $res_array[up_url]\r";
708     $res=keep_remote($localfile,$res_array['up_url']);
709     print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";
710     if (!$res) {
711     pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
712     $notdown++;
713     $tot--;
714     print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
715     #print "\n Not Downlaoded: $res_array[up_url]\n";
716     unlink($localfile);
717     continue;
718     }
719     foreach($changes_files as $filename) {
720 atomo64-guest 59 $command="tar --wildcards -zxOf " . $localfile . " *$filename 2>/dev/null";
721 atomo64-guest 54 exec($command,$up_changes,$cmdres);
722     $up_changes=rtrim(implode("\n",$up_changes));
723     if ($cmdres==0 AND strlen($up_changes)>20) {
724     $sw=true;
725     $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);
726     if ($conv_res) $up_changes=$conv_res;
727 atomo64-guest 66 $up_changes=pg_escape_string($up_changes);
728 atomo64-guest 54 $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";
729     pg_exec($db,$up_changes_sql);
730     #print "\nFound $filename on $res_array[up_url]\n";
731     $found++;
732     break;
733     }
734     }
735     if (!$sw) {
736     pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
737     #print "\n Not Found any on $res_array[up_url]\n";
738     $notfound++;
739     }
740     unlink($localfile);
741     print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
742     }
743     pg_close($db);
744 evaso-guest 17 }
745 evaso-guest 5 function uscan_foo($name,$version,$watch){
746 atomo64-guest 46 global $base, $uscan;
747 atomo64-guest 54
748 evaso-guest 43 $tmpnam=tempnam('/tmp/', $name.'_watch');
749 evaso-guest 42 $fw=fopen($tmpnam,"w");
750 atomo64-guest 54 fwrite($fw,$watch);
751     fclose($fw);
752    
753     $uscan_res=shell_exec("$uscan --dehs --package $name --upstream-version " . escapeshellarg(vers_conv($version)) . " --watchfile $tmpnam 2>/dev/null 2>&1");
754    
755 evaso-guest 43 unlink($tmpnam);
756 atomo64-guest 54
757     $p = xml_parser_create();
758     xml_parse_into_struct($p, $uscan_res, $vals, $index);
759     xml_parser_free($p);
760    
761     $upstream=$vals[$index['UPSTREAM-VERSION'][0]][value];
762     $up_url=$vals[$index['UPSTREAM-URL'][0]][value];
763     $deb_vers=$vals[$index['DEBIAN-UVERSION'][0]][value];
764 atomo64-guest 73 $deb_mangled_vers=$vals[$index['DEBIAN-MANGLED-UVERSION'][0]][value];
765 atomo64-guest 54 $warnings=$vals[$index['WARNINGS'][0]][value];
766     $updated=$vals[$index['STATUS'][0]][value];
767    
768 atomo64-guest 95 if ($updated=="up to date") $updated=1;
769     else $updated=0;
770 atomo64-guest 54
771     if ($upstream=='') $upstream=null;
772     if ($up_url=='') $up_url=null;
773     if ($deb_vers=='') $deb_vers=null;
774     if ($warnings=='') $warnings=null;
775 atomo64-guest 73 if ($deb_mangled_vers=='') $deb_mangled_vers=null;
776 atomo64-guest 54
777 atomo64-guest 121 $ret = array();
778     $ret['uversion'] = $upstream;
779     $ret['warnings'] = $warnings;
780     $ret['uurl'] = $up_url;
781     $ret['dversion'] = $deb_vers;
782     $ret['dversionmangled'] = $deb_mangled_vers;
783     $ret['updated'] = $updated;
784    
785     return $ret;
786 atomo64-guest 54 }
787 evaso-guest 5 function db_up_error() {
788     global $dbconn;
789 atomo64-guest 54 check_db();
790 atomo64-guest 64 $db = pg_pconnect($dbconn) ;
791 atomo64-guest 54 $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";
792 atomo64-guest 59 print "\nDownloading bugs list =>";
793 atomo64-guest 133 if (!($ferrors=file_get_contents($bugurl))) {
794     /*die_status ("I can't keep error page from bugs.debian.org");*/
795     fwrite(STDERR, "I can't keep error page from bugs.debian.org, but not aborting");
796     return false;
797     }
798 atomo64-guest 54 print "Done\n";
799     $ferrors=explode("<li>",$ferrors);
800     $totline=count($ferrors);
801     #$errors[]=array();
802     #array_pop($errors);
803 atomo64-guest 59 print "Total lines => $totline\n";
804 atomo64-guest 54 foreach ($ferrors as $line) {
805     # print $line . "\n";
806 atomo64-guest 150 if (preg_match("/Package:.+?>([^<]+)<\/a>.+?<strong>((\d+)\syears?\sand\s|)(\d+)\sdays?/sS",$line,$matches)) {
807 atomo64-guest 54 $errors[$matches[1]][err_tot]++;
808     $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];
809     #if ($matches[3]) print "Anni $matches[3]";
810     #print "Giorni $matches[4]\n";
811     }
812 atomo64-guest 59 print "\rLines left => " . $totline--;
813 atomo64-guest 54 }
814     #arsort($errors);
815     $total=count($errors);
816 atomo64-guest 59 foreach ($errors as $pkg_name=>$value) {
817     pg_exec($db, "UPDATE binpkgs SET up_error=" . $value[err_tot] . ",avg_error_date=" . $value[days]/$value[err_tot] . " WHERE bin_name='$pkg_name'") or die_status("Error in upstream bug query");
818 atomo64-guest 54 #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot] . "\n";
819 atomo64-guest 59 print "\rBugs left => " . $total--;
820 atomo64-guest 54 }
821     pg_close($db);
822 evaso-guest 5 }
823 atomo64-guest 99 function watch_wizard($src_name='%', $checkStamps = false, $limit = null){
824     global $dirs,$dbconn,$sfIgnore,$watchFileExts,$watchFileDversionMangle,$watchFormat,$minimumAge;
825 atomo64-guest 54 $db = pg_pconnect($dbconn) or die_status ("Db error");
826     $checked=$errors=$notfound=$verok=$notmatch=0;
827    
828 atomo64-guest 62 // prepare some stuff that needs to be done once per run
829 atomo64-guest 73 $opts = array();
830     $exts = '';
831     $dvmanglers = '';
832 atomo64-guest 62
833     foreach ($watchFileExts as $ext) {
834     $exts .= $ext . '|';
835     }
836     unset($ext);
837    
838 atomo64-guest 73 if ($exts != '') {
839     $opts[] = 'uversionmangle=s/\.(' . substr($exts, 0, -1) . ')$//i';
840 atomo64-guest 84 $exts = '(' . $exts . ')';
841 atomo64-guest 62 }
842    
843 atomo64-guest 73 foreach ($watchFileDversionMangle as $dvmangler) {
844     $dvmanglers .= $dvmangler . '|';
845     }
846     unset($dvmangler);
847    
848     if ($dvmanglers != '') {
849     $opts[] = 'dversionmangle=s/[-.+~]?(' . substr($dvmanglers, 0, -1) . ')(.*)$//i';
850     }
851    
852 atomo64-guest 84 $opts[] = 'pasv';
853    
854 atomo64-guest 73 if (is_array($opts)) {
855     $_opts = '';
856     foreach ($opts as $opt) {
857     $_opts .= $opt . ',';
858     }
859     $opts = '';
860     if ($_opts != ',') {
861     $opts = 'opts=' . substr($_opts, 0, -1) . ' \\';
862     }
863     }
864    
865 atomo64-guest 99 $postWHERE = '';
866     if ($checkStamps) {
867 atomo64-guest 121 $postWHERE = "AND (lastcheck<='".sqldate(time() - ($minimumAge*24*3600))."' OR lastcheck IS NULL)"
868 atomo64-guest 122 . " AND wwiz_type!='no_cright' AND wwiz_type!='notmatch'";
869 atomo64-guest 99 }
870    
871     $postORDER = '';
872     if ($limit !== null && is_int($limit)) {
873     $postORDER = 'LIMIT ' . $limit;
874     }
875    
876 atomo64-guest 122 $sqlstring="SELECT name,version,dversionmangled,section,dist, wwiz_version FROM pkgs
877 atomo64-guest 99 WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name' $postWHERE ORDER BY lastcheck DESC $postORDER";
878 atomo64-guest 71 $rsql=pg_exec($db, $sqlstring);
879    
880 atomo64-guest 54 while ($res_array=pg_fetch_array($rsql)) {
881    
882     if (substr($res_array["name"],0,3)=='lib')
883     $initial=substr($res_array["name"],0,4);
884     else
885     $initial=substr($res_array["name"],0,1);
886    
887     $url="http://packages.debian.org/changelogs/pool/" .
888     $res_array["section"] . "/" .
889     $initial . "/" .
890     $res_array["name"] ."/" .
891     $res_array["name"] . "_" .
892     $res_array["version"] . "/copyright";
893     $contents=@file_get_contents($url) or '';
894     $generatedWatches = array();
895    
896     if (!$contents) {
897     $url="http://packages.debian.org/changelogs/pool/" .
898     $res_array["section"] . "/" .
899     $initial . "/" .
900     $res_array["name"] ."/" .
901     $res_array["name"] . "_" .
902     $res_array["version"] . "/" . $res_array["name"] . ".copyright";
903     $contents=@file_get_contents($url) or '';
904     }
905    
906 atomo64-guest 73 if (!$contents) {
907     $url="http://packages.debian.org/changelogs/pool/" .
908     $res_array["section"] . "/" .
909     $initial . "/" .
910     $res_array["name"] ."/" .
911     "current/copyright";
912     $contents=@file_get_contents($url) or '';
913     }
914    
915 atomo64-guest 54 if ($contents) {
916    
917     if (preg_match ('/(sf\.net|sourceforge\.net)/i', $contents, $matches) ||
918     (preg_match ('/([\w\-]+)\.(sf\.net|sourceforge\.net)/i', $contents, $matches)
919     && !in_array($matches[1], $sfIgnore))) {
920     preg_match("/([^\d]+)/",$res_array['name'],$dmatches);
921     $depured_debian_name=$dmatches[1];
922     /*$matches[1]="ftp://download.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";*/
923 atomo64-guest 62 $generatedWatches[]=array('url'=>"http://sf.net/" . $depured_debian_name . "/");
924 atomo64-guest 54 $generatedWatches[]=array('url'=>"http://sf.net/" . $depured_debian_name . "/",'name'=>$depured_debian_name);
925    
926     }
927    
928 atomo64-guest 67 if (preg_match ('#((ftp://|http://|https://)\S+(html|htm|/(?(?=.+(tar|tgz|zip|gz|bz2))|[^>\s\)\a]+)))#i',$contents,$matches)) {
929 atomo64-guest 54 $final=substr($matches[1],-1);
930     if ($final=='.') {
931     $matches[1]=substr($matches[1],0,-1);
932     $final=substr($matches[1],-1);
933     }
934 atomo64-guest 58 if ($matches[2]=="http://" || $matches[2]=="https://") {
935 atomo64-guest 54 $space=' ' . "(?:.*/)?";
936     } elseif ($final!='/') {
937     $matches[1].='/';
938     }
939 atomo64-guest 59 $generatedWatches[]=array('url' => preg_replace("#((http|https|ftp)://.+):(.*)#i",'$1' . '$3', $matches[1],1), 'space' => $space);
940 atomo64-guest 54 }
941    
942 atomo64-guest 67 if (preg_match ('#((ftp|http)\.\S+(html|htm|/(?(?=.+(tar|tgz|zip|gz|bz2))|[^>\s\)\a]+)))#i',$contents,$matches)) {
943 atomo64-guest 54 $final=substr($matches[1],-1);
944     if ($final=='.') {
945     $matches[1]=substr($matches[1],0,-1);
946     $final=substr($matches[1],-1);
947     }
948     $space = '';
949     if ($matches[2]=="http") {
950     $space=' ' . "(?:.*/)?";
951     } elseif ($final!='/') {
952     $matches[1].='/';
953     }
954 atomo64-guest 59 $generatedWatches[]=array('url'=>preg_replace('#(http|ftp)\.(.+)#i','$1://$0', $matches[1],1), 'space'=>$space);
955 atomo64-guest 54 }
956    
957     if (empty($generatedWatches)) {
958     $notmatch++;
959     $contents=iconv("ISO-8859-1","UTF-8",$contents);
960 atomo64-guest 121 pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . pg_escape_string($contents) . "', wwiz_type='notmatch', up_url='', up_changes='', lastcheck=now(), keep_changes='f' where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
961 atomo64-guest 54
962     } else {
963    
964 atomo64-guest 56 $defs = array('name' => $res_array['name'], 'url' => '', 'space' => '');
965 atomo64-guest 54
966     foreach ($generatedWatches as $tWatch) {
967    
968 atomo64-guest 59 if (!is_array($tWatch)) {
969     $tWatch = array($tWatch);
970     }
971    
972 atomo64-guest 54 $tWatch = array_merge($defs, $tWatch);
973    
974 atomo64-guest 62 $watch = sprintf($watchFormat, $tWatch['url'], $tWatch['space'], $tWatch['name'], $exts, $opts);
975 atomo64-guest 54
976 atomo64-guest 62 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
977 atomo64-guest 54
978 atomo64-guest 121 if ($uscan_res['uversion'] && levenshtein($uscan_res['uversion'], $res_array['version']) < strlen($res_array['version'])) {
979 atomo64-guest 71 break;
980 atomo64-guest 54 }
981     }
982    
983 atomo64-guest 121 if ($uscan_res['warnings'] && !$uscan_res['uversion']) {
984     pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . pg_escape_string($uscan_res['warnings']) . "', wwiz_type='error', up_url='', up_changes='', lastcheck=now(), keep_changes='f' where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
985 atomo64-guest 54 $errors++;
986     } else {
987     $verok++;
988 atomo64-guest 121 if ($uscan_res['uversion'] != $res_array['wwiz_version']) {
989 atomo64-guest 54 $keep_changes = 1;
990     } else {
991     $keep_changes = 0;
992     }
993 atomo64-guest 76
994     $dversionmangled = $res_array[version];
995 atomo64-guest 121 if ($uscan_res['dversionmangled']!=null && $uscan_res['dversionmangled'] != vers_conv($res_array['version'])) $dversionmangled=$uscan_res['dversionmangled'];
996 atomo64-guest 76
997 atomo64-guest 121 $updated = $uscan_res['updated'];
998 atomo64-guest 95 /*if ($updated)*/ {
999 atomo64-guest 121 $updated = is_updated($uscan_res['uversion'], $dversionmangled, $dversionmangled != $uscan_res['dversionmangled']);
1000 atomo64-guest 76 }
1001 atomo64-guest 70
1002 atomo64-guest 78 /*if (!$updated) {
1003 atomo64-guest 121 events::newVersion($res_array['name'], $res_array['version'], $uscan_res['uversion'], $res_array['dist']);
1004 atomo64-guest 78 }*/
1005 atomo64-guest 70
1006 atomo64-guest 121 pg_exec($db, "UPDATE pkgs SET wwiz='" . pg_escape_string($watch) ."', wwiz_type='watch', wwiz_version='" . pg_escape_string($uscan_res['uversion']) . "',dversionmangled='".pg_escape_string($dversionmangled)."', up_url='" . pg_escape_string($uscan_res['uurl']) . "', keep_changes='$keep_changes', updated='$updated', lastcheck=now() where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
1007 atomo64-guest 54 }
1008     }
1009     } else {
1010     $notfound++;
1011     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'] ."'");
1012     }
1013     print "Package checked: " . $checked++ .
1014     " OK: $verok Error: $errors Not Matched: $notmatch\r";
1015     }
1016     print "\nCopyright files not found: $notfound\n";
1017 atomo64-guest 59 print "Automatic generated watch file failures: $errors\n";
1018 atomo64-guest 54 pg_close($db);
1019 evaso-guest 13 }
1020 evaso-guest 26 function dbqa_conn ($dbname,$mode) {
1021 atomo64-guest 54 $id = dba_open($dbname, $mode, "db4");
1022     if (!$id) {
1023     die_status("dba_open failed\n");
1024     }
1025     return $id;
1026 evaso-guest 26 }
1027     function dehsqa_db () {
1028 atomo64-guest 54 global $dirs,$dbconn;
1029     $dir1='/org/alioth.debian.org/chroot/home/groups/dehs/htdocs/';
1030     $dir2="/org/qa.debian.org/data/dehs/";
1031     $dists=array("unstable","experimental");
1032     foreach ($dists as $dist) {
1033     $id=dbqa_conn($dir1 . "dehs_qa_" . $dist . ".db",'n');
1034     $db = pg_pconnect($dbconn) or die_status ("Db error");
1035 atomo64-guest 106 $sql="SELECT name, id, up_version, wwiz_version, watch, wwiz_type, watch_warn, version, dversionmangled, updated FROM pkgs WHERE dist='$dist'";
1036 atomo64-guest 54 $rsql=pg_exec($db, $sql);
1037     while($res_array=pg_fetch_array($rsql)) {
1038    
1039 atomo64-guest 63 if (!$res_array[watch]) { if ($res_array['wwiz_type']==null) $version="N/A"; else $version='-';}
1040 atomo64-guest 54 elseif (!$res_array[up_version]) $version="Error";
1041     else $version=$res_array[up_version];
1042    
1043     if ($res_array['wwiz_type']=="watch") $wwiz=$res_array['wwiz_version'];
1044     else $wwiz=$res_array[wwiz_type];
1045    
1046 atomo64-guest 155 // the updated flag is set to true even when up_version could not be determined
1047     $updated = ($res_array['updated'] && ($res_array[up_version] || $res_array[wwiz_version]))? 'yes' : 'no';
1048 atomo64-guest 106
1049 atomo64-guest 54 $xml="<data><id>$res_array[id]</id>" .
1050     "<up_version>$version</up_version>" .
1051 atomo64-guest 73 "<dversion>$res_array[version]</dversion>" .
1052     "<dversionmangled>$res_array[dversionmangled]</dversionmangled>" .
1053 atomo64-guest 155 "<uptodate>$updated</uptodate>" .
1054 atomo64-guest 54 "<wwiz>$wwiz</wwiz>" .
1055     "</data>";
1056     $value=addslashes($xml);
1057     dba_replace($res_array[name],$xml,$id);
1058     }
1059     dba_optimize($id);
1060     dba_close($id);
1061     pg_close($db);
1062     }
1063 evaso-guest 26 }
1064     function md5_of_file($inFile) {
1065 atomo64-guest 54 if (file_exists($inFile)) {
1066     return md5_file($inFile);
1067     } else {
1068     return false;
1069     }
1070 evaso-guest 26 }
1071 evaso-guest 5 function update_all() {
1072 atomo64-guest 111 global $email, $timeStamp_basedCheck;
1073 atomo64-guest 54 $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1074 atomo64-guest 85 echo __FUNCTION__ . ' started the ' . $intime . "\n";
1075 atomo64-guest 54 db_add();
1076 atomo64-guest 59 dl_diffs();
1077 atomo64-guest 144 dl_from_vcs('%','%',in_array('vcs',$timeStamp_basedCheck));
1078 atomo64-guest 54 db_up_error();
1079 atomo64-guest 99 db_upstream('%', in_array('all', $timeStamp_basedCheck));
1080     watch_wizard('%', in_array('all', $timeStamp_basedCheck));
1081 atomo64-guest 54 up_changes();
1082     dehsqa_db();
1083     $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1084     $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";
1085     mail($email, "DEHS report", $messaggio,
1086 atomo64-guest 162 "From: dehs-noreply@{$_SERVER['SERVER_NAME']}");
1087 evaso-guest 5 }
1088 atomo64-guest 64 function update_new() {
1089 atomo64-guest 144 global $email_all, $timeStamp_basedCheck;
1090 atomo64-guest 64 $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1091 atomo64-guest 85 echo __FUNCTION__ . ' started the ' . $intime . "\n";
1092 atomo64-guest 102 db_add();
1093 atomo64-guest 64
1094     global $dbconn;
1095     check_db();
1096     $db = pg_pconnect($dbconn);
1097 atomo64-guest 133 $sql = "SELECT name FROM pkgs WHERE"
1098     ." (md5_diff!=md5_atsource or md5_diff is null)"
1099     ." or (lastcheck is null and watch is not null)"
1100 atomo64-guest 134 ." or (lastpkgsourcesupdate>lastcheck)";
1101 atomo64-guest 64 $rsql=pg_exec($db, $sql);
1102     $res_array=pg_fetch_all($rsql);
1103     pg_close($db);
1104    
1105     dl_diffs();
1106     db_up_error();
1107     if ($res_array !== false) {
1108     foreach ($res_array as $entry) {
1109 atomo64-guest 144 dl_from_vcs($entry['name'],'%',in_array('vcs',$timeStamp_basedCheck));
1110 atomo64-guest 64 db_upstream($entry['name']);
1111     up_changes($entry['name']);
1112 atomo64-guest 69 watch_wizard($entry['name']);
1113 atomo64-guest 64 }
1114     dehsqa_db();
1115     }
1116     $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1117     $messaggio="Dehs executed successful\nBegin: $intime\nPackages processed: ".((!is_array($res_array))?'none':count($res_array))."\nEnd: $endtime";
1118     mail($email_all, "DEHS update_new report", $messaggio,
1119 atomo64-guest 162 "From: dehs-noreply@{$_SERVER['SERVER_NAME']}");
1120 atomo64-guest 64 }
1121 atomo64-guest 65 function update_bogus() {
1122 atomo64-guest 66 global $email_all,$dbconn;
1123 atomo64-guest 65 $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1124 atomo64-guest 85 echo __FUNCTION__ . ' started the ' . $intime . "\n";
1125 atomo64-guest 65 check_db();
1126     $db = pg_pconnect($dbconn);
1127     $sql="SELECT name FROM pkgs WHERE up_version='' AND watch!=''";
1128     $rsql=pg_exec($db, $sql);
1129     $res_array=pg_fetch_all($rsql);
1130 atomo64-guest 77 $orig_count = ((!is_array($res_array))?0:count($res_array));
1131 atomo64-guest 65 if ($res_array !== false) {
1132     foreach ($res_array as $entry) {
1133     db_upstream($entry['name']);
1134     up_changes($entry['name']);
1135     }
1136     dehsqa_db();
1137     }
1138 atomo64-guest 77 $sql="SELECT COUNT(name) FROM pkgs WHERE up_version='' AND watch!=''";
1139     $rsql=pg_exec($db, $sql);
1140     $res_array=pg_fetch_array($rsql);
1141     $new_count = (int)$res_array[0];
1142     pg_close($db);
1143 atomo64-guest 65 $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1144 atomo64-guest 77 $messaggio="Dehs executed successful\nBegin: $intime\n".
1145     "Packages processed: $orig_count\n" .
1146     "New bogus watches count: $new_count\nEnd: $endtime";
1147 atomo64-guest 65 mail($email_all, "DEHS update_bogus report", $messaggio,
1148 atomo64-guest 162 "From: dehs-noreply@{$_SERVER['SERVER_NAME']}");
1149 atomo64-guest 65 }
1150 atomo64-guest 49 function die_status($msg = '', $status = 1) {
1151 atomo64-guest 54 if (empty($msg)) {
1152     die($status);
1153     } else if (is_int($msg) && $status == 1 /* default */) {
1154     die($msg);
1155     } else {
1156 atomo64-guest 113 fwrite(STDERR, $msg);
1157 atomo64-guest 54 die($status);
1158     }
1159 atomo64-guest 49 }
1160 atomo64-guest 70
1161 atomo64-guest 95 function sqldate($timestamp=null) {
1162     if ($timestamp===null) $timestamp=time();
1163     return gmdate("Y-m-d H:i:s", $timestamp);
1164     }
1165    
1166 atomo64-guest 149 function dl_from_vcs($package = '%', $dist = '%', $checkStamps = false, $db = null) {
1167 atomo64-guest 144 global $minimumVCSAge;
1168     $local_db = false;
1169    
1170     if (!$fetchWatchFromVCS) {
1171     return false;
1172     }
1173    
1174     if ($db === null) {
1175     $local_db = true;
1176     $db = pg_pconnect($dbconn) or die_status(pg_last_error($db));
1177     }
1178    
1179 atomo64-guest 149 $sql="SELECT name,dist,vcs_type,vcs FROM pkgs WHERE name LIKE '$package' AND "
1180     . "dist LIKE '$dist' AND vcs!='' AND vcs IS NOT NULL AND vcs_type!='' AND vcs_type IS NOT NULL "
1181 atomo64-guest 144 . ($checkStamps? "AND (lastvcsfetch IS NULL OR lastvcsfetch <='"
1182     . (sqldate(time() - ($minimumVCSAge*24*3600)))."') "
1183     : '');
1184     $rsql=pg_exec($db, $sql);
1185     while ($res = pg_fetch_array($rsql)) {
1186     // avoid miss-parsed entries:
1187     if ( count(explode('\n',$res['vcs_type'])) > 1
1188     || count(explode('\n',$res['vcs'])) > 1)
1189     continue;
1190    
1191 atomo64-guest 149 print "Fetching watch file of $res[name] (type: $res[vcs_type]) from $[vcs]\n";
1192    
1193 atomo64-guest 144 $tmp_dir = shell_exec('mktemp -d') or die_status("mktemp failed to create directory!");
1194     $vcs_watch = null;
1195     if (exec('debcheckout.pl -t ' . escapeshellarg($res['vcs_type'])
1196     . ' -f debian/watch '
1197     . escapeshellarg($res['vcs'])
1198     . ' ' . escapeshellarg($tmp_dir))) {
1199     $vcs_watch = implode('\n', file($tmp_dir . '/debian/watch'));
1200     }
1201    
1202     shell_exec('rm -rf ' . escapeshellarg($tmp_dir));
1203    
1204     $dehs_opts = array();
1205     $dehs_opts['vcs'] = 0;
1206    
1207     if (preg_match('/#\s*DEHS\s*:\s*([\w\s,]+)/i', $vcs_watch, $matches)) {
1208     $_dehs_opts = array_map('trim',explode(',', $matches[1]));
1209     switch ($_dehs_opts) {
1210     case 'vcs':
1211     $dehs_opts['vcs'] = 1;
1212     break;
1213     case 'novcs':
1214     $dehs_opts['vcs'] = 0;
1215     break;
1216     }
1217     }
1218    
1219     if ($vcs_watch != null) {
1220     $vcs_watch = iconv('ISO-8859-1', 'UTF-8', $vcs_watch);
1221     $vcs_watch = pg_escape_string($vcs_watch);
1222     }
1223    
1224     pg_exec($db, "UPDATE pkgs SET vcs_watch='" . $vcs_watch . "', lastvcsfetch = now(), vcsoversource='$dehs_opts[vcs]' WHERE name='$res[name]' AND dist='$res[dist]'") or die_status("Query error");
1225     }
1226    
1227     if ($local_db) pg_close($db);
1228     return true;
1229     }
1230    
1231 atomo64-guest 46 ?>

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5