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