]> git.donarmstrong.com Git - debbugs.git/blob - scripts/gen-indices
* Only replace indices if changes have been made to the index
[debbugs.git] / scripts / gen-indices
1 #!/usr/bin/perl
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.
5
6 # Copyright (c) 2005/08/03 Anthony Towns
7 # Copyright 2007, 2008 by Don Armstrong <don@donarmstrong.com>.
8
9 use warnings;
10 use strict;
11
12 use DB_File;
13 use MLDBM qw(DB_FILE Storable);
14 use Fcntl qw/O_RDWR O_CREAT O_TRUNC/;
15 use File::Copy;
16
17 use Getopt::Long;
18 use Pod::Usage;
19
20 use File::stat;
21 use List::Util qw(min);
22
23 =head1 NAME
24
25 gen-indices - Generates index files for the cgi scripts
26
27 =head1 SYNOPSIS
28
29  gen-indices [options]
30
31  Options:
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
37
38 =head1 OPTIONS
39
40 =over
41
42 =itme B<--quick>
43
44 Only update changed bugs
45
46 =item B<--debug, -d>
47
48 Debug verbosity. (Default 0)
49
50 =item B<--help, -h>
51
52 Display brief useage information.
53
54 =item B<--man, -m>
55
56 Display this manual.
57
58 =back
59
60 =head1 EXAMPLES
61
62
63 =cut
64
65 # Use portable Storable images
66 $MLDBM::DumpMeth=q(portable);
67
68
69 my %options = (debug           => 0,
70                help            => 0,
71                man             => 0,
72                quick           => 0,
73                index_path      => undef,
74                );
75
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};
79
80 use Debbugs::Config qw(:config);
81 use Debbugs::Common qw(getparsedaddrs getbugcomponent lockpid);
82 use Debbugs::Status qw(readbug);
83 use Debbugs::Log;
84
85 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
86
87 my $verbose = $options{debug};
88 my $indexdest = $options{index_path} || $config{spool_dir};
89
90 my $initialdir = "db-h";
91 my $suffix = "";
92
93 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
94     $initialdir = "archive";
95     $suffix = "-arc";
96 }
97
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;
102           exit 0;
103      }
104      print STDERR "Another gen-indices is running; stopping\n";
105      exit 1;
106 }
107
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');
110 my $indexes;
111 my %slow_index = ();
112 my %fast_index = ();
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} = {};
117      }
118      $indexes = \%fast_index;
119 }
120 else {
121      $indexes = \%slow_index;
122 }
123 my $time = undef;
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;
133              }
134              tie %{$slow_index{$i}}, MLDBM => "$indexdest/by-$i$suffix.idx.new",
135                   O_RDWR|O_CREAT, 0666
136                        or die "$0: can't create by-$i$suffix-idx.new: $!";
137         }
138         else {
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: $!";
142
143         }
144         $time = 0 if not defined $time;
145 }
146
147 sub addbugtoindex {
148      my ($index, $bug, @values) = @_;
149
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"}--;
157           }
158           delete $indexes->{reverse}{"$index $bug"};
159      }
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};
164           $temp->{$bug} = 1;
165           $indexes->{$index}->{$key} = $temp;
166      }
167      $indexes->{reverse}{"$index $bug"} = [@values];
168 }
169
170 sub emailfromrfc822 {
171         my $email = shift;
172         $email =~ s/\s*\(.*\)\s*//;
173         $email = $1 if ($email =~ m/<(.*)>/);
174         return $email;
175 }
176
177 my $modification_made = 0;
178 my $cnt = 0;
179
180 my @dirs = ($initialdir);
181 while (my $dir = shift @dirs) {
182         printf "Doing dir %s ...\n", $dir if $verbose;
183
184         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
185         my @subdirs = readdir(DIR);
186         closedir(DIR);
187
188         my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
189         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
190
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";
196                      next;
197                 }
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"}));
208                 # handle log entries
209                 # do this in eval to avoid exploding on jacked logs
210                 eval {
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)
218                                        );
219                      }
220                 };
221                 if ($@) {
222                      print STDERR "Problem dealing with log of $bug: $@";
223                 }
224            }
225 }
226
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};
232           }
233           print "Dealt with index $key1\n" if $verbose;
234      }
235 }
236
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");
244     }
245     else {
246         unlink("$indexdest/by-$i$suffix.idx.new");
247     }
248 }
249
250 unlink($config{spool_dir}.'/lock/gen-indices')