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'}}},
241 # select bugs which have been modified since the last updatex
243 $bugs = $s->resultset('Bug')->
253 for my $bug (@bugs) {
254 my $status = read_bug(bug => $bug->id);
255 next unless defined $status;
256 for my $suite (@suites) {
258 bug_presence(bug => $bug->id,
260 dist => $suite->{suite_name},
262 $s->resultset('BugStatusCache')->
263 update_bug_status($bug->id,
272 if ($bugs->is_paged) {
274 $bugs->pager->last_page;
276 $p->target($last_page) if defined $p;
277 for my $page (1..$last_page) {
278 my $bugs_on_page = $bugs;
279 if ($bugs->is_paged) {
280 $bugs_on_page = $bugs->page($page);
282 $s->txn_do($update_bug,
283 $bugs_on_page->all());
284 $p->update($page) if defined $p;
289 sub handle_subcommand_arguments {
290 my ($argv,$args) = @_;
292 Getopt::Long::GetOptionsFromArray($argv,
297 for my $arg (keys %{$args}) {
298 next unless $args->{$arg};
299 my $r_arg = $arg; # real argument name
300 $r_arg =~ s/[=\|].+//g;
301 if (not defined $subopt->{$r_arg}) {
302 push @usage_errors, "You must give a $r_arg option";
305 pod2usage(join("\n",@usage_errors)) if @usage_errors;
310 my ($subcommand,$config,$options) = @_;
311 if (not lockpid($config->{spool_dir}.'/lock/debbugs-updatesqlcache-$subcommand')) {
312 if ($options->{quick}) {
313 # If this is a quick run, just exit
314 print STDERR "Another debbugs-updatesqlcache is running; stopping\n" if $options->{verbose};
317 print STDERR "Another debbugs-updatesqlcache is running; stopping\n";
324 # connect to the database; figure out how to handle errors
326 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
327 die "Unable to connect to database: ";
331 my ($dirs,$p,$what,$verbose,$sub) = @_;
333 my $tot_dirs = @dirs;
335 my $avg_subfiles = 0;
336 my $completed_files = 0;
337 while (my $dir = shift @dirs) {
338 printf "Doing dir %s ...\n", $dir if $verbose;
340 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
341 my @subdirs = readdir(DIR);
344 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
346 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
348 if ($avg_subfiles == 0) {
349 $avg_subfiles = @list;
352 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
353 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
356 for my $bug (@list) {
358 $p->update($completed_files) if $p;
359 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);