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 --service, -s service name
25 --sysconfdir, -c postgresql service config dir
26 --spool-dir debbugs spool directory
27 --debug, -d debugging level (Default 0)
28 --help, -h display this help
29 --man, -m display manual
37 Only load changed bugs
41 Postgreql service to use; defaults to debbugs
43 =item B<--sysconfdir,-c>
45 System configuration directory to use; if not set, defaults to the
46 postgresql default. [Operates by setting PGSYSCONFDIR]
50 Debbugs spool directory; defaults to the value configured in the
51 debbugs configuration file.
59 Display brief useage information.
73 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list);
74 use Debbugs::Config qw(:config);
75 use Debbugs::Status qw(read_bug split_status_fields);
78 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue);
83 my %options = (debug => 0,
97 'spool_dir|spool-dir=s',
98 'debug|d+','help|h|?','man|m');
100 pod2usage() if $options{help};
101 pod2usage({verbose=>2}) if $options{man};
103 $DEBUG = $options{debug};
106 $options{verbose} = $options{verbose} - $options{quiet};
108 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
110 if (exists $options{sysconfdir}) {
111 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
112 delete $ENV{PGSYSCONFDIR};
114 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
118 if (exists $options{spool_dir} and defined $options{spool_dir}) {
119 $config{spool_dir} = $options{spool_dir};
121 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
123 my $verbose = $options{debug};
125 my $initialdir = "db-h";
127 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
128 $initialdir = "archive";
131 if (not lockpid($config{spool_dir}.'/lock/debbugs-loadsql')) {
132 if ($options{quick}) {
133 # If this is a quick run, just exit
134 print STDERR "Another debbugs-loadsql is running; stopping\n" if $verbose;
137 print STDERR "Another debbugs-loadsql is running; stopping\n";
141 # connect to the database; figure out how to handle errors properly
143 my $schema = Debbugs::DB->connect('dbi:Pg:service='.$options{service}) or
144 die "Unable to connect to database: ";
147 my $start_time = time;
150 my @dirs = ($initialdir);
154 while (my $dir = shift @dirs) {
155 printf "Doing dir %s ...\n", $dir if $verbose;
157 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
158 my @subdirs = readdir(DIR);
161 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
162 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
164 for my $bug (@list) {
165 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
166 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
167 if (not defined $stat) {
168 print STDERR "Unable to stat $bug $!\n";
171 next if $stat->mtime < $time;
172 my $data = read_bug(bug => $bug,
173 location => $initialdir);
174 load_bug(db => $schema,
175 data => split_status_fields($data),
180 handle_load_bug_queue(db => $schema,