]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
only update the progress bar if it exists
[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         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             while (<$fh>) {
408                 chomp;
409                 next unless length $_;
410                 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
411                 # if $srcver is not defined, this is probably a broken
412                 # .debinfo file [they were causing #686106, see commit
413                 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
414                 # the file, so we'll fudge it from the filename.
415                 if (not defined $srcver) {
416                     ($srcname,$srcver) = ($binarch,$srcname);
417                     ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
418                 }
419                 if (not defined $srcver) {
420                     print STDERR "malformed debinfo (no srcver): $file\n";
421                     next;
422                 }
423                 push @debinfos,
424                     [$binname,$binver,$binarch,$srcname,$srcver,$ct_date];
425             }
426         }
427         $s->txn_do(
428             sub {
429                 for my $di (@debinfos) {
430                     Debbugs::DB::Load::load_debinfo($s,@{$di}[0..5],\%cache);
431                 }
432             });
433         $p->update($p->last_update()+@v) if $p;
434     }
435     $p->remove() if $p;
436 }
437
438 sub add_maintainers {
439     my ($options,$opts,$p,$config,$argv) = @_;
440
441     my $s = db_connect($options);
442     my $maintainers = getsourcemaintainers() // {};
443     $p->target(2) if $p;
444     ## get all of the maintainers, and add the missing ones
445     my $maints = $s->resultset('Maintainer')->
446         get_maintainers(values %{$maintainers});
447     $p->update() if $p;
448     my @svs = $s->resultset('SrcVer')->
449         search({maintainer => undef
450                },
451               {join => 'src_pkg',
452                group_by => 'me.src_pkg, src_pkg.pkg',
453                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
454                columns => [qw(me.src_pkg src_pkg.pkg)],
455               }
456               )->all();
457     $p->target(2+@svs) if $p;
458     $p->update() if $p;
459     for my $sv (@svs) {
460         if (exists $maintainers->{$sv->{src_pkg}{pkg}}) {
461             my $pkg = $sv->{src_pkg}{pkg};
462             my $maint = $maints->
463                {$maintainers->{$pkg}};
464             $s->txn_do(sub {$s->resultset('SrcVer')->
465                                 search({maintainer => undef,
466                                         'src_pkg.pkg' => $pkg
467                                        },
468                                       {join => 'src_pkg'}
469                                       )->update({maintainer => $maint})
470                                   });
471         }
472         $p->update() if $p;
473     }
474     $p->remove() if $p;
475 }
476
477 sub add_configuration {
478     my ($options,$opts,$p,$config,$argv) = @_;
479
480     my $s = db_connect($options);
481
482     # tags
483     # add all tags
484     my %tags;
485     for my $tag (@{$config{tags}}) {
486         $tags{$tag} = 1;
487         $s->resultset('Tag')->find_or_create({tag => $tag});
488     }
489     # mark obsolete tags
490     for my $tag ($s->resultset('Tag')->search_rs()->all()) {
491         next if exists $tags{$tag->tag};
492         $tag->obsolete(1);
493         $tag->update;
494     }
495
496     # severities
497     my %sev_names;
498     my $order = -1;
499     for my $sev_name (($config{default_severity},@{$config{severity_list}})) {
500         # add all severitites
501         my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
502         # mark strong severities
503         if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
504             $sev->strong(1);
505         }
506         $sev->ordering($order);
507         $sev->update();
508         $order++;
509         $sev_names{$sev_name} = 1;
510     }
511     # mark obsolete severities
512     for my $sev ($s->resultset('Severity')->search_rs()->all()) {
513         next if exists $sev_names{$sev->severity()};
514         $sev->obsolete(1);
515         $sev->update();
516     }
517 }
518
519 sub add_suite {
520     my ($options,$opts,$p,$config,$argv) = @_;
521     # suites
522
523     my $s = db_connect($options);
524     my $dist_dir = IO::Dir->new($opts->{ftpdists});
525     my @dist_names =
526         grep { $_ !~ /^\./ and
527                -d $opts->{ftpdists}.'/'.$_ and
528                not -l $opts->{ftpdists}.'/'.$_
529            } $dist_dir->read;
530     while (my $dist = shift @dist_names) {
531         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
532         my ($dist_info,$package_files) =
533             read_release_file($dist_dir.'/Release');
534         load_suite($s,$dist_info);
535     }
536 }
537
538 sub add_logs {
539     my ($options,$opts,$p,$config,$argv) = @_;
540
541     chdir($config->{spool_dir}) or
542         die "chdir $config->{spool_dir} failed: $!";
543
544     my $verbose = $options->{debug};
545
546     my $initialdir = "db-h";
547
548     if (defined $argv->[0] and $argv->[0] eq "archive") {
549         $initialdir = "archive";
550     }
551     my $s = db_connect($options);
552
553     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
554               $p,
555               'log',
556               $verbose,
557               sub {
558                   my $bug = shift;
559                   my $stat = stat(getbugcomponent($bug,'log',$initialdir));
560                   if (not defined $stat) {
561                       print STDERR "Unable to stat $bug $!\n";
562                       next;
563                   }
564                   if ($options{quick}) {
565                       my $rs = $s->resultset('Bug')->
566                           search({id=>$bug})->single();
567                       return if defined $rs and
568                           $stat->mtime <= $rs->last_modified()->epoch();
569                   }
570                   eval {
571                       load_bug_log(db => $s,
572                                    bug => $bug);
573                   };
574                   if ($@) {
575                       die "failure while trying to load bug log $bug\n$@";
576                   }
577               });
578 }
579
580 sub add_bugs_and_logs {
581     my ($options,$opts,$p,$config,$argv) = @_;
582
583     chdir($config->{spool_dir}) or
584         die "chdir $config->{spool_dir} failed: $!";
585
586     my $verbose = $options->{debug};
587
588     my $initialdir = "db-h";
589
590     if (defined $argv->[0] and $argv->[0] eq "archive") {
591         $initialdir = "archive";
592     }
593     my $s = db_connect($options);
594
595     my %tags;
596     my %severities;
597     my %queue;
598
599     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
600               $p,
601               'summary',
602               $verbose,
603               sub {
604                   my @bugs = @_;
605                   my @bugs_to_update;
606                   if ($options{quick}) {
607                       @bugs_to_update =
608                           bugs_to_update($s,$initialdir,@bugs);
609                   } else {
610                       @bugs_to_update = @bugs;
611                   }
612                   eval {
613                       $s->txn_do(sub {
614                                      for my $bug (@bugs_to_update) {
615                                          load_bug(db => $s,
616                                                   bug => $bug,
617                                                   tags => \%tags,
618                                                   severities => \%severities,
619                                                   queue => \%queue);
620                                      }
621                                  });
622                   };
623                   if ($@) {
624                       die "failure while trying to load bug: $@";
625                   }
626                   for my $bug (@bugs) {
627                       my $stat = stat(getbugcomponent($bug,'log',$initialdir));
628                       if (not defined $stat) {
629                           print STDERR "Unable to stat $bug $!\n";
630                           next;
631                       }
632                       if ($options{quick}) {
633                           my $rs = $s->resultset('Bug')->
634                               search({id=>$bug})->single();
635                           return if defined $rs and
636                               $stat->mtime <= $rs->last_modified()->epoch();
637                       }
638                       eval {
639                           load_bug_log(db => $s,
640                                        bug => $bug);
641                       };
642                       if ($@) {
643                           die "failure while trying to load bug log $bug\n$@";
644                       }
645                   }
646               },
647               50
648              );
649     handle_load_bug_queue(db=>$s,
650                           queue => \%queue,
651                          );
652
653 }
654
655 sub add_packages {
656     my ($options,$opts,$p,$config,$argv) = @_;
657
658     my $dist_dir = IO::Dir->new($opts->{ftpdists});
659     my @dist_names =
660         grep { $_ !~ /^\./ and
661                -d $opts->{ftpdists}.'/'.$_ and
662                not -l $opts->{ftpdists}.'/'.$_
663            } $dist_dir->read;
664     my %s_p;
665     while (my $dist = shift @dist_names) {
666         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
667         my ($dist_info,$package_files) =
668             read_release_file($dist_dir.'/Release');
669         $s_p{$dist_info->{Codename}} = $package_files;
670     }
671     my $tot = 0;
672     for my $suite (keys %s_p) {
673         for my $component (keys %{$s_p{$suite}}) {
674             $tot += scalar keys %{$s_p{$suite}{$component}};
675         }
676     }
677     $p->target($tot) if $p;
678     my $i = 0;
679     my $avg_pkgs = 0;
680     my $tot_suites = scalar keys %s_p;
681     my $done_suites=0;
682     my $completed_pkgs=0;
683     # parse packages files
684     for my $suite (keys %s_p) {
685         my @pkgs;
686         for my $component (keys %{$s_p{$suite}}) {
687             my @archs = keys %{$s_p{$suite}{$component}};
688             if (grep {$_ eq 'source'} @archs) {
689                 @archs = ('source',grep {$_ ne 'source'} @archs);
690             }
691             for my $arch (@archs) {
692                 my $pfh =  open_compressed_file($s_p{$suite}{$component}{$arch}) or
693                     die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
694                 local $_;
695                 local $/ = '';  # paragraph mode
696                 while (<$pfh>) {
697                     my %pkg;
698                     for my $field (qw(Package Maintainer Version Source)) {
699                         /^\Q$field\E: (.*)/m;
700                         $pkg{$field} = $1;
701                     }
702                     next unless defined $pkg{Package} and
703                         defined $pkg{Version};
704                     push @pkgs,[$arch,$component,\%pkg];
705                 }
706             }
707         }
708         my $s = db_connect($options);
709         if ($avg_pkgs==0) {
710             $avg_pkgs = @pkgs;
711         }
712         $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
713                    $completed_pkgs+@pkgs) if $p;
714         load_packages($s,
715                       $suite,
716                       \@pkgs,
717                       $p);
718         $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
719         $completed_pkgs += @pkgs;
720         $done_suites++;
721     }
722     $p->remove() if $p;
723 }
724
725 sub handle_subcommand_arguments {
726     my ($argv,$args) = @_;
727     my $subopt = {};
728     Getopt::Long::GetOptionsFromArray($argv,
729                               $subopt,
730                               keys %{$args},
731                              );
732     my @usage_errors;
733     for my $arg  (keys %{$args}) {
734         next unless $args->{$arg};
735         my $r_arg = $arg; # real argument name
736         $r_arg =~ s/[=\|].+//g;
737         if (not defined $subopt->{$r_arg}) {
738             push @usage_errors, "You must give a $r_arg option";
739         }
740     }
741     pod2usage(join("\n",@usage_errors)) if @usage_errors;
742     return $subopt;
743 }
744
745 sub get_lock{
746     my ($subcommand,$config,$options) = @_;
747     if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
748         if ($options->{quick}) {
749             # If this is a quick run, just exit
750             print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
751             exit 0;
752         }
753         print STDERR "Another debbugs-loadsql is running; stopping\n";
754         exit 1;
755     }
756 }
757
758 sub db_connect {
759     my ($options) = @_;
760     # connect to the database; figure out how to handle errors
761     # properly here.
762     my $s = Debbugs::DB->connect($options->{dsn} //
763                                  $options->{service}) or
764         die "Unable to connect to database: ";
765 }
766
767 sub read_release_file {
768     my ($file) = @_;
769     # parse release
770     my $rfh =  open_compressed_file($file) or
771         die "Unable to open $file for reading: $!";
772     my %dist_info;
773     my $in_sha1;
774     my %p_f;
775     while (<$rfh>) {
776         chomp;
777         if (s/^(\S+):\s*//) {
778             if ($1 eq 'SHA1'or $1 eq 'SHA256') {
779                 $in_sha1 = 1;
780                 next;
781             }
782             $dist_info{$1} = $_;
783         } elsif ($in_sha1) {
784             s/^\s//;
785             my ($sha,$size,$f) = split /\s+/,$_;
786             next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
787             next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
788             my ($component,$arch,$package_source) = ($1,$2,$3);
789             $arch =~ s/binary-//;
790             next if exists $p_f{$component}{$arch};
791             $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
792         }
793     }
794     return (\%dist_info,\%p_f);
795 }
796
797 sub walk_bugs {
798     my ($dirs,$p,$what,$verbose,$sub,$n) = @_;
799     my @dirs = @{$dirs};
800     my $tot_dirs = @dirs;
801     my $done_dirs = 0;
802     my $avg_subfiles = 0;
803     my $completed_files = 0;
804     $n //= 1;
805     while (my $dir = shift @dirs) {
806         printf "Doing dir %s ...\n", $dir if $verbose;
807
808         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
809         my @subdirs = readdir(DIR);
810         closedir(DIR);
811
812         my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
813         $tot_dirs -= @dirs;
814         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
815         $tot_dirs += @dirs;
816         if ($avg_subfiles == 0) {
817             $avg_subfiles = @list;
818         }
819
820         $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
821         $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
822         $done_dirs += 1;
823
824         my $it = natatime $n,@list;
825         while (my @bugs = $it->()) {
826           $sub->(@bugs);
827           $completed_files += scalar @bugs;
828           $p->update($completed_files) if $p;
829           print "Up to $completed_files bugs...\n"
830             if ($completed_files % 100 == 0 && $verbose);
831         }
832     }
833     $p->remove() if $p;
834 }
835
836
837 sub bugs_to_update {
838     my ($s,$initialdir,@bugs) = @_;
839     my @bugs_to_update;
840     for my $bug (@bugs) {
841         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
842         if (not defined $stat) {
843             print STDERR "Unable to stat $bug $!\n";
844             next;
845         }
846         my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
847         next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
848         push @bugs_to_update, $bug;
849     }
850     @bugs_to_update;
851 }
852
853
854 __END__