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 'suites' => {function => \&add_suites,
152 'logs' => {function => \&add_logs,
154 'help' => {function => sub {pod2usage({verbose => 2});}}
158 $options{verbose} = $options{verbose} - $options{quiet};
160 if ($options{progress}) {
161 eval "use Term::ProgressBar";
162 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
166 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
168 if (exists $options{sysconfdir}) {
169 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
170 delete $ENV{PGSYSCONFDIR};
172 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
176 if (exists $options{spool_dir} and defined $options{spool_dir}) {
177 $config{spool_dir} = $options{spool_dir};
181 if ($options{progress}) {
182 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
183 warn "Unable to initialize progress bar: $@" if not $prog_bar;
187 my ($subcommand) = shift @ARGV;
188 if (not defined $subcommand) {
189 $subcommand = 'help';
190 print STDERR "You must provide a subcommand; displaying usage.\n";
192 } elsif (not exists $subcommands{$subcommand}) {
193 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
198 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
199 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
202 my ($options,$opts,$p,$config,$argv) = @_;
203 chdir($config->{spool_dir}) or
204 die "chdir $config->{spool_dir} failed: $!";
206 my $verbose = $options->{debug};
208 my $initialdir = "db-h";
210 if (defined $argv->[0] and $argv->[0] eq "archive") {
211 $initialdir = "archive";
213 my $s = db_connect($options);
217 my $start_time = time;
222 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
228 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
229 if (not defined $stat) {
230 print STDERR "Unable to stat $bug $!\n";
233 if ($options{quick}) {
234 my $rs = $s->resultset('Bug')->search({bug=>$bug})->single();
235 next if defined $rs and $stat->mtime < $rs->last_modified()->epoch();
237 my $data = read_bug(bug => $bug,
238 location => $initialdir);
241 data => split_status_fields($data),
243 severities => \%severities,
248 print STDERR Dumper($data) if $DEBUG;
249 die "failure while trying to load bug $bug\n$@";
253 handle_load_bug_queue(db => $s,
258 my ($options,$opts,$p,$config,$argv) = @_;
260 my $s = db_connect($options);
262 my @files = @{$argv};
263 $p->target(scalar @files) if $p;
264 for my $file (@files) {
265 my $fh = IO::File->new($file,'r') or
266 die "Unable to open $file for reading: $!";
271 next unless length $_;
272 if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
273 push @versions, [$1,$2];
278 for my $i (reverse 0..($#versions)) {
280 if (not defined $src_pkgs{$versions[$i][0]}) {
281 $src_pkgs{$versions[$i][0]} =
282 $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
284 $sp = $src_pkgs{$versions[$i][0]};
285 # There's probably something wrong if the source package
286 # doesn't exist, but we'll skip it for now
287 next unless defined $sp;
288 my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
289 ver => $versions[$i][1],
291 if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
292 $sv->update({based_on => $ancestor_sv->id()})
302 my ($options,$opts,$p,$config,$argv) = @_;
304 my @files = @{$argv};
305 return unless @files;
306 my $s = db_connect($options);
308 $p->target(scalar @files) if $p;
309 for my $file (@files) {
310 my $fh = IO::File->new($file,'r') or
311 die "Unable to open $file for reading: $!";
312 my $f_stat = stat($file);
315 next unless length $_;
316 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
317 # if $srcver is not defined, this is probably a broken
318 # .debinfo file [they were causing #686106, see commit
319 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
320 # the file, so we'll fudge it from the filename.
321 if (not defined $srcver) {
322 ($srcname,$srcver) = ($binarch,$srcname);
323 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
325 my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
326 # update the creation date if the data we have is earlier
327 my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
328 if ($ct_date < $sp->creation) {
329 $sp->creation($ct_date);
330 $sp->last_modified(DateTime->now);
333 my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
335 if (not defined $sv->upload_date() or $ct_date < $sv->upload_date()) {
336 $sv->upload_date($ct_date);
340 if (defined $arch{$binarch}) {
341 $arch = $arch{$binarch};
343 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
344 $arch{$binarch} = $arch;
346 my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
347 $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
348 src_ver => $sv->id(),
358 sub add_maintainers {
359 my ($options,$opts,$p,$config,$argv) = @_;
361 my $s = db_connect($options);
362 my $maintainers = getsourcemaintainers();
363 $p->target(scalar keys %{$maintainers}) if $p;
364 for my $pkg (keys %{$maintainers}) {
365 my $maint = $maintainers->{$pkg};
366 # see if a maintainer already exists; if so, we don't do
368 my $maint_r = $s->resultset('Maintainer')->
369 find({name => $maint});
370 if (not defined $maint_r) {
371 # get e-mail address of maintainer
372 my $addr = getparsedaddrs($maint);
373 my $e_mail = $addr->address();
374 my $full_name = $addr->phrase();
375 $full_name =~ s/^\"|\"$//g;
376 $full_name =~ s/^\s+|\s+$//g;
378 my $correspondent = $s->resultset('Correspondent')->
379 find_or_create({addr => $e_mail});
380 if (length $full_name) {
381 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
382 {full_name => $full_name}) if length $full_name;
383 $c_full_name->update({last_seen => 'NOW()'});
386 $s->resultset('Maintainer')->
387 find_or_create({name => $maint,
388 correspondent => $correspondent,
391 # add the maintainer to the source package for packages with
394 $s->resultset('SrcPkg')->search({pkg => $pkg})->
395 search_related_rs('src_vers',{ maintainer => undef})->
396 update_all({maintainer => $maint_r->id()});
403 sub add_configuration {
404 my ($options,$opts,$p,$config,$argv) = @_;
406 my $s = db_connect($options);
415 for my $sev_name (@{$config{severities}}) {
416 # add all severitites
417 my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
418 # mark strong severities
419 if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
425 $sev_names{$sev_name} = 1;
427 # mark obsolete severities
428 for my $sev ($s->resultset('Severity')->find()) {
429 next if exists $sev_names{$sev->severity()};
436 my ($options,$opts,$p,$config,$argv) = @_;
438 die "add_suite is currently not implemented; modify suites manually using SQL."
442 my ($options,$opts,$p,$config,$argv) = @_;
444 chdir($config->{spool_dir}) or
445 die "chdir $config->{spool_dir} failed: $!";
447 my $verbose = $options->{debug};
449 my $initialdir = "db-h";
451 if (defined $argv->[0] and $argv->[0] eq "archive") {
452 $initialdir = "archive";
454 my $s = db_connect($options);
458 my $start_time = time;
460 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
467 load_bug_log(db => $s,
471 die "failure while trying to load bug log $bug\n$@";
480 sub handle_subcommand_arguments {
481 my ($argv,$args) = @_;
483 Getopt::Long::GetOptionsFromArray($argv,
488 for my $arg (keys %{$args}) {
489 next unless $args->{$arg};
490 my $r_arg = $arg; # real argument name
491 $r_arg =~ s/[=\|].+//g;
492 if (not defined $subopt->{$r_arg}) {
493 push @usage_errors, "You must give a $r_arg option";
496 pod2usage(join("\n",@usage_errors)) if @usage_errors;
501 my ($subcommand,$config,$options) = @_;
502 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
503 if ($options->{quick}) {
504 # If this is a quick run, just exit
505 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
508 print STDERR "Another debbugs-loadsql is running; stopping\n";
515 # connect to the database; figure out how to handle errors
517 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
518 die "Unable to connect to database: ";
522 my ($dirs,$p,$what,$verbose,$sub) = @_;
524 my $tot_dirs = @dirs;
526 my $avg_subfiles = 0;
527 my $completed_files = 0;
528 while (my $dir = shift @dirs) {
529 printf "Doing dir %s ...\n", $dir if $verbose;
531 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
532 my @subdirs = readdir(DIR);
535 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
537 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
539 if ($avg_subfiles == 0) {
540 $avg_subfiles = @list;
543 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
544 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
547 for my $bug (@list) {
549 $p->update($completed_files) if $p;
550 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);