]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
Merge branch 'master' into database
[debbugs.git] / bin / debbugs-loadsql
1 #! /usr/bin/perl
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>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long qw(:config no_ignore_case);
12 use Pod::Usage;
13
14 =head1 NAME
15
16 debbugs-loadsql -- load debbugs sql database
17
18 =head1 SYNOPSIS
19
20 debbugs-loadsql [options]
21
22  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
31
32 =head1 OPTIONS
33
34 =over
35
36 =item B<--quick, -q>
37
38 Only load changed bugs
39
40 =item B<--progress>
41
42 Show progress bar (requires Term::ProgressBar)
43
44 =item B<--service,-s>
45
46 Postgreql service to use; defaults to debbugs
47
48 =item B<--sysconfdir,-c>
49
50 System configuration directory to use; if not set, defaults to the
51 postgresql default. [Operates by setting PGSYSCONFDIR]
52
53 =item B<--spool-dir>
54
55 Debbugs spool directory; defaults to the value configured in the
56 debbugs configuration file.
57
58 =item B<--verbose>
59
60 Output more information about what is happening. Probably not useful
61 if you also set --progress.
62
63 =item B<--debug, -d
64
65 Debug verbosity.
66
67 =item B<--help, -h>
68
69 Display brief useage information.
70
71 =item B<--man, -m>
72
73 Display this manual.
74
75 =back
76
77
78 =cut
79
80
81 use vars qw($DEBUG);
82
83 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list);
84 use Debbugs::Config qw(:config);
85 use Debbugs::Status qw(read_bug split_status_fields);
86 use Debbugs::Log;
87 use Debbugs::DB;
88 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue);
89 use DateTime;
90 use File::stat;
91
92
93 my %options =
94     (debug           => 0,
95      help            => 0,
96      man             => 0,
97      verbose         => 0,
98      quiet           => 0,
99      quick           => 0,
100      service         => 'debbugs',
101      progress        => 0,
102     );
103
104
105 GetOptions(\%options,
106            'quick|q',
107            'service|s',
108            'sysconfdir|c',
109            'progress!',
110            'spool_dir|spool-dir=s',
111            'verbose|v+',
112            'quiet+',
113            'debug|d+','help|h|?','man|m');
114
115 pod2usage() if $options{help};
116 pod2usage({verbose=>2}) if $options{man};
117
118 $DEBUG = $options{debug};
119
120 my @USAGE_ERRORS;
121 $options{verbose} = $options{verbose} - $options{quiet};
122
123 if ($options{progress}) {
124     eval "use Term::ProgressBar";
125     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
126 }
127
128
129 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
130
131 if (exists $options{sysconfdir}) {
132     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
133         delete $ENV{PGSYSCONFDIR};
134     } else {
135         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
136     }
137 }
138
139 if (exists $options{spool_dir} and defined $options{spool_dir}) {
140     $config{spool_dir} = $options{spool_dir};
141 }
142 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
143
144 my $verbose = $options{debug};
145
146 my $initialdir = "db-h";
147
148 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
149     $initialdir = "archive";
150 }
151
152 if (not lockpid($config{spool_dir}.'/lock/debbugs-loadsql')) {
153      if ($options{quick}) {
154           # If this is a quick run, just exit
155           print STDERR "Another debbugs-loadsql is running; stopping\n" if $verbose;
156           exit 0;
157      }
158      print STDERR "Another debbugs-loadsql is running; stopping\n";
159      exit 1;
160 }
161
162
163 # connect to the database; figure out how to handle errors properly
164 # here.
165 my $schema = Debbugs::DB->connect('dbi:Pg:service='.$options{service}) or
166     die "Unable to connect to database: ";
167
168 my $time = 0;
169 my $start_time = time;
170
171
172 my @dirs = (@ARGV?@ARGV : $initialdir);
173 my $cnt = 0;
174 my %tags;
175 my %severities;
176 my %queue;
177 my $tot_dirs = @ARGV ? @ARGV : 0;
178 my $done_dirs = 0;
179 my $avg_subfiles = 0;
180 my $completed_files = 0;
181 my $p;
182 if ($options{progress}) {
183     $p = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
184     warn "Unable to initialize progress bar: $@" if not $p;
185 }
186 while (my $dir = shift @dirs) {
187     printf "Doing dir %s ...\n", $dir if $verbose;
188
189     opendir(DIR, "$dir/.") or die "opendir $dir: $!";
190     my @subdirs = readdir(DIR);
191     closedir(DIR);
192
193     my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
194     $tot_dirs -= @dirs;
195     push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
196     $tot_dirs += @dirs;
197     if ($avg_subfiles == 0) {
198         $avg_subfiles = @list;
199     }
200
201     $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
202     $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
203     $done_dirs += 1;
204
205     for my $bug (@list) {
206         $completed_files++;
207         $p->update($completed_files) if $p;
208         print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
209         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
210         if (not defined $stat) {
211             print STDERR "Unable to stat $bug $!\n";
212             next;
213         }
214         next if $stat->mtime < $time;
215         my $data = read_bug(bug => $bug,
216                             location => $initialdir);
217         eval {
218             load_bug(db => $schema,
219                      data => split_status_fields($data),
220                      tags => \%tags,
221                      severities => \%severities,
222                      queue => \%queue);
223         };
224         if ($@) {
225             use Data::Dumper;
226             print STDERR Dumper($data) if $DEBUG;
227             die "failure while trying to load bug $bug\n$@";
228         }
229     }
230 }
231 $p->remove() if $p;
232 handle_load_bug_queue(db => $schema,
233                       queue => \%queue);
234
235
236
237 __END__