]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-rebuild-index.db
include function in instalsql for bin ver/src pkg linking
[debbugs.git] / bin / debbugs-rebuild-index.db
1 #! /usr/bin/perl
2 # debbugs-rebuild-index.db 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 2012 by Don Armstrong <don@donarmstrong.com>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long qw(:config no_ignore_case);
12 use Pod::Usage;
13
14 =head1 NAME
15
16 debbugs-rebuild-index.db -- rebuild Debbug's index.db
17
18 =head1 SYNOPSIS
19
20 debbugs-rebuild-index.db [options]
21
22  Options:
23   --spool-dir debbugs spool directory
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--spool-dir>
33
34 Debbugs spool directory; defaults to the value configured in the
35 debbugs configuration file.
36
37 =item B<--debug, -d>
38
39 Debug verbosity.
40
41 =item B<--help, -h>
42
43 Display brief useage information.
44
45 =item B<--man, -m>
46
47 Display this manual.
48
49 =back
50
51 =head1 EXAMPLES
52
53 Rebuild the index.db for db-h.
54
55  debbugs-rebuild-index.db;
56
57 Rebuild the index.db for archive
58
59  debbugs-rebuild-index.db archive;
60
61
62 =cut
63
64
65 use vars qw($DEBUG);
66
67 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list);
68 use Debbugs::Config qw(:config);
69 use Debbugs::Status qw(read_bug split_status_fields generate_index_db_line);
70
71 my %options = (debug           => 0,
72                help            => 0,
73                man             => 0,
74                verbose         => 0,
75                quiet           => 0,
76                quick           => 0,
77                service         => 'debbugs',
78               );
79
80
81 GetOptions(\%options,
82            'quick|q',
83            'service|s',
84            'sysconfdir|c',
85            'spool_dir|spool-dir=s',
86            'debug|d+','help|h|?','man|m');
87
88 pod2usage() if $options{help};
89 pod2usage({verbose=>2}) if $options{man};
90
91 $DEBUG = $options{debug};
92
93 my @USAGE_ERRORS;
94 $options{verbose} = $options{verbose} - $options{quiet};
95
96 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
97
98 if (exists $options{spool_dir} and defined $options{spool_dir}) {
99     $config{spool_dir} = $options{spool_dir};
100 }
101 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
102
103 my $verbose = $options{debug};
104
105 my $initialdir = "db-h";
106
107 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
108     $initialdir = "archive";
109 }
110
111 if (not lockpid($config{spool_dir}.'/lock/debbugs-rebuild-index.db')) {
112      print STDERR "Another debbugs-rebuild-index.db is running; stopping\n";
113      exit 1;
114 }
115
116 my $fh_type = $initialdir;
117 # if initaldir is db-h, the file is db.
118 $fh_type = 'db' if $initialdir eq 'db-h';
119
120 my $file = "index.${fh_type}.realtime";
121 my $idx_rebuild = IO::File->new($file.'.rebuild','w')
122     or die "Couldn't open ${file}.rebuild: $!";
123
124
125 my @dirs = ($initialdir);
126 my $cnt = 0;
127 my %bugs;
128 while (my $dir = shift @dirs) {
129     printf "Doing dir %s ...\n", $dir if $verbose;
130
131     opendir(DIR, "$dir/.") or die "opendir $dir: $!";
132     my @subdirs = readdir(DIR);
133     closedir(DIR);
134
135     my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
136     push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
137
138     for my $bug (@list) {
139         print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
140         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
141         if (not defined $stat) {
142             print STDERR "Unable to stat $bug $!\n";
143             next;
144         }
145         my $data = read_bug(bug => $bug,
146                             location => $initialdir);
147         my $line = generate_index_db_line($data);
148         $bugs{$bug} = $line;
149     }
150 }
151 binmode($idx_rebuild,':raw:encoding(UTF-8)');
152 print {$idx_rebuild} $bugs{$_} foreach sort {$a <=> $b} keys %bugs;
153 close($idx_rebuild);
154 rename("$file.rebuild", $file);
155
156
157 __END__