]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
admin: improve warnings and errors for missing modules
[public-inbox.git] / script / public-inbox-index
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Basic tool to create a Xapian search index for a git repository
5 # configured for public-inbox.
6 # Usage with libeatmydata <https://www.flamingspork.com/projects/libeatmydata/>
7 # highly recommended: eatmydata public-inbox-index REPO_DIR
8
9 use strict;
10 use warnings;
11 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
12 my $usage = "public-inbox-index REPO_DIR";
13 use PublicInbox::Admin qw(resolve_repo_dir);
14 PublicInbox::Admin::require_or_die('-index');
15 require PublicInbox::Config;
16
17 my $config = eval { PublicInbox::Config->new } || eval {
18         warn "public-inbox unconfigured for serving, indexing anyways...\n";
19         undef;
20 };
21
22 my $reindex;
23 my $prune;
24 my $jobs = undef;
25 my $indexlevel;
26 my %opts = (
27         '--reindex' => \$reindex,
28         '--jobs|j=i' => \$jobs,
29         '--prune' => \$prune,
30         'L|indexlevel=s' => \$indexlevel,
31 );
32 GetOptions(%opts) or die "bad command-line args\n$usage";
33 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
34
35 my @dirs;
36
37 if (@ARGV) {
38         @dirs = map { resolve_repo_dir($_) } @ARGV;
39 } else {
40         @dirs = (resolve_repo_dir());
41 }
42
43 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
44 usage() unless @dirs;
45
46 defined($config) and $config->each_inbox(sub {
47         my ($ibx) = @_;
48
49         for my $i (0..$#dirs) {
50                 next if $dirs[$i] ne $ibx->{mainrepo};
51                 $dirs[$i] = $ibx;
52         }
53 });
54
55 my @inboxes;
56 my $mods = {};
57
58 foreach my $dir (@dirs) {
59         my $ibx = $dir;
60         if (!ref($ibx)) {
61                 unless (-d $dir) {
62                         die "$dir does not appear to be an inbox repository\n";
63                 }
64                 $ibx = PublicInbox::Inbox->new({
65                         mainrepo => $dir,
66                         name => 'unnamed',
67                         indexlevel => $indexlevel,
68                         version => -f "$dir/inbox.lock" ? 2 : 1,
69                 });
70         } elsif (defined $indexlevel && !defined($ibx->{indexlevel})) {
71                 # XXX: users can shoot themselves in the foot, with this...
72                 $ibx->{indexlevel} = $indexlevel;
73         }
74         push @inboxes, $ibx;
75         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
76 }
77
78 PublicInbox::Admin::require_or_die(keys %$mods);
79
80 require PublicInbox::SearchIdx;
81 index_inbox($_) for @inboxes;
82
83 sub index_inbox {
84         my ($repo) = @_;
85         if (ref($repo) && ($repo->{version} || 1) == 2) {
86                 eval { require PublicInbox::V2Writable };
87                 die "v2 requirements not met: $@\n" if $@;
88                 my $v2w = eval {
89                         PublicInbox::V2Writable->new($repo, {nproc=>$jobs});
90                 };
91                 if (defined $jobs) {
92                         if ($jobs == 0) {
93                                 $v2w->{parallel} = 0;
94                         } else {
95                                 my $n = $v2w->{partitions};
96                                 if ($jobs != ($n + 1)) {
97                                         warn
98 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
99                                 }
100                         }
101                 }
102                 local $SIG{__WARN__} = sub {
103                         print STDERR $v2w->{current_info}, ': ', @_;
104                 };
105                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
106         } else {
107                 my $s = PublicInbox::SearchIdx->new($repo, 1);
108                 $s->index_sync({ reindex => $reindex });
109         }
110 }