1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # common stuff for administrative command-line tools
5 # Unstable internal API
6 package PublicInbox::Admin;
10 use base qw(Exporter);
11 our @EXPORT_OK = qw(resolve_repo_dir);
13 sub resolve_repo_dir {
15 my $prefix = defined $cd ? $cd : './';
16 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
18 return abs_path($prefix);
21 my @cmd = qw(git rev-parse --git-dir);
22 my $cmd = join(' ', @cmd);
23 my $pid = open my $fh, '-|';
24 defined $pid or die "forking $cmd failed: $!\n";
27 chdir $cd or die "chdir $cd failed: $!\n";
30 die "Failed to exec $cmd: $!\n";
36 close $fh or die "error in $cmd: $!\n";
39 return abs_path($cd) if ($dir eq '.' && defined $cd);
44 # for unconfigured inboxes
45 sub detect_indexlevel ($) {
48 # brand new or never before indexed inboxes default to full
49 return 'full' unless $ibx->over;
50 delete $ibx->{over}; # don't leave open FD lying around
53 my $srch = $ibx->search or return $l;
54 delete $ibx->{search}; # don't leave open FD lying around
55 if (my $xdb = $srch->xdb) {
57 my $m = $xdb->get_metadata('indexlevel');
62 $ibx->{mainrepo} has unexpected indexlevel in Xapian: $m
70 my ($argv, $warn_on_unconfigured) = @_;
71 require PublicInbox::Config;
72 require PublicInbox::Inbox;
74 my @ibxs = map { resolve_repo_dir($_) } @$argv;
75 push(@ibxs, resolve_repo_dir()) unless @ibxs;
78 if (my $config = eval { PublicInbox::Config->new }) {
79 $config->each_inbox(sub {
81 $dir2ibx{abs_path($ibx->{mainrepo})} = $ibx;
83 } elsif ($warn_on_unconfigured) {
84 # do we really care about this? It's annoying...
85 warn $warn_on_unconfigured, "\n";
87 for my $i (0..$#ibxs) {
89 $ibxs[$i] = $dir2ibx{$dir} ||= do {
90 my $name = "unconfigured-$i";
91 PublicInbox::Inbox->new({
93 address => [ "$name\@example.com" ],
95 # TODO: consumers may want to warn on this:
103 # TODO: make Devel::Peek optional, only used for daemon
104 my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
105 my @over_mod = qw(DBD::SQLite DBI);
107 -index => [ @base_mod, @over_mod ],
109 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
112 sub scan_ibx_modules ($$) {
113 my ($mods, $ibx) = @_;
114 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
115 $mods->{'Search::Xapian'} = 1;
117 $mods->{$_} = 1 foreach @over_mod;
124 while (my $mod = shift @mods) {
125 if (my $groups = $mod_groups{$mod}) {
126 push @mods, @$groups;
129 $err->{$mod} = $@ if $@;
132 scalar keys %$err ? $err : undef;
135 sub missing_mod_msg {
137 my @mods = map { "`$_'" } sort keys %$err;
138 my $last = pop @mods;
139 @mods ? (join(', ', @mods)."' and $last") : $last
143 my $err = check_require(@_) or return;
144 die missing_mod_msg($err)." required for $0\n";
147 sub indexlevel_ok_or_die ($) {
148 my ($indexlevel) = @_;
150 if ($indexlevel eq 'basic') {
152 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
156 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
159 my $err = check_require($req) or return;
160 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
164 my ($ibx, $opt) = @_;
165 my $jobs = delete $opt->{jobs} if $opt;
166 if (ref($ibx) && ($ibx->{version} || 1) == 2) {
167 eval { require PublicInbox::V2Writable };
168 die "v2 requirements not met: $@\n" if $@;
169 my $v2w = eval { $ibx->importer(0) } || eval {
170 PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
174 $v2w->{parallel} = 0;
176 my $n = $v2w->{partitions};
177 if ($jobs != ($n + 1)) {
179 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
183 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
184 local $SIG{__WARN__} = sub {
185 $warn_cb->($v2w->{current_info}, ': ', @_);
187 $v2w->index_sync($opt);
189 require PublicInbox::SearchIdx;
190 my $s = PublicInbox::SearchIdx->new($ibx, 1);
191 $s->index_sync($opt);
195 sub progress_prepare ($) {
198 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
199 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
200 $opt->{quiet} = !$opt->{verbose};
203 open my $null, '>', '/dev/null' or
204 die "failed to open /dev/null: $!\n";
205 $opt->{1} = fileno($null); # suitable for spawn() redirect
206 $opt->{-dev_null} = $null;
208 $opt->{verbose} ||= 1;
209 $opt->{-progress} = sub { print STDERR @_ };