From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 34D2920189 for ; Wed, 15 Jun 2016 00:37:44 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 3/9] learn: remove IPC::Run dependency Date: Wed, 15 Jun 2016 00:37:36 +0000 Message-Id: <20160615003742.22538-4-e@80x24.org> In-Reply-To: <20160615003742.22538-1-e@80x24.org> References: <20160615003742.22538-1-e@80x24.org> List-Id: 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 ; $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; +}