2 # gen-indices generates bug index files, 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.
6 # Copyright (c) 2005/08/03 Anthony Towns
7 # Copyright 2007, 2008 by Don Armstrong <don@donarmstrong.com>.
13 use MLDBM qw(DB_FILE Storable);
14 use Fcntl qw/O_RDWR O_CREAT O_TRUNC/;
21 use List::Util qw(min);
25 gen-indices - Generates index files for the cgi scripts
32 --index-path path to index location
33 --quick update changed bugs
34 --debug, -d debugging level (Default 0)
35 --help, -h display this help
36 --man, -m display manual
44 Only update changed bugs
48 Debug verbosity. (Default 0)
52 Display brief useage information.
65 # Use portable Storable images
66 $MLDBM::DumpMeth=q(portable);
69 my %options = (debug => 0,
76 GetOptions(\%options,'quick!','index_path|index-path=s','debug|d+','help|h|?','man|m') or pod2usage(2);
77 pod2usage(1) if $options{help};
78 pod2usage(-verbose=>2) if $options{man};
80 use Debbugs::Config qw(:config);
81 use Debbugs::Common qw(getparsedaddrs getbugcomponent lockpid);
82 use Debbugs::Status qw(readbug);
85 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
87 my $verbose = $options{debug};
88 my $indexdest = $options{index_path} || $config{spool_dir};
90 my $initialdir = "db-h";
93 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
94 $initialdir = "archive";
98 if (not lockpid($config{spool_dir}.'/lock/gen-indices')) {
99 if ($options{quick}) {
100 # If this is a quick run, just exit
101 print STDERR "Another gen-indices is running; stopping\n" if $verbose;
104 print STDERR "Another gen-indices is running; stopping\n";
108 # NB: The reverse index is special; it's used to clean up during updates to bugs
109 my @indexes = ('package', 'tag', 'severity','owner','submitter-email','status','correspondent','reverse');
113 if (not $options{quick}) {
114 # We'll trade memory for speed here if we're not doing a quick rebuild
115 for my $indexes (@indexes) {
116 $fast_index{$indexes} = {};
118 $indexes = \%fast_index;
121 $indexes = \%slow_index;
124 my $start_time = time;
125 for my $i (@indexes) {
126 $slow_index{$i} = {};
127 if ($options{quick}) {
128 if (-e "$indexdest/by-$i${suffix}.idx") {
129 system('cp','-a',"$indexdest/by-$i${suffix}.idx","$indexdest/by-$i${suffix}.idx.new") == 0
130 or die "Error creating the new index";
131 my $stat = stat("$indexdest/by-$i${suffix}.idx") or die "Unable to stat $indexdest/by-$i${suffix}.idx";
132 $time = defined $time ? min($time,$stat->mtime) : $stat->mtime;
134 tie %{$slow_index{$i}}, MLDBM => "$indexdest/by-$i$suffix.idx.new",
136 or die "$0: can't create by-$i$suffix-idx.new: $!";
139 tie %{$slow_index{$i}}, MLDBM => "$indexdest/by-$i$suffix.idx.new",
140 O_RDWR|O_CREAT|O_TRUNC, 0666
141 or die "$0: can't create by-$i$suffix-idx.new: $!";
144 $time = 0 if not defined $time;
148 my ($index, $bug, @values) = @_;
150 if (exists $indexes->{reverse}{"$index $bug"}) {
151 # We do this insanity to work around a "feature" in MLDBM
152 for my $key (@{$indexes->{reverse}{"$index $bug"}}) {
153 my $temp = $indexes->{$index}{$key};
154 delete $temp->{$bug};
155 $indexes->{$index}{$key} = $temp;
156 $indexes->{$index}{"count $key"}--;
158 delete $indexes->{reverse}{"$index $bug"};
160 for my $key (@values) {
161 $indexes->{$index}->{"count $key"}++;
162 # We do this insanity to work around a "feature" in MLDBM
163 my $temp = $indexes->{$index}->{$key};
165 $indexes->{$index}->{$key} = $temp;
167 $indexes->{reverse}{"$index $bug"} = [@values];
170 sub emailfromrfc822 {
172 $email =~ s/\s*\(.*\)\s*//;
173 $email = $1 if ($email =~ m/<(.*)>/);
177 my $modification_made = 0;
180 my @dirs = ($initialdir);
181 while (my $dir = shift @dirs) {
182 printf "Doing dir %s ...\n", $dir if $verbose;
184 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
185 my @subdirs = readdir(DIR);
188 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
189 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
191 for my $bug (@list) {
192 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
193 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
194 if (not defined $stat) {
195 print STDERR "Unable to stat $bug $!\n";
198 next if $stat->mtime < $time;
199 my $fdata = readbug($bug, $initialdir);
200 $modification_made = 1;
201 addbugtoindex("package", $bug, split /[\s,]+/, $fdata->{"package"});
202 addbugtoindex("tag", $bug, split /[\s,]+/, $fdata->{"keywords"});
203 addbugtoindex('submitter-email', $bug,
204 map {lc($_->address)} getparsedaddrs($fdata->{originator}));
205 addbugtoindex("severity", $bug, $fdata->{"severity"});
206 addbugtoindex("owner", $bug,
207 map {lc($_->address)} getparsedaddrs($fdata->{"owner"}));
209 # do this in eval to avoid exploding on jacked logs
211 my $log = Debbugs::Log->new(bug_num => $bug);
212 while (my $record = $log->read_record()) {
213 next unless $record->{type} eq 'incoming-recv';
214 # we use a regex here, because a full mime parse will be slow.
215 my ($from) = $record->{text} =~ /^From:\s+(.+?)^\S/ism;
216 addbugtoindex('correspondent',$bug,
217 map {lc($_->address)} getparsedaddrs($from)
222 print STDERR "Problem dealing with log of $bug: $@";
227 if (not $options{quick}) {
228 # put the fast index into the slow index
229 for my $key1 (keys %fast_index) {
230 for my $key2 (keys %{$fast_index{$key1}}) {
231 $slow_index{$key1}{$key2} = $fast_index{$key1}{$key2};
233 print "Dealt with index $key1\n" if $verbose;
237 for my $i (@indexes) {
238 untie %{$slow_index{$i}};
239 # Only move if we've made changes, otherwise unlink
240 if ($modification_made) {
241 move("$indexdest/by-$i$suffix.idx.new", "$indexdest/by-$i$suffix.idx");
242 # We do this, because old versions of touch don't support -d '@epoch'
243 system('touch','-d',"1/1/1970 UTC + ${start_time}secs","$indexdest/by-$i$suffix.idx");
246 unlink("$indexdest/by-$i$suffix.idx.new");
250 unlink($config{spool_dir}.'/lock/gen-indices')