From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id yPXJIl5xvV5CDwAA0tVLHw (envelope-from ) for ; Thu, 14 May 2020 16:27:10 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id GHo3HV5xvV4KAgAAB5/wlQ (envelope-from ) for ; Thu, 14 May 2020 16:27:10 +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 06C2C940D60 for ; Thu, 14 May 2020 16:27:10 +0000 (UTC) Received: from localhost ([::1]:41908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jZGhR-0005gk-0m for larch@yhetil.org; Thu, 14 May 2020 12:27:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jZGhK-0005fA-1h for guix-patches@gnu.org; Thu, 14 May 2020 12:27:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:51254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jZGhJ-0008WE-OS for guix-patches@gnu.org; Thu, 14 May 2020 12:27:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jZGhJ-0000Sm-Lh for guix-patches@gnu.org; Thu, 14 May 2020 12:27:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#41253] [PATCH v2] guix repl: Add script execution. References: In-Reply-To: Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 14 May 2020 16:27:01 +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 Cc: Konrad Hinsen Received: via spool by 41253-submit@debbugs.gnu.org id=B41253.15894735711714 (code B ref 41253); Thu, 14 May 2020 16:27:01 +0000 Received: (at 41253) by debbugs.gnu.org; 14 May 2020 16:26:11 +0000 Received: from localhost ([127.0.0.1]:34567 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jZGgM-0000RJ-1D for submit@debbugs.gnu.org; Thu, 14 May 2020 12:26:11 -0400 Received: from mail-wm1-f54.google.com ([209.85.128.54]:52655) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jZGgJ-0000Qm-Cw for 41253@debbugs.gnu.org; Thu, 14 May 2020 12:26:00 -0400 Received: by mail-wm1-f54.google.com with SMTP id m24so21632726wml.2 for <41253@debbugs.gnu.org>; Thu, 14 May 2020 09:25:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PIxbLdsDWgyWhB9Lkvptx87D9xgCo3M98WCl4Kqcitg=; b=CzWxU2ToEnvA+Nj/K7+8WGMP2rkqdmdxzB+RsYBhuHMC9o/xGE+Eq7kcqTk7gzeFv7 qiFYD44wsguE9vyR3oNnu42YE4LSiz/bzr0wGLLSH7tlopzVskXci1kb1y1DwWlbNtXV 08fVVfX+rNxHozopWaXAjmV18g6r8IhqPmENKc6AmqEDheWyK1GHexG/3KQERxmFTX61 fmB73W1ezsESoSXQq48Cbpyc1Mu7ZZxQuULEl60u4qRCjsZuMdM4HDf15lFGmWz+oWY0 vmOc6ydbXgbXnaAtEgkkPsS4KwXLGWPTchzvn+IIvF9mf8D3kIKLlZpzxLE7lmgjUfGr hW1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PIxbLdsDWgyWhB9Lkvptx87D9xgCo3M98WCl4Kqcitg=; b=rPOTlQ0+znBofjX3NxZJ3nz+n/PRd6wE+MQhQc0vKCevcn9bldBfjyPyKg/0fSusFx q5vPHhXQvjCkyhjcBDMLw91O1idStEsv/pPEuug8at1vB081v8UmJw+plcNbsyAvg+7q H0b5XQJOqs+vk+ihrEUa6inZt7IOvhcLRSFdFjkQjrJrIbAvUDGxwDbvX3JnIlu6WAe7 Ay59y6JryQAXEsvNSpv1EzPJzBcdialOy/g+vXstv6IcrjvT8PCJn5kf4yMc3HF/PsUL 36LgSO1F+MCB/cU1nmF3WsOseYkUY1fXulOcJCEvXVydcicmf3a+IclKI3uNm01L1+VI y3Bg== X-Gm-Message-State: AGi0PubNsEi3Svg24A0aPnuRDQIguVDCeDuRKyRr+9hDdf8j0aV5kQOq He4tugxk1mmnjBpug+pXNK+LfIbV X-Google-Smtp-Source: APiQypKH50i94ypMpvlPgAvrXs7pL4fP9CO4fJIo/rpxJEgUNunIr9FPYUBR858N6xd9zvv7XJXE5A== X-Received: by 2002:a1c:f207:: with SMTP id s7mr21973009wmc.14.1589473553091; Thu, 14 May 2020 09:25:53 -0700 (PDT) Received: from localhost.localdomain (57.246.195.77.rev.sfr.net. [77.195.246.57]) by smtp.gmail.com with ESMTPSA id 2sm4871243wre.25.2020.05.14.09.25.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2020 09:25:52 -0700 (PDT) From: zimoun Date: Thu, 14 May 2020 18:25:37 +0200 Message-Id: <20200514162537.29071-1-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.26.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) 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=gmail.com header.s=20161025 header.b=CzWxU2To; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (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: 1.59 X-TUID: XIrMeH0BbVV5 From: Konrad Hinsen * 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 | 1 + doc/guix.texi | 38 +++++++++++++++----- guix/scripts/repl.scm | 80 ++++++++++++++++++++++++++----------------- tests/guix-repl.sh | 70 +++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 40 deletions(-) create mode 100644 tests/guix-repl.sh diff --git a/Makefile.am b/Makefile.am index 752445afcb..eb4e360c6d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -471,6 +471,7 @@ SH_TESTS = \ tests/guix-environment-container.sh \ tests/guix-graph.sh \ tests/guix-describe.sh \ + tests/guix-repl.sh \ tests/guix-lint.sh TESTS = $(SCM_TESTS) $(SH_TESTS) diff --git a/doc/guix.texi b/doc/guix.texi index d6fbd85fde..68f1a8bba3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -238,7 +238,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 Defining Packages @@ -5415,7 +5415,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 @node Package Modules @@ -8134,12 +8134,31 @@ has an associated gexp compiler, such as a @code{}. @node Invoking guix repl @section Invoking @command{guix repl} -@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,,, guile, -GNU Guile Reference Manual}). Compared to just launching the @command{guile} +@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{files} +@end example + +When at least one @var{files} argument is provided, @var{files} are +executed as Guile scripts in the given order: + +@example +$ guix repl my-script.scm +@end example + +Otherwise a Guile REPL is started: @example $ guix repl @@ -8188,11 +8207,12 @@ Add @var{directory} to the front of the package module search path (@pxref{Package Modules}). This allows users to define their own packages and make them visible to -the command-line tool. +the scripts or REPL. @item -q Inhibit loading of the @file{~/.guile} file. By default, that -configuration file is loaded when spawning a @code{guile} REPL. +configuration file is loaded when executing scripts or spawning +a @code{guile} REPL. @end table @c ********************************************************************* diff --git a/guix/scripts/repl.scm b/guix/scripts/repl.scm index ff1f208894..f4cb744bbd 100644 --- a/guix/scripts/repl.scm +++ b/guix/scripts/repl.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès ;;; Copyright © 2020 Simon Tournier +;;; Copyright © 2020 Konrad Hinsen ;;; ;;; This file is part of GNU Guix. ;;; @@ -32,10 +33,12 @@ ;;; 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. (define %default-options - `((type . guile))) + `((scripts . ()) + (type . guile))) (define %options (list (option '(#\h "help") #f #f @@ -63,8 +66,9 @@ (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...] [FILES...] +In the Guix execution environment, run FILES as Guile scripts, +or start a Guile REPL if no FILES are given,\n")) (display (G_ " -t, --type=TYPE 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)) + (alist-cons 'scripts + (cons arg (cdr (assq 'scripts result))) + result)) %default-options)) (define user-config @@ -148,29 +153,42 @@ call THUNK." (lambda (home) (string-append home "/.guile")))) + (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))) + (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)))))))) - -;; Local Variables: -;; eval: (put 'call-with-connection 'scheme-indent-function 1) -;; End: + (let ((scripts (reverse (assoc-ref opts 'scripts)))) + + (for-each (lambda (script) + (save-module-excursion + (lambda () + (set-user-module) + (load script)))) + scripts) + + (when (null? scripts) + (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))))))))) + + ;; Local Variables: + ;; eval: (put 'call-with-connection 'scheme-indent-function 1) + ;; End: + ) diff --git a/tests/guix-repl.sh b/tests/guix-repl.sh new file mode 100644 index 0000000000..b93d48248d --- /dev/null +++ b/tests/guix-repl.sh @@ -0,0 +1,70 @@ +# GNU Guix --- Functional package management for GNU +# Copyright © 2020 Simon Tournier +# +# 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="`mktemp -d`" +export test_directory +trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT + +tmpfile="$test_directory/foo.scm" +rm -f "$tmpfile" +trap 'rm -f "$tmpfile"' EXIT + +module_dir="t-guix-repl-$$" +mkdir "$module_dir" +trap 'rm -rf "$module_dir"' EXIT + + +cat > "$tmpfile"< "$module_dir/foo.scm"< "$tmpfile"<