]> git.donarmstrong.com Git - debbugs.git/blobdiff - bin/debbugs-loadsql
allow providing a directory of debinfo files to loadsql
[debbugs.git] / bin / debbugs-loadsql
index c583af03a44c8526a1965b5a5eb2aa56d67a9073..b718a05961c862565059a4c7d5c9e7efcd9ac924 100755 (executable)
@@ -142,6 +142,7 @@ use DateTime;
 use File::stat;
 use File::Basename;
 use File::Spec;
+use File::Find;
 use IO::Dir;
 use IO::File;
 use IO::Uncompress::AnyUncompress;
@@ -163,6 +164,7 @@ Getopt::Long::Configure('pass_through');
 GetOptions(\%options,
            'quick|q',
            'service|s=s',
+          'dsn=s',
            'sysconfdir|c=s',
            'progress!',
            'spool_dir|spool-dir=s',
@@ -183,7 +185,9 @@ my %subcommands =
      'versions' => {function => \&add_versions,
                    },
      'debinfo' => {function => \&add_debinfo,
-                   arguments => {'0|null' => 0},
+                   arguments => {'0|null' => 0,
+                                'debinfo_dir|debinfo-dir=s' => 0,
+                               },
                   },
      'maintainers' => {function => \&add_maintainers,
                       },
@@ -195,6 +199,8 @@ my %subcommands =
                  },
      'logs' => {function => \&add_logs,
                },
+     'bugs_and_logs' => {function => \&add_bugs_and_logs,
+                       },
      'packages' => {function => \&add_packages,
                    arguments => {'ftpdists=s' => 1,
                                  'suites=s@' => 0,
@@ -265,8 +271,6 @@ sub add_bugs {
     my $s = db_connect($options);
 
 
-    my $time = 0;
-    my $start_time = time;
     my %tags;
     my %severities;
     my %queue;
@@ -292,18 +296,10 @@ sub add_bugs {
                my @bugs = @_;
                my @bugs_to_update;
                if ($options{quick}) {
-                 for my $bug (@bugs) {
-                   my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
-                   if (not defined $stat) {
-                      print STDERR "Unable to stat $bug $!\n";
-                      next;
-                   }
-                   my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
-                   next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
-                   push @bugs_to_update, $bug;
-                  }
+                   @bugs_to_update =
+                       bugs_to_update($s,$initialdir,@bugs);
                } else {
-                 @bugs_to_update = @bugs;
+                   @bugs_to_update = @bugs;
                }
                eval {
                  $s->txn_do(sub {
@@ -351,19 +347,21 @@ sub add_versions {
             my $sp;
             if (not defined $src_pkgs{$versions[$i][0]}) {
                 $src_pkgs{$versions[$i][0]} =
-                    $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
+                    $s->resultset('SrcPkg')->
+                   get_src_pkg_id($versions[$i][0]);
             }
             $sp = $src_pkgs{$versions[$i][0]};
             # There's probably something wrong if the source package
             # doesn't exist, but we'll skip it for now
-            next unless defined $sp;
-            my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
+            last if not defined $sp;
+            my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp,
                                                     ver => $versions[$i][1],
                                                    });
+           last if not defined $sv;
             if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
-                $sv->update({based_on => $ancestor_sv->id()})
+                $sv->update({based_on => $ancestor_sv})
             }
-            $ancestor_sv = $sv;
+            $ancestor_sv = $sv->id();
         }
         $p->update() if $p;
     }
@@ -374,65 +372,65 @@ sub add_debinfo {
     my ($options,$opts,$p,$config,$argv) = @_;
 
     my @files = @{$argv};
+    if (exists $opts->{debinfo_dir} and not @files) {
+       find(sub {
+                if (-f $_ and /\.debinfo$/) {
+                    push @files, $File::Find::name;
+                }
+            },
+            $opts->{debinfo_dir}
+           );
+    }
     if (not @files) {
        {
-           if ($opts->{0}) {
-               local $/ = "\0";
-           }
+          local $/ = "\n";
+           local $/ = "\0" if $opts->{0};
            while (<STDIN>) {
+              s/\n$// unless $opts->{0};
+              s/\0$// if $opts->{0};
                push @files, $_;
            }
        }
     }
     return unless @files;
     my $s = db_connect($options);
-    my %arch;
     $p->target(scalar @files) if $p;
-    for my $file (@files) {
-        my $fh = IO::File->new($file,'r') or
-            die "Unable to open $file for reading: $!";
-        my $f_stat = stat($file);
-        while (<$fh>) {
-            chomp;
-            next unless length $_;
-            my ($binname, $binver, $binarch, $srcname, $srcver) = split;
-            # if $srcver is not defined, this is probably a broken
-            # .debinfo file [they were causing #686106, see commit
-            # 49c85ab8 in dak.] Basically, $binarch didn't get put into
-            # the file, so we'll fudge it from the filename.
-            if (not defined $srcver) {
-                ($srcname,$srcver) = ($binarch,$srcname);
-                ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
-            }
-            my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
-            # update the creation date if the data we have is earlier
-            my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
-            if ($ct_date < $sp->creation) {
-                $sp->creation($ct_date);
-                $sp->last_modified(DateTime->now);
-                $sp->update;
-            }
-            my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
-                                                              ver => $srcver});
-            if (not defined $sv->upload_date() or $ct_date < $sv->upload_date()) {
-                $sv->upload_date($ct_date);
-                $sv->update;
-            }
-            my $arch;
-            if (defined $arch{$binarch}) {
-                $arch = $arch{$binarch};
-            } else {
-                $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
-                $arch{$binarch} = $arch;
-            }
-            my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
-            $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
-                                                     src_ver => $sv->id(),
-                                                     arch    => $arch->id(),
-                                                     ver        => $binver,
-                                                    });
-        }
-        $p->update() if $p;
+    my $it = natatime 100, @files;
+    while (my @v = $it->()) {
+       my %cache;
+       my @debinfos;
+       for my $file (@v) {
+           my $fh = IO::File->new($file,'r') or
+               die "Unable to open $file for reading: $!";
+           my $f_stat = stat($file);
+           my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
+           while (<$fh>) {
+               chomp;
+               next unless length $_;
+               my ($binname, $binver, $binarch, $srcname, $srcver) = split;
+               # if $srcver is not defined, this is probably a broken
+               # .debinfo file [they were causing #686106, see commit
+               # 49c85ab8 in dak.] Basically, $binarch didn't get put into
+               # the file, so we'll fudge it from the filename.
+               if (not defined $srcver) {
+                   ($srcname,$srcver) = ($binarch,$srcname);
+                   ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
+               }
+               if (not defined $srcver) {
+                   print STDERR "malformed debinfo (no srcver): $file\n";
+                   next;
+               }
+               push @debinfos,
+                   [$binname,$binver,$binarch,$srcname,$srcver,$ct_date];
+           }
+       }
+       $s->txn_do(
+           sub {
+               for my $di (@debinfos) {
+                   Debbugs::DB::Load::load_debinfo($s,@{$di}[0..5],\%cache);
+               }
+           });
+       $p->update($p->last_update()+@v) if $p;
     }
     $p->remove() if $p;
 }
