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>.
11 use Getopt::Long qw(:config no_ignore_case);
16 debbugs-loadsql -- load debbugs sql database
20 debbugs-loadsql [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
48 Add source maintainers
56 Only load changed bugs
60 Show progress bar (requires Term::ProgressBar)
64 Postgreql service to use; defaults to debbugs
66 =item B<--sysconfdir,-c>
68 System configuration directory to use; if not set, defaults to the
69 postgresql default. [Operates by setting PGSYSCONFDIR]
73 Debbugs spool directory; defaults to the value configured in the
74 debbugs configuration file.
78 Output more information about what is happening. Probably not useful
79 if you also set --progress.
87 Display brief useage information.
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);
106 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue);
118 service => $config{debbugs_db},
122 Getopt::Long::Configure('pass_through');
123 GetOptions(\%options,
128 'spool_dir|spool-dir=s',
131 'debug|d+','help|h|?','man|m');
132 Getopt::Long::Configure('default');
134 pod2usage() if $options{help};
135 pod2usage({verbose=>2}) if $options{man};
137 $DEBUG = $options{debug};
140 ('bugs' => {function => \&add_bugs,
142 'versions' => {function => \&add_versions,
144 'debinfo' => {function => \&add_debinfo,
146 'maintainers' => {function => \&add_maintainers,
148 'configuration' => {function => \&add_configuration,
150 'logs' => {function => \&add_logs,
152 'help' => {function => sub {pod2usage({verbose => 2});}}
156 $options{verbose} = $options{verbose} - $options{quiet};
158 if ($options{progress}) {
159 eval "use Term::ProgressBar";
160 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
164 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
166 if (exists $options{sysconfdir}) {
167 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
168 delete $ENV{PGSYSCONFDIR};
170 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
174 if (exists $options{spool_dir} and defined $options{spool_dir}) {
175 $config{spool_dir} = $options{spool_dir};
179 if ($options{progress}) {
180 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
181 warn "Unable to initialize progress bar: $@" if not $prog_bar;
185 my ($subcommand) = shift @ARGV;
186 if (not defined $subcommand) {
187 $subcommand = 'help';
188 print STDERR "You must provide a subcommand; displaying usage.\n";
190 } elsif (not exists $subcommands{$subcommand}) {
191 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
196 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
197 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
200 my ($options,$opts,$p,$config,$argv) = @_;
201 chdir($config->{spool_dir}) or
202 die "chdir $config->{spool_dir} failed: $!";
204 my $verbose = $options->{debug};
206 my $initialdir = "db-h";
208 if (defined $argv->[0] and $argv->[0] eq "archive") {
209 $initialdir = "archive";
211 my $s = db_connect($options);
215 my $start_time = time;
218 my @dirs = (@{$argv}?@{$argv} : $initialdir);
223 my $tot_dirs = @{$argv}? @{$argv} : 0;
225 my $avg_subfiles = 0;
226 my $completed_files = 0;
227 while (my $dir = shift @dirs) {
228 printf "Doing dir %s ...\n", $dir if $verbose;
230 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
231 my @subdirs = readdir(DIR);
234 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
236 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
238 if ($avg_subfiles == 0) {
239 $avg_subfiles = @list;
242 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
243 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
246 for my $bug (@list) {
248 $p->update($completed_files) if $p;
249 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
250 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
251 if (not defined $stat) {
252 print STDERR "Unable to stat $bug $!\n";
255 if ($options{quick}) {
256 my $rs = $s->resultset('Bug')->search({bug=>$bug})->single();
257 next if defined $rs and $stat->mtime < $rs->last_modified()->epoch();
259 my $data = read_bug(bug => $bug,
260 location => $initialdir);
263 data => split_status_fields($data),
265 severities => \%severities,
270 print STDERR Dumper($data) if $DEBUG;
271 die "failure while trying to load bug $bug\n$@";
276 handle_load_bug_queue(db => $s,
281 my ($options,$opts,$p,$config,$argv) = @_;
283 my $s = db_connect($options);
285 my @files = @{$argv};
286 $p->target(scalar @files) if $p;
287 for my $file (@files) {
288 my $fh = IO::File->new($file,'r') or
289 die "Unable to open $file for reading: $!";
294 next unless length $_;
295 if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
296 push @versions, [$1,$2];
301 for my $i (reverse 0..($#versions)) {
303 if (not defined $src_pkgs{$versions[$i][0]}) {
304 $src_pkgs{$versions[$i][0]} =
305 $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
307 $sp = $src_pkgs{$versions[$i][0]};
308 # There's probably something wrong if the source package
309 # doesn't exist, but we'll skip it for now
310 next unless defined $sp;
311 my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
312 ver => $versions[$i][1],
314 if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
315 $sv->update({based_on => $ancestor_sv->id()})
325 my ($options,$opts,$p,$config,$argv) = @_;
327 my @files = @{$argv};
328 return unless @files;
329 my $s = db_connect($options);
331 $p->target(scalar @files) if $p;
332 for my $file (@files) {
333 my $fh = IO::File->new($file,'r') or
334 die "Unable to open $file for reading: $!";
335 my $f_stat = stat($file);
338 next unless length $_;
339 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
340 # if $srcver is not defined, this is probably a broken
341 # .debinfo file [they were causing #686106, see commit
342 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
343 # the file, so we'll fudge it from the filename.
344 if (not defined $srcver) {
345 ($srcname,$srcver) = ($binarch,$srcname);
346 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
348 my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
349 my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
352 if (defined $arch{$binarch}) {
353 $arch = $arch{$binarch};
355 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
356 $arch{$binarch} = $arch;
358 my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
359 $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
360 src_ver => $sv->id(),
370 sub add_maintainers {
371 my ($options,$opts,$p,$config,$argv) = @_;
373 my $s = db_connect($options);
374 my $maintainers = getsourcemaintainers();
375 $p->target(scalar keys %{$maintainers}) if $p;
376 for my $pkg (keys %{$maintainers}) {
377 my $maint = $maintainers->{$pkg};
378 # see if a maintainer already exists; if so, we don't do
380 my $maint_r = $s->resultset('Maintainer')->
381 find({name => $maint});
382 if (not defined $maint_r) {
383 # get e-mail address of maintainer
384 my $addr = getparsedaddrs($maint);
385 my $e_mail = $addr->address();
386 my $full_name = $addr->phrase();
387 $full_name =~ s/^\"|\"$//g;
388 $full_name =~ s/^\s+|\s+$//g;
390 my $correspondent = $s->resultset('Correspondent')->
391 find_or_create({addr => $e_mail});
392 if (length $full_name) {
393 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
394 {full_name => $full_name}) if length $full_name;
395 $c_full_name->update({last_seen => 'NOW()'});
398 $s->resultset('Maintainer')->
399 find_or_create({name => $maint,
400 correspondent => $correspondent,
403 # add the maintainer to the source package for packages with
406 $s->resultset('SrcPkg')->search({pkg => $pkg})->
407 search_related_rs('src_vers',{ maintainer => undef})->
408 update_all({maintainer => $maint_r->id()});
415 sub add_configuration {
416 my ($options,$opts,$p,$config,$argv) = @_;
420 my ($options,$opts,$p,$config,$argv) = @_;
423 sub handle_subcommand_arguments {
424 my ($argv,$args) = @_;
426 Getopt::Long::GetOptionsFromArray($argv,
431 for my $arg (keys %{$args}) {
432 next unless $args->{$arg};
433 my $r_arg = $arg; # real argument name
434 $r_arg =~ s/[=\|].+//g;
435 if (not defined $subopt->{$r_arg}) {
436 push @usage_errors, "You must give a $r_arg option";
439 pod2usage(join("\n",@usage_errors)) if @usage_errors;
444 my ($subcommand,$config,$options) = @_;
445 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
446 if ($options->{quick}) {
447 # If this is a quick run, just exit
448 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
451 print STDERR "Another debbugs-loadsql is running; stopping\n";
458 # connect to the database; figure out how to handle errors
460 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
461 die "Unable to connect to database: ";