]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
79296e1988c361e07aebacb08b82588545915385
[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]
21
22  Options:
23   --quick, -q only load changed bugs
24   --progress Show progress bar
25   --service, -s service name
26   --sysconfdir, -c postgresql service config dir
27   --spool-dir debbugs spool directory
28   --debug, -d debugging level (Default 0)
29   --help, -h display this help
30   --man, -m display manual
31
32 =head1 SUBCOMMANDS
33
34 =head2 help
35
36 Display this manual
37
38 =head2 bugs
39
40 Add bugs
41
42 =head2 versions
43
44 Add versions
45
46 =head2 maintainers
47
48 Add source maintainers
49
50 =head1 OPTIONS
51
52 =over
53
54 =item B<--quick, -q>
55
56 Only load changed bugs
57
58 =item B<--progress>
59
60 Show progress bar (requires Term::ProgressBar)
61
62 =item B<--service,-s>
63
64 Postgreql service to use; defaults to debbugs
65
66 =item B<--sysconfdir,-c>
67
68 System configuration directory to use; if not set, defaults to the
69 postgresql default. [Operates by setting PGSYSCONFDIR]
70
71 =item B<--spool-dir>
72
73 Debbugs spool directory; defaults to the value configured in the
74 debbugs configuration file.
75
76 =item B<--verbose>
77
78 Output more information about what is happening. Probably not useful
79 if you also set --progress.
80
81 =item B<--debug, -d>
82
83 Debug verbosity.
84
85 =item B<--help, -h>
86
87 Display brief useage information.
88
89 =item B<--man, -m>
90
91 Display this manual.
92
93 =back
94
95
96 =cut
97
98
99 use vars qw($DEBUG);
100
101 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list getsourcemaintainers);
102 use Debbugs::Config qw(:config);
103 use Debbugs::Status qw(read_bug split_status_fields);
104 use Debbugs::Log;
105 use Debbugs::DB;
106 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue);
107 use DateTime;
108 use File::stat;
109
110
111 my %options =
112     (debug           => 0,
113      help            => 0,
114      man             => 0,
115      verbose         => 0,
116      quiet           => 0,
117      quick           => 0,
118      service         => $config{debbugs_db},
119      progress        => 0,
120     );
121
122 my $gop = Getopt::Long::Parser->new();
123 $gop->configure('pass_through');
124 $gop->getoptions(\%options,
125                  'quick|q',
126                  'service|s',
127                  'sysconfdir|c',
128                  'progress!',
129                  'spool_dir|spool-dir=s',
130                  'verbose|v+',
131                  'quiet+',
132                  'debug|d+','help|h|?','man|m');
133 $gop->getoptions('default');
134
135 pod2usage() if $options{help};
136 pod2usage({verbose=>2}) if $options{man};
137
138 $DEBUG = $options{debug};
139
140 my %subcommands =
141     ('bugs' => {function => \&add_bugs,
142                },
143      'versions' => {function => \&add_versions,
144                    },
145      'debinfo' => {function => \&add_debinfo,
146                   },
147      'maintainers' => {function => \&add_maintainers,
148                       },
149      'configuration' => {function => \&add_configuration,
150                         },
151      'logs' => {function => \&add_logs,
152                },
153      'help' => {function => sub {pod2usage({verbose => 2});}}
154     );
155
156 my @USAGE_ERRORS;
157 $options{verbose} = $options{verbose} - $options{quiet};
158
159 if ($options{progress}) {
160     eval "use Term::ProgressBar";
161     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
162 }
163
164
165 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
166
167 if (exists $options{sysconfdir}) {
168     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
169         delete $ENV{PGSYSCONFDIR};
170     } else {
171         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
172     }
173 }
174
175 if (exists $options{spool_dir} and defined $options{spool_dir}) {
176     $config{spool_dir} = $options{spool_dir};
177 }
178
179 my $prog_bar;
180 if ($options{progress}) {
181     $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
182     warn "Unable to initialize progress bar: $@" if not $prog_bar;
183 }
184
185
186 my ($subcommand) = shift @ARGV;
187
188 my $opts =
189     handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments},$gop);
190 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
191
192 sub add_bugs {
193     my ($options,$opts,$p,$config,$argv) = @_;
194     chdir($config->{spool_dir}) or
195         die "chdir $config->{spool_dir} failed: $!";
196
197     my $verbose = $options->{debug};
198
199     my $initialdir = "db-h";
200
201     if (defined $argv->[0] and $argv->[0] eq "archive") {
202         $initialdir = "archive";
203     }
204     my $s = db_connect($options);
205
206
207     my $time = 0;
208     my $start_time = time;
209
210
211     my @dirs = (@{$argv}?@{$argv} : $initialdir);
212     my $cnt = 0;
213     my %tags;
214     my %severities;
215     my %queue;
216     my $tot_dirs = @{$argv}? @{$argv} : 0;
217     my $done_dirs = 0;
218     my $avg_subfiles = 0;
219     my $completed_files = 0;
220     while (my $dir = shift @dirs) {
221         printf "Doing dir %s ...\n", $dir if $verbose;
222
223         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
224         my @subdirs = readdir(DIR);
225         closedir(DIR);
226
227         my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
228         $tot_dirs -= @dirs;
229         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
230         $tot_dirs += @dirs;
231         if ($avg_subfiles == 0) {
232             $avg_subfiles = @list;
233         }
234
235         $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
236         $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
237         $done_dirs += 1;
238
239         for my $bug (@list) {
240             $completed_files++;
241             $p->update($completed_files) if $p;
242             print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
243             my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
244             if (not defined $stat) {
245                 print STDERR "Unable to stat $bug $!\n";
246                 next;
247             }
248             next if $stat->mtime < $time;
249             my $data = read_bug(bug => $bug,
250                                 location => $initialdir);
251             eval {
252                 load_bug(db => $s,
253                          data => split_status_fields($data),
254                          tags => \%tags,
255                          severities => \%severities,
256                          queue => \%queue);
257             };
258             if ($@) {
259                 use Data::Dumper;
260                 print STDERR Dumper($data) if $DEBUG;
261                 die "failure while trying to load bug $bug\n$@";
262             }
263         }
264     }
265     $p->remove() if $p;
266     handle_load_bug_queue(db => $s,
267                           queue => \%queue);
268 }
269
270 sub add_versions {
271     my ($options,$opts,$p,$config,$argv) = @_;
272
273     my $s = db_connect($options);
274
275     my @files = @{$argv};
276     $p->target(scalar @files) if $p;
277     for my $file (@files) {
278         my $fh = IO::File->new($file,'r') or
279             die "Unable to open $file for reading: $!";
280         my @versions;
281         my %src_pkgs;
282         while (<$fh>) {
283             chomp;
284             next unless length $_;
285             if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
286                 push @versions, [$1,$2];
287             }
288         }
289         close($fh);
290         my $ancestor_sv;
291         for my $i (reverse 0..($#versions)) {
292             my $sp;
293             if (not defined $src_pkgs{$versions[$i][0]}) {
294                 $src_pkgs{$versions[$i][0]} =
295                     $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
296             }
297             $sp = $src_pkgs{$versions[$i][0]};
298             # There's probably something wrong if the source package
299             # doesn't exist, but we'll skip it for now
300             next unless defined $sp;
301             my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
302                                                     ver => $versions[$i][1],
303                                                    });
304             if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
305                 $sv->update({based_on => $ancestor_sv->id()})
306             }
307             $ancestor_sv = $sv;
308         }
309         $p->update() if $p;
310     }
311     $p->remove() if $p;
312 }
313
314 sub add_debinfo {
315     my ($options,$opts,$p,$config,$argv) = @_;
316
317     my @files = @{$argv};
318     return unless @files;
319     my $s = db_connect($options);
320     my %arch;
321     $p->target(scalar @files) if $p;
322     for my $file (@files) {
323         my $fh = IO::File->new($file,'r') or
324             die "Unable to open $file for reading: $!";
325         my $f_stat = stat($file);
326         while (<$fh>) {
327             chomp;
328             next unless length $_;
329             my ($binname, $binver, $binarch, $srcname, $srcver) = split;
330             # if $srcver is not defined, this is probably a broken
331             # .debinfo file [they were causing #686106, see commit
332             # 49c85ab8 in dak.] Basically, $binarch didn't get put into
333             # the file, so we'll fudge it from the filename.
334             if (not defined $srcver) {
335                 ($srcname,$srcver) = ($binarch,$srcname);
336                 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
337             }
338             my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
339             my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
340                                                               ver => $srcver});
341             my $arch;
342             if (defined $arch{$binarch}) {
343                 $arch = $arch{$binarch};
344             } else {
345                 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
346                 $arch{$binarch} = $arch;
347             }
348             my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
349             $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
350                                                      src_ver => $sv->id(),
351                                                      arch    => $arch->id(),
352                                                      ver        => $binver,
353                                                     });
354         }
355         $p->update() if $p;
356     }
357     $p->remove() if $p;
358 }
359
360 sub add_maintainers {
361     my ($options,$opts,$p,$config,$argv) = @_;
362
363     my $s = db_connect($options);
364     my $maintainers = getsourcemaintainers();
365     $p->target(scalar keys %{$maintainers}) if $p;
366     for my $pkg (keys %{$maintainers}) {
367         my $maint = $maintainers->{$pkg};
368         # see if a maintainer already exists; if so, we don't do
369         # anything here
370         my $maint_r = $s->resultset('Maintainer')->
371             find({name => $maint});
372         if (not defined $maint_r) {
373             # get e-mail address of maintainer
374             my $addr = getparsedaddrs($maint);
375             my $e_mail = $addr->address();
376             my $full_name = $addr->phrase();
377             $full_name =~ s/^\"|\"$//g;
378             $full_name =~ s/^\s+|\s+$//g;
379             # find correspondent
380             my $correspondent = $s->resultset('Correspondent')->
381                 find_or_create({addr => $e_mail});
382             if (length $full_name) {
383                 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
384                                                                         {full_name => $full_name}) if length $full_name;
385                 $c_full_name->update({last_seen => 'NOW()'});
386             }
387             $maint_r =
388                 $s->resultset('Maintainer')->
389                 find_or_create({name => $maint,
390                                 correspondent => $correspondent,
391                                });
392         }
393         # add the maintainer to the source package for packages with
394         # no maintainer
395         $s->txn_do(sub {
396                       $s->resultset('SrcPkg')->search({pkg => $pkg})->
397                           search_related_rs('src_vers',{ maintainer => undef})->
398                           update_all({maintainer => $maint_r->id()});
399                   });
400         $p->update() if $p;
401     }
402     $p->remove() if $p;
403 }
404
405 sub add_configuration {
406     my ($options,$opts,$p,$config,$argv) = @_;
407 }
408
409 sub add_logs {
410     my ($options,$opts,$p,$config,$argv) = @_;
411 }
412
413 sub handle_subcommand_arguments {
414     my ($argv,$args,$gop) = @_;
415     my $subopt = {};
416     $gop->getoptionsfromarray($argv,
417                               $subopt,
418                               keys %{$args},
419                              );
420     my @usage_errors;
421     for my $arg  (keys %{$args}) {
422         next unless $args->{$arg};
423         my $r_arg = $arg; # real argument name
424         $r_arg =~ s/[=\|].+//g;
425         if (not defined $subopt->{$r_arg}) {
426             push @usage_errors, "You must give a $r_arg option";
427         }
428     }
429     pod2usage(join("\n",@usage_errors)) if @usage_errors;
430     return $subopt;
431 }
432
433 sub get_lock{
434     my ($subcommand,$config,$options) = @_;
435     if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
436         if ($options->{quick}) {
437             # If this is a quick run, just exit
438             print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
439             exit 0;
440         }
441         print STDERR "Another debbugs-loadsql is running; stopping\n";
442         exit 1;
443     }
444 }
445
446 sub db_connect {
447     my ($options) = @_;
448     # connect to the database; figure out how to handle errors
449     # properly here.
450     my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
451         die "Unable to connect to database: ";
452 }
453
454
455
456 __END__