+
+ 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);
+
+ walk_bugs(dirs => [(@{$argv}?@{$argv} : $initialdir)],
+ progress_bar => $p,
+ bug_file => 'log',
+ $verbose?(logging => \*STDERR):(),
+ callback =>
+ sub {
+ my $bug = shift;
+ 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$@";
+ }
+ });
+}
+
+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(dirs => [(@{$argv}?@{$argv} : $initialdir)],
+ progress_bar => $p,
+ $verbose?(logging => \*STDERR):(),
+ callback =>
+ sub {
+ my @bugs = @_;
+ my @bugs_to_update;
+ if ($options{quick}) {
+ @bugs_to_update =
+ bugs_to_update($s,@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$@";
+ }
+ }
+ },
+ bugs_per_call => 50,
+ );
+ handle_load_bug_queue(db=>$s,
+ queue => \%queue,
+ );
+
+}
+
+sub add_packages {
+ my ($options,$opts,$p,$config,$argv) = @_;
+
+ my $dist_dir = IO::Dir->new($opts->{ftpdists});
+ my @dist_names =
+ grep { $_ !~ /^\./ and
+ -d $opts->{ftpdists}.'/'.$_ and
+ not -l $opts->{ftpdists}.'/'.$_
+ } $dist_dir->read;
+ my %s_p;
+ while (my $dist = shift @dist_names) {
+ my $dist_dir = $opts->{ftpdists}.'/'.$dist;
+ my ($dist_info,$package_files) =
+ read_release_file($dist_dir.'/Release');
+ $s_p{$dist_info->{Codename}} = $package_files;
+ }
+ my $tot = 0;
+ for my $suite (keys %s_p) {
+ for my $component (keys %{$s_p{$suite}}) {
+ $tot += scalar keys %{$s_p{$suite}{$component}};
+ }
+ }
+ $p->target($tot) if $p;
+ my $i = 0;
+ my $avg_pkgs = 0;
+ my $tot_suites = scalar keys %s_p;
+ my $done_suites=0;
+ my $completed_pkgs=0;
+ # parse packages files
+ for my $suite (keys %s_p) {
+ my @pkgs;
+ for my $component (keys %{$s_p{$suite}}) {
+ my @archs = keys %{$s_p{$suite}{$component}};
+ if (grep {$_ eq 'source'} @archs) {
+ @archs = ('source',grep {$_ ne 'source'} @archs);
+ }
+ for my $arch (@archs) {
+ my $pfh = open_compressed_file($s_p{$suite}{$component}{$arch}) or
+ die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
+ local $_;
+ local $/ = ''; # paragraph mode
+ while (<$pfh>) {
+ my %pkg;
+ for my $field (qw(Package Maintainer Version Source)) {
+ /^\Q$field\E: (.*)/m;
+ $pkg{$field} = $1;
+ }
+ next unless defined $pkg{Package} and
+ defined $pkg{Version};
+ push @pkgs,[$arch,$component,\%pkg];
+ }
+ }
+ }
+ my $s = db_connect($options);
+ if ($avg_pkgs==0) {
+ $avg_pkgs = @pkgs;
+ }
+ $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
+ $completed_pkgs+@pkgs) if $p;
+ load_packages($s,
+ $suite,
+ \@pkgs,
+ $p);
+ $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
+ $completed_pkgs += @pkgs;
+ $done_suites++;
+ }
+ $p->remove() if $p;