2 # debbugs-updatesqlcache 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 2016 by Don Armstrong <don@donarmstrong.com>.
11 use Getopt::Long qw(:config no_ignore_case);
16 debbugs-updatesqlcache -- Update Debbugs SQL Cache
20 debbugs-updatesqlcache [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 Only update things which may have changed
52 Show progress bar (requires Term::ProgressBar)
56 Postgreql service to use; defaults to debbugs
58 =item B<--sysconfdir,-c>
60 System configuration directory to use; if not set, defaults to the
61 postgresql default. [Operates by setting PGSYSCONFDIR]
65 Debbugs spool directory; defaults to the value configured in the
66 debbugs configuration file.
70 Output more information about what is happening. Probably not useful
71 if you also set --progress.
79 Display brief useage information.
93 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list getsourcemaintainers);
94 use Debbugs::Config qw(:config);
95 use Debbugs::Status qw(bug_presence read_bug);
108 service => $config{debbugs_db},
112 Getopt::Long::Configure('pass_through');
113 GetOptions(\%options,
118 'spool_dir|spool-dir=s',
121 'debug|d+','help|h|?','man|m');
122 Getopt::Long::Configure('default');
124 pod2usage() if $options{help};
125 pod2usage({verbose=>2}) if $options{man};
127 $DEBUG = $options{debug};
130 ('update' => {function => \&update_cache,
132 'help' => {function => sub {pod2usage({verbose => 2});}}
136 $options{verbose} = $options{verbose} - $options{quiet};
138 if ($options{progress}) {
139 eval "use Term::ProgressBar";
140 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
144 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
146 if (exists $options{sysconfdir}) {
147 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
148 delete $ENV{PGSYSCONFDIR};
150 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
154 if (exists $options{spool_dir} and defined $options{spool_dir}) {
155 $config{spool_dir} = $options{spool_dir};
159 if ($options{progress}) {
160 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
161 warn "Unable to initialize progress bar: $@" if not $prog_bar;
165 my ($subcommand) = shift @ARGV;
166 if (not defined $subcommand) {
167 $subcommand = 'help';
168 print STDERR "You must provide a subcommand; displaying usage.\n";
170 } elsif (not exists $subcommands{$subcommand}) {
171 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
176 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
177 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
180 my ($options,$opts,$p,$config,$argv) = @_;
182 my $verbose = $options->{debug};
183 # select bugs to update
185 # basically, if this is a quick run, we want any bug which has
186 # been modified or any bug which belongs to a package which has a
187 # new version; otherwise, walk every bug
188 my $s = db_connect($options);
190 # get all of the possible architectures that we might care about
191 # select distinct s.codename,a.arch from bin_associations ba join bin_ver bv on ba.bin=bv.id join suite s on ba.suite=s.id join arch a on bv.arch=a.id;
194 $s->resultset('Suite')->
195 search_rs({active => 1,
197 {result_class => 'DBIx::Class::ResultClass::HashRefInflator'}
200 if ($options->{quick}) {
201 # identify the last time that we ran this query
202 my $last_query_time =
203 $s->resultset('BugStatusCache')->
206 order_by => { -desc => 'asof' },
207 columns => [qw(asof)],
210 my $dtf = $s->storage->datetime_parser;
211 if (defined $last_query_time) {
212 $last_query_time = $last_query_time->asof();
214 $last_query_time = DateTime->from_epoch(0);
216 # select last status update
217 $last_query_time = $dtf->format_datetime($last_query_time);
218 $bugs = $s->resultset('Bug')->
219 search_rs({-or => {'bin_associations.modified' => {'>=',$last_query_time},
220 'src_associations.modified' => {'>=',$last_query_time},
221 'me.log_modified' => {'>=',$last_query_time},
222 'me.last_modified' => {'>=',$last_query_time},
225 {join => [{bug_binpackages =>
228 'bin_associations'}}},
232 'src_associations'}}},
239 # select bugs which have been modified since the last updatex
241 $bugs = $s->resultset('Bug')->
251 for my $bug (@bugs) {
252 my $status = read_bug(bug => $bug->id);
253 next unless defined $status;
254 for my $suite (@suites) {
256 bug_presence(bug => $bug->id,
258 dist => $suite->{suite_name},
260 $s->resultset('BugStatusCache')->
261 update_bug_status($bug->id,
270 if ($bugs->is_paged) {
272 $bugs->pager->last_page;
274 $p->target($last_page) if defined $p;
275 for my $page (1..$last_page) {
276 my $bugs_on_page = $bugs;
277 if ($bugs->is_paged) {
278 $bugs_on_page = $bugs->page($page);
280 $s->txn_do($update_bug,
281 $bugs_on_page->all());
282 $p->update($page) if defined $p;
287 sub handle_subcommand_arguments {
288 my ($argv,$args) = @_;
290 Getopt::Long::GetOptionsFromArray($argv,
295 for my $arg (keys %{$args}) {
296 next unless $args->{$arg};
297 my $r_arg = $arg; # real argument name
298 $r_arg =~ s/[=\|].+//g;
299 if (not defined $subopt->{$r_arg}) {
300 push @usage_errors, "You must give a $r_arg option";
303 pod2usage(join("\n",@usage_errors)) if @usage_errors;
308 my ($subcommand,$config,$options) = @_;
309 if (not lockpid($config->{spool_dir}.'/lock/debbugs-updatesqlcache-$subcommand')) {
310 if ($options->{quick}) {
311 # If this is a quick run, just exit
312 print STDERR "Another debbugs-updatesqlcache is running; stopping\n" if $options->{verbose};
315 print STDERR "Another debbugs-updatesqlcache is running; stopping\n";
322 # connect to the database; figure out how to handle errors
324 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
325 die "Unable to connect to database: ";
329 my ($dirs,$p,$what,$verbose,$sub) = @_;
331 my $tot_dirs = @dirs;
333 my $avg_subfiles = 0;
334 my $completed_files = 0;
335 while (my $dir = shift @dirs) {
336 printf "Doing dir %s ...\n", $dir if $verbose;
338 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
339 my @subdirs = readdir(DIR);
342 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
344 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
346 if ($avg_subfiles == 0) {
347 $avg_subfiles = @list;
350 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
351 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
354 for my $bug (@list) {
356 $p->update($completed_files) if $p;
357 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);