From 5e9931c213685938c79d855856a16ff9131772d5 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Thu, 28 May 2020 12:00:38 -0400 Subject: Add --no-cover and -o - to output to stdout By request, provide a way to output the results of b4 am to stdout. This way it can be piped straight to "git am". E.g.: b4 diff 20200526205322.23465-1-mic@digikod.net -o - | git am Requested-by: Rob Herring Signed-off-by: Konstantin Ryabitsev --- b4/__init__.py | 6 ++++-- b4/command.py | 4 +++- b4/diff.py | 2 +- b4/mbox.py | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 12 deletions(-) (limited to 'b4') diff --git a/b4/__init__.py b/b4/__init__.py index a462fe9..5858a0d 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -200,7 +200,7 @@ class LoreMailbox: if reused: continue # Try to backfill from that project - tmp_mbox = mkstemp()[1] + tmp_mbox = mkstemp('b4-backfill-mbox')[1] get_pi_thread_by_msgid(patch.msgid, tmp_mbox, useproject=projmap[entry[1]]) mbx = mailbox.mbox(tmp_mbox) was = len(self.msgid_map) @@ -2003,7 +2003,9 @@ def get_pi_thread_by_msgid(msgid, savefile, useproject=None, nocache=False): logger.debug('t_mbx_url=%s', t_mbx_url) logger.critical('Grabbing thread from %s', projurl.split('://')[1]) - in_mbxf = get_pi_thread_by_url(t_mbx_url, '%s-loose' % savefile, nocache=nocache) + + tmp_mbox = mkstemp('b4-lookup-mbox')[1] + in_mbxf = get_pi_thread_by_url(t_mbx_url, tmp_mbox, nocache=nocache) if not in_mbxf: return None in_mbx = mailbox.mbox(in_mbxf) diff --git a/b4/command.py b/b4/command.py index d0d084e..ffa58ef 100644 --- a/b4/command.py +++ b/b4/command.py @@ -17,7 +17,7 @@ def cmd_mbox_common_opts(sp): sp.add_argument('msgid', nargs='?', help='Message ID to process, or pipe a raw message') sp.add_argument('-o', '--outdir', default='.', - help='Output into this directory') + help='Output into this directory (or use - to output mailbox contents to stdout)') sp.add_argument('-p', '--use-project', dest='useproject', default=None, help='Use a specific project instead of guessing (linux-mm, linux-hardening, etc)') sp.add_argument('-c', '--check-newer-revisions', dest='checknewer', action='store_true', default=False, @@ -111,6 +111,8 @@ def cmd(): sp_am.add_argument('-3', '--prep-3way', dest='threeway', action='store_true', default=False, help='Prepare for a 3-way merge ' '(tries to ensure that all index blobs exist by making a fake commit range)') + sp_am.add_argument('--no-cover', dest='nocover', action='store_true', default=False, + help='Do not save the cover letter (on by default when using -o -)') sp_am.set_defaults(func=cmd_am) # b4 attest diff --git a/b4/diff.py b/b4/diff.py index 33a8b0c..7e9125b 100644 --- a/b4/diff.py +++ b/b4/diff.py @@ -57,7 +57,7 @@ def diff_same_thread_series(cmdargs): lmbx.add_message(msg) mbx.close() - os.unlink(mboxfile) + os.unlink(savefile) if wantvers and len(wantvers) == 1: upper = max(lmbx.series.keys()) diff --git a/b4/mbox.py b/b4/mbox.py index 50e1471..3e69a32 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -15,6 +15,7 @@ import re import time import json import fnmatch +import shutil import urllib.parse import xml.etree.ElementTree @@ -29,6 +30,8 @@ logger = b4.logger def mbox_to_am(mboxfile, cmdargs): config = b4.get_main_config() outdir = cmdargs.outdir + if outdir == '-': + cmdargs.nocover = True wantver = cmdargs.wantver wantname = cmdargs.wantname covertrailers = cmdargs.covertrailers @@ -60,11 +63,16 @@ def mbox_to_am(mboxfile, cmdargs): slug = lser.get_slug(extended=True) gitbranch = lser.get_slug(extended=False) - am_filename = os.path.join(outdir, '%s.mbx' % slug) - am_cover = os.path.join(outdir, '%s.cover' % slug) + if outdir != '-': + am_filename = os.path.join(outdir, '%s.mbx' % slug) + am_cover = os.path.join(outdir, '%s.cover' % slug) - if os.path.exists(am_filename): - os.unlink(am_filename) + if os.path.exists(am_filename): + os.unlink(am_filename) + else: + # Create a temporary file that we will remove later + am_filename = mkstemp('b4-am-stdout')[1] + am_cover = None logger.info('---') if cmdargs.cherrypick: @@ -96,6 +104,7 @@ def mbox_to_am(mboxfile, cmdargs): cherrypick = list(b4.parse_int_range(cmdargs.cherrypick, upper=len(lser.patches)-1)) else: cherrypick = None + logger.critical('Writing %s', am_filename) mbx = mailbox.mbox(am_filename) am_mbx = lser.save_am_mbox(mbx, noaddtrailers=cmdargs.noaddtrailers, @@ -144,7 +153,7 @@ def mbox_to_am(mboxfile, cmdargs): if not lser.complete: logger.critical('WARNING: Thread incomplete!') - if lser.has_cover: + if lser.has_cover and not cmdargs.nocover: lser.save_cover(am_cover) top_msgid = None @@ -218,9 +227,13 @@ def mbox_to_am(mboxfile, cmdargs): logger.critical(' git am %s', am_filename) am_mbx.close() - thanks_record_am(lser, cherrypick=cherrypick) + if cmdargs.outdir == '-': + logger.info('---') + with open(am_filename, 'r') as fh: + shutil.copyfileobj(fh, sys.stdout) + os.unlink(am_filename) - return am_filename + thanks_record_am(lser, cherrypick=cherrypick) def thanks_record_am(lser, cherrypick=None): @@ -461,7 +474,9 @@ def main(cmdargs): msgid = b4.get_msgid(cmdargs) wantname = cmdargs.wantname outdir = cmdargs.outdir - if wantname: + if outdir == '-': + savefile = mkstemp('b4-mbox')[1] + elif wantname: savefile = os.path.join(outdir, wantname) else: # Save it into msgid.mbox @@ -493,3 +508,9 @@ def main(cmdargs): mbx = mailbox.mbox(threadmbox) logger.critical('Saved %s', threadmbox) logger.critical('%s messages in the thread', len(mbx)) + mbx.close() + if cmdargs.outdir == '-': + logger.info('---') + with open(threadmbox, 'r') as fh: + shutil.copyfileobj(fh, sys.stdout) + os.unlink(threadmbox) -- cgit v1.2.3