3 # Generates by-*.idx files for the CGI scripts
4 # Copyright (c) 2005/08/03 Anthony Towns
10 use Fcntl qw/O_RDWR O_CREAT O_TRUNC/;
17 use List::Util qw(min);
21 gen-indices - Generates index files for the cgi scripts
28 --quick update changed bugs
29 --debug, -d debugging level (Default 0)
30 --help, -h display this help
31 --man, -m display manual
39 Only update changed bugs
43 Debug verbosity. (Default 0)
47 Display brief useage information.
61 my %options = (debug => 0,
67 GetOptions(\%options,'quick!','debug|d+','help|h|?','man|m') or pod2usage(2);
70 require '/etc/debbugs/config';
71 require '/org/bugs.debian.org/scripts/errorlib';
73 chdir('/org/bugs.debian.org/spool') or die "chdir spool: $!\n";
75 my $verbose = $options{debug};
76 my $indexdest = "/org/bugs.debian.org/spool";
78 my $initialdir = "db-h";
81 if ($ARGV[0] eq "archive") {
82 $initialdir = "archive";
86 my @indexes = ('package', 'tag', 'severity', 'submitter-email');
89 my $start_time = time;
90 for my $i (@indexes) {
92 if ($options{quick}) {
93 if (-e "$indexdest/by-$i${suffix}.idx") {
94 system('cp','-a',"$indexdest/by-$i${suffix}.idx","$indexdest/by-$i${suffix}.idx.new") == 0
95 or die "Error creating the new index";
96 my $stat = stat("$indexdest/by-$i${suffix}.idx") or die "Unable to stat $indexdest/by-$i${suffix}.idx";
97 $time = defined $time ? min($time,$stat->mtime) : $stat->mtime;
99 tie %{$index{$i}}, DB_File => "$indexdest/by-$i$suffix.idx.new",
101 or die "$0: can't create by-$i$suffix-idx.new: $!";
104 tie %{$index{$i}}, DB_File => "$indexdest/by-$i$suffix.idx.new",
105 O_RDWR|O_CREAT|O_TRUNC, 0666
106 or die "$0: can't create by-$i$suffix-idx.new: $!";
109 $time = 0 if not defined $time;
113 my ($i, $k, $bug) = @_;
116 if (exists $index{$i}->{"count $k"}) {
117 $cnt = unpack 'N', $index{$i}->{"count $k"};
119 $index{$i}->{"count $k"} = (pack 'N', 1+$cnt);
120 my $which = $cnt - ($cnt % 100);
121 $index{$i}->{"$which $k"} = '' unless defined $index{$i}->{"$which $k"};
122 $index{$i}->{"$which $k"} .= (pack 'N', $bug);
125 sub emailfromrfc822 {
127 $email =~ s/\s*\(.*\)\s*//;
128 $email = $1 if ($email =~ m/<(.*)>/);
134 my @dirs = ($initialdir);
135 while (my $dir = shift @dirs) {
136 printf "Doing dir %s ...\n", $dir if $verbose;
138 opendir(DIR, "$dir/.") or die "opendir $dir: $!\n";
139 my @subdirs = readdir(DIR);
142 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
143 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
146 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
147 my $stat = stat(getbugcomponent($f,'summary'));
148 next if $stat->mtime < $time;
149 my $fdata = readbug($f, $initialdir);
150 for my $p (split /[\s,]+/, $fdata->{"package"}) {
151 addbugtoindex("package", $p, $f);
153 for my $t (split /[\s,]+/, $fdata->{"keywords"}) {
154 addbugtoindex("tag", $t, $f);
156 addbugtoindex('submitter-email',
157 emailfromrfc822($fdata->{"originator"}), $f);
158 addbugtoindex("severity", $fdata->{"severity"}, $f);
162 for my $i (@indexes) {
163 untie %{$indexes{$i}};
164 move("$indexdest/by-$i$suffix.idx.new", "$indexdest/by-$i$suffix.idx");
165 system('touch','-d',"1/1/1970 + ${start_time}secs","$indexdest/by-$i$suffix.idx");