]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
e639e0ee27793438f82ffaab54d9671fc847be2d
[debbugs.git] / bin / debbugs-loadsql
1 #! /usr/bin/perl
2 # debbugs-loadsql is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long qw(:config no_ignore_case);
12 use Pod::Usage;
13
14 =head1 NAME
15
16 debbugs-loadsql -- load debbugs sql database
17
18 =head1 SYNOPSIS
19
20 debbugs-loadsql [options] [subcommand]
21
22  Subcommands:
23   bugs help versions configuration
24   suites logs packages debinfo
25  Options:
26   --quick, -q only load changed things
27   --progress Show progress bar
28   --service, -s service name
29   --sysconfdir, -c postgresql service config dir
30   --spool-dir debbugs spool directory
31   --debug, -d debugging level (Default 0)
32   --help, -h display this help
33   --man, -m display manual
34
35 =head1 SUBCOMMANDS
36
37 =head2 help
38
39 Display this manual
40
41 =head2 bugs
42
43 Add bugs (subject, number, etc) to the database
44
45    --preload create all bugs first, then add information
46
47 =head2 versions
48
49 Add version descendant information (which version is based on which version) to
50 the database
51
52 =head2 maintainers
53
54 Add source maintainers to the BTS
55
56 =head2 configuration
57
58 Add debbugs configuration information (tags, severity, etc)
59
60 =head2 suites
61
62 Add suite information from ftp distribution
63
64   --ftpdists location of FTP mirror
65
66 =head2 logs
67
68 Add bug logs
69
70 =head2 packages
71
72 Add package information from the ftp archive
73
74   --ftpdists location of FTP mirror
75   --suites Suite to operate on
76
77 =head2 debinfo
78
79 Add package information from a debinfo file
80
81   --null -0 names of debinfo files are null separated
82
83 =head1 OPTIONS
84
85 =over
86
87 =item B<--quick, -q>
88
89 Only load changed bugs
90
91 =item B<--progress>
92
93 Show progress bar (requires Term::ProgressBar)
94
95 =item B<--service,-s>
96
97 Postgreql service to use; defaults to debbugs
98
99 =item B<--sysconfdir,-c>
100
101 System configuration directory to use; if not set, defaults to the
102 postgresql default. [Operates by setting PGSYSCONFDIR]
103
104 =item B<--spool-dir>
105
106 Debbugs spool directory; defaults to the value configured in the
107 debbugs configuration file.
108
109 =item B<--verbose>
110
111 Output more information about what is happening. Probably not useful
112 if you also set --progress.
113
114 =item B<--debug, -d>
115
116 Debug verbosity.
117
118 =item B<--help, -h>
119
120 Display brief useage information.
121
122 =item B<--man, -m>
123
124 Display this manual.
125
126 =back
127
128
129 =cut
130
131
132 use vars qw($DEBUG);
133
134 use Debbugs::Common (qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list getsourcemaintainers),
135                      qw(hash_slice open_compressed_file),);
136 use Debbugs::Config qw(:config);
137 use Debbugs::Status qw(read_bug split_status_fields);
138 use Debbugs::Log;
139 use Debbugs::DB;
140 use Debbugs::DB::Load qw(:load_bug :load_package :load_suite);
141 use DateTime;
142 use File::stat;
143 use File::Basename;
144 use File::Spec;
145 use File::Find;
146 use IO::Dir;
147 use IO::File;
148 use IO::Uncompress::AnyUncompress;
149 use Encode qw(decode_utf8);
150 use List::MoreUtils qw(natatime);
151
152 my %options =
153     (debug           => 0,
154      help            => 0,
155      man             => 0,
156      verbose         => 0,
157      quiet           => 0,
158      quick           => 0,
159      service         => $config{debbugs_db},
160      progress        => 0,
161     );
162
163 Getopt::Long::Configure('pass_through');
164 GetOptions(\%options,
165            'quick|q',
166            'service|s=s',
167            'dsn=s',
168            'sysconfdir|c=s',
169            'progress!',
170            'spool_dir|spool-dir=s',
171            'verbose|v+',
172            'quiet+',
173            'debug|d+','help|h|?','man|m');
174 Getopt::Long::Configure('default');
175
176 pod2usage() if $options{help};
177 pod2usage({verbose=>2}) if $options{man};
178
179 $DEBUG = $options{debug};
180
181 my %subcommands =
182     ('bugs' => {function => \&add_bugs,
183                 arguments => {'preload' => 0},
184                },
185      'versions' => {function => \&add_versions,
186                    },
187      'debinfo' => {function => \&add_debinfo,
188                    arguments => {'0|null' => 0,
189                                  'debinfo_dir|debinfo-dir=s' => 0,
190                                 },
191                   },
192      'maintainers' => {function => \&add_maintainers,
193                       },
194      'configuration' => {function => \&add_configuration,
195                         },
196      'suites' => {function => \&add_suite,
197                   arguments => {'ftpdists=s' => 1,
198                                },
199                  },
200      'logs' => {function => \&add_logs,
201                },
202      'bugs_and_logs' => {function => \&add_bugs_and_logs,
203                         },
204      'packages' => {function => \&add_packages,
205                     arguments => {'ftpdists=s' => 1,
206                                   'suites=s@' => 0,
207                                  },
208                    },
209      'help' => {function => sub {pod2usage({verbose => 2});}}
210     );
211
212 my @USAGE_ERRORS;
213 $options{verbose} = $options{verbose} - $options{quiet};
214
215 if ($options{progress}) {
216     eval "use Term::ProgressBar";
217     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
218 }
219
220
221 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
222
223 if (exists $options{sysconfdir}) {
224     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
225         delete $ENV{PGSYSCONFDIR};
226     } else {
227         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
228     }
229 }
230
231 if (exists $options{spool_dir} and defined $options{spool_dir}) {
232     $config{spool_dir} = $options{spool_dir};
233 }
234
235 my $prog_bar;
236 if ($options{progress}) {
237     $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
238     warn "Unable to initialize progress bar: $@" if not $prog_bar;
239 }
240
241
242 my ($subcommand) = shift @ARGV;
243 if (not defined $subcommand) {
244     $subcommand = 'help';
245     print STDERR "You must provide a subcommand; displaying usage.\n";
246     pod2usage();
247 } elsif (not exists $subcommands{$subcommand}) {
248     print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
249     pod2usage();
250 }
251
252 binmode(STDOUT,':encoding(UTF-8)');
253 binmode(STDERR,':encoding(UTF-8)');
254
255 my $opts =
256     handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
257 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
258
259 sub add_bugs {
260     my ($options,$opts,$p,$config,$argv) = @_;
261     chdir($config->{spool_dir}) or
262         die "chdir $config->{spool_dir} failed: $!";
263
264     my $verbose = $options->{debug};
265
266     my $initialdir = "db-h";
267
268     if (defined $argv->[0] and $argv->[0] eq "archive") {
269         $initialdir = "archive";
270     }
271     my $s = db_connect($options);
272
273
274     my %tags;
275     my %severities;
276     my %queue;
277
278     if ($opts->{preload}) {
279         my @bugs;
280         walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
281                   undef,
282                   'summary',
283                   undef,
284                   sub {
285                     push @bugs,@_;
286                   },
287                   10000
288                  );
289         $s->resultset('Bug')->quick_insert_bugs(@bugs);
290     }
291     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
292               $p,
293               'summary',
294               $verbose,
295               sub {
296                 my @bugs = @_;
297                 my @bugs_to_update;
298                 if ($options{quick}) {
299                     @bugs_to_update =
300                         bugs_to_update($s,$initialdir,@bugs);
301                 } else {
302                     @bugs_to_update = @bugs;
303                 }
304                 eval {
305                   $s->txn_do(sub {
306                                for my $bug (@bugs_to_update) {
307                                  load_bug(db => $s,
308                                           bug => $bug,
309                                           tags => \%tags,
310                                           severities => \%severities,
311                                           queue => \%queue);
312                                }
313                              });
314                 };
315                 if ($@) {
316                   die "failure while trying to load bug: $@";
317                 }
318               },
319               50
320              );
321     handle_load_bug_queue(db => $s,
322                           queue => \%queue);
323 }
324
325 sub add_versions {
326     my ($options,$opts,$p,$config,$argv) = @_;
327
328     my $s = db_connect($options);
329
330     my @files = @{$argv};
331     $p->target(scalar @files) if $p;
332     for my $file (@files) {
333         my $fh = IO::File->new($file,'r') or
334             die "Unable to open $file for reading: $!";
335         my @versions;
336         my %src_pkgs;
337         while (<$fh>) {
338             chomp;
339             next unless length $_;
340             if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
341                 push @versions, [$1,$2];
342             }
343         }
344         close($fh);
345         my $ancestor_sv;
346         for my $i (reverse 0..($#versions)) {
347             my $sp;
348             if (not defined $src_pkgs{$versions[$i][0]}) {
349                 $src_pkgs{$versions[$i][0]} =
350                     $s->resultset('SrcPkg')->
351                     get_src_pkg_id($versions[$i][0]);
352             }
353             $sp = $src_pkgs{$versions[$i][0]};
354             # There's probably something wrong if the source package
355             # doesn't exist, but we'll skip it for now
356             last if not defined $sp;
357             my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp,
358                                                     ver => $versions[$i][1],
359                                                    });
360             last if not defined $sv;
361             if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
362                 $sv->update({based_on => $ancestor_sv})
363             }
364             $ancestor_sv = $sv->id();
365         }
366         $p->update() if $p;
367     }
368     $p->remove() if $p;
369 }
370
371 sub add_debinfo {
372     my ($options,$opts,$p,$config,$argv) = @_;
373
374     my @files = @{$argv};
375     if (exists $opts->{debinfo_dir} and not @files) {
376         find(sub {
377                  if (-f $_ and /\.debinfo$/) {
378                      push @files, $File::Find::name;
379                  }
380              },
381              $opts->{debinfo_dir}
382             );
383     }
384     if (not @files) {
385        {
386            local $/ = "\n";
387            local $/ = "\0" if $opts->{0};
388            while (<STDIN>) {
389                s/\n$// unless $opts->{0};
390                s/\0$// if $opts->{0};
391                push @files, $_;
392            }
393        }
394     }
395     return unless @files;
396     my $s = db_connect($options);
397     $p->target(scalar @files) if $p;
398     my $it = natatime 100, @files;
399     while (my @v = $it->()) {
400         my %cache;
401         my @debinfos;
402 FILE:   for my $file (@v) {
403             my $fh = IO::File->new($file,'r') or
404                 die "Unable to open $file for reading: $!";
405             my $f_stat = stat($file);
406             my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
407             my @file_debinfos;
408             while (<$fh>) {
409                 chomp;
410                 next unless length $_;
411                 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
412                 # if $srcver is not defined, this is probably a broken
413                 # .debinfo file [they were causing #686106, see commit
414                 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
415                 # the file, so we'll fudge it from the filename.
416                 if (not defined $srcver) {
417                     ($srcname,$srcver) = ($binarch,$srcname);
418                     ($binarch) = $file =~ /_([a-z0-9-]+)\.debinfo/;
419                 }
420                 # It turns out that there are debinfo files which are horribly
421                 # screwed up, and have junk in them. We need to discard them
422                 # completely
423                 if (not defined $srcname or
424                     not defined $srcver or
425                     not defined $binname or
426                     not defined $binver or
427                     $srcname !~ /^$config{package_name_re}$/o or
428                     $binname !~ /^$config{package_name_re}$/o or
429                     $srcver !~ /^$config{package_version_re}$/o or
430                     $binver !~ /^$config{package_version_re}$/o
431                    ) {
432                     print STDERR "malformed debinfo: $file\n";
433                     next FILE;
434                 }
435                 push @file_debinfos,
436                     [$binname,$binver,$binarch,$srcname,$srcver,$ct_date];
437             }
438             push @debinfos,
439                 @file_debinfos;
440         }
441         $s->txn_do(
442             sub {
443                 for my $di (@debinfos) {
444                     Debbugs::DB::Load::load_debinfo($s,@{$di}[0..5],\%cache);
445                 }
446             });
447         $p->update($p->last_update()+@v) if $p;
448     }
449     $p->remove() if $p;
450 }
451
452 sub add_maintainers {
453     my ($options,$opts,$p,$config,$argv) = @_;
454
455     my $s = db_connect($options);
456     my $maintainers = getsourcemaintainers() // {};
457     $p->target(2) if $p;
458     ## get all of the maintainers, and add the missing ones
459     my $maints = $s->resultset('Maintainer')->
460         get_maintainers(values %{$maintainers});
461     $p->update() if $p;
462     my @svs = $s->resultset('SrcVer')->
463         search({maintainer => undef
464                },
465               {join => 'src_pkg',
466                group_by => 'me.src_pkg, src_pkg.pkg',
467                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
468                columns => [qw(me.src_pkg src_pkg.pkg)],
469               }
470               )->all();
471     $p->target(2+@svs) if $p;
472     $p->update() if $p;
473     for my $sv (@svs) {
474         if (exists $maintainers->{$sv->{src_pkg}{pkg}}) {
475             my $pkg = $sv->{src_pkg}{pkg};
476             my $maint = $maints->
477                {$maintainers->{$pkg}};
478             $s->txn_do(sub {$s->resultset('SrcVer')->
479                                 search({maintainer => undef,
480                                         'src_pkg.pkg' => $pkg
481                                        },
482                                       {join => 'src_pkg'}
483                                       )->update({maintainer => $maint})
484                                   });
485         }
486         $p->update() if $p;
487     }
488     $p->remove() if $p;
489 }
490
491 sub add_configuration {
492     my ($options,$opts,$p,$config,$argv) = @_;
493
494     my $s = db_connect($options);
495
496     # tags
497     # add all tags
498     my %tags;
499     for my $tag (@{$config{tags}}) {
500         $tags{$tag} = 1;
501         $s->resultset('Tag')->find_or_create({tag => $tag});
502     }
503     # mark obsolete tags
504     for my $tag ($s->resultset('Tag')->search_rs()->all()) {
505         next if exists $tags{$tag->tag};
506         $tag->obsolete(1);
507         $tag->update;
508     }
509
510     # severities
511     my %sev_names;
512     my $order = -1;
513     for my $sev_name (($config{default_severity},@{$config{severity_list}})) {
514         # add all severitites
515         my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
516         # mark strong severities
517         if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
518             $sev->strong(1);
519         }
520         $sev->ordering($order);
521         $sev->update();
522         $order++;
523         $sev_names{$sev_name} = 1;
524     }
525     # mark obsolete severities
526     for my $sev ($s->resultset('Severity')->search_rs()->all()) {
527         next if exists $sev_names{$sev->severity()};
528         $sev->obsolete(1);
529         $sev->update();
530     }
531 }
532
533 sub add_suite {
534     my ($options,$opts,$p,$config,$argv) = @_;
535     # suites
536
537     my $s = db_connect($options);
538     my $dist_dir = IO::Dir->new($opts->{ftpdists});
539     my @dist_names =
540         grep { $_ !~ /^\./ and
541                -d $opts->{ftpdists}.'/'.$_ and
542                not -l $opts->{ftpdists}.'/'.$_
543            } $dist_dir->read;
544     while (my $dist = shift @dist_names) {
545         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
546         my ($dist_info,$package_files) =
547             read_release_file($dist_dir.'/Release');
548         load_suite($s,$dist_info);
549     }
550 }
551
552 sub add_logs {
553     my ($options,$opts,$p,$config,$argv) = @_;
554
555     chdir($config->{spool_dir}) or
556         die "chdir $config->{spool_dir} failed: $!";
557
558     my $verbose = $options->{debug};
559
560     my $initialdir = "db-h";
561
562     if (defined $argv->[0] and $argv->[0] eq "archive") {
563         $initialdir = "archive";
564     }
565     my $s = db_connect($options);
566
567     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
568               $p,
569               'log',
570               $verbose,
571               sub {
572                   my $bug = shift;
573                   my $stat = stat(getbugcomponent($bug,'log',$initialdir));
574                   if (not defined $stat) {
575                       print STDERR "Unable to stat $bug $!\n";
576                       next;
577                   }
578                   if ($options{quick}) {
579                       my $rs = $s->resultset('Bug')->
580                           search({id=>$bug})->single();
581                       return if defined $rs and
582                           $stat->mtime <= $rs->last_modified()->epoch();
583                   }
584                   eval {
585                       load_bug_log(db => $s,
586                                    bug => $bug);
587                   };
588                   if ($@) {
589                       die "failure while trying to load bug log $bug\n$@";
590                   }
591               });
592 }
593
594 sub add_bugs_and_logs {
595     my ($options,$opts,$p,$config,$argv) = @_;
596
597     chdir($config->{spool_dir}) or
598         die "chdir $config->{spool_dir} failed: $!";
599
600     my $verbose = $options->{debug};
601
602     my $initialdir = "db-h";
603
604     if (defined $argv->[0] and $argv->[0] eq "archive") {
605         $initialdir = "archive";
606     }
607     my $s = db_connect($options);
608
609     my %tags;
610     my %severities;
611     my %queue;
612
613     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
614               $p,
615               'summary',
616               $verbose,
617               sub {
618                   my @bugs = @_;
619                   my @bugs_to_update;
620                   if ($options{quick}) {
621                       @bugs_to_update =
622                           bugs_to_update($s,$initialdir,@bugs);
623                   } else {
624                       @bugs_to_update = @bugs;
625                   }
626                   eval {
627                       $s->txn_do(sub {
628                                      for my $bug (@bugs_to_update) {
629                                          load_bug(db => $s,
630                                                   bug => $bug,
631                                                   tags => \%tags,
632                                                   severities => \%severities,
633                                                   queue => \%queue);
634                                      }
635                                  });
636                   };
637                   if ($@) {
638                       die "failure while trying to load bug: $@";
639                   }
640                   for my $bug (@bugs) {
641                       my $stat = stat(getbugcomponent($bug,'log',$initialdir));
642                       if (not defined $stat) {
643                           print STDERR "Unable to stat $bug $!\n";
644                           next;
645                       }
646                       if ($options{quick}) {
647                           my $rs = $s->resultset('Bug')->
648                               search({id=>$bug})->single();
649                           return if defined $rs and
650                               $stat->mtime <= $rs->last_modified()->epoch();
651                       }
652                       eval {
653                           load_bug_log(db => $s,
654                                        bug => $bug);
655                       };
656                       if ($@) {
657                           die "failure while trying to load bug log $bug\n$@";
658                       }
659                   }
660               },
661               50
662              );
663     handle_load_bug_queue(db=>$s,
664                           queue => \%queue,
665                          );
666
667 }
668
669 sub add_packages {
670     my ($options,$opts,$p,$config,$argv) = @_;
671
672     my $dist_dir = IO::Dir->new($opts->{ftpdists});
673     my @dist_names =
674         grep { $_ !~ /^\./ and
675                -d $opts->{ftpdists}.'/'.$_ and
676                not -l $opts->{ftpdists}.'/'.$_
677            } $dist_dir->read;
678     my %s_p;
679     while (my $dist = shift @dist_names) {
680         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
681         my ($dist_info,$package_files) =
682             read_release_file($dist_dir.'/Release');
683         $s_p{$dist_info->{Codename}} = $package_files;
684     }
685     my $tot = 0;
686     for my $suite (keys %s_p) {
687         for my $component (keys %{$s_p{$suite}}) {
688             $tot += scalar keys %{$s_p{$suite}{$component}};
689         }
690     }
691     $p->target($tot) if $p;
692     my $i = 0;
693     my $avg_pkgs = 0;
694     my $tot_suites = scalar keys %s_p;
695     my $done_suites=0;
696     my $completed_pkgs=0;
697     # parse packages files
698     for my $suite (keys %s_p) {
699         my @pkgs;
700         for my $component (keys %{$s_p{$suite}}) {
701             my @archs = keys %{$s_p{$suite}{$component}};
702             if (grep {$_ eq 'source'} @archs) {
703                 @archs = ('source',grep {$_ ne 'source'} @archs);
704             }
705             for my $arch (@archs) {
706                 my $pfh =  open_compressed_file($s_p{$suite}{$component}{$arch}) or
707                     die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
708                 local $_;
709                 local $/ = '';  # paragraph mode
710                 while (<$pfh>) {
711                     my %pkg;
712                     for my $field (qw(Package Maintainer Version Source)) {
713                         /^\Q$field\E: (.*)/m;
714                         $pkg{$field} = $1;
715                     }
716                     next unless defined $pkg{Package} and
717                         defined $pkg{Version};
718                     push @pkgs,[$arch,$component,\%pkg];
719                 }
720             }
721         }
722         my $s = db_connect($options);
723         if ($avg_pkgs==0) {
724             $avg_pkgs = @pkgs;
725         }
726         $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
727                    $completed_pkgs+@pkgs) if $p;
728         load_packages($s,
729                       $suite,
730                       \@pkgs,
731                       $p);
732         $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
733         $completed_pkgs += @pkgs;
734         $done_suites++;
735     }
736     $p->remove() if $p;
737 }
738
739 sub handle_subcommand_arguments {
740     my ($argv,$args) = @_;
741     my $subopt = {};
742     Getopt::Long::GetOptionsFromArray($argv,
743                               $subopt,
744                               keys %{$args},
745                              );
746     my @usage_errors;
747     for my $arg  (keys %{$args}) {
748         next unless $args->{$arg};
749         my $r_arg = $arg; # real argument name
750         $r_arg =~ s/[=\|].+//g;
751         if (not defined $subopt->{$r_arg}) {
752             push @usage_errors, "You must give a $r_arg option";
753         }
754     }
755     pod2usage(join("\n",@usage_errors)) if @usage_errors;
756     return $subopt;
757 }
758
759 sub get_lock{
760     my ($subcommand,$config,$options) = @_;
761     if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
762         if ($options->{quick}) {
763             # If this is a quick run, just exit
764             print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
765             exit 0;
766         }
767         print STDERR "Another debbugs-loadsql is running; stopping\n";
768         exit 1;
769     }
770 }
771
772 sub db_connect {
773     my ($options) = @_;
774     # connect to the database; figure out how to handle errors
775     # properly here.
776     my $s = Debbugs::DB->connect($options->{dsn} //
777                                  $options->{service}) or
778         die "Unable to connect to database: ";
779 }
780
781 sub read_release_file {
782     my ($file) = @_;
783     # parse release
784     my $rfh =  open_compressed_file($file) or
785         die "Unable to open $file for reading: $!";
786     my %dist_info;
787     my $in_sha1;
788     my %p_f;
789     while (<$rfh>) {
790         chomp;
791         if (s/^(\S+):\s*//) {
792             if ($1 eq 'SHA1'or $1 eq 'SHA256') {
793                 $in_sha1 = 1;
794                 next;
795             }
796             $dist_info{$1} = $_;
797         } elsif ($in_sha1) {
798             s/^\s//;
799             my ($sha,$size,$f) = split /\s+/,$_;
800             next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
801             next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
802             my ($component,$arch,$package_source) = ($1,$2,$3);
803             $arch =~ s/binary-//;
804             next if exists $p_f{$component}{$arch};
805             $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
806         }
807     }
808     return (\%dist_info,\%p_f);
809 }
810
811 sub walk_bugs {
812     my ($dirs,$p,$what,$verbose,$sub,$n) = @_;
813     my @dirs = @{$dirs};
814     my $tot_dirs = @dirs;
815     my $done_dirs = 0;
816     my $avg_subfiles = 0;
817     my $completed_files = 0;
818     $n //= 1;
819     while (my $dir = shift @dirs) {
820         printf "Doing dir %s ...\n", $dir if $verbose;
821
822         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
823         my @subdirs = readdir(DIR);
824         closedir(DIR);
825
826         my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
827         $tot_dirs -= @dirs;
828         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
829         $tot_dirs += @dirs;
830         if ($avg_subfiles == 0) {
831             $avg_subfiles = @list;
832         }
833
834         $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
835         $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
836         $done_dirs += 1;
837
838         my $it = natatime $n,@list;
839         while (my @bugs = $it->()) {
840           $sub->(@bugs);
841           $completed_files += scalar @bugs;
842           $p->update($completed_files) if $p;
843           print "Up to $completed_files bugs...\n"
844             if ($completed_files % 100 == 0 && $verbose);
845         }
846     }
847     $p->remove() if $p;
848 }
849
850
851 sub bugs_to_update {
852     my ($s,$initialdir,@bugs) = @_;
853     my @bugs_to_update;
854     for my $bug (@bugs) {
855         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
856         if (not defined $stat) {
857             print STDERR "Unable to stat $bug $!\n";
858             next;
859         }
860         my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
861         next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
862         push @bugs_to_update, $bug;
863     }
864     @bugs_to_update;
865 }
866
867
868 __END__