]> Sergey Matveev's repositories - public-inbox.git/commitdiff
learn: remove IPC::Run dependency
authorEric Wong <e@80x24.org>
Wed, 15 Jun 2016 00:14:23 +0000 (00:14 +0000)
committerEric Wong <e@80x24.org>
Wed, 15 Jun 2016 00:15:08 +0000 (00:15 +0000)
We'll be relying on our spawn implementation, for now;
since it'll be consistent with the rest of our code and
can optionally take advantage of vfork.

script/public-inbox-learn

index bfbf023363295f15ce95d40bd2de79a3294a2c76..783cf03ad00c4622d2b127f37e4b9cb0495ffb3e 100755 (executable)
@@ -14,17 +14,35 @@ use Email::MIME;
 use Email::MIME::ContentType;
 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
 use PublicInbox::Address;
-use IPC::Run qw/run/;
+use PublicInbox::Spawn qw(spawn);
 my $train = shift or die "usage: $usage\n";
 if ($train !~ /\A(?:ham|spam)\z/) {
        die "`$train' not recognized.\nusage: $usage\n";
 }
 
 my $pi_config = PublicInbox::Config->new;
+my $err;
 my $mime = Email::MIME->new(eval {
        local $/;
        my $data = scalar <STDIN>;
        $data =~ s/\AFrom [^\r\n]*\r?\n//s;
+       eval {
+               my @cmd = (qw(spamc -L), $train);
+               my ($r, $w);
+               pipe($r, $w) or die "pipe failed: $!";
+               open my $null, '>', '/dev/null' or
+                                       die "failed to open /dev/null: $!";
+               my $nullfd = fileno($null);
+               my %rdr = (0 => fileno($r), 1 => $nullfd, 2 => $nullfd);
+               my $pid = spawn(\@cmd, undef, \%rdr);
+               close $null;
+               close $r or die "close \$r failed: $!";
+               print $w $data or die "print \$w failed: $!";
+               close $w or die "close \$w failed: $!";
+               waitpid($pid, 0);
+               die "spamc failed with: $?\n" if $?;
+       };
+       $err = $@;
        $data
 });
 
@@ -43,14 +61,10 @@ if ($train eq "ham") {
        PublicInbox::Filter->run($mime);
 }
 
-my $err = 0;
-my @output = qw(> /dev/null > /dev/null);
-
 # n.b. message may be cross-posted to multiple public-inboxes
 foreach my $recipient (keys %dests) {
        my $dst = $pi_config->lookup($recipient) or next;
        my $git_dir = $dst->{mainrepo} or next;
-       my ($out, $err) = ("", "");
        my $git = PublicInbox::Git->new($git_dir);
        # We do not touch GIT_COMMITTER_* env here so we can track
        # who trained the message.
@@ -74,15 +88,13 @@ foreach my $recipient (keys %dests) {
                $im->add($mime);
        }
        $im->done;
-       my $in = $mime->as_string;
-       if (!run([qw(spamc -L), $train], \$in, @output)) {
-               $err = 1;
-       }
-
-       $err or eval {
+       eval {
                require PublicInbox::SearchIdx;
                PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
        };
 }
 
-exit $err;
+if ($err) {
+       warn $err;
+       exit 1;
+}