/[collab-qa]/udd/udd/bugs_gatherer.pl
ViewVC logotype

Diff of /udd/udd/bugs_gatherer.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

udd/src/udd/bugs_gatherer.pl revision 1055 by lucas, Sun Aug 10 13:44:59 2008 UTC udd/udd/bugs_gatherer.pl revision 1351 by lucas, Fri Dec 19 13:30:01 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl -w
2  # Last-Modified: <Sun Aug 10 10:31:57 2008>  # Last-Modified: <Mon Aug 18 14:29:47 2008>
3    
4  use strict;  use strict;
5  use warnings;  use warnings;
# Line 10  use FindBin '$Bin'; Line 10  use FindBin '$Bin';
10  use lib $Bin, qw{/org/udd.debian.net/mirrors/bugs.debian.org/perl};  use lib $Bin, qw{/org/udd.debian.net/mirrors/bugs.debian.org/perl};
11    
12  use DBI;  use DBI;
13    use DBI qw{:sql_types};
14  use YAML::Syck;  use YAML::Syck;
15  use Time::Local;  use Time::Local;
16    
17  use Debbugs::Bugs qw{get_bugs};  use Debbugs::Bugs qw{get_bugs};
18  use Debbugs::Status qw{read_bug get_bug_status bug_presence};  use Debbugs::Status qw{read_bug get_bug_status bug_presence};
19  use Debbugs::Packages qw{binarytosource};  use Debbugs::Packages qw{binarytosource getpkgsrc};
20  use Debbugs::Config qw{:globals};  use Debbugs::Config qw{:globals %config};
21  use Debbugs::User;  use Debbugs::User;
22  #use Debbugs::User qw{read_usertags};  #use Debbugs::User qw{read_usertags};
23    
24  $YAML::Syck::ImplicitTyping = 1;  $YAML::Syck::ImplicitTyping = 1;
25    
26    #Used for measuring time
27    our $t;
28    our $timing = 0;
29    our @archs = grep {  !/(^m68k$|^kfreebsd|^hurd)/ } @{$config{default_architectures}};
30    
31  # Return the list of usernames  # Return the list of usernames
32  sub get_bugs_users {  sub get_bugs_users {
33          my $topdir = "$gSpoolDir/user";          my $topdir = "$gSpoolDir/user";
# Line 87  sub without_duplicates { Line 93  sub without_duplicates {
93          return (grep { ($h{$_}++ == 0) || 0 } @_);          return (grep { ($h{$_}++ == 0) || 0 } @_);
94  }  }
95    
96  sub main {  sub run_usertags {
97          if(@ARGV != 2) {          my ($config, $source, $dbh) = @_;
                 print STDERR "Usage: $0 <config> <source>";  
                 exit 1;  
         }  
   
         my $config = LoadFile($ARGV[0]) or die "Could not load configuration: $!";  
         my $source = $ARGV[1];  
98          my %src_config = %{$config->{$source}};          my %src_config = %{$config->{$source}};
99            my $table = $src_config{'usertags-table'} or die "usertags-table not specified for source $source";
100          my $dbname = $config->{general}->{dbname};          our $timing;
101          # Connection to DB          our $t;
         my $dbh = DBI->connect("dbi:Pg:dbname=$dbname");  
         # We want to commit the transaction as a hole at the end  
         $dbh->{AutoCommit} = 0;  
         my $timing = 1;  
         my $t;  
102    
103    
104          $t = time();          $t = time();
105          # Free usertags table          # Free usertags table
106          $dbh->prepare("DELETE FROM bug_user_tags")->execute() or die          $dbh->do("DELETE FROM $table") or die
107                  "Couldn't empty bug_user_tags: $!";                  "Couldn't empty $table: $!";
108          print "Deleting usertags: ",(time() - $t),"s\n" if $timing;          print "Deleting usertags: ",(time() - $t),"s\n" if $timing;
109          $t = time();          $t = time();
110          # read and insert user tags          # read and insert user tags
# Line 121  sub main { Line 116  sub main {
116                  $user = $dbh->quote($user);                  $user = $dbh->quote($user);
117                  foreach my $tag (keys %tags) {                  foreach my $tag (keys %tags) {
118                          my $qtag = $dbh->quote($tag);                          my $qtag = $dbh->quote($tag);
119                          map { $dbh->prepare("INSERT INTO bug_user_tags VALUES ($user, $qtag, $_)")->execute() or die $! } @{$tags{$tag}};                          map { $dbh->do("INSERT INTO $table (email, tag, id) VALUES ($user, $qtag, $_)") or die $! } @{$tags{$tag}};
120                  }                  }
121          }          }
122    }
123    
124    sub run {
125            my ($config, $source, $dbh) = @_;
126    
127            our $t;
128            our $timing;
129    
130            run_usertags($config, $source, $dbh);
131          print "Inserting usertags: ",(time() - $t),"s\n" if $timing;          print "Inserting usertags: ",(time() - $t),"s\n" if $timing;
132          $t = time();          $t = time();
133    
134            my %src_config = %{$config->{$source}};
135            my $table = $src_config{table};
136            my $archived_table = $src_config{'archived-table'};
137    
138            my %pkgsrc = %{getpkgsrc()};
139    
140          my @modified_bugs;          my @modified_bugs;
141    
142          if($src_config{archived}) {          if($src_config{archived}) {
143                  @modified_bugs = get_bugs(archive => 1);                  # some bugs (the unarchived ones) are in both list. exclude them.
144                    my %unarchived;
145                    foreach my $b (get_bugs()) {
146                            $unarchived{$b} = 1;
147                    }
148                    foreach my $b (get_bugs(archive => 1)) {
149                            push(@modified_bugs, $b) if not $unarchived{$b};
150                    }
151          } else {          } else {
152                  @modified_bugs = get_bugs();                  @modified_bugs = get_bugs();
153          }          }
154            my @modified_bugs2;
155            if ($src_config{debug}) {
156                    print "Running in debug mode with restricted bug list!!\n";
157                    foreach my $b (@modified_bugs) {
158                            push(@modified_bugs2, $b) if ($b =~ /58$/);
159                    }
160                    @modified_bugs = @modified_bugs2;
161            }
162    
163          print "Fetching list of ",scalar(@modified_bugs), " bugs to insert: ",(time() - $t),"s\n" if $timing;          print "Fetching list of ",scalar(@modified_bugs), " bugs to insert: ",(time() - $t),"s\n" if $timing;
164          $t = time();          $t = time();
165    
166          # Get the bugs we want to import          foreach my $prefix ($table, $archived_table) {
167          # my @bugs = $src_config{archived} ? get_bugs(archive => 1) : get_bugs();                  foreach my $postfix (qw{_packages _merged_with _found_in _fixed_in _tags}, '') {
168                            my $sth = $dbh->prepare("DELETE FROM $prefix$postfix WHERE id = \$1");
169          # Delete all bugs we are going to import                          map {
170          my $modbugs = join(", ", @modified_bugs);                                  $sth->execute($_) or die $!;
171          for my $table qw{bugs_archived bugs bug_merged_with bug_found_in bug_fixed_in bug_tags} {                          } @modified_bugs;
172                  $dbh->prepare("DELETE FROM $table WHERE id IN ($modbugs)")->execute() or die $!                  }
173          }          }
174          print "Deleting bugs: ",(time() - $t),"s\n" if $timing;          print "Deleting bugs: ",(time() - $t),"s\n" if $timing;
175          $t = time();          $t = time();
176    
         # Used to chache binary to source mappings  
         my %binarytosource = ();  
         # XXX What if a bug is in location 'db' (which currently doesn't exist)  
