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