]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
for installsql and loadsql, use the git INC if run out of git
[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_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                     $srcname !~ /^$config{package_name_re}$/o or
441                     $binname !~ /^$config{package_name_re}$/o or
442                     $srcver !~ /^$config{package_version_re}$/o or
443                     $binver !~ /^$config{package_version_re}$/o
444                    ) {
445                     print STDERR "malformed debinfo: $file\n";
446                     next FILE;
447                 }
448                 push @file_debinfos,
449                     [$binname,$binver,$binarch,$srcname,$srcver,$ct_date];
450             }
451             push @debinfos,
452                 @file_debinfos;
453         }
454         $s->txn_do(
455             sub {
456                 for my $di (@debinfos) {
457                     Debbugs::DB::Load::load_debinfo($s,@{$di}[0..5],\%cache);
458                 }
459             });
460         $p->update($p->last_update()+@v) if $p;
461     }
462     $p->remove() if $p;
463 }
464
465 sub add_maintainers {
466     my ($options,$opts,$p,$config,$argv) = @_;
467
468     my $s = db_connect($options);
469     my $maintainers = getsourcemaintainers() // {};
470     $p->target(2) if $p;
471     ## get all of the maintainers, and add the missing ones
472     my $maints = $s->resultset('Maintainer')->
473         get_maintainers(values %{$maintainers});
474     $p->update() if $p;
475     my @svs = $s->resultset('SrcVer')->
476         search({maintainer => undef
477                },
478               {join => 'src_pkg',
479                group_by => 'me.src_pkg, src_pkg.pkg',
480                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
481                columns => [qw(me.src_pkg src_pkg.pkg)],
482               }
483               )->all();
484     $p->target(2+@svs) if $p;
485     $p->update() if $p;
486     for my $sv (@svs) {
487         if (exists $maintainers->{$sv->{src_pkg}{pkg}}) {
488             my $pkg = $sv->{src_pkg}{pkg};
489             my $maint = $maints->
490                {$maintainers->{$pkg}};
491             $s->txn_do(sub {$s->resultset('SrcVer')->
492                                 search({maintainer => undef,
493                                         'src_pkg.pkg' => $pkg
494                                        },
495                                       {join => 'src_pkg'}
496                                       )->update({maintainer => $maint})
497                                   });
498         }
499         $p->update() if $p;
500     }
501     $p->remove() if $p;
502 }
503
504 sub add_configuration {
505     my ($options,$opts,$p,$config,$argv) = @_;
506
507     my $s = db_connect($options);
508
509     # tags
510     # add all tags
511     my %tags;
512     for my $tag (@{$config{tags}}) {
513         $tags{$tag} = 1;
514         $s->resultset('Tag')->find_or_create({tag => $tag});
515     }
516     # mark obsolete tags
517     for my $tag ($s->resultset('Tag')->search_rs()->all()) {
518         next if exists $tags{$tag->tag};
519         $tag->obsolete(1);
520         $tag->update;
521     }
522
523     # severities
524     my %sev_names;
525     my $order = -1;
526     for my $sev_name (($config{default_severity},@{$config{severity_list}})) {
527         # add all severitites
528         my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
529         # mark strong severities
530         if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
531             $sev->strong(1);
532         }
533         $sev->ordering($order);
534         $sev->update();
535         $order++;
536         $sev_names{$sev_name} = 1;
537     }
538     # mark obsolete severities
539     for my $sev ($s->resultset('Severity')->search_rs()->all()) {
540         next if exists $sev_names{$sev->severity()};
541         $sev->obsolete(1);
542         $sev->update();
543     }
544 }
545
546 sub add_suite {
547     my ($options,$opts,$p,$config,$argv) = @_;
548     # suites
549
550     my $s = db_connect($options);
551     my $dist_dir = IO::Dir->new($opts->{ftpdists});
552     my @dist_names =
553         grep { $_ !~ /^\./ and
554                -d $opts->{ftpdists}.'/'.$_ and
555                not -l $opts->{ftpdists}.'/'.$_
556            } $dist_dir->read;
557     while (my $dist = shift @dist_names) {
558         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
559         my ($dist_info,$package_files) =
560             read_release_file($dist_dir.'/Release');
561         load_suite($s,$dist_info);
562     }
563 }
564
565 sub add_logs {
566     my ($options,$opts,$p,$config,$argv) = @_;
567
568     chdir($config->{spool_dir}) or
569         die "chdir $config->{spool_dir} failed: $!";
570
571     my $verbose = $options->{debug};
572
573     my $initialdir = "db-h";
574
575     if (defined $argv->[0] and $argv->[0] eq "archive") {
576         $initialdir = "archive";
577     }
578     my $s = db_connect($options);
579
580     walk_bugs(dirs => [(@{$argv}?@{$argv} : $initialdir)],
581               progress_bar => $p,
582               bug_file => 'log',
583               $verbose?(logging => \*STDERR):(),
584               callback =>
585               sub {
586                   my $bug = shift;
587                   my $stat = stat(getbugcomponent($bug,'log',$initialdir));
588                   if (not defined $stat) {
589                       print STDERR "Unable to stat $bug $!\n";
590                       next;
591                   }
592                   if ($options{quick}) {
593                       my $rs = $s->resultset('Bug')->
594                           search({id=>$bug})->single();
595                       return if defined $rs and
596                           $stat->mtime <= $rs->last_modified()->epoch();
597                   }
598                   eval {
599                       load_bug_log(db => $s,
600                                    bug => $bug);
601                   };
602                   if ($@) {
603                       die "failure while trying to load bug log $bug\n$@";
604                   }
605               });
606 }
607
608 sub add_bugs_and_logs {
609     my ($options,$opts,$p,$config,$argv) = @_;
610
611     chdir($config->{spool_dir}) or
612         die "chdir $config->{spool_dir} failed: $!";
613
614     my $verbose = $options->{debug};
615
616     my $initialdir = "db-h";
617
618     if (defined $argv->[0] and $argv->[0] eq "archive") {
619         $initialdir = "archive";
620     }
621     my $s = db_connect($options);
622
623     my %tags;
624     my %severities;
625     my %queue;
626
627     walk_bugs(dirs => [(@{$argv}?@{$argv} : $initialdir)],
628               progress_bar => $p,
629               $verbose?(logging => \*STDERR):(),
630               callback =>
631               sub {
632                   my @bugs = @_;
633                   my @bugs_to_update;
634                   if ($options{quick}) {
635                       @bugs_to_update =
636                           bugs_to_update($s,@bugs);
637                   } else {
638                       @bugs_to_update = @bugs;
639                   }
640                   eval {
641                       $s->txn_do(sub {
642                                      for my $bug (@bugs_to_update) {
643                                          load_bug(db => $s,
644                                                   bug => $bug,
645                                                   tags => \%tags,
646                                                   severities => \%severities,
647                                                   queue => \%queue);
648                                      }
649                                  });
650                   };
651                   if ($@) {
652                       die "failure while trying to load bug: $@";
653                   }
654                   for my $bug (@bugs) {
655                       my $stat = stat(getbugcomponent($bug,'log',$initialdir));
656                       if (not defined $stat) {
657                           print STDERR "Unable to stat $bug $!\n";
658                           next;
659                       }
660                       if ($options{quick}) {
661                           my $rs = $s->resultset('Bug')->
662                               search({id=>$bug})->single();
663                           return if defined $rs and
664                               $stat->mtime <= $rs->last_modified()->epoch();
665                       }
666                       eval {
667                           load_bug_log(db => $s,
668                                        bug => $bug);
669                       };
670                       if ($@) {
671                           die "failure while trying to load bug log $bug\n$@";
672                       }
673                   }
674               },
675               bugs_per_call => 50,
676              );
677     handle_load_bug_queue(db=>$s,
678                           queue => \%queue,
679                          );
680
681 }
682
683 sub add_packages {
684     my ($options,$opts,$p,$config,$argv) = @_;
685
686     my $dist_dir = IO::Dir->new($opts->{ftpdists});
687     my @dist_names =
688         grep { $_ !~ /^\./ and
689                -d $opts->{ftpdists}.'/'.$_ and
690                not -l $opts->{ftpdists}.'/'.$_
691            } $dist_dir->read;
692     my %s_p;
693     while (my $dist = shift @dist_names) {
694         my $dist_dir = $opts->{ftpdists}.'/'.$dist;
695         my ($dist_info,$package_files) =
696             read_release_file($dist_dir.'/Release');
697         $s_p{$dist_info->{Codename}} = $package_files;
698     }
699     my $tot = 0;
700     for my $suite (keys %s_p) {
701         for my $component (keys %{$s_p{$suite}}) {
702             $tot += scalar keys %{$s_p{$suite}{$component}};
703         }
704     }
705     $p->target($tot) if $p;
706     my $i = 0;
707     my $avg_pkgs = 0;
708     my $tot_suites = scalar keys %s_p;
709     my $done_suites=0;
710     my $completed_pkgs=0;
711     # parse packages files
712     for my $suite (keys %s_p) {
713         my @pkgs;
714         for my $component (keys %{$s_p{$suite}}) {
715             my @archs = keys %{$s_p{$suite}{$component}};
716             if (grep {$_ eq 'source'} @archs) {
717                 @archs = ('source',grep {$_ ne 'source'} @archs);
718             }
719             for my $arch (@archs) {
720                 my $pfh =  open_compressed_file($s_p{$suite}{$component}{$arch}) or
721                     die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
722                 local $_;
723                 local $/ = '';  # paragraph mode
724                 while (<$pfh>) {
725                     my %pkg;
726                     for my $field (qw(Package Maintainer Version Source)) {
727                         /^\Q$field\E: (.*)/m;
728                         $pkg{$field} = $1;
729                     }
730                     next unless defined $pkg{Package} and
731                         defined $pkg{Version};
732                     push @pkgs,[$arch,$component,\%pkg];
733                 }
734             }
735         }
736         my $s = db_connect($options);
737         if ($avg_pkgs==0) {
738             $avg_pkgs = @pkgs;
739         }
740         $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
741                    $completed_pkgs+@pkgs) if $p;
742         load_packages($s,
743                       $suite,
744                       \@pkgs,
745                       $p);
746         $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
747         $completed_pkgs += @pkgs;
748         $done_suites++;
749     }
750     $p->remove() if $p;
751 }
752
753 sub handle_subcommand_arguments {
754     my ($argv,$args) = @_;
755     my $subopt = {};
756     Getopt::Long::GetOptionsFromArray($argv,
757                               $subopt,
758                               keys %{$args},
759                              );
760     my @usage_errors;
761     for my $arg  (keys %{$args}) {
762         next unless $args->{$arg};
763         my $r_arg = $arg; # real argument name
764         $r_arg =~ s/[=\|].+//g;
765         if (not defined $subopt->{$r_arg}) {
766             push @usage_errors, "You must give a $r_arg option";
767         }
768     }
769     pod2usage(join("\n",@usage_errors)) if @usage_errors;
770     return $subopt;
771 }
772
773 sub get_lock{
774     my ($subcommand,$config,$options) = @_;
775     if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
776         if ($options->{quick}) {
777             # If this is a quick run, just exit
778             print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
779             exit 0;
780         }
781         print STDERR "Another debbugs-loadsql is running; stopping\n";
782         exit 1;
783     }
784 }
785
786 sub db_connect {
787     my ($options) = @_;
788     # connect to the database; figure out how to handle errors
789     # properly here.
790     my $s = Debbugs::DB->connect($options->{dsn} //
791                                  $options->{service}) or
792         die "Unable to connect to database: ";
793 }
794
795 sub read_release_file {
796     my ($file) = @_;
797     # parse release
798     my $rfh =  open_compressed_file($file) or
799         die "Unable to open $file for reading: $!";
800     my %dist_info;
801     my $in_sha1;
802     my %p_f;
803     while (<$rfh>) {
804         chomp;
805         if (s/^(\S+):\s*//) {
806             if ($1 eq 'SHA1'or $1 eq 'SHA256') {
807                 $in_sha1 = 1;
808                 next;
809             }
810             $dist_info{$1} = $_;
811         } elsif ($in_sha1) {
812             s/^\s//;
813             my ($sha,$size,$f) = split /\s+/,$_;
814             next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
815             next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
816             my ($component,$arch,$package_source) = ($1,$2,$3);
817             $arch =~ s/binary-//;
818             next if exists $p_f{$component}{$arch};
819             $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
820         }
821     }
822     return (\%dist_info,\%p_f);
823 }
824
825
826 sub bugs_to_update {
827     my ($s,@bugs) = @_;
828     my @bugs_to_update;
829     for my $bug (@bugs) {
830         my $stat = stat(getbugcomponent($bug,'summary',getbuglocation($bug,'summary')));
831         if (not defined $stat) {
832             print STDERR "Unable to stat $bug $!\n";
833             next;
834         }
835         my $rs = $s->resultset('Bug')->search({id=>$bug})->single();
836         next if defined $rs and $stat->mtime <= $rs->last_modified()->epoch();
837         push @bugs_to_update, $bug;
838     }
839     @bugs_to_update;
840 }
841
842
843 __END__