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