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();
391 ## get all of the maintainers, and add the missing ones
392 my $maints = $s->resultset('Maintainer')->
393 get_maintainers(values %{$maintainers});
395 my @svs = $s->resultset('SrcVer')->
396 search({maintainer => undef
399 group_by => 'me.src_pkg, src_pkg.pkg',
400 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
401 columns => [qw(me.src_pkg src_pkg.pkg)],
404 $p->target(2+@svs) if $p;
407 if (exists $maintainers->{$sv->{src_pkg}{pkg}}) {
408 my $pkg = $sv->{src_pkg}{pkg};
409 my $maint = $maints->
410 {$maintainers->{$pkg}};
411 $s->txn_do(sub {$s->resultset('SrcVer')->
412 search({maintainer => undef,
413 'src_pkg.pkg' => $pkg
416 )->update({maintainer => $maint})
424 sub add_configuration {
425 my ($options,$opts,$p,$config,$argv) = @_;
427 my $s = db_connect($options);
436 for my $sev_name (@{$config{severities}}) {
437 # add all severitites
438 my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
439 # mark strong severities
440 if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
446 $sev_names{$sev_name} = 1;
448 # mark obsolete severities
449 for my $sev ($s->resultset('Severity')->find()) {
450 next if exists $sev_names{$sev->severity()};
457 my ($options,$opts,$p,$config,$argv) = @_;
460 my $s = db_connect($options);
461 my $dist_dir = IO::Dir->new($opts->{ftpdists});
463 grep { $_ !~ /^\./ and
464 -d $opts->{ftpdists}.'/'.$_ and
465 not -l $opts->{ftpdists}.'/'.$_
467 while (my $dist = shift @dist_names) {
468 my $dist_dir = $opts->{ftpdists}.'/'.$dist;
469 my ($dist_info,$package_files) =
470 read_release_file($dist_dir.'/Release');
471 load_suite($s,$dist_info);
476 my ($options,$opts,$p,$config,$argv) = @_;
478 chdir($config->{spool_dir}) or
479 die "chdir $config->{spool_dir} failed: $!";
481 my $verbose = $options->{debug};
483 my $initialdir = "db-h";
485 if (defined $argv->[0] and $argv->[0] eq "archive") {
486 $initialdir = "archive";
488 my $s = db_connect($options);
492 my $start_time = time;
494 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
501 load_bug_log(db => $s,
505 die "failure while trying to load bug log $bug\n$@";
511 my ($options,$opts,$p,$config,$argv) = @_;
513 my $dist_dir = IO::Dir->new($opts->{ftpdists});
515 grep { $_ !~ /^\./ and
516 -d $opts->{ftpdists}.'/'.$_ and
517 not -l $opts->{ftpdists}.'/'.$_
520 while (my $dist = shift @dist_names) {
521 my $dist_dir = $opts->{ftpdists}.'/'.$dist;
522 my ($dist_info,$package_files) =
523 read_release_file($dist_dir.'/Release');
524 $s_p{$dist_info->{Codename}} = $package_files;
527 for my $suite (keys %s_p) {
528 for my $component (keys %{$s_p{$suite}}) {
529 $tot += scalar keys %{$s_p{$suite}{$component}};
532 $p->target($tot) if $p;
535 my $tot_suites = scalar keys %s_p;
537 my $completed_pkgs=0;
538 # parse packages files
539 for my $suite (keys %s_p) {
541 for my $component (keys %{$s_p{$suite}}) {
542 my @archs = keys %{$s_p{$suite}{$component}};
543 if (grep {$_ eq 'source'} @archs) {
544 @archs = ('source',grep {$_ ne 'source'} @archs);
546 for my $arch (@archs) {
547 my $pfh = open_compressed_file($s_p{$suite}{$component}{$arch}) or
548 die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
550 local $/ = ''; # paragraph mode
553 for my $field (qw(Package Maintainer Version Source)) {
554 /^\Q$field\E: (.*)/m;
557 next unless defined $pkg{Package} and
558 defined $pkg{Version};
559 push @pkgs,[$arch,$component,\%pkg];
563 my $s = db_connect($options);
567 $p->target($avg_pkgs*($tot_suites-$done_suites-1)+
568 $completed_pkgs+@pkgs) if $p;
573 $avg_pkgs=($avg_pkgs*$done_suites + @pkgs)/($done_suites+1);
574 $completed_pkgs += @pkgs;
580 sub handle_subcommand_arguments {
581 my ($argv,$args) = @_;
583 Getopt::Long::GetOptionsFromArray($argv,
588 for my $arg (keys %{$args}) {
589 next unless $args->{$arg};
590 my $r_arg = $arg; # real argument name
591 $r_arg =~ s/[=\|].+//g;
592 if (not defined $subopt->{$r_arg}) {
593 push @usage_errors, "You must give a $r_arg option";
596 pod2usage(join("\n",@usage_errors)) if @usage_errors;
601 my ($subcommand,$config,$options) = @_;
602 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
603 if ($options->{quick}) {
604 # If this is a quick run, just exit
605 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
608 print STDERR "Another debbugs-loadsql is running; stopping\n";
615 # connect to the database; figure out how to handle errors
617 my $s = Debbugs::DB->connect($options->{service}) or
618 die "Unable to connect to database: ";
621 sub open_compressed_file {
624 my $mode = '<:encoding(UTF-8)';
626 if ($file =~ /\.gz$/) {
627 $mode = '-|:encoding(UTF-8)';
628 push @opts,'gzip','-dc';
630 if ($file =~ /\.xz$/) {
631 $mode = '-|:encoding(UTF-8)';
632 push @opts,'xz','-dc';
634 if ($file =~ /\.bz2$/) {
635 $mode = '-|:encoding(UTF-8)';
636 push @opts,'bzip2','-dc';
638 open($fh,$mode,@opts,$file);
642 sub read_release_file {
645 my $rfh = open_compressed_file($file) or
646 die "Unable to open $file for reading: $!";
652 if (s/^(\S+):\s*//) {
653 if ($1 eq 'SHA1'or $1 eq 'SHA256') {
660 my ($sha,$size,$f) = split /\s+/,$_;
661 next unless $f =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
662 next unless $f =~ m{^([^/]+)/([^/]+)/([^/]+)$};
663 my ($component,$arch,$package_source) = ($1,$2,$3);
664 $arch =~ s/binary-//;
665 next if exists $p_f{$component}{$arch};
666 $p_f{$component}{$arch} = File::Spec->catfile(dirname($file),$f);
669 return (\%dist_info,\%p_f);
673 my ($dirs,$p,$what,$verbose,$sub) = @_;
675 my $tot_dirs = @dirs;
677 my $avg_subfiles = 0;
678 my $completed_files = 0;
679 while (my $dir = shift @dirs) {
680 printf "Doing dir %s ...\n", $dir if $verbose;
682 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
683 my @subdirs = readdir(DIR);
686 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
688 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
690 if ($avg_subfiles == 0) {
691 $avg_subfiles = @list;
694 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
695 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
698 for my $bug (@list) {
700 $p->update($completed_files) if $p;
701 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);