177          my $location = $src_config{archived} ? 'archive' : 'db_h';          my $location = $src_config{archived} ? 'archive' : 'db_h';
178          my $table = $src_config{archived} ? 'bugs_archived' : 'bugs';          $table = $src_config{archived} ? $archived_table : $table;
179          # Read all bugs          # Read all bugs
180            my $insert_bugs_handle = $dbh->prepare("INSERT INTO $table (id, package, source, arrival, status, severity, submitter, owner, title, last_modified, affects_stable, affects_testing, affects_unstable, affects_experimental) VALUES (\$1, \$2, \$3, \$4::abstime, \$5, \$6, \$7, \$8, \$9, \$10::abstime, \$11, \$12, \$13, \$14)");
181            my $insert_bugs_packages_handle = $dbh->prepare("INSERT INTO ${table}_packages (id, package, source) VALUES (\$1, \$2, \$3)");
182            my $insert_bugs_found_handle = $dbh->prepare("INSERT INTO ${table}_found_in (id, version) VALUES (\$1, \$2)");
183            my $insert_bugs_fixed_handle = $dbh->prepare("INSERT INTO ${table}_fixed_in (id, version) VALUES (\$1, \$2)");
184            my $insert_bugs_merged_handle = $dbh->prepare("INSERT INTO ${table}_merged_with (id, merged_with) VALUES (\$1, \$2)");
185            my $insert_bugs_tags_handle = $dbh->prepare("INSERT INTO ${table}_tags (id, tag) VALUES (\$1, \$2)");
186            $insert_bugs_handle->bind_param(4, undef, SQL_INTEGER);
187            $insert_bugs_handle->bind_param(10, undef, SQL_INTEGER);
188    
189            $t = time();
190          foreach my $bug_nr (@modified_bugs) {          foreach my $bug_nr (@modified_bugs) {
191                  # Fetch bug using Debbugs                  # Fetch bug using Debbugs
192                  # Bugs which were once archived and have been unarchived again will appear in get_bugs(archive => 1).                  # Bugs which were once archived and have been unarchived again will appear in get_bugs(archive => 1).
# Line 162  sub main { Line 196  sub main {
196                  my %bug = %{get_bug_status(bug => $bug_nr, status => $bug_ref)};                  my %bug = %{get_bug_status(bug => $bug_nr, status => $bug_ref)};
197    
198                  # Convert data where necessary                  # Convert data where necessary
199                  map { $bug{$_} = $dbh->quote($bug{$_}) } qw(subject originator owner pending);                  my @found_versions = @{$bug{found_versions}};
200                  my @found_versions = map { $dbh->quote($_) } @{$bug{found_versions}};                  my @fixed_versions = @{$bug{fixed_versions}};
201                  my @fixed_versions = map { $dbh->quote($_) } @{$bug{fixed_versions}};                  my @tags = split / /, $bug{keywords};
                 my @tags = map {$dbh->quote($_) } split / /, $bug{keywords};  
202    
203                  # log_modified and date are not necessarily set. If they are not available, they                  # log_modified and date are not necessarily set. If they are not available, they
204                  # are assumed to be epoch (i.e. bug #4170)                  # are assumed to be epoch (i.e. bug #4170)
205                  map {                  map {
206                          if($bug{$_}) {                          if($bug{$_}) {
207                                  $bug{$_} = "$bug{$_}::abstime";                                  #$bug{$_} = "$bug{$_}::abstime";
208                                    $bug{$_} = int($bug{$_});
209                          } else {                          } else {
210                                  $bug{$_} = '0::abstime';                                  $bug{$_} = 0;
211                          }                          }
212                  } qw{date log_modified};                  } qw{date log_modified};
213    
214    
215                  if(not exists $binarytosource{$bug{package}}) {                  my $source = exists($pkgsrc{$bug{package}}) ? $pkgsrc{$bug{package}} : $bug{package};
                         $binarytosource{$bug{package}} = (binarytosource($bug{package}))[0];  
                 }  
                 my $source = $binarytosource{$bug{package}};  
   
                 if(not defined $source) {  
                 # if source is not defined, then we $bug{package} is likely to  
                 # be a source package name (or the source package has the same  
                 # name as the binary package). See #480818 for ex.  
                         $source = $dbh->quote($bug{package});  
                 } else {  
                         $source = $dbh->quote($source);  
                 }  
216    
217                  #Calculate bug presence in distributions                  #Calculate bug presence in distributions
218                  my $present_in_stable =                  my ($present_in_stable, $present_in_testing, $present_in_unstable, $present_in_experimental);
219                          bug_presence(bug => $bug_nr, status => \%bug,                  if($src_config{archived}) {
220                                           dist => 'stable');                          $present_in_stable = $present_in_testing = $present_in_unstable = $present_in_experimental = 'FALSE';
                 my $present_in_testing =  
                         bug_presence(bug => $bug_nr, status => \%bug,  
                                          dist => 'testing');  
                 my $present_in_unstable =  
                         bug_presence(bug => $bug_nr, status => \%bug,  
                                          dist => 'unstable');  
                 if(!defined($present_in_stable) or !defined($present_in_unstable) or !defined($present_in_testing)) {  
                         print "NUMBER: $bug_nr\n";  
                 }  
                 if(defined($present_in_stable) and ($present_in_stable eq 'absent' or $present_in_stable eq 'fixed')) {  
                         $present_in_stable = 'FALSE';  
                 } else {  
                         $present_in_stable = 'TRUE';  
                 }  
                 if(defined($present_in_testing) and ($present_in_testing eq 'absent' or $present_in_testing eq 'fixed')) {  
                         $present_in_testing = 'FALSE';  
221                  } else {                  } else {
222                          $present_in_testing = 'TRUE';                          $present_in_stable =
223                  }                                  bug_presence(bug => $bug_nr, status => \%bug,
224                  if(defined($present_in_unstable) and ($present_in_unstable eq 'absent' or $present_in_unstable eq 'fixed')) {                                                           dist => 'stable',
225                          $present_in_unstable = 'FALSE';                                                           arch => \@archs);
226                  } else {                          $present_in_testing =
227                          $present_in_unstable = 'TRUE';                                  bug_presence(bug => $bug_nr, status => \%bug,
228                                                             dist => 'testing',
229                                                             arch => \@archs);
230                            $present_in_unstable =
231                                    bug_presence(bug => $bug_nr, status => \%bug,
232                                                             dist => 'unstable',
233                                                             arch => \@archs);
234                            $present_in_experimental =
235                                    bug_presence(bug => $bug_nr, status => \%bug,
236                                                             dist => 'experimental',
237                                                             arch => \@archs);
238    
239                            if(!defined($present_in_stable) or !defined($present_in_unstable) or !defined($present_in_testing) or !defined($present_in_experimental)) {
240                                    print "NUMBER: $bug_nr\n";
241                            }
242    
243                            if(defined($present_in_stable) and ($present_in_stable eq 'absent' or $present_in_stable eq 'fixed')) {
244                                    $present_in_stable = 'FALSE';
245                            } else {
246                                    $present_in_stable = 'TRUE';
247                            }
248                            if(defined($present_in_testing) and ($present_in_testing eq 'absent' or $present_in_testing eq 'fixed')) {
249                                    $present_in_testing = 'FALSE';
250                            } else {
251                                    $present_in_testing = 'TRUE';
252                            }
253                            if(defined($present_in_unstable) and ($present_in_unstable eq 'absent' or $present_in_unstable eq 'fixed')) {
254                                    $present_in_unstable = 'FALSE';
255                            } else {
256                                    $present_in_unstable = 'TRUE';
257                            }
258                            if(defined($present_in_experimental) and ($present_in_experimental eq 'absent' or $present_in_experimental eq 'fixed')) {
259                                    $present_in_experimental = 'FALSE';
260                            } else {
261                                    $present_in_experimental = 'TRUE';
262                            }
263                  }                  }
264    
265                  # Insert data into bugs table                  # Insert data into bugs table
266                  my $query = "INSERT INTO $table VALUES ($bug_nr, '$bug{package}', $source, $bug{date}, \                  $insert_bugs_handle->execute($bug_nr, $bug{package}, $source, $bug{date}, $bug{pending},
267                               E$bug{pending}, '$bug{severity}', E$bug{originator}, E$bug{owner}, \                          $bug{severity}, $bug{originator}, $bug{owner}, $bug{subject}, $bug{log_modified},
268                                           E$bug{subject}, $bug{log_modified}, $present_in_stable,                          $present_in_stable, $present_in_testing, $present_in_unstable, $present_in_experimental) or die $!;
269                                           $present_in_testing, $present_in_unstable)";  
270                  # Execute insertion                  my $src;
271                  my $sth = $dbh->prepare($query);                  foreach my $pkg (keys %{{ map { $_ => 1 } split(/\s*,\s*/, $bug{package})}}) {
272                  $sth->execute() or die $!;                          $src = exists($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : $pkg;
273                            $insert_bugs_packages_handle->execute($bug_nr, $pkg, $src) or die $!;
274                    }
275    
276                  # insert data into bug_fixed_in and bug_found_in tables                  # insert data into bug_fixed_in and bug_found_in tables
277                  foreach my $version (without_duplicates(@found_versions)) {                  foreach my $version (without_duplicates(@found_versions)) {
278                          $query = "INSERT INTO bug_found_in VALUES ($bug_nr, $version)";                          $insert_bugs_found_handle->execute($bug_nr, $version) or die $!;
                         $dbh->prepare($query)->execute() or die $!;  
279                  }                  }
280                  foreach my $version (without_duplicates(@fixed_versions)) {                  foreach my $version (without_duplicates(@fixed_versions)) {
281                          $query = "INSERT INTO bug_fixed_in VALUES ($bug_nr, $version)";                          $insert_bugs_fixed_handle->execute($bug_nr, $version) or die $!;
                         $dbh->prepare($query)->execute() or die $!;  
282                  }                  }
283                  foreach my $mergee (without_duplicates(split / /, $bug{mergedwith})) {                  foreach my $mergee (without_duplicates(split / /, $bug{mergedwith})) {
284                          $query = "INSERT INTO bug_merged_with VALUES ($bug_nr, $mergee)";                          $insert_bugs_merged_handle->execute($bug_nr, $mergee) or die $!;
                         $dbh->prepare($query)->execute() or die $!;  
285                  }                  }
286                  foreach my $tag (without_duplicates(@tags)) {                  foreach my $tag (without_duplicates(@tags)) {
287                          $query = "INSERT INTO bug_tags VALUES ($bug_nr, $tag)";                          $insert_bugs_tags_handle->execute($bug_nr, $tag) or die $!;
                         $dbh->prepare($query)->execute() or die $!;  
288                  }                  }
289          }          }
   
