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 :load_package);
110 use IO::Uncompress::AnyUncompress;
119 service => $config{debbugs_db},
123 Getopt::Long::Configure('pass_through');
124 GetOptions(\%options,
129 'spool_dir|spool-dir=s',
132 'debug|d+','help|h|?','man|m');
133 Getopt::Long::Configure('default');
135 pod2usage() if $options{help};
136 pod2usage({verbose=>2}) if $options{man};
138 $DEBUG = $options{debug};
141 ('bugs' => {function => \&add_bugs,
143 'versions' => {function => \&add_versions,
145 'debinfo' => {function => \&add_debinfo,
146 arguments => {'0|null' => 0},
148 'maintainers' => {function => \&add_maintainers,
150 'configuration' => {function => \&add_configuration,
152 'suites' => {function => \&add_suites,
154 'logs' => {function => \&add_logs,
156 'packages' => {function => \&add_packages,
157 arguments => {'ftpdists=s' => 1,
160 'help' => {function => sub {pod2usage({verbose => 2});}}
164 $options{verbose} = $options{verbose} - $options{quiet};
166 if ($options{progress}) {
167 eval "use Term::ProgressBar";
168 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
172 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
174 if (exists $options{sysconfdir}) {
175 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
176 delete $ENV{PGSYSCONFDIR};
178 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
182 if (exists $options{spool_dir} and defined $options{spool_dir}) {
183 $config{spool_dir} = $options{spool_dir};
187 if ($options{progress}) {
188 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
189 warn "Unable to initialize progress bar: $@" if not $prog_bar;
193 my ($subcommand) = shift @ARGV;
194 if (not defined $subcommand) {
195 $subcommand = 'help';
196 print STDERR "You must provide a subcommand; displaying usage.\n";
198 } elsif (not exists $subcommands{$subcommand}) {
199 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
204 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
205 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
208 my ($options,$opts,$p,$config,$argv) = @_;
209 chdir($config->{spool_dir}) or
210 die "chdir $config->{spool_dir} failed: $!";
212 my $verbose = $options->{debug};
214 my $initialdir = "db-h";
216 if (defined $argv->[0] and $argv->[0] eq "archive") {
217 $initialdir = "archive";
219 my $s = db_connect($options);
223 my $start_time = time;
228 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
234 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
235 if (not defined $stat) {
236 print STDERR "Unable to stat $bug $!\n";
239 if ($options{quick}) {
240 my $rs = $s->resultset('Bug')->search({bug=>$bug})->single();
241 next if defined $rs and $stat->mtime < $rs->last_modified()->epoch();
243 my $data = read_bug(bug => $bug,
244 location => $initialdir);
247 data => split_status_fields($data),
249 severities => \%severities,
254 print STDERR Dumper($data) if $DEBUG;
255 die "failure while trying to load bug $bug\n$@";
259 handle_load_bug_queue(db => $s,
264 my ($options,$opts,$p,$config,$argv) = @_;
266 my $s = db_connect($options);
268 my @files = @{$argv};
269 $p->target(scalar @files) if $p;
270 for my $file (@files) {
271 my $fh = IO::File->new($file,'r') or
272 die "Unable to open $file for reading: $!";
277 next unless length $_;
278 if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
279 push @versions, [$1,$2];
284 for my $i (reverse 0..($#versions)) {
286 if (not defined $src_pkgs{$versions[$i][0]}) {
287 $src_pkgs{$versions[$i][0]} =
288 $s->resultset('SrcPkg')->find_or_create({pkg => $versions[$i][0]});
290 $sp = $src_pkgs{$versions[$i][0]};
291 # There's probably something wrong if the source package
292 # doesn't exist, but we'll skip it for now
293 next unless defined $sp;
294 my $sv = $s->resultset('SrcVer')->find({src_pkg=>$sp->id(),
295 ver => $versions[$i][1],
297 if (defined $ancestor_sv and defined $sv and not defined $sv->based_on()) {
298 $sv->update({based_on => $ancestor_sv->id()})
308 my ($options,$opts,$p,$config,$argv) = @_;
310 my @files = @{$argv};
321 return unless @files;
322 my $s = db_connect($options);
324 $p->target(scalar @files) if $p;
325 for my $file (@files) {
326 my $fh = IO::File->new($file,'r') or
327 die "Unable to open $file for reading: $!";
328 my $f_stat = stat($file);
331 next unless length $_;
332 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
333 # if $srcver is not defined, this is probably a broken
334 # .debinfo file [they were causing #686106, see commit
335 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
336 # the file, so we'll fudge it from the filename.
337 if (not defined $srcver) {
338 ($srcname,$srcver) = ($binarch,$srcname);
339 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
341 my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
342 # update the creation date if the data we have is earlier
343 my $ct_date = DateTime->from_epoch(epoch => $f_stat->ctime);
344 if ($ct_date < $sp->creation) {
345 $sp->creation($ct_date);
346 $sp->last_modified(DateTime->now);
349 my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
351 if (not defined $sv->upload_date() or $ct_date < $sv->upload_date()) {
352 $sv->upload_date($ct_date);
356 if (defined $arch{$binarch}) {
357 $arch = $arch{$binarch};
359 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
360 $arch{$binarch} = $arch;
362 my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
363 $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
364 src_ver => $sv->id(),
374 sub add_maintainers {
375 my ($options,$opts,$p,$config,$argv) = @_;
377 my $s = db_connect($options);
378 my $maintainers = getsourcemaintainers();
379 $p->target(scalar keys %{$maintainers}) if $p;
380 for my $pkg (keys %{$maintainers}) {
381 my $maint = $maintainers->{$pkg};
382 # see if a maintainer already exists; if so, we don't do
384 my $maint_r = $s->resultset('Maintainer')->
385 find({name => $maint});
386 if (not defined $maint_r) {
387 # get e-mail address of maintainer
388 my $addr = getparsedaddrs($maint);
389 my $e_mail = $addr->address();
390 my $full_name = $addr->phrase();
391 $full_name =~ s/^\"|\"$//g;
392 $full_name =~ s/^\s+|\s+$//g;
394 my $correspondent = $s->resultset('Correspondent')->
395 find_or_create({addr => $e_mail});
396 if (length $full_name) {
397 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
398 {full_name => $full_name}) if length $full_name;
399 $c_full_name->update({last_seen => 'NOW()'});
402 $s->resultset('Maintainer')->
403 find_or_create({name => $maint,
404 correspondent => $correspondent,
407 # add the maintainer to the source package for packages with
410 $s->resultset('SrcPkg')->search({pkg => $pkg})->
411 search_related_rs('src_vers',{ maintainer => undef})->
412 update_all({maintainer => $maint_r->id()});
419 sub add_configuration {
420 my ($options,$opts,$p,$config,$argv) = @_;
422 my $s = db_connect($options);
431 for my $sev_name (@{$config{severities}}) {
432 # add all severitites
433 my $sev = $s->resultset('Severity')->find_or_create({severity => $sev_name});
434 # mark strong severities
435 if (grep {$_ eq $sev_name} @{$config{strong_severities}}) {
441 $sev_names{$sev_name} = 1;
443 # mark obsolete severities
444 for my $sev ($s->resultset('Severity')->find()) {
445 next if exists $sev_names{$sev->severity()};
452 my ($options,$opts,$p,$config,$argv) = @_;
454 die "add_suite is currently not implemented; modify suites manually using SQL."
458 my ($options,$opts,$p,$config,$argv) = @_;
460 chdir($config->{spool_dir}) or
461 die "chdir $config->{spool_dir} failed: $!";
463 my $verbose = $options->{debug};
465 my $initialdir = "db-h";
467 if (defined $argv->[0] and $argv->[0] eq "archive") {
468 $initialdir = "archive";
470 my $s = db_connect($options);
474 my $start_time = time;
476 walk_bugs([(@{$argv}?@{$argv} : $initialdir)],
483 load_bug_log(db => $s,
487 die "failure while trying to load bug log $bug\n$@";
493 my ($options,$opts,$p,$config,$argv) = @_;
495 my $s = db_connect($options);
497 my $dist_dir = IO::Dir->new($opts->{ftpdists});
499 grep { $_ !~ /^\./ and
500 -d $opts->{ftpdists}.'/'.$_ and
501 not -l $opts->{ftpdists}.'/'.$_
505 while (my $dist = shift @dist_names) {
506 my $dist_dir = $opts->{ftpdists}.'/'.$dist;
508 my $rfh = IO::Uncompress::AnyUncompress->new($dist_dir.'/Release');
514 if (s/^(\S+):\s*//) {
515 if ($1 eq 'SHA1'or $1 eq 'SHA256') {
522 my ($sha,$size,$file) = split /\s+/,$_;
523 next unless $file =~ /(?:Packages|Sources)(?:\.gz|\.xz)$/;
524 next unless $file =~ m{^([^/]+)/([^/]+)/([^/]+)$};
525 my ($component,$arch,$package_source) = ($1,$2,$3);
526 $arch =~ s/binary-//;
527 next if exists $p_f{$component}{$arch};
528 $p_f{$component}{$arch} = $dist_dir.'/'.$file;
531 $s_p{$dist_info{Suite}} = \%p_f;
532 $s_info{$dist_info{Suite}} = \%s_info;
534 # parse packages files
535 for my $suite (keys %s_p) {
536 for my $component (keys %{$s_p{$suite}}) {
537 for my $arch (keys %{$s_p{$suite}{$component}}) {
538 my $pfh = IO::Uncompress::AnyUncompress->new($s_p{$suite}{$component}{$arch}) or
539 die "Unable to open $s_p{$suite}{$component}{$arch} for reading: $!";
544 load_package($s,$suite,$component,$arch,\%pkg);
548 if (my ($key, $value) = m/^(\S+): (.*)/) {
556 $pkg{$lastkey} .= "\n" . $_;
560 load_package($s,$suite,$component,$arch,\%pkg);
569 sub handle_subcommand_arguments {
570 my ($argv,$args) = @_;
572 Getopt::Long::GetOptionsFromArray($argv,
577 for my $arg (keys %{$args}) {
578 next unless $args->{$arg};
579 my $r_arg = $arg; # real argument name
580 $r_arg =~ s/[=\|].+//g;
581 if (not defined $subopt->{$r_arg}) {
582 push @usage_errors, "You must give a $r_arg option";
585 pod2usage(join("\n",@usage_errors)) if @usage_errors;
590 my ($subcommand,$config,$options) = @_;
591 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
592 if ($options->{quick}) {
593 # If this is a quick run, just exit
594 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
597 print STDERR "Another debbugs-loadsql is running; stopping\n";
604 # connect to the database; figure out how to handle errors
606 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
607 die "Unable to connect to database: ";
611 my ($dirs,$p,$what,$verbose,$sub) = @_;
613 my $tot_dirs = @dirs;
615 my $avg_subfiles = 0;
616 my $completed_files = 0;
617 while (my $dir = shift @dirs) {
618 printf "Doing dir %s ...\n", $dir if $verbose;
620 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
621 my @subdirs = readdir(DIR);
624 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
626 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
628 if ($avg_subfiles == 0) {
629 $avg_subfiles = @list;
632 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
633 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
636 for my $bug (@list) {
638 $p->update($completed_files) if $p;
639 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);