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/<(.*)>/);
179 my @dirs = ($initialdir);
180 while (my $dir = shift @dirs) {
181 printf "Doing dir %s ...\n", $dir if $verbose;
183 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
184 my @subdirs = readdir(DIR);
187 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
188 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
190 for my $bug (@list) {
191 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
192 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
193 if (not defined $stat) {
194 print STDERR "Unable to stat $bug $!\n";
197 next if $stat->mtime < $time;
198 my $fdata = readbug($bug, $initialdir);
199 addbugtoindex("package", $bug, split /[\s,]+/, $fdata->{"package"});
200 addbugtoindex("tag", $bug, split /[\s,]+/, $fdata->{"keywords"});
201 addbugtoindex('submitter-email', $bug,
202 map {lc($_->address)} getparsedaddrs($fdata->{originator}));
203 addbugtoindex("severity", $bug, $fdata->{"severity"});
204 addbugtoindex("owner", $bug,
205 map {lc($_->address)} getparsedaddrs($fdata->{"owner"}));
207 # do this in eval to avoid exploding on jacked logs
209 my $log = Debbugs::Log->new(bug_num => $bug);
210 while (my $record = $log->read_record()) {
211 next unless $record->{type} eq 'incoming-recv';
212 # we use a regex here, because a full mime parse will be slow.
213 my ($from) = $record->{text} =~ /^From:\s+(.+?)^\S/ism;
214 addbugtoindex('correspondent',$bug,
215 map {lc($_->address)} getparsedaddrs($from)
220 print STDERR "Problem dealing with log of $bug: $@";
225 if (not $options{quick}) {
226 # put the fast index into the slow index
227 for my $key1 (keys %fast_index) {
228 for my $key2 (keys %{$fast_index{$key1}}) {
229 $slow_index{$key1}{$key2} = $fast_index{$key1}{$key2};
231 print "Dealt with index $key1\n" if $verbose;
236 for my $i (@indexes) {
237 untie %{$slow_index{$i}};
238 move("$indexdest/by-$i$suffix.idx.new", "$indexdest/by-$i$suffix.idx");
239 # We do this, because old versions of touch don't support -d '@epoch'
240 system('touch','-d',"1/1/1970 UTC + ${start_time}secs","$indexdest/by-$i$suffix.idx");
243 unlink($config{spool_dir}.'/lock/gen-indices')