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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5