From 1f5a362bfba9b611311d32903fdcd39b14f6f7c3 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Fri, 4 Oct 2024 13:59:53 +0100 Subject: command-line-tool: Move canonicalize-json to (ravanan work utils). * ravanan/command-line-tool.scm (canonicalize-json): Move to (ravanan work utils). Move comment about G-expressions to build-command-line-tool-script. * ravanan/work/utils.scm (ravanan): Import (ravanan work vectors). --- ravanan/command-line-tool.scm | 25 +++---------------------- ravanan/work/utils.scm | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/ravanan/command-line-tool.scm b/ravanan/command-line-tool.scm index 83ccfa2..4c1fa43 100644 --- a/ravanan/command-line-tool.scm +++ b/ravanan/command-line-tool.scm @@ -586,28 +586,6 @@ The returned G-expression will reference an @code{inputs-directory} variable." (list id (copy-input-files input)))) inputs))) -(define (canonicalize-json tree) - "Canonicalize JSON @var{tree} by recursively sorting objects in lexicographic -order of keys." - ;; We need to canonicalize JSON trees before inserting them into - ;; G-expressions. If we don't, we would have degenerate G-expressions that - ;; produce exactly the same result. - (cond - ;; Sort objects by lexicographic order of keys, and recurse. - ((list? tree) - (sort (map (match-lambda - ((key . value) - (cons key (canonicalize-json value)))) - tree) - (match-lambda* - (((key1 . _) (key2 . _)) - (string< key1 key2))))) - ;; Do not rearrange arrays. Just recurse. - ((vector? tree) - (vector-map canonicalize-json tree)) - ;; Atoms - (else tree))) - (define (find-requirement requirements class) "Find requirement of @var{class} among @var{requirements} and return a maybe-monadic value." @@ -977,6 +955,9 @@ directory of the workflow." (call-with-temporary-directory (lambda (inputs-directory) + ;; We need to canonicalize JSON trees before inserting them + ;; into G-expressions. If we don't, we would have degenerate + ;; G-expressions that produce exactly the same result. (let ((inputs #$(copy-input-files-gexp (canonicalize-json inputs))) (runtime `(("cores" . ,#$(cores batch-system))))) diff --git a/ravanan/work/utils.scm b/ravanan/work/utils.scm index 15349b2..62b02ef 100644 --- a/ravanan/work/utils.scm +++ b/ravanan/work/utils.scm @@ -23,11 +23,13 @@ #:use-module (ice-9 filesystem) #:use-module (ice-9 match) #:use-module (ice-9 popen) + #:use-module (ravanan work vectors) #:export (list->dotted-list alist=? assoc-ref* assoc-set json-ref + canonicalize-json call-with-temporary-directory call-with-input-pipe)) @@ -72,6 +74,25 @@ mutated." other-keys)) (() scm))) +(define (canonicalize-json tree) + "Canonicalize JSON @var{tree} by recursively sorting objects in lexicographic +order of keys." + (cond + ;; Sort objects by lexicographic order of keys, and recurse. + ((list? tree) + (sort (map (match-lambda + ((key . value) + (cons key (canonicalize-json value)))) + tree) + (match-lambda* + (((key1 . _) (key2 . _)) + (string< key1 key2))))) + ;; Do not rearrange arrays. Just recurse. + ((vector? tree) + (vector-map canonicalize-json tree)) + ;; Atoms + (else tree))) + (define* (call-with-temporary-directory proc #:optional (parent-directory (getcwd))) "Call @var{proc} with a new temporary directory in -- cgit v1.2.3