From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 8NAGBnwA5V5fegAA0tVLHw (envelope-from ) for ; Sat, 13 Jun 2020 16:36:12 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id SJvtAXwA5V6UbwAA1q6Kng (envelope-from ) for ; Sat, 13 Jun 2020 16:36:12 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id D4EF89404E2 for ; Sat, 13 Jun 2020 16:36:10 +0000 (UTC) Received: from localhost ([::1]:59204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jk98b-00029l-E9 for larch@yhetil.org; Sat, 13 Jun 2020 12:36:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33186) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jk98U-00028h-FY for guix-patches@gnu.org; Sat, 13 Jun 2020 12:36:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:59116) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jk98U-0000Vx-6i for guix-patches@gnu.org; Sat, 13 Jun 2020 12:36:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jk98U-0005nw-3D for guix-patches@gnu.org; Sat, 13 Jun 2020 12:36:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#41253] [PATCH v5] guix repl: Add script execution. Resent-From: Konrad Hinsen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 13 Jun 2020 16:36:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41253 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 41253@debbugs.gnu.org Received: via spool by 41253-submit@debbugs.gnu.org id=B41253.159206610622242 (code B ref 41253); Sat, 13 Jun 2020 16:36:02 +0000 Received: (at 41253) by debbugs.gnu.org; 13 Jun 2020 16:35:06 +0000 Received: from localhost ([127.0.0.1]:42429 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jk97L-0005m2-51 for submit@debbugs.gnu.org; Sat, 13 Jun 2020 12:35:06 -0400 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]:51453) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jk97I-0005lm-0v for 41253@debbugs.gnu.org; Sat, 13 Jun 2020 12:34:49 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 2E5EB3CD; Sat, 13 Jun 2020 12:34:42 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sat, 13 Jun 2020 12:34:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.net; h= from:to:subject:in-reply-to:references:date:message-id :mime-version:content-type:content-transfer-encoding; s=fm3; bh= zzM3Q3hC6sSUHU0E50RZ49STmEn4ozjADkXQ2IKEqy8=; b=I5K43kg/WuGiVWle DMYOTpVWaGqAbyzwOlPh7Mrp1Nkra50No2LsdI/en7caySwpwkRQz2o+p6xm7ebt 1fPWZoLwuae7fxezIRKYQ72z4eQ1gqfuF4QX+ZM9uM83yT9JeCkryCrO/h4H+9bB 8gMjkyAMUoHY9j3dS8V3vJajA+YIwblfXZTPIOF66VetsWLcIlVvXJcm/SvyRFIW 3DgWktemR6G7381ZD0wNneV65xaWNAXn4qdIofdEM1lMGn+LQb6757jO//jiJlTs WX03bP5wrJRIyGC1qJqHfONJGBPT/ONziguKfq2BicDd1n8pYtjyAulacriVIhJj TXKoyg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=zzM3Q3hC6sSUHU0E50RZ49STmEn4ozjADkXQ2IKEq y8=; b=oMXqr1/HQjs02M9d4t02lEeHHFE8WfJ0zEpyFAqmVm9h87l26lAM0TX9O p9B9zuheKYgRuJcQv1N5qZOUT/0S6VYbdVMbEqp56gBkrAC+4C+oRbt9wKyUYA0i ao4AhxtvYYW2YnnmcMt1KBTqt9WmTL9ZOf/ZE9vpzTNyDqIvs4d6E9VDd4lCppSQ Kcgx0s5Oh+oDfMyYt/e0njM7dye0olK+T1nymzRc1lps0mnfmgAHjgQi8fgSkIEX h0pbuy4UY2EENFEvwHBbTGVvshdWuOdlTtP42N9YHv5sE4zr5nJazK4mqVCXnvao B3fG4J836aOyUepW94/aBNXkTOuRg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudeifedguddtfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufgjfhffkfggtgfgsehtqh ertddttdejnecuhfhrohhmpefmohhnrhgrugcujfhinhhsvghnuceokhhonhhrrggurdhh ihhnshgvnhesfhgrshhtmhgrihhlrdhnvghtqeenucggtffrrghtthgvrhhnpeegfefhfe ekhedtkedutdfftdeltdevffffiefhkefgkeefffelfeevkeehgeehieenucffohhmrghi nhepghhnuhdrohhrghenucfkphepjeejrddvtdehrddugeefrdduudehnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepkhhonhhrrggurdhhihhn shgvnhesfhgrshhtmhgrihhlrdhnvght X-ME-Proxy: Received: from KHs-MacBook (115.143.205.77.rev.sfr.net [77.205.143.115]) by mail.messagingengine.com (Postfix) with ESMTPA id 859623060FE7; Sat, 13 Jun 2020 12:34:40 -0400 (EDT) From: Konrad Hinsen In-Reply-To: References: <20200514162537.29071-1-zimon.toutoune@gmail.com> Date: Sat, 13 Jun 2020 18:34:38 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -1.0 (-) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (body hash did not verify) header.d=fastmail.net header.s=fm3 header.b=I5K43kg/; dkim=fail (body hash did not verify) header.d=messagingengine.com header.s=fm3 header.b=oMXqr1/H; dmarc=fail reason="SPF not aligned (relaxed)" header.from=fastmail.net (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: 0.09 X-TUID: K2gFesWUgcOi * guix/scripts/repl.scm: Add filename options for script execution. * doc/guix.texi (Invoking guix repl): Document it. * tests/guix-repl.sh: Test it. * Makefile.am: (SH_TESTS): Add it. --- Makefile.am | 1 + doc/guix.texi | 51 +++++++++++++++++++++----- guix/scripts/repl.scm | 84 +++++++++++++++++++++++++++++-------------- tests/guix-repl.sh | 84 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 34 deletions(-) create mode 100644 tests/guix-repl.sh diff --git a/Makefile.am b/Makefile.am index 9cf9318e8a..8988cdfa12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -477,6 +477,7 @@ SH_TESTS =3D \ tests/guix-environment-container.sh \ tests/guix-graph.sh \ tests/guix-describe.sh \ + tests/guix-repl.sh \ tests/guix-lint.sh =20 TESTS =3D $(SCM_TESTS) $(SH_TESTS) diff --git a/doc/guix.texi b/doc/guix.texi index 15e077a41c..8ad3a833c6 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -239,7 +239,7 @@ Programming Interface * Derivations:: Low-level interface to package derivations. * The Store Monad:: Purely functional interface to the store. * G-Expressions:: Manipulating build expressions. -* Invoking guix repl:: Fiddling with Guix interactively. +* Invoking guix repl:: Programming Guix in Guile =20 Defining Packages =20 @@ -5474,7 +5474,7 @@ package definitions. * Derivations:: Low-level interface to package derivations. * The Store Monad:: Purely functional interface to the store. * G-Expressions:: Manipulating build expressions. -* Invoking guix repl:: Fiddling with Guix interactively. +* Invoking guix repl:: Programming Guix in Guile @end menu =20 @node Package Modules @@ -8248,12 +8248,47 @@ has an associated gexp compiler, such as a @code{}. @node Invoking guix repl @section Invoking @command{guix repl} =20 -@cindex REPL, read-eval-print loop -The @command{guix repl} command spawns a Guile @dfn{read-eval-print loop} -(REPL) for interactive programming (@pxref{Using Guile Interactively,,, gu= ile, -GNU Guile Reference Manual}). Compared to just launching the @command{gui= le} +@cindex REPL, read-eval-print loop, script +The @command{guix repl} command makes it easier to program Guix in Guile +by launching a Guile @dfn{read-eval-print loop} (REPL) for interactive +programming (@pxref{Using Guile Interactively,,, guile, +GNU Guile Reference Manual}), or by running Guile scripts +(@pxref{Running Guile Scripts,,, guile, +GNU Guile Reference Manual}). +Compared to just launching the @command{guile} command, @command{guix repl} guarantees that all the Guix modules and all = its -dependencies are available in the search path. You can use it this way: +dependencies are available in the search path. + +The general syntax is: + +@example +guix repl @var{options} [@var{file} @var{args}] +@end example + +When a @var{file} argument is provided, @var{file} is +executed as a Guile scripts: + +@example +guix repl my-script.scm +@end example + +To pass arguments to the script, use @code{--} to prevent them from +being interpreted as arguments to @command{guix repl} itself: + +@example +guix repl -- my-script.scm --input=3Dfoo.txt +@end example + +To make a script executable directly from the shell, using the guix +executable that is on the user's search path, add the following two +lines at the top of the script: + +@example +@code{#!/usr/bin/env -S guix repl --} +@code{!#} +@end example + +Without a file name argument, a Guile REPL is started: =20 @example $ guix repl @@ -8302,7 +8337,7 @@ Add @var{directory} to the front of the package modul= e search path (@pxref{Package Modules}). =20 This allows users to define their own packages and make them visible to -the command-line tool. +the script or REPL. =20 @item -q Inhibit loading of the @file{~/.guile} file. By default, that diff --git a/guix/scripts/repl.scm b/guix/scripts/repl.scm index ff1f208894..51fffa278a 100644 --- a/guix/scripts/repl.scm +++ b/guix/scripts/repl.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright =C2=A9 2018, 2019, 2020 Ludovic Court=C3=A8s ;;; Copyright =C2=A9 2020 Simon Tournier +;;; Copyright =C2=A9 2020 Konrad Hinsen ;;; ;;; This file is part of GNU Guix. ;;; @@ -22,6 +23,7 @@ #:use-module (guix scripts) #:use-module (guix repl) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) #:use-module (srfi srfi-37) #:use-module (ice-9 match) #:use-module (rnrs bytevectors) @@ -32,7 +34,8 @@ =20 ;;; Commentary: ;;; -;;; This command provides a Guile REPL +;;; This command provides a Guile script runner and REPL in an environment +;;; that contains all the modules comprising Guix. =20 (define %default-options `((type . guile))) @@ -63,8 +66,9 @@ =20 =20 (define (show-help) - (display (G_ "Usage: guix repl [OPTIONS...] -Start a Guile REPL in the Guix execution environment.\n")) + (display (G_ "Usage: guix repl [OPTIONS...] [-- FILE ARGS...] +In the Guix execution environment, run FILE as a Guile script with +command-line arguments ARGS. If no FILE is given, start a Guile REPL.\n")) (display (G_ " -t, --type=3DTYPE start a REPL of the given TYPE")) (display (G_ " @@ -135,12 +139,13 @@ call THUNK." (define (guix-repl . args) (define opts - ;; Return the list of package names. (args-fold* args %options (lambda (opt name arg result) (leave (G_ "~A: unrecognized option~%") name)) (lambda (arg result) - (leave (G_ "~A: extraneous argument~%") arg)) + (append `((script . ,arg) + (ignore-dot-guile . #t)) + result)) %default-options)) =20 (define user-config @@ -148,28 +153,55 @@ call THUNK." (lambda (home) (string-append home "/.guile")))) =20 + (define (set-user-module) + (set-current-module user-module) + (when (and (not (assoc-ref opts 'ignore-dot-guile?)) + user-config + (file-exists? user-config)) + (load user-config))) + + (define script + (reverse + (filter-map (match-lambda + (('script . script) script) + (_ #f)) + opts))) + + (define script-file + (let ((file (car script)) + (directory (getcwd))) + (canonicalize-path + (cond ((string-prefix? "/" file) file) + (else (string-append directory "/" file)))))) + (with-error-handling - (let ((type (assoc-ref opts 'type))) - (call-with-connection (assoc-ref opts 'listen) - (lambda () - (case type - ((guile) - (save-module-excursion - (lambda () - (set-current-module user-module) - (when (and (not (assoc-ref opts 'ignore-dot-guile?)) - user-config - (file-exists? user-config)) - (load user-config)) - - ;; Do not exit repl on SIGINT. - ((@@ (ice-9 top-repl) call-with-sigint) - (lambda () - (start-repl)))))) - ((machine) - (machine-repl)) - (else - (leave (G_ "~a: unknown type of REPL~%") type)))))))) + + (unless (null? script) + ;; Run script + (save-module-excursion + (lambda () + (set-program-arguments (cons script-file (cdr script))) + (set-user-module) + (load script-file)))) + + (when (null? script) + ;; Start REPL + (let ((type (assoc-ref opts 'type))) + (call-with-connection (assoc-ref opts 'listen) + (lambda () + (case type + ((guile) + (save-module-excursion + (lambda () + (set-user-module) + ;; Do not exit repl on SIGINT. + ((@@ (ice-9 top-repl) call-with-sigint) + (lambda () + (start-repl)))))) + ((machine) + (machine-repl)) + (else + (leave (G_ "~a: unknown type of REPL~%") type))))))))) =20 ;; Local Variables: ;; eval: (put 'call-with-connection 'scheme-indent-function 1) diff --git a/tests/guix-repl.sh b/tests/guix-repl.sh new file mode 100644 index 0000000000..e1c2b8241f --- /dev/null +++ b/tests/guix-repl.sh @@ -0,0 +1,84 @@ +# GNU Guix --- Functional package management for GNU +# Copyright =C2=A9 2020 Simon Tournier +# Copyright =C2=A9 2020 Konrad Hinsen +# +# This file is part of GNU Guix. +# +# GNU Guix is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or (at +# your option) any later version. +# +# GNU Guix is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Guix. If not, see . + +# +# Test the `guix repl' command-line utility. +# + +guix repl --version + +test_directory=3D"`mktemp -d`" +export test_directory +trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT + +tmpfile=3D"$test_directory/foo.scm" +rm -f "$tmpfile" +trap 'rm -f "$tmpfile"' EXIT + +module_dir=3D"t-guix-repl-$$" +mkdir "$module_dir" +trap 'rm -rf "$module_dir"' EXIT + + +cat > "$tmpfile"< "$module_dir/foo.scm"< "$tmpfile"< "$tmpfile"< "$tmpfile"<