diff mbox

[03/12] import: utils: Add some utilities.

Message ID 20161211172537.23315-4-david@craven.ch
State New
Headers show

Commit Message

David Craven Dec. 11, 2016, 5:25 p.m. UTC
* gnu/import/utils.scm (json-fetch, maybe-inputs, maybe-native-inputs,
  package->definition): New variables.
---
 guix/import/utils.scm | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

Comments

Ludovic Courtès Dec. 13, 2016, 5:29 p.m. UTC | #1
David Craven <david@craven.ch> skribis:

> * gnu/import/utils.scm (json-fetch, maybe-inputs, maybe-native-inputs,
    ^^^
guix/

>   package->definition): New variables.

IIRC there was an argument against adding ‘json-fetch’ in this module
earlier this year, because that would make Guile-JSON mandatory.  Could

If you can leave that bit out, that LGTM!

Thanks,
Ludo’.
David Craven Dec. 13, 2016, 6:09 p.m. UTC | #2
> IIRC there was an argument against adding ‘json-fetch’ in this module
> earlier this year, because that would make Guile-JSON mandatory.

Yep. This is a mistake. I thought that you suggested using http-fetch
to avoid pulling in guile-json, but that was because of the temporary
file thing. I think Eric already pushed a patch for this...
diff mbox

Patch

diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index 057c2d9c7..e2e90676a 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -22,6 +22,8 @@ 
   #:use-module (guix base32)
   #:use-module ((guix build download) #:prefix build:)
   #:use-module (guix hash)
+  #:use-module (guix http-client)
+  #:use-module (json)
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix utils)
   #:use-module (ice-9 match)
@@ -34,8 +36,13 @@ 
             assoc-ref*
 
             url-fetch
+            json-fetch
             guix-hash-url
 
+            maybe-inputs
+            maybe-native-inputs
+            package->definition
+
             spdx-string->license
             license->symbol
 
@@ -205,3 +212,39 @@  into a proper sentence and by using two spaces between sentences."
     ;; Use double spacing between sentences
     (regexp-substitute/global #f "\\. \\b"
                               cleaned 'pre ".  " 'post)))
+
+(define (package-names->package-inputs names)
+  (map (lambda (input)
+         (list input (list 'unquote (string->symbol input))))
+       names))
+
+(define (maybe-inputs package-names)
+  "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a
+package definition."
+  (match (package-names->package-inputs package-names)
+    (()
+     '())
+    ((package-inputs ...)
+     `((inputs (,'quasiquote ,package-inputs))))))
+
+(define (maybe-native-inputs package-names)
+  "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a
+package definition."
+  (match (package-names->package-inputs package-names)
+    (()
+     '())
+    ((package-inputs ...)
+     `((native-inputs (,'quasiquote ,package-inputs))))))
+
+(define* (json-fetch url)
+  "Return an alist representation of the url metadata."
+  (let* ((port (http-fetch url))
+         (result (json->scm port)))
+    (close-port port)
+    (hash-table->alist result)))
+
+(define (package->definition guix-package)
+  (match guix-package
+    (('package ('name (? string? name)) _ ...)
+     `(define-public ,(string->symbol name)
+        ,guix-package))))