Message ID | 20220514230508.27885-1-plattfot@posteo.net |
---|---|
State | New |
Headers |
Return-Path: <guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org> X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id C76A927BBEA; Sun, 15 May 2022 00:06:44 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 92F2627BBE9 for <patchwork@mira.cbaines.net>; Sun, 15 May 2022 00:06:44 +0100 (BST) Received: from localhost ([::1]:41222 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from <guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org>) id 1nq0qR-0007Zd-Qb for patchwork@mira.cbaines.net; Sat, 14 May 2022 19:06:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33590) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1nq0pn-0007J2-CQ for guix-patches@gnu.org; Sat, 14 May 2022 19:06:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:53811) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1nq0pm-0001UJ-J3 for guix-patches@gnu.org; Sat, 14 May 2022 19:06:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1nq0pm-0002ki-Ev for guix-patches@gnu.org; Sat, 14 May 2022 19:06:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#55420] [PATCH 1/2] guix: emacs-utils: Add emacs-batch-script. References: <20220514230017.27372-1-plattfot@posteo.net> In-Reply-To: <20220514230017.27372-1-plattfot@posteo.net> Resent-From: Fredrik Salomonsson <plattfot@posteo.net> Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org> Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 14 May 2022 23:06:02 +0000 Resent-Message-ID: <handler.55420.B55420.165256952210513@debbugs.gnu.org> Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55420 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 55420@debbugs.gnu.org Cc: Fredrik Salomonsson <plattfot@posteo.net> Received: via spool by 55420-submit@debbugs.gnu.org id=B55420.165256952210513 (code B ref 55420); Sat, 14 May 2022 23:06:02 +0000 Received: (at 55420) by debbugs.gnu.org; 14 May 2022 23:05:22 +0000 Received: from localhost ([127.0.0.1]:47705 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>) id 1nq0p8-0002jU-IL for submit@debbugs.gnu.org; Sat, 14 May 2022 19:05:22 -0400 Received: from mout01.posteo.de ([185.67.36.65]:57871) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <plattfot@posteo.net>) id 1nq0p7-0002jG-AC for 55420@debbugs.gnu.org; Sat, 14 May 2022 19:05:21 -0400 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 81DBB240026 for <55420@debbugs.gnu.org>; Sun, 15 May 2022 01:05:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1652569515; bh=nLx4SCMIiphivWa8LKGj/QosQ91bRVpPl9ClRBXDdgo=; h=From:To:Cc:Subject:Date:From; b=QlanBXaZby38Pny+B9mYRgIDbAEHP/ixHxJ4GqrEqaf9MJLUQGFr1f/G2AwSlXVeZ cdUD5PRssgyD6ronRXwUcitOkkfSanmftJhPyk0Q6pEMZk8+m7kipuBKoLjkxC4Xgx 0bhrsy5tCwSUInxp6ZNQrHpsAKh/fDugxzn/oWiHkBjdXKQ9dFKllRxgMgchksyet5 +Lvr1+8O+FYUBSehIfmGK09t3J0DzjS9qrSROVRf4oPkPqQW+eA5JQ1VSWSKShJL0h L3fmpxsyWQhpi9m4Y1jP+fqAPwRpFG8T/SHkpSifiE/WxBzqXQI/fV3sPDjVVrfw9N 4QuQDCPpzeibg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4L11Ly4LH7z9rxD; Sun, 15 May 2022 01:05:14 +0200 (CEST) From: Fredrik Salomonsson <plattfot@posteo.net> Date: Sat, 14 May 2022 23:05:07 +0000 Message-Id: <20220514230508.27885-1-plattfot@posteo.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: <guix-patches.gnu.org> List-Unsubscribe: <https://lists.gnu.org/mailman/options/guix-patches>, <mailto:guix-patches-request@gnu.org?subject=unsubscribe> List-Archive: <https://lists.gnu.org/archive/html/guix-patches> List-Post: <mailto:guix-patches@gnu.org> List-Help: <mailto:guix-patches-request@gnu.org?subject=help> List-Subscribe: <https://lists.gnu.org/mailman/listinfo/guix-patches>, <mailto:guix-patches-request@gnu.org?subject=subscribe> Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" <guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org> X-getmail-retrieved-from-mailbox: Patches |
Series |
Add a function to parse emacs elisp's package header
|
|
Commit Message
Fredrik Salomonsson
May 14, 2022, 11:05 p.m. UTC
* guix/build/emacs-utils.scm (emacs-batch-script): New procedure. --- guix/build/emacs-utils.scm | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Comments
Hi Fredrik, The patches LGTM modulo cosmetic issues: Fredrik Salomonsson <plattfot@posteo.net> skribis: > * guix/build/emacs-utils.scm (emacs-batch-script): New procedure. [...] > +(define (emacs-batch-script expr) > + "Execute the Elisp code EXPR in Emacs batch mode and return output." > + (call-with-port > + (open-pipe* > + OPEN_READ > + (%emacs) "--quick" "--batch" > + (string-append "--eval=" (expr->string expr))) > + read-string)) I suggest something like: (let* ((pipe (open-pipe* …)) (output (read-string pipe)) (status (close-pipe pipe))) (unless (zero? status) ;; Use SRFI-34 + either a &message condition or (better) ;; a dedicate SRFI-35 condition type for the error. (raise (condition …))) output) That way, execution failures would be caught and reported. Ludo’.
Hi Ludo, Ludovic Courtès <ludo@gnu.org> writes: > Hi Fredrik, > > The patches LGTM modulo cosmetic issues: > > Fredrik Salomonsson <plattfot@posteo.net> skribis: > >> * guix/build/emacs-utils.scm (emacs-batch-script): New procedure. > > [...] > >> +(define (emacs-batch-script expr) >> + "Execute the Elisp code EXPR in Emacs batch mode and return output." >> + (call-with-port >> + (open-pipe* >> + OPEN_READ >> + (%emacs) "--quick" "--batch" >> + (string-append "--eval=" (expr->string expr))) >> + read-string)) > > I suggest something like: > > (let* ((pipe (open-pipe* …)) > (output (read-string pipe)) > (status (close-pipe pipe))) > (unless (zero? status) > ;; Use SRFI-34 + either a &message condition or (better) > ;; a dedicate SRFI-35 condition type for the error. > (raise (condition …))) > output) > > That way, execution failures would be caught and reported. Thank you for the feedback. I update the procedure to use a dedicated SRFI-35 condition type. But I cannot figure out how to capture the error message from emacs, as I want the condition type to contain both the expression as well as the error you get from emacs. Here is what I have so far: (define-condition-type &emacs-batch-error &error emacs-batch-error? (expression emacs-batch-error-expression) (message emacs-batch-error-message)) (define (emacs-batch-script expr) "Execute the Elisp code EXPR in Emacs batch mode and return output." (let* ((error-pipe (open-output-string)) (pipe (with-error-to-port error-pipe (lambda () (open-pipe* OPEN_READ (%emacs) "--quick" "--batch" (string-append "--eval=" (expr->string expr)))))) (output (read-string pipe)) (error (get-output-string error-pipe)) (status (close-pipe pipe))) (unless (zero? status) (raise (condition (&emacs-batch-error (expression expr) (message error))))) output)) Here is the output when I test it out in the guix repl: -------------------------------------------------------------------------------- scheme@(guix-user)> (use-modules (guix build emacs-utils)) (use-modules (guix build emacs-utils)) scheme@(guix-user)> (emacs-batch-script '(prog (princ "hello"))) (emacs-batch-script '(prog (princ "hello"))) ice-9/boot-9.scm:1685:16: In procedure raise-exception: Wrong type (expecting exact integer): #<&emacs-batch-error expression: (prog (princ "hello")) message: ""> Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guix-user) [1]> -------------------------------------------------------------------------------- Note the message is empty in #<&emacs-batch-error…>. It should contain: Debugger entered--Lisp error: (void-function prog) (prog (princ "hello")) command-line-1(("--eval=(prog (princ \"hello\"))")) command-line() normal-top-level() Any idea what I'm doing wrong? Thanks
Hi, Fredrik Salomonsson <plattfot@posteo.net> skribis: > Here is what I have so far: > > (define-condition-type &emacs-batch-error &error > emacs-batch-error? > (expression emacs-batch-error-expression) > (message emacs-batch-error-message)) > > (define (emacs-batch-script expr) > "Execute the Elisp code EXPR in Emacs batch mode and return output." > (let* ((error-pipe (open-output-string)) > (pipe (with-error-to-port error-pipe > (lambda () > (open-pipe* > OPEN_READ > (%emacs) "--quick" "--batch" > (string-append "--eval=" (expr->string expr)))))) > (output (read-string pipe)) > (error (get-output-string error-pipe)) > (status (close-pipe pipe))) > (unless (zero? status) > (raise (condition (&emacs-batch-error > (expression expr) > (message error))))) > output)) Unfortunately ‘open-pipe*’ is not smart enough to redirect stderr to a non-file port (a string port in this case). One way around it would be to merge stdout and stderr, like so: (parameterize ((current-error-port (current-output-port))) (open-pipe* …)) but then you get both on the same stream, which could be a problem for instance if Emacs emits warnings and such. You could work around it by establishing a second pipe: (match (pipe) ((stderr-input . stderr-output) (parameterize ((current-error-port stderr-output)) (open-pipe* …)))) Here you should be able to read, in the parent process, from ‘stderr-input’. Another option is to not try to capture stderr: after all, that’ll get printed on the screen anyway. HTH, Ludo’.
Hi Ludo, Ludovic Courtès <ludo@gnu.org> writes: > Hi, > > Fredrik Salomonsson <plattfot@posteo.net> skribis: > >> Here is what I have so far: >> >> (define-condition-type &emacs-batch-error &error >> emacs-batch-error? >> (expression emacs-batch-error-expression) >> (message emacs-batch-error-message)) >> >> (define (emacs-batch-script expr) >> "Execute the Elisp code EXPR in Emacs batch mode and return output." >> (let* ((error-pipe (open-output-string)) >> (pipe (with-error-to-port error-pipe >> (lambda () >> (open-pipe* >> OPEN_READ >> (%emacs) "--quick" "--batch" >> (string-append "--eval=" (expr->string expr)))))) >> (output (read-string pipe)) >> (error (get-output-string error-pipe)) >> (status (close-pipe pipe))) >> (unless (zero? status) >> (raise (condition (&emacs-batch-error >> (expression expr) >> (message error))))) >> output)) > > Unfortunately ‘open-pipe*’ is not smart enough to redirect stderr to a > non-file port (a string port in this case). > > One way around it would be to merge stdout and stderr, like so: > > (parameterize ((current-error-port (current-output-port))) > (open-pipe* …)) > > but then you get both on the same stream, which could be a problem for > instance if Emacs emits warnings and such. > > You could work around it by establishing a second pipe: > > (match (pipe) > ((stderr-input . stderr-output) > (parameterize ((current-error-port stderr-output)) > (open-pipe* …)))) > > Here you should be able to read, in the parent process, from > ‘stderr-input’. > > Another option is to not try to capture stderr: after all, that’ll get > printed on the screen anyway. I just sent in v2 of the patches. Changes are: - emacs-batch-script will now raise an &emacs-batch-error if emacs batch script fails. I opted to capture the stderr and package that up in the condition. Thank you for the pointers! I removed the expression slot I had in my prototype. It felt redundant, emacs error message should be enough. - tests/build-emacs-utils.scm: I added tests for the two new procedures. It was good that I added the tests as I had missed to use (srfi srfi-34) in build/emacs-utils.scm during my prototyping and that generated the cryptic error: Wrong type (expecting exact integer): #<&emacs-batch-error…> Took me a while to figure that one out. But now all is good. The tests are passing. Thanks again.
diff --git a/guix/build/emacs-utils.scm b/guix/build/emacs-utils.scm index 60a754b9e9..8d40b9e139 100644 --- a/guix/build/emacs-utils.scm +++ b/guix/build/emacs-utils.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2014 Alex Kost <alezost@gmail.com> ;;; Copyright © 2018, 2020, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com> ;;; Copyright © 2019 Liliana Marie Prikler <liliana.prikler@gmail.com> +;;; Copyright © 2022 Fredrik Salomonsson <plattfot@posteo.net> ;;; ;;; This file is part of GNU Guix. ;;; @@ -22,10 +23,13 @@ (define-module (guix build emacs-utils) #:use-module (guix build utils) #:use-module (ice-9 format) + #:use-module (ice-9 popen) + #:use-module (ice-9 rdelim) #:export (%emacs emacs-batch-eval emacs-batch-edit-file emacs-batch-disable-compilation + emacs-batch-script emacs-generate-autoloads emacs-byte-compile-directory @@ -69,6 +73,15 @@ (define (emacs-batch-disable-compilation file) (add-file-local-variable 'no-byte-compile t) (basic-save-buffer)))) +(define (emacs-batch-script expr) + "Execute the Elisp code EXPR in Emacs batch mode and return output." + (call-with-port + (open-pipe* + OPEN_READ + (%emacs) "--quick" "--batch" + (string-append "--eval=" (expr->string expr))) + read-string)) + (define (emacs-generate-autoloads name directory) "Generate autoloads for Emacs package NAME placed in DIRECTORY." (let* ((file (string-append directory "/" name "-autoloads.el"))