unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
* [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently.
@ 2020-06-30 22:09 Jakub Kądziołka
  2020-06-30 22:11 ` [bug#42146] [PATCH 2/?] build: bootstrap-configure: Allow lack of matches in substitute Jakub Kądziołka
                   ` (7 more replies)
  0 siblings, 8 replies; 27+ messages in thread
From: Jakub Kądziołka @ 2020-06-30 22:09 UTC (permalink / raw)
  To: 42146; +Cc: mbakke

* guix/build/utils.scm (substitute, substitute*)[require-matches?]: New
  argument.
---
Of course, this will require some changes in the build recipes. However,
many of the bugs I have seen before could've been prevented by this
behavior, so I believe it is worth it.

I am currently running 'guix build hello' with these changes; I will
send any follow-up fixes needed to this bug#.

 guix/build/utils.scm | 47 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index dc55c6745d..1bfb774c60 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -6,6 +6,7 @@
 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
 ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
+;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -747,29 +748,42 @@ PROC's result is returned."
       (lambda (key . args)
         (false-if-exception (delete-file template))))))
 
-(define (substitute file pattern+procs)
+(define* (substitute file pattern+procs #:key (require-matches? #t))
   "PATTERN+PROCS is a list of regexp/two-argument-procedure pairs.  For each
 line of FILE, and for each PATTERN that it matches, call the corresponding
 PROC as (PROC LINE MATCHES); PROC must return the line that will be written as
 a substitution of the original line.  Be careful about using '$' to match the
-end of a line; by itself it won't match the terminating newline of a line."
-  (let ((rx+proc  (map (match-lambda
-                        (((? regexp? pattern) . proc)
-                         (cons pattern proc))
-                        ((pattern . proc)
-                         (cons (make-regexp pattern regexp/extended)
-                               proc)))
-                       pattern+procs)))
+end of a line; by itself it won't match the terminating newline of a line.
+
+By default, SUBSTITUTE will raise a &message condition if one of the patterns
+fails to match. If a lack of matches is acceptable, pass #:require-matches? #f
+to disable this check."
+  (let ((rx+proc (map (match-lambda
+                       (((? regexp? pattern) . proc)
+                        (cons* #f "<unknown>" pattern proc))
+                       ((pattern . proc)
+                        (cons* #f pattern (make-regexp pattern regexp/extended)
+                              proc)))
+                      pattern+procs)))
     (with-atomic-file-replacement file
       (lambda (in out)
         (let loop ((line (read-line in 'concat)))
           (if (eof-object? line)
-              #t
+              (when require-matches?
+                (for-each
+                  (match-lambda
+                    ((#f pat . _)
+                     (raise (condition
+                             (&message
+                              (message (format #f "substitute: ~a: pattern failed to match: ~a" file pat))))))
+                    ((#t . _) #t))
+                  rx+proc))
               (let ((line (fold (lambda (r+p line)
                                   (match r+p
-                                    ((regexp . proc)
+                                    ((_ _ regexp . proc)
                                      (match (list-matches regexp line)
                                        ((and m+ (_ _ ...))
+                                        (set-car! r+p #t)
                                         (proc line m+))
                                        (_ line)))))
                                 line
@@ -814,9 +828,17 @@ match substring.
 Alternatively, FILE may be a list of file names, in which case they are
 all subject to the substitutions.
 
+By default, SUBSTITUTE* will raise a &message condition if one of the patterns
+fails to match on one of the files. If a lack of matches is acceptable,
+add #:require-matches? #f after FILE to disable this check.
+
 Be careful about using '$' to match the end of a line; by itself it won't
 match the terminating newline of a line."
     ((substitute* file ((regexp match-var ...) body ...) ...)
+     (substitute* file #:require-matches? #t
+                  ((regexp match-var ...) body ...) ...))
+    ((substitute* file #:require-matches? require-matches?
+                  ((regexp match-var ...) body ...) ...)
      (let ()
        (define (substitute-one-file file-name)
          (substitute
@@ -840,7 +862,8 @@ match the terminating newline of a line."
                                       (begin body ...)
                                       (substring l o (match:start m))
                                       r))))))))
-                ...)))
+                ...)
+          #:require-matches? require-matches?))
 
        (match file
          ((files (... ...))
-- 
2.26.2





^ permalink raw reply related	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2023-10-23 16:27 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-30 22:09 [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently Jakub Kądziołka
2020-06-30 22:11 ` [bug#42146] [PATCH 2/?] build: bootstrap-configure: Allow lack of matches in substitute Jakub Kądziołka
2020-06-30 22:11 ` [bug#42146] [PATCH 3/?] gnu: commencement: Build fix for %bootstrap-mes-rewired Jakub Kądziołka
     [not found] ` <handler.42146.B.159355497531278.ack@debbugs.gnu.org>
2020-06-30 22:50   ` [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently Jakub Kądziołka
2023-10-19 13:14     ` Maxim Cournoyer
2020-07-01  0:42 ` [bug#42146] [PATCH 4/?] gnu: mes-boot: Use verbosity settings integrated into buildsystem Jakub Kądziołka
2020-07-01  0:42 ` [bug#42146] [PATCH 5/?] build-system/gnu: Allow lack of matches in substitution phases Jakub Kądziołka
2020-07-01  9:55 ` [bug#42146] [PATCH 6/6] gnu: glibc-mesboot0: Fixup the fixup-configure phase Jakub Kądziołka
2023-10-19 20:33 ` [bug#42146] [PATCH 1/3] build: Relocate <regexp*> record and associated procedures here Maxim Cournoyer
2023-10-19 20:33   ` [bug#42146] [PATCH 2/3] build: substitute: Error when no substitutions were done Maxim Cournoyer
2023-10-19 20:49     ` [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently Ludovic Courtès
2023-10-20 18:50       ` Bruno Victal
2023-10-20 21:11         ` Maxim Cournoyer
2023-10-19 20:33   ` [bug#42146] [PATCH 3/3] build: bootstrap-configure: Allow lack of matches in substitute Maxim Cournoyer
2023-10-19 20:51     ` [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently Ludovic Courtès
2023-10-19 20:40   ` Ludovic Courtès
2023-10-19 23:54     ` Maxim Cournoyer
2023-10-23  8:42       ` Ludovic Courtès
2023-10-23 15:05         ` Maxim Cournoyer
2023-10-20 15:11     ` Simon Tournier
2023-10-20 15:39       ` Maxim Cournoyer
2023-10-20  0:57 ` [bug#42146] [PATCH v3 1/3] build: Relocate <regexp*> record and associated procedures here Maxim Cournoyer
2023-10-20  0:57   ` [bug#42146] [PATCH v3 2/3] build: substitute: Error when no substitutions were done Maxim Cournoyer
2023-10-23  8:48     ` [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently Ludovic Courtès
2023-10-23 16:25       ` Maxim Cournoyer
2023-10-23  8:51     ` Ludovic Courtès
2023-10-20  0:57   ` [bug#42146] [PATCH v3 3/3] build: bootstrap-configure: Allow lack of matches in substitute Maxim Cournoyer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).