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),
103 use Debbugs::Config qw(:config);
104 use Debbugs::Status qw(read_bug split_status_fields);
107 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue :load_package :load_suite);
114 use IO::Uncompress::AnyUncompress;
115 use Encode qw(decode_utf8);
124 service => $config{debbugs_db},
128 Getopt::Long::Configure('pass_through');
129 GetOptions(\%options,
134 'spool_dir|spool-dir=s',
137 'debug|d+','help|h|?','man|m');
138 Getopt::Long::Configure('default');
140 pod2usage() if $options{help};
141 pod2usage({verbose=>2}) if $options{man};
143 $DEBUG = $options{debug};
146 ('bugs' => {function => \&add_bugs,
148 'versions' => {function => \&add_versions,
150 'debinfo' => {function => \&add_debinfo,
151 arguments => {'0|null' => 0},
153 'maintainers' => {function => \&add_maintainers,
155 'configuration' => {function => \&add_configuration,
157 'suites' => {function => \&add_suite,
158 arguments => {'ftpdists=s' => 1,
161 'logs' => {function => \&add_logs,
163 'packages' => {function => \&add_packages,
164 arguments => {'ftpdists=s' => 1,
168 'help' => {function => sub {pod2usage({verbose => 2});}}
172 $options{verbose} = $options{verbose} - $options{quiet};
174 if ($options{progress}) {
175 eval "use Term::ProgressBar";
176 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
180 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
182 if (exists $options{sysconfdir}) {
183 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
184 delete $ENV{PGSYSCONFDIR};
186 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
190 if (exists $options{spool_dir} and defined $options{spool_dir}) {
191 $config{spool_dir} = $options{spool_dir};
195 if ($options{progress}) {
196 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
197 warn "Unable to initialize progress bar: $@" if not $prog_bar;
201 my ($subcommand) = shift @ARGV;
202 if (not defined $subcommand) {
203 $subcommand = 'help';
204 print STDERR "You must provide a subcommand; displaying usage.\n";
206 } elsif (not exists $subcommands{$subcommand}) {
207 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
211 binmode(STDOUT,':encoding(UTF-8)');
212 binmode(STDERR,':encoding(UTF-8)');
215 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
216 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
219 my ($options,$opts,$p,$config,$argv) = @_;
220 chdir($config->{spool_dir}) or
221 die "chdir $config->{spool_dir} failed: $!";
223 my $verbose = $options->{debug};
225 my $initialdir = "db-h";
227 if (defined $argv->[0] and $argv->[0] eq "archive") {
228 $initialdir = "archive";
230 my $s = db_connect($options);
234 my $start_time = time;
239 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
245 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
246 if (not defined $stat) {
247 print STDERR "Unable to stat $bug $!\n";
250 if ($options{quick}) {
251 my $rs = $s->resultset('Bug')->search({bug=>$bug})->single();
252 next if defined $rs and $stat->mtime < $rs->last_modified()->epoch();
254 my $data = read_bug(bug => $bug,
255 location => $initialdir);
258 data => split_status_fields($data),
260 severities => \%severities,
265 print STDERR Dumper($data) if $DEBUG;
266 die "failure while trying to load bug $bug\n$@";
270 handle_load_bug_queue(db => $s,
275 my ($options,$opts,$p,$config,$argv) = @_;
277 my $s = db_connect($options);
279 my @files = @{$argv};
280 $p->target(scalar @files) if $p;
281 for my $file (@files) {
282 my $fh = IO::File->new($file,'r') or
283 die "Unable to open $file for reading: $!";
288 next unless length $_;
289 if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
290 push @versions, [$1,$2];
295 for my $i (reverse 0..($#versions)) {
297 if (not defined $src_pkgs{$versions[$i][0]}) {
298 $src_pkgs{$versions[$i][0]} =
299 $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
301 $sp = $src_pkgs{$versions[$i][0]};
302 # There's probably something wrong if the source package
303 # doesn't exist, but we'll skip it for now
304 next unless defined $sp;
305 my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
306 ver => $versions[$i][1],
308 if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
309 $sv->update({based_on => $ancestor_sv->id()})
319 my ($options,$opts,$p,$config,$argv) = @_;
321 my @files = @{$argv};
332 return unless @files;
333 my $s = db_connect($options);
335 $p->target(scalar @files) if $p;
336 for my $file (@files) {
337 my $fh = IO::File->new($file,'r') or
338 die "Unable to open $file for reading: $!";
339 my $f_stat = stat($file);
342 next unless length $_;
343 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
344 # if $srcver is not defined, this is probably a broken
345 # .debinfo file [they were causing #686106, see commit
346 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
347 # the file, so we'll fudge it from the filename.
348 if (not defined $srcver) {
349 ($srcname,$srcver) = ($binarch,$srcname);
350 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
352 my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
353 # update the creation date if the data we have is earlier
354 my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
355 if ($ct_date < $sp->creation) {
356 $sp->creation($ct_date);
357 $sp->last_modified(DateTime->now);
360 my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
362 if (not defined $sv->upload_date() or $ct_date < $sv->upload_date()) {
363 $sv->upload_date($ct_date);
367 if (defined $arch{$binarch}) {
368 $arch = $arch{$binarch};
370 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
371 $arch{$binarch} = $arch;
373 my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
374 $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
375 src_ver => $sv->id(),
385 sub add_maintainers {
386 my ($options,$opts,$p,$config,$argv) = @_;
388 my $s = db_connect($options);
389 my $maintainers = getsourcemaintainers();
390 $p->target(scalar keys %{$maintainers}) if $p;
391 for my $pkg (keys %{$maintainers}) {
392 my $maint = $maintainers->{$pkg};
393 # see if a maintainer already exists; if so, we don't do
395 my $maint_r = $s->resultset('Maintainer')->
396 find({name => $maint});
397 if (not defined $maint_r) {
398 # get e-mail address of maintainer
399 my $addr = getparsedaddrs($maint);
400 my $e_mail = $addr->address();
401 my $full_name = $addr->phrase();
402 $full_name =~ s/^\"|\"$//g;
403 $full_name =~ s/^\s+|\s+$//g;
405 my $correspondent = $s->resultset('Correspondent')->
406 find_or_create({addr => $e_mail});
407 if (length $full_name) {
408 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
409 {full_name => $full_name}) if length $full_name;
410 $c_full_name->update({last_seen => 'NOW()'});
413 $s->resultset('Maintainer')->
414 find_or_create({name => $maint,
415 correspondent => $correspondent,
418 # add the maintainer to the source package for packages with
421 $s->resultset('SrcPkg')->search({pkg => $pkg})->
422 search_related_rs('src_vers',{ maintainer => undef})->
423 update_all({maintainer => $maint_r->id()});
430 sub add_configuration {
431 my ($options,$opts,$p,$config,$argv) = @_;
433 my $s = db_connect($options);
442 for my $sev_name (@{$config{severities}}) {
443 # add all severitites
444 my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
445 # mark strong severities
446 if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
452 $sev_names{$sev_name} = 1;
454 # mark obsolete severities
455 for my $sev ($s->resultset('Severity')->find()) {
456 next if exists $sev_names{$sev->severity()};
463 my ($options,$opts,$p,$config,$argv) = @_;
466 my $s = db_connect($options);
467 my $dist_dir = IO::Dir->new($opts->{ftpdists});
469 grep { $_ !~ /^\./ and
470 -d $opts->{ftpdists}.'/'.$_ and
471 not -l $opts->{ftpdists}.'/'.$_
473 while (my $dist = shift @dist_names) {
474 my $dist_dir = $opts->{ftpdists}.'/'.$dist;
475 my ($dist_info,$package_files) =
476 read_release_file($dist_dir.'/Release');
477 load_suite($s,$dist_info);
482 my ($options,$opts,$p,$config,$argv) = @_;
484 chdir($config->{spool_dir}) or
485 die "chdir $config->{spool_dir} failed: $!";
487 my $verbose = $options->{debug};
489 my $initialdir = "db-h";
491 if (defined $argv->[0] and $argv->[0] eq "archive") {
492 $initialdir = "archive";
494 my $s = db_connect($options);
498 my $start_time = time;
500 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
507 load_bug_log(db => $s,
511 die "failure while trying to load bug log $bug\n$@";
517 my ($options,$opts,$p,$config,$argv) = @_;
519 my $dist_dir = IO::Dir->new($opts->{ftpdists});
521 grep { $_ !~ /^\./ and
522 -d $opts->{ftpdists}.'/'.$_ and
523 not -l $opts->{ftpdists}.'/'.$_
526 while (my $dist = shift @dist_names) {
527 my $dist_dir = $opts->{ftpdists}.'/'.$dist;
528 my ($dist_info,$package_files) =
529 read_release_file($dist_dir.'/Release');
530 $s_p{$dist_info->{Codename}} = $package_files;
533 for my $suite (keys %s_p) {
534 for my $component (keys %{$s_p{$suite}}) {
535 $tot += scalar keys %{$s_p{$suite}{$component}};
538 $p->target($tot) if $p;
541 my $tot_suites = scalar keys %s_p;
543 my $completed_pkgs=0;
544 # parse packages files
545 for my $suite (keys %s_p) {
547 for my $component (keys %{$s_p{$suite}}) {
548 my @archs = keys %{$s_p{$suite}{$component}};
549 if (grep {$_ eq 'source'} @archs) {
550 @archs = ('source',grep {$_ ne 'source'} @archs);
552 for my $arch (@archs) {
553 my $pfh = open_compressed_file($s_p{$suite}{$component}{$arch}) or
554 die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
556 local $/ = ''; # paragraph mode
559 for my $field (qw(Package Maintainer Version Source)) {
560 /^\Q$field\E: (.*)/m;
563 next unless defined $pkg{Package} and
564 defined $pkg{Version};
565 push @pkgs,[$arch,$component,\%pkg];
569 my $s = db_connect($options);
573 $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
574 $completed_pkgs+@pkgs) if $p;
579 $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
580 $completed_pkgs += @pkgs;
586 sub handle_subcommand_arguments {
587 my ($argv,$args) = @_;
589 Getopt::Long::GetOptionsFromArray($argv,
594 for my $arg (keys %{$args}) {
595 next unless $args->{$arg};
596 my $r_arg = $arg; # real argument name
597 $r_arg =~ s/[=\|].+//g;
598 if (not defined $subopt->{$r_arg}) {
599 push @usage_errors, "You must give a $r_arg option";
602 pod2usage(join("\n",@usage_errors)) if @usage_errors;
607 my ($subcommand,$config,$options) = @_;
608 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
609 if ($options->{quick}) {
610 # If this is a quick run, just exit
611 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
614 print STDERR "Another debbugs-loadsql is running; stopping\n";
621 # connect to the database; figure out how to handle errors
623 my $s = Debbugs::DB->connect($options->{service}) or
624 die "Unable to connect to database: ";
627 sub open_compressed_file {
630 my $mode = '<:encoding(UTF-8)';
632 if ($file =~ /\.gz$/) {
633 $mode = '-|:encoding(UTF-8)';
634 push @opts,'gzip','-dc';
636 if ($file =~ /\.xz$/) {
637 $mode = '-|:encoding(UTF-8)';
638 push @opts,'xz','-dc';
640 if ($file =~ /\.bz2$/) {
641 $mode = '-|:encoding(UTF-8)';
642 push @opts,'bzip2','-dc';
644 open($fh,$mode,@opts,$file);
648 sub read_release_file {
651 my $rfh = open_compressed_file($file) or
652 die "Unable to open $file for reading: $!";
658 if (s/^(\S+):\s*//) {
659 if ($1 eq 'SHA1'or $1 eq 'SHA256') {
666 my ($sha,$size,$f) = split /\s+/,$_;
667 next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
668 next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
669 my ($component,$arch,$package_source) = ($1,$2,$3);
670 $arch =~ s/binary-//;
671 next if exists $p_f{$component}{$arch};
672 $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
675 return (\%dist_info,\%p_f);
679 my ($dirs,$p,$what,$verbose,$sub) = @_;
681 my $tot_dirs = @dirs;
683 my $avg_subfiles = 0;
684 my $completed_files = 0;
685 while (my $dir = shift @dirs) {
686 printf "Doing dir %s ...\n", $dir if $verbose;
688 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
689 my @subdirs = readdir(DIR);
692 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
694 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
696 if ($avg_subfiles == 0) {
697 $avg_subfiles = @list;
700 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
701 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
704 for my $bug (@list) {
706 $p->update($completed_files) if $p;
707 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);