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