@@ -552,10 +550,6 @@ sub add_logs {
     }
     my $s = db_connect($options);
 
-
-    my $time = 0;
-    my $start_time = time;
-
     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
               $p,
               'log',
@@ -568,8 +562,10 @@ sub add_logs {
                       next;
                   }
                   if ($options{quick}) {
-                      my $rs = $s->resultset('Bug')->search({bug=>$bug})->single();
-                      next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
+                      my $rs = $s->resultset('Bug')->
+                         search({id=>$bug})->single();
+                      return if defined $rs and
+                         $stat->mtime <= $rs->last_modified()->epoch();
                   }
                   eval {
                       load_bug_log(db => $s,
@@ -581,6 +577,81 @@ sub add_logs {
               });
 }
 
+sub add_bugs_and_logs {
+    my ($options,$opts,$p,$config,$argv) = @_;
+
+    chdir($config->{spool_dir}) or
+        die "chdir $config->{spool_dir} failed: $!";
+
+    my $verbose = $options->{debug};
+
+    my $initialdir = "db-h";
+
+    if (defined $argv->[0] and $argv->[0] eq "archive") {
+        $initialdir = "archive";
+    }
+    my $s = db_connect($options);
+
+    my %tags;
+    my %severities;
+    my %queue;
+
+    walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
+              $p,
+              'summary',
+              $verbose,
+              sub {
+                  my @bugs = @_;
+                 my @bugs_to_update;
+                 if ($options{quick}) {
+                     @bugs_to_update =
+                         bugs_to_update($s,$initialdir,@bugs);
+                 } else {
+                     @bugs_to_update = @bugs;
+                 }
+                 eval {
+                     $s->txn_do(sub {
+                                    for my $bug (@bugs_to_update) {
+                                        load_bug(db => $s,
+                                                 bug => $bug,
+                                                 tags => \%tags,
+                                                 severities => \%severities,
+                                                 queue => \%queue);
+                                    }
+                                });
+                 };
+                 if ($@) {
+                     die "failure while trying to load bug: $@";
+                 }
+                 for my $bug (@bugs) {
+                     my $stat = stat(getbugcomponent($bug,'log',$initialdir));
+                     if (not defined $stat) {
+                         print STDERR "Unable to stat $bug $!\n";
+                         next;
+                     }
+                     if ($options{quick}) {
+                         my $rs = $s->resultset('Bug')->
+                             search({id=>$bug})->single();
+                         return if defined $rs and
+                             $stat->mtime <= $rs->last_modified()->epoch();
+                     }
+                     eval {
+                         load_bug_log(db => $s,
+                                      bug => $bug);
+                     };
+                     if ($@) {
+                         die "failure while trying to load bug log $bug\n$@";
+                     }
+                 }
+              },
+             50
+            );
+    handle_load_bug_queue(db=>$s,
+                         queue => \%queue,
+                        );
+
+}
+
 sub add_packages {
     my ($options,$opts,$p,$config,$argv) = @_;
 
@@ -688,7 +759,8 @@ sub db_connect {
     my ($options) = @_;
     # connect to the database; figure out how to handle errors
     # properly here.
-    my $s = Debbugs::DB->connect($options->{service}) or
+    my $s = Debbugs::DB->connect($options->{dsn} //
+                                $options->{service}) or
         die "Unable to connect to database: ";
 }
 
@@ -762,5 +834,21 @@ sub walk_bugs {
 }
 
 
+sub bugs_to_update {
+    my ($s,$initialdir,@bugs) = @_;
+    my @bugs_to_update;
+    for my $bug (@bugs) {
+       my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
+       if (not defined $stat) {
+           print STDERR "Unable to stat $bug $!\n";
+           next;
+       }
+       my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
+       next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
+       push @bugs_to_update, $bug;
+    }
+    @bugs_to_update;
+}
+
 
 __END__