]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
remove useless $time and $start_time variables
[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 IO::Dir;
146 use IO::File;
147 use IO::Uncompress::AnyUncompress;
148 use Encode qw(decode_utf8);
149 use List::MoreUtils qw(natatime);
150
151 my %options =
152     (debug           => 0,
153      help            => 0,
154      man             => 0,
155      verbose         => 0,
156      quiet           => 0,
157      quick           => 0,
158      service         => $config{debbugs_db},
159      progress        => 0,
160     );
161
162 Getopt::Long::Configure('pass_through');
163 GetOptions(\%options,
164            'quick|q',
165            'service|s=s',
166            'sysconfdir|c=s',
167            'progress!',
168            'spool_dir|spool-dir=s',
169            'verbose|v+',
170            'quiet+',
171            'debug|d+','help|h|?','man|m');
172 Getopt::Long::Configure('default');
173
174 pod2usage() if $options{help};
175 pod2usage({verbose=>2}) if $options{man};
176
177 $DEBUG = $options{debug};
178
179 my %subcommands =
180     ('bugs' => {function => \&add_bugs,
181                 arguments => {'preload' => 0},
182                },
183      'versions' => {function => \&add_versions,
184                    },
185      'debinfo' => {function => \&add_debinfo,
186                    arguments => {'0|null' => 0},
187                   },
188      'maintainers' => {function => \&add_maintainers,
189                       },
190      'configuration' => {function => \&add_configuration,
191                         },
192      'suites' => {function => \&add_suite,
193                   arguments => {'ftpdists=s' => 1,
194                                },
195                  },
196      'logs' => {function => \&add_logs,
197                },
198      'bugs_and_logs' => {function => \&add_bugs_and_logs,
199                         },
200      'packages' => {function => \&add_packages,
201                     arguments => {'ftpdists=s' => 1,
202                                   'suites=s@' => 0,
203                                  },
204                    },
205      'help' => {function => sub {pod2usage({verbose => 2});}}
206     );
207
208 my @USAGE_ERRORS;
209 $options{verbose} = $options{verbose} - $options{quiet};
210
211 if ($options{progress}) {
212     eval "use Term::ProgressBar";
213     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
214 }
215
216
217 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
218
219 if (exists $options{sysconfdir}) {
220     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
221         delete $ENV{PGSYSCONFDIR};
222     } else {
223         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
224     }
225 }
226
227 if (exists $options{spool_dir} and defined $options{spool_dir}) {
228     $config{spool_dir} = $options{spool_dir};
229 }
230
231 my $prog_bar;
232 if ($options{progress}) {
233     $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
234     warn "Unable to initialize progress bar: $@" if not $prog_bar;
235 }
236
237
238 my ($subcommand) = shift @ARGV;
239 if (not defined $subcommand) {
240     $subcommand = 'help';
241     print STDERR "You must provide a subcommand; displaying usage.\n";
242     pod2usage();
243 } elsif (not exists $subcommands{$subcommand}) {
244     print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
245     pod2usage();
246 }
247
248 binmode(STDOUT,':encoding(UTF-8)');
249 binmode(STDERR,':encoding(UTF-8)');
250
251 my $opts =
252     handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
253 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
254
255 sub add_bugs {
256     my ($options,$opts,$p,$config,$argv) = @_;
257     chdir($config->{spool_dir}) or
258         die "chdir $config->{spool_dir} failed: $!";
259
260     my $verbose = $options->{debug};
261
262     my $initialdir = "db-h";
263
264     if (defined $argv->[0] and $argv->[0] eq "archive") {
265         $initialdir = "archive";
266     }
267     my $s = db_connect($options);
268
269
270     my %tags;
271     my %severities;
272     my %queue;
273
274     if ($opts->{preload}) {
275         my @bugs;
276         walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
277                   undef,
278                   'summary',
279                   undef,
280                   sub {
281                     push @bugs,@_;
282                   },
283                   10000
284                  );
285         $s->resultset('Bug')->quick_insert_bugs(@bugs);
286     }
287     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
288               $p,
289               'summary',
290               $verbose,
291               sub {
292                 my @bugs = @_;
293                 my @bugs_to_update;
294                 if ($options{quick}) {
295                     @bugs_to_update =
296                         bugs_to_update($s,$initialdir,@bugs);
297                 } else {
298                     @bugs_to_update = @bugs;
299                 }
300                 eval {
301                   $s->txn_do(sub {
302                                for my $bug (@bugs_to_update) {
303                                  load_bug(db => $s,
304                                           bug => $bug,
305                                           tags => \%tags,
306                                           severities => \%severities,
307                                           queue => \%queue);
308                                }
309                              });
310                 };
311                 if ($@) {
312                   die "failure while trying to load bug: $@";
313                 }
314               },
315               50
316              );
317     handle_load_bug_queue(db => $s,
318                           queue => \%queue);
319 }
320
321 sub add_versions {
322     my ($options,$opts,$p,$config,$argv) = @_;
323
324     my $s = db_connect($options);
325
326     my @files = @{$argv};
327     $p->target(scalar @files) if $p;
328     for my $file (@files) {
329         my $fh = IO::File->new($file,'r') or
330             die "Unable to open $file for reading: $!";
331         my @versions;
332         my %src_pkgs;
333         while (<$fh>) {
334             chomp;
335             next unless length $_;
336             if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
337                 push @versions, [$1,$2];
338             }
339         }
340         close($fh);
341         my $ancestor_sv;
342         for my $i (reverse 0..($#versions)) {
343             my $sp;
344             if (not defined $src_pkgs{$versions[$i][0]}) {
345                 $src_pkgs{$versions[$i][0]} =
346                     $s->resultset('SrcPkg')->
347                     get_src_pkg_id($versions[$i][0]);
348             }
349             $sp = $src_pkgs{$versions[$i][0]};
350             # There's probably something wrong if the source package
351             # doesn't exist, but we'll skip it for now
352             last if not defined $sp;
353             my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp,
354                                                     ver => $versions[$i][1],
355                                                    });
356             last if not defined $sv;
357             if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
358                 $sv->update({based_on => $ancestor_sv})
359             }
360             $ancestor_sv = $sv->id();
361         }
362         $p->update() if $p;
363     }
364     $p->remove() if $p;
365 }
366
367 sub add_debinfo {
368     my ($options,$opts,$p,$config,$argv) = @_;
369
370     my @files = @{$argv};
371     if (not @files) {
372        {
373            local $/ = "\n";
374            local $/ = "\0" if $opts->{0};
375            while (<STDIN>) {
376                s/\n$// unless $opts->{0};
377                s/\0$// if $opts->{0};
378                push @files, $_;
379            }
380        }
381     }
382     return unless @files;
383     my $s = db_connect($options);
384     $p->target(scalar @files) if $p;
385     my $it = natatime 100, @files;
386     while (my @v = $it->()) {
387         my %cache;
388         my @debinfos;
389         for my $file (@v) {
390             my $fh = IO::File->new($file,'r') or
391                 die "Unable to open $file for reading: $!";
392             my $f_stat = stat($file);
393             my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
394             while (<$fh>) {
395                 chomp;
396                 next unless length $_;
397                 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
398                 # if $srcver is not defined, this is probably a broken
399                 # .debinfo file [they were causing #686106, see commit
400                 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
401                 # the file, so we'll fudge it from the filename.
402                 if (not defined $srcver) {
403                     ($srcname,$srcver) = ($binarch,$srcname);
404                     ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
405                 }
406                 if (not defined $srcver) {
407                     print STDERR "malformed debinfo (no srcver): $file\n";
408                     next;
409                 }
410                 push @debinfos,
411                     [$binname,$binver,$binarch,$srcname,$srcver,$ct_date];
412             }
413         }
414         $s->txn_do(
415             sub {
416                 for my $di (@debinfos) {
417                     Debbugs::DB::Load::load_debinfo($s,@{$di}[0..5],\%cache);
418                 }
419             });
420         $p->update($p->last_update()+@v) if $p;
421     }
422     $p->remove() if $p;
423 }
424
425 sub add_maintainers {
426     my ($options,$opts,$p,$config,$argv) = @_;
427
428     my $s = db_connect($options);
429     my $maintainers = getsourcemaintainers();
430     $p->target(2) if $p;
431     ## get all of the maintainers, and add the missing ones
432     my $maints = $s->resultset('Maintainer')->
433         get_maintainers(values %{$maintainers});
434     $p->update();
435     my @svs = $s->resultset('SrcVer')->
436         search({maintainer => undef
437                },
438               {join => 'src_pkg',
439                group_by => 'me.src_pkg, src_pkg.pkg',
440                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
441                columns => [qw(me.src_pkg src_pkg.pkg)],
442               }
443               )->all();
444     $p->target(2+@svs) if $p;
445     $p->update() if $p;
446     for my $sv (@svs) {
447         if (exists $maintainers->{$sv->{src_pkg}{pkg}}) {
448             my $pkg = $sv->{src_pkg}{pkg};
449             my $maint = $maints->
450                {$maintainers->{$pkg}};
451             $s->txn_do(sub {$s->resultset('SrcVer')->
452                                 search({maintainer => undef,
453                                         'src_pkg.pkg' => $pkg
454                                        },
455                                       {join => 'src_pkg'}
456                                       )->update({maintainer => $maint})
457                                   });
458         }
459         $p->update() if $p;
460     }
461     $p->remove() if $p;
462 }
463
464 sub add_configuration {
465     my ($options,$opts,$p,$config,$argv) = @_;
466
467     my $s = db_connect($options);
468
469     # tags
470     # add all tags
471     my %tags;
472     for my $tag (@{$config{tags}}) {
473         $tags{$tag} = 1;
474         $s->resultset('Tag')->find_or_create({tag => $tag});
475     }
476     # mark obsolete tags
477     for my $tag ($s->resultset('Tag')->search_rs()->all()) {
478         next if exists $tags{$tag->tag};
479         $tag->obsolete(1);
480         $tag->update;
481     }
482
483     # severities
484     my %sev_names;
485     my $order = -1;
486     for my $sev_name (($config{default_severity},@{$config{severity_list}})) {
487         # add all severitites
488         my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
489         # mark strong severities
490         if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
491             $sev->strong(1);
492         }
493         $sev->ordering($order);
494         $sev->update();
495         $order++;
496         $sev_names{$sev_name} = 1;
497     }
498     # mark obsolete severities
499     for my $sev ($s->resultset('Severity')->search_rs()->all()) {
500         next if exists $sev_names{$sev->severity()};
501         $sev->obsolete(1);
502         $sev->update();
503     }
504 }
505
506 sub add_suite {
507     my ($options,$opts,$p,$config,$argv) = @_;
508     # suites
509
510     my $s = db_connect($options);
511     my $dist_dir = IO::Dir->new($opts->{ftpdists});
512     my @dist_names =
513         grep { $_ !~ /^\./ and
514                -d $opts->{ftpdists}.'/'.$_ and
515                not -l $opts->{ftpdists}.'/'.$_
516            } $dist_dir->read;
517     while (my $dist = shift @dist_names) {
518         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
519         my ($dist_info,$package_files) =
520             read_release_file($dist_dir.'/Release');
521         load_suite($s,$dist_info);
522     }
523 }
524
525 sub add_logs {
526     my ($options,$opts,$p,$config,$argv) = @_;
527
528     chdir($config->{spool_dir}) or
529         die "chdir $config->{spool_dir} failed: $!";
530
531     my $verbose = $options->{debug};
532
533     my $initialdir = "db-h";
534
535     if (defined $argv->[0] and $argv->[0] eq "archive") {
536         $initialdir = "archive";
537     }
538     my $s = db_connect($options);
539
540     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
541               $p,
542               'log',
543               $verbose,
544               sub {
545                   my $bug = shift;
546                   my $stat = stat(getbugcomponent($bug,'log',$initialdir));
547                   if (not defined $stat) {
548                       print STDERR "Unable to stat $bug $!\n";
549                       next;
550                   }
551                   if ($options{quick}) {
552                       my $rs = $s->resultset('Bug')->
553                           search({id=>$bug})->single();
554                       return if defined $rs and
555                           $stat->mtime <= $rs->last_modified()->epoch();
556                   }
557                   eval {
558                       load_bug_log(db => $s,
559                                    bug => $bug);
560                   };
561                   if ($@) {
562                       die "failure while trying to load bug log $bug\n$@";
563                   }
564               });
565 }
566
567 sub add_bugs_and_logs {
568     my ($options,$opts,$p,$config,$argv) = @_;
569
570     chdir($config->{spool_dir}) or
571         die "chdir $config->{spool_dir} failed: $!";
572
573     my $verbose = $options->{debug};
574
575     my $initialdir = "db-h";
576
577     if (defined $argv->[0] and $argv->[0] eq "archive") {
578         $initialdir = "archive";
579     }
580     my $s = db_connect($options);
581
582     my %tags;
583     my %severities;
584     my %queue;
585
586     walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
587               $p,
588               'summary',
589               $verbose,
590               sub {
591                   my @bugs = @_;
592                   my @bugs_to_update;
593                   if ($options{quick}) {
594                       @bugs_to_update =
595                           bugs_to_update($s,$initialdir,@bugs);
596                   } else {
597                       @bugs_to_update = @bugs;
598                   }
599                   eval {
600                       $s->txn_do(sub {
601                                      for my $bug (@bugs_to_update) {
602                                          load_bug(db => $s,
603                                                   bug => $bug,
604                                                   tags => \%tags,
605                                                   severities => \%severities,
606                                                   queue => \%queue);
607                                      }
608                                  });
609                   };
610                   if ($@) {
611                       die "failure while trying to load bug: $@";
612                   }
613                   for my $bug (@bugs) {
614                       my $stat = stat(getbugcomponent($bug,'log',$initialdir));
615                       if (not defined $stat) {
616                           print STDERR "Unable to stat $bug $!\n";
617                           next;
618                       }
619                       if ($options{quick}) {
620                           my $rs = $s->resultset('Bug')->
621                               search({id=>$bug})->single();
622                           return if defined $rs and
623                               $stat->mtime <= $rs->last_modified()->epoch();
624                       }
625                       eval {
626                           load_bug_log(db => $s,
627                                        bug => $bug);
628                       };
629                       if ($@) {
630                           die "failure while trying to load bug log $bug\n$@";
631                       }
632                   }
633               },
634               50
635              );
636     handle_load_bug_queue(db=>$s,
637                           queue => \%queue,
638                          );
639
640 }
641
642 sub add_packages {
643     my ($options,$opts,$p,$config,$argv) = @_;
644
645     my $dist_dir = IO::Dir->new($opts->{ftpdists});
646     my @dist_names =
647         grep { $_ !~ /^\./ and
648                -d $opts->{ftpdists}.'/'.$_ and
649                not -l $opts->{ftpdists}.'/'.$_
650            } $dist_dir->read;
651     my %s_p;
652     while (my $dist = shift @dist_names) {
653         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
654         my ($dist_info,$package_files) =
655             read_release_file($dist_dir.'/Release');
656         $s_p{$dist_info->{Codename}} = $package_files;
657     }
658     my $tot = 0;
659     for my $suite (keys %s_p) {
660         for my $component (keys %{$s_p{$suite}}) {
661             $tot += scalar keys %{$s_p{$suite}{$component}};
662         }
663     }
664     $p->target($tot) if $p;
665     my $i = 0;
666     my $avg_pkgs = 0;
667     my $tot_suites = scalar keys %s_p;
668     my $done_suites=0;
669     my $completed_pkgs=0;
670     # parse packages files
671     for my $suite (keys %s_p) {
672         my @pkgs;
673         for my $component (keys %{$s_p{$suite}}) {
674             my @archs = keys %{$s_p{$suite}{$component}};
675             if (grep {$_ eq 'source'} @archs) {
676                 @archs = ('source',grep {$_ ne 'source'} @archs);
677             }
678             for my $arch (@archs) {
679                 my $pfh =  open_compressed_file($s_p{$suite}{$component}{$arch}) or
680                     die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
681                 local $_;
682                 local $/ = '';  # paragraph mode
683                 while (<$pfh>) {
684                     my %pkg;
685                     for my $field (qw(Package Maintainer Version Source)) {
686                         /^\Q$field\E: (.*)/m;
687                         $pkg{$field} = $1;
688                     }
689                     next unless defined $pkg{Package} and
690                         defined $pkg{Version};
691                     push @pkgs,[$arch,$component,\%pkg];
692                 }
693             }
694         }
695         my $s = db_connect($options);
696         if ($avg_pkgs==0) {
697             $avg_pkgs = @pkgs;
698         }
699         $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
700                    $completed_pkgs+@pkgs) if $p;
701         load_packages($s,
702                       $suite,
703                       \@pkgs,
704                       $p);
705         $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
706         $completed_pkgs += @pkgs;
707         $done_suites++;
708     }
709     $p->remove() if $p;
710 }
711
712 sub handle_subcommand_arguments {
713     my ($argv,$args) = @_;
714     my $subopt = {};
715     Getopt::Long::GetOptionsFromArray($argv,
716                               $subopt,
717                               keys %{$args},
718                              );
719     my @usage_errors;
720     for my $arg  (keys %{$args}) {
721         next unless $args->{$arg};
722         my $r_arg = $arg; # real argument name
723         $r_arg =~ s/[=\|].+//g;
724         if (not defined $subopt->{$r_arg}) {
725             push @usage_errors, "You must give a $r_arg option";
726         }
727     }
728     pod2usage(join("\n",@usage_errors)) if @usage_errors;
729     return $subopt;
730 }
731
732 sub get_lock{
733     my ($subcommand,$config,$options) = @_;
734     if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
735         if ($options->{quick}) {
736             # If this is a quick run, just exit
737             print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
738             exit 0;
739         }
740         print STDERR "Another debbugs-loadsql is running; stopping\n";
741         exit 1;
742     }
743 }
744
745 sub db_connect {
746     my ($options) = @_;
747     # connect to the database; figure out how to handle errors
748     # properly here.
749     my $s = Debbugs::DB->connect($options->{service}) or
750         die "Unable to connect to database: ";
751 }
752
753 sub read_release_file {
754     my ($file) = @_;
755     # parse release
756     my $rfh =  open_compressed_file($file) or
757         die "Unable to open $file for reading: $!";
758     my %dist_info;
759     my $in_sha1;
760     my %p_f;
761     while (<$rfh>) {
762         chomp;
763         if (s/^(\S+):\s*//) {
764             if ($1 eq 'SHA1'or $1 eq 'SHA256') {
765                 $in_sha1 = 1;
766                 next;
767             }
768             $dist_info{$1} = $_;
769         } elsif ($in_sha1) {
770             s/^\s//;
771             my ($sha,$size,$f) = split /\s+/,$_;
772             next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
773             next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
774             my ($component,$arch,$package_source) = ($1,$2,$3);
775             $arch =~ s/binary-//;
776             next if exists $p_f{$component}{$arch};
777             $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
778         }
779     }
780     return (\%dist_info,\%p_f);
781 }
782
783 sub walk_bugs {
784     my ($dirs,$p,$what,$verbose,$sub,$n) = @_;
785     my @dirs = @{$dirs};
786     my $tot_dirs = @dirs;
787     my $done_dirs = 0;
788     my $avg_subfiles = 0;
789     my $completed_files = 0;
790     $n //= 1;
791     while (my $dir = shift @dirs) {
792         printf "Doing dir %s ...\n", $dir if $verbose;
793
794         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
795         my @subdirs = readdir(DIR);
796         closedir(DIR);
797
798         my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
799         $tot_dirs -= @dirs;
800         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
801         $tot_dirs += @dirs;
802         if ($avg_subfiles == 0) {
803             $avg_subfiles = @list;
804         }
805
806         $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
807         $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
808         $done_dirs += 1;
809
810         my $it = natatime $n,@list;
811         while (my @bugs = $it->()) {
812           $sub->(@bugs);
813           $completed_files += scalar @bugs;
814           $p->update($completed_files) if $p;
815           print "Up to $completed_files bugs...\n"
816             if ($completed_files % 100 == 0 && $verbose);
817         }
818     }
819     $p->remove() if $p;
820 }
821
822
823 sub bugs_to_update {
824     my ($s,$initialdir,@bugs) = @_;
825     my @bugs_to_update;
826     for my $bug (@bugs) {
827         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
828         if (not defined $stat) {
829             print STDERR "Unable to stat $bug $!\n";
830             next;
831         }
832         my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
833         next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
834         push @bugs_to_update, $bug;
835     }
836     @bugs_to_update;
837 }
838
839
840 __END__