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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5