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
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;
84 for my $i (0..$#ibxs) {
86 $ibxs[$i] = $dir2ibx{$dir} ||= do {
87 my $name = "unconfigured-$i";
88 PublicInbox::Inbox->new({
90 address => [ "$name\@example.com" ],
92 # TODO: consumers may want to warn on this:
100 # TODO: make Devel::Peek optional, only used for daemon
101 my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
102 my @over_mod = qw(DBD::SQLite DBI);
104 -index => [ @base_mod, @over_mod ],
106 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
109 sub scan_ibx_modules ($$) {
110 my ($mods, $ibx) = @_;
111 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
112 $mods->{'Search::Xapian'} = 1;
114 $mods->{$_} = 1 foreach @over_mod;
121 while (my $mod = shift @mods) {
122 if (my $groups = $mod_groups{$mod}) {
123 push @mods, @$groups;
126 $err->{$mod} = $@ if $@;
129 scalar keys %$err ? $err : undef;
132 sub missing_mod_msg {
134 my @mods = map { "`$_'" } sort keys %$err;
135 my $last = pop @mods;
136 @mods ? (join(', ', @mods)."' and $last") : $last
140 my $err = check_require(@_) or return;
141 die missing_mod_msg($err)." required for $0\n";
144 sub indexlevel_ok_or_die ($) {
145 my ($indexlevel) = @_;
147 if ($indexlevel eq 'basic') {
149 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
153 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
156 my $err = check_require($req) or return;
157 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
161 my ($ibx, $opt) = @_;
162 my $jobs = delete $opt->{jobs} if $opt;
163 if (ref($ibx) && ($ibx->{version} || 1) == 2) {
164 eval { require PublicInbox::V2Writable };
165 die "v2 requirements not met: $@\n" if $@;
166 my $v2w = eval { $ibx->importer(0) } || eval {
167 PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
171 $v2w->{parallel} = 0;
173 my $n = $v2w->{partitions};
174 if ($jobs != ($n + 1)) {
176 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
180 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
181 local $SIG{__WARN__} = sub {
182 $warn_cb->($v2w->{current_info}, ': ', @_);
184 $v2w->index_sync($opt);
186 require PublicInbox::SearchIdx;
187 my $s = PublicInbox::SearchIdx->new($ibx, 1);
188 $s->index_sync($opt);
192 sub progress_prepare ($) {
195 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
196 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
197 $opt->{quiet} = !$opt->{verbose};
200 open my $null, '>', '/dev/null' or
201 die "failed to open /dev/null: $!\n";
202 $opt->{1} = fileno($null); # suitable for spawn() redirect
203 $opt->{-dev_null} = $null;
205 $opt->{verbose} ||= 1;
206 $opt->{-progress} = sub { print STDERR @_ };