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