unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/9] learn: remove IPC::Run dependency
Date: Wed, 15 Jun 2016 00:37:36 +0000	[thread overview]
Message-ID: <20160615003742.22538-4-e@80x24.org> (raw)
In-Reply-To: <20160615003742.22538-1-e@80x24.org>

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 | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/script/public-inbox-learn b/script/public-inbox-learn
index bfbf023..783cf03 100755
--- a/script/public-inbox-learn
+++ b/script/public-inbox-learn
@@ -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;
+}

  parent reply	other threads:[~2016-06-15  0:37 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-15  0:37 [PATCH 0/9] big mda filter changes Eric Wong
2016-06-15  0:37 ` [PATCH 1/9] drop dependency on File::Path::Expand Eric Wong
2016-06-15  0:37 ` [PATCH 2/9] t/feed.t: make IPC::Run usage optional Eric Wong
2016-06-15  0:37 ` Eric Wong [this message]
2016-06-15  0:37 ` [PATCH 4/9] t/mda.t: remove senseless use of Email::Filter Eric Wong
2016-06-15  0:37 ` [PATCH 5/9] t/mda: use only Maildir for testing Eric Wong
2016-06-15  0:37 ` [PATCH 6/9] mda: precheck no longer depends on Email::Filter Eric Wong
2016-06-15  0:37 ` [PATCH 7/9] filter: begin work on a new filter API Eric Wong
2016-06-15  0:37 ` [PATCH 8/9] emergency: implement new emergency Maildir delivery Eric Wong
2016-06-15  0:37 ` [PATCH 9/9] mda: hook up new filter functionality Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160615003742.22538-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).