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);
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 ($opts->{quick}) {
201 # select last status update
203 # select bugs which have been modified since the last updatex
205 $bugs = $s->resultset('Bug')->
212 for my $bug (@bugs) {
213 my $status = read_bug(bug => $bug->id);
214 for my $suite (@suites) {
216 bug_presence(bug => $bug->id,
218 dist => $suite->{suite_name},
220 $s->resultset('BugStatusCache')->
221 update_bug_status($bug->id,
230 if ($bugs->is_paged) {
232 $bugs->pager->last_page;
234 $p->target($last_page) if defined $p;
235 for my $page (1..$last_page) {
236 my $bugs_on_page = $bugs;
237 if ($bugs->is_paged) {
238 $bugs_on_page = $bugs->page($page);
240 $s->txn_do($update_bug,
241 $bugs_on_page->all());
246 sub handle_subcommand_arguments {
247 my ($argv,$args) = @_;
249 Getopt::Long::GetOptionsFromArray($argv,
254 for my $arg (keys %{$args}) {
255 next unless $args->{$arg};
256 my $r_arg = $arg; # real argument name
257 $r_arg =~ s/[=\|].+//g;
258 if (not defined $subopt->{$r_arg}) {
259 push @usage_errors, "You must give a $r_arg option";
262 pod2usage(join("\n",@usage_errors)) if @usage_errors;
267 my ($subcommand,$config,$options) = @_;
268 if (not lockpid($config->{spool_dir}.'/lock/debbugs-updatesqlcache-$subcommand')) {
269 if ($options->{quick}) {
270 # If this is a quick run, just exit
271 print STDERR "Another debbugs-updatesqlcache is running; stopping\n" if $options->{verbose};
274 print STDERR "Another debbugs-updatesqlcache is running; stopping\n";
281 # connect to the database; figure out how to handle errors
283 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
284 die "Unable to connect to database: ";
288 my ($dirs,$p,$what,$verbose,$sub) = @_;
290 my $tot_dirs = @dirs;
292 my $avg_subfiles = 0;
293 my $completed_files = 0;
294 while (my $dir = shift @dirs) {
295 printf "Doing dir %s ...\n", $dir if $verbose;
297 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
298 my @subdirs = readdir(DIR);
301 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
303 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
305 if ($avg_subfiles == 0) {
306 $avg_subfiles = @list;
309 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
310 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
313 for my $bug (@list) {
315 $p->update($completed_files) if $p;
316 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);