290          print "Inserting bugs: ",(time() - $t),"s\n" if $timing;          print "Inserting bugs: ",(time() - $t),"s\n" if $timing;
291          $t = time();  }
292    
293    sub main {
294            if(@ARGV != 3) {
295                    print STDERR "Usage: $0 <config> <command> <source>\n";
296                    exit 1;
297            }
298    
299            our $t = time();
300            our $timing;
301    
302            my $config = LoadFile($ARGV[0]) or die "Could not load configuration: $!";
303            my $command = $ARGV[1];
304            my $source = $ARGV[2];
305    
306            my $dbname = $config->{general}->{dbname};
307            my $dbport;
308            if ($config->{general}->{dbport} ne '') {
309              $dbport = ";port=".$config->{general}->{dbport};
310            } else {
311              $dbport = "";
312            }
313            # Connection to DB
314            my $dbh = DBI->connect("dbi:Pg:dbname=$dbname".$dbport);
315            # We want to commit the transaction as a hole at the end
316            $dbh->{AutoCommit} = 0;
317            $dbh->do('SET CONSTRAINTS ALL DEFERRED');
318    
319            if($command eq 'run') {
320                    run($config, $source, $dbh);
321            } else {
322                    print STDERR "<command> has to be one of run, drop and setup\n";
323                    exit(1)
324            }
325    
326          $dbh->commit();          $dbh->commit();
327          print "Committing bugs: ",(time() - $t),"s\n" if $timing;          print "Committing bugs: ",(time() - $t),"s\n" if $timing;
328  }  }

Legend:
Removed from v.1055  
changed lines
  Added in v.1351

  ViewVC Help
Powered by ViewVC 1.1.5