From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 28257431FC0 for ; Tue, 21 May 2013 07:24:29 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.098 X-Spam-Level: X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oH2lzD4WqwCW for ; Tue, 21 May 2013 07:24:23 -0700 (PDT) Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E0BB4431FB6 for ; Tue, 21 May 2013 07:24:22 -0700 (PDT) Received: from smtp.qmul.ac.uk ([138.37.6.40]) by mail2.qmul.ac.uk with esmtp (Exim 4.71) (envelope-from ) id 1UenUA-0005zN-Sf; Tue, 21 May 2013 15:24:19 +0100 Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost) by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71) (envelope-from ) id 1UenUA-00010W-Df; Tue, 21 May 2013 15:24:18 +0100 From: Mark Walters To: Austin Clements , notmuch@notmuchmail.org Subject: Re: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes In-Reply-To: <1368851472-5382-3-git-send-email-amdragon@mit.edu> References: <1368851472-5382-1-git-send-email-amdragon@mit.edu> <1368851472-5382-3-git-send-email-amdragon@mit.edu> User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1 (x86_64-pc-linux-gnu) Date: Tue, 21 May 2013 15:24:17 +0100 Message-ID: <87li782yy6.fsf@qmul.ac.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Sender-Host-Address: 93.97.24.31 X-QM-SPAM-Info: Sender has good ham record. :) X-QM-Body-MD5: 186b403f88e335fa20af8637280609d5 (of first 20000 bytes) X-SpamAssassin-Score: -0.2 X-SpamAssassin-SpamBar: / X-SpamAssassin-Report: The QM spam filters have analysed this message to determine if it is spam. We require at least 5.0 points to mark a message as spam. This message scored -0.2 points. Summary of the scoring: * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider * (markwalters1009[at]gmail.com) * -0.2 AWL AWL: From: address is in the auto white-list X-QM-Scan-Virus: ClamAV says the message is clean X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 May 2013 14:24:29 -0000 Hi I am working my way through this set. On Sat, 18 May 2013, Austin Clements wrote: > This provides a new notmuch-lib utility to start an asynchronous > notmuch process that handles redirecting of stderr and checking of the > exit status. This is similar to `notmuch-call-notmuch-json', but for > asynchronous processes (and it leaves output processing to the > caller). > --- > emacs/notmuch-lib.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 69 insertions(+), 4 deletions(-) > > diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el > index 59b1ce3..a543471 100644 > --- a/emacs/notmuch-lib.el > +++ b/emacs/notmuch-lib.el > @@ -383,18 +383,21 @@ signaled error. This function does not return." > (error "%s" (concat msg (when extra > " (see *Notmuch errors* for more details)")))) > > -(defun notmuch-check-async-exit-status (proc msg) > +(defun notmuch-check-async-exit-status (proc msg &optional command err-file) > "If PROC exited abnormally, pop up an error buffer and signal an error. > > This is a wrapper around `notmuch-check-exit-status' for > asynchronous process sentinels. PROC and MSG must be the > -arguments passed to the sentinel." > +arguments passed to the sentinel. COMMAND and ERR-FILE, if > +provided, are passed to `notmuch-check-exit-status'. If COMMAND > +is not provided, it is taken from `process-command'." > (let ((exit-status > (case (process-status proc) > ((exit) (process-exit-status proc)) > ((signal) msg)))) > (when exit-status > - (notmuch-check-exit-status exit-status (process-command proc))))) > + (notmuch-check-exit-status exit-status (or command (process-command proc)) > + nil err-file)))) > > (defun notmuch-check-exit-status (exit-status command &optional output err-file) > "If EXIT-STATUS is non-zero, pop up an error buffer and signal an error. > @@ -448,7 +451,7 @@ You may need to restart Emacs or upgrade your notmuch package.")) > )))) > > (defun notmuch-call-notmuch-json (&rest args) > - "Invoke `notmuch-command' with `args' and return the parsed JSON output. > + "Invoke `notmuch-command' with ARGS and return the parsed JSON output. > > The returned output will represent objects using property lists > and arrays as lists. If notmuch exits with a non-zero status, I think I would prefer that this patch is split here. The stuff above is "obviously correct" and could go in independently of the rest. > @@ -469,6 +472,68 @@ an error." > (json-read))) > (delete-file err-file))))) > > +(defun notmuch-start-notmuch (name buffer sentinel &rest args) > + "Start and return an asynchronous notmuch command. > + > +This starts and returns an asynchronous process running > +`notmuch-command' with ARGS. The exit status is checked via > +`notmuch-check-async-exit-status'. Output written to stderr is > +redirected and displayed when the process exits (even if the > +process exits successfully). NAME and BUFFER are the same as in > +`start-process'. SENTINEL is a process sentinel function to call > +when the process exits, or nil for none. The caller must *not* > +invoke `set-process-sentinel' directly on the returned process, > +as that will interfere with the handling of stderr and the exit > +status." > + > + ;; There is no way (as of Emacs 24.3) to capture stdout and stderr > + ;; separately for asynchronous processes, or even to redirect stderr > + ;; to a file, so we use a trivial shell wrapper to send stderr to a > + ;; temporary file and clean things up in the sentinel. > + (let* ((err-file (make-temp-file "nmerr")) > + ;; Use a pipe > + (process-connection-type nil) > + (proc (apply #'start-process name buffer > + "sh" "-c" > + "ERR=\"$1\"; shift; exec \"$0\" \"$@\" 2>\"$ERR\"" > + notmuch-command err-file args))) I would like some other people to look at this carefully as I won't spot errors in quoting or shell side effects or whether PATH is the same as emacs would use etc. > + (process-put proc 'err-file err-file) > + (process-put proc 'sub-sentinel sentinel) > + (process-put proc 'real-command (cons notmuch-command args)) > + (set-process-sentinel proc #'notmuch-start-notmuch-sentinel) > + proc)) > + > +(defun notmuch-start-notmuch-sentinel (proc event) > + (let ((err-file (process-get proc 'err-file)) > + (sub-sentinel (process-get proc 'sub-sentinel)) > + (real-command (process-get proc 'real-command))) > + (condition-case-unless-debug err > + (progn > + ;; Invoke the sub-sentinel, if any > + (when sub-sentinel > + (funcall sub-sentinel proc event)) > + ;; Check the exit status. This will signal an error if the > + ;; exit status is non-zero. > + (notmuch-check-async-exit-status proc event real-command err-file) > + ;; If that didn't signal an error, then any error output was > + ;; really warning output. Show warnings, if any. > + (let ((warnings > + (with-temp-buffer > + (unless (= (second (insert-file-contents err-file)) 0) > + (end-of-line) > + ;; Show first line; stuff remaining lines in the > + ;; errors buffer. > + (let ((l1 (buffer-substring (point-min) (point)))) > + (skip-chars-forward "\n") > + (cons l1 (unless (eobp) > + (buffer-substring (point) (point-max))))))))) > + (when warnings > + (notmuch-logged-error (car warnings) (cdr warnings))))) > + (error > + ;; Don't signal an error from a sentinel I found this comment confusing as I was thinking of signal in the non-technical sense of "tell the user". Best wishes Mark > + (message "%s" (error-message-string err)))) > + (ignore-errors (delete-file err-file)))) > + > ;; This variable is used only buffer local, but it needs to be > ;; declared globally first to avoid compiler warnings. > (defvar notmuch-show-process-crypto nil) > -- > 1.7.10.4