diff mbox series

mbox: Add the --pipe-each-message argument

Message ID 20221115032439.24876-1-palmer@rivosinc.com (mailing list archive)
State Rejected
Headers show
Series mbox: Add the --pipe-each-message argument | expand

Commit Message

Palmer Dabbelt Nov. 15, 2022, 3:24 a.m. UTC
I've got an oddball mailbox format, so rather than trying to directly
encode it into b4 this just lets b4 pipe each message into a command
that sorts things out.

Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
---
This seems excessively ugly, I couldn't quite figure out why just
` save_git_am_mbox(list(msg))` wasn't doing it so I gave up.  Happy to
go do it right, but this works for me.

Also: this is on top of the patch stack I sent up earlier, sorry if
there's some conflicts.  I haven't jumped to a newer master yet...
---
 b4/command.py |  2 ++
 b4/mbox.py    | 13 +++++++++++++
 man/b4.5.rst  |  2 ++
 3 files changed, 17 insertions(+)

Comments

Konstantin Ryabitsev Nov. 15, 2022, 2:48 p.m. UTC | #1
On Mon, Nov 14, 2022 at 07:24:39PM -0800, Palmer Dabbelt wrote:
> I've got an oddball mailbox format, so rather than trying to directly
> encode it into b4 this just lets b4 pipe each message into a command
> that sorts things out.

I'm not sure I want to go that route -- formail already exists and does it
better, e.g.:

    b4 -q mbox -o- 20221115144005.2478-1-johan+linaro@kernel.org | formail -ds grep '^Subject'

Would that be a suitable approach?

-K
diff mbox series

Patch

diff --git a/b4/command.py b/b4/command.py
index b68cf72..c32be13 100644
--- a/b4/command.py
+++ b/b4/command.py
@@ -36,6 +36,8 @@  def cmd_mbox_common_opts(sp):
                     help='Filename to name the mbox destination')
     sp.add_argument('-M', '--save-as-maildir', dest='maildir', action='store_true', default=False,
                     help='Save as maildir (avoids mbox format ambiguities)')
+    sp.add_argument('--pipe-each-message', dest='pipe_each_message', default=None,
+                    help='Pipe each message to an instance of the given command')
 
 
 def cmd_am_common_opts(sp):
diff --git a/b4/mbox.py b/b4/mbox.py
index 5842bf9..88a7667 100644
--- a/b4/mbox.py
+++ b/b4/mbox.py
@@ -20,6 +20,7 @@  import pathlib
 import tempfile
 import io
 import shlex
+import subprocess
 import argparse
 
 import urllib.parse
@@ -819,6 +820,18 @@  def main(cmdargs):
         logger.info('Saved maildir %s', savename)
         return
 
+    if cmdargs.pipe_each_message:
+        for msg in msgs:
+            proc = subprocess.Popen(cmdargs.pipe_each_message,
+                                    stdin=subprocess.PIPE,
+                                    encoding='utf8')
+            gen = email.generator.Generator(proc.stdin)
+            gen.flatten(msg)
+            gen.write("\n")
+            proc.stdin.close()
+            proc.wait()
+        return
+
     with open(savename, 'w') as fh:
         b4.save_git_am_mbox(msgs, fh)
 
diff --git a/man/b4.5.rst b/man/b4.5.rst
index dfe52e4..5126abf 100644
--- a/man/b4.5.rst
+++ b/man/b4.5.rst
@@ -89,6 +89,8 @@  options:
                         Save as maildir (avoids mbox format ambiguities)
   -f, --filter-dupes
                         When adding messages to existing maildir, filter out duplicates
+  --pipe-each-message
+                        Pipe each message to an instance of the given command
 
 
 *Example*: b4 mbox 20200313231252.64999-1-keescook@chromium.org