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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5