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 my $gop = Getopt::Long::Parser->new();
123 $gop->configure('pass_through');
124 $gop->getoptions(\%options,
129 'spool_dir|spool-dir=s',
132 'debug|d+','help|h|?','man|m');
133 $gop->getoptions('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,
147 'maintainers' => {function => \&add_maintainers,
149 'configuration' => {function => \&add_configuration,
151 'logs' => {function => \&add_logs,
153 'help' => {function => sub {pod2usage({verbose => 2});}}
157 $options{verbose} = $options{verbose} - $options{quiet};
159 if ($options{progress}) {
160 eval "use Term::ProgressBar";
161 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
165 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
167 if (exists $options{sysconfdir}) {
168 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
169 delete $ENV{PGSYSCONFDIR};
171 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
175 if (exists $options{spool_dir} and defined $options{spool_dir}) {
176 $config{spool_dir} = $options{spool_dir};
180 if ($options{progress}) {
181 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
182 warn "Unable to initialize progress bar: $@" if not $prog_bar;
186 my ($subcommand) = shift @ARGV;
187 if (not defined $subcommand) {
188 $subcommand = 'help';
189 print STDERR "You must provide a subcommand; displaying help";
193 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments},$gop);
194 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
197 my ($options,$opts,$p,$config,$argv) = @_;
198 chdir($config->{spool_dir}) or
199 die "chdir $config->{spool_dir} failed: $!";
201 my $verbose = $options->{debug};
203 my $initialdir = "db-h";
205 if (defined $argv->[0] and $argv->[0] eq "archive") {
206 $initialdir = "archive";
208 my $s = db_connect($options);
212 my $start_time = time;
215 my @dirs = (@{$argv}?@{$argv} : $initialdir);
220 my $tot_dirs = @{$argv}? @{$argv} : 0;
222 my $avg_subfiles = 0;
223 my $completed_files = 0;
224 while (my $dir = shift @dirs) {
225 printf "Doing dir %s ...\n", $dir if $verbose;
227 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
228 my @subdirs = readdir(DIR);
231 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
233 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
235 if ($avg_subfiles == 0) {
236 $avg_subfiles = @list;
239 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
240 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
243 for my $bug (@list) {
245 $p->update($completed_files) if $p;
246 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
247 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
248 if (not defined $stat) {
249 print STDERR "Unable to stat $bug $!\n";
252 next if $stat->mtime < $time;
253 my $data = read_bug(bug => $bug,
254 location => $initialdir);
257 data => split_status_fields($data),
259 severities => \%severities,
264 print STDERR Dumper($data) if $DEBUG;
265 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};
322 return unless @files;
323 my $s = db_connect($options);
325 $p->target(scalar @files) if $p;
326 for my $file (@files) {
327 my $fh = IO::File->new($file,'r') or
328 die "Unable to open $file for reading: $!";
329 my $f_stat = stat($file);
332 next unless length $_;
333 my ($binname, $binver, $binarch, $srcname, $srcver) = split;
334 # if $srcver is not defined, this is probably a broken
335 # .debinfo file [they were causing #686106, see commit
336 # 49c85ab8 in dak.] Basically, $binarch didn't get put into
337 # the file, so we'll fudge it from the filename.
338 if (not defined $srcver) {
339 ($srcname,$srcver) = ($binarch,$srcname);
340 ($binarch) = $file =~ /_([^\.]+)\.debinfo/;
342 my $sp = $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
343 my $sv = $s->resultset('SrcVer')->find_or_create({src_pkg =>$sp->id(),
346 if (defined $arch{$binarch}) {
347 $arch = $arch{$binarch};
349 $arch = $s->resultset('Arch')->find_or_create({arch => $binarch});
350 $arch{$binarch} = $arch;
352 my $bp = $s->resultset('BinPkg')->find_or_create({pkg => $binname});
353 $s->resultset('BinVer')->find_or_create({bin_pkg => $bp->id(),
354 src_ver => $sv->id(),
364 sub add_maintainers {
365 my ($options,$opts,$p,$config,$argv) = @_;
367 my $s = db_connect($options);
368 my $maintainers = getsourcemaintainers();
369 $p->target(scalar keys %{$maintainers}) if $p;
370 for my $pkg (keys %{$maintainers}) {
371 my $maint = $maintainers->{$pkg};
372 # see if a maintainer already exists; if so, we don't do
374 my $maint_r = $s->resultset('Maintainer')->
375 find({name => $maint});
376 if (not defined $maint_r) {
377 # get e-mail address of maintainer
378 my $addr = getparsedaddrs($maint);
379 my $e_mail = $addr->address();
380 my $full_name = $addr->phrase();
381 $full_name =~ s/^\"|\"$//g;
382 $full_name =~ s/^\s+|\s+$//g;
384 my $correspondent = $s->resultset('Correspondent')->
385 find_or_create({addr => $e_mail});
386 if (length $full_name) {
387 my $c_full_name = $correspondent->find_or_create_related('correspondent_full_names',
388 {full_name => $full_name}) if length $full_name;
389 $c_full_name->update({last_seen => 'NOW()'});
392 $s->resultset('Maintainer')->
393 find_or_create({name => $maint,
394 correspondent => $correspondent,
397 # add the maintainer to the source package for packages with
400 $s->resultset('SrcPkg')->search({pkg => $pkg})->
401 search_related_rs('src_vers',{ maintainer => undef})->
402 update_all({maintainer => $maint_r->id()});
409 sub add_configuration {
410 my ($options,$opts,$p,$config,$argv) = @_;
414 my ($options,$opts,$p,$config,$argv) = @_;
417 sub handle_subcommand_arguments {
418 my ($argv,$args,$gop) = @_;
420 Getopt::Long::GetOptionsFromArray($argv,
425 for my $arg (keys %{$args}) {
426 next unless $args->{$arg};
427 my $r_arg = $arg; # real argument name
428 $r_arg =~ s/[=\|].+//g;
429 if (not defined $subopt->{$r_arg}) {
430 push @usage_errors, "You must give a $r_arg option";
433 pod2usage(join("\n",@usage_errors)) if @usage_errors;
438 my ($subcommand,$config,$options) = @_;
439 if (not lockpid($config->{spool_dir}.'/lock/debbugs-loadsql-$subcommand')) {
440 if ($options->{quick}) {
441 # If this is a quick run, just exit
442 print STDERR "Another debbugs-loadsql is running; stopping\n" if $options->{verbose};
445 print STDERR "Another debbugs-loadsql is running; stopping\n";
452 # connect to the database; figure out how to handle errors
454 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
455 die "Unable to connect to database: ";