utils: Use '@' for separating package names and version numbers.
[jackhill/guix/guix.git] / guix / utils.scm
index 7b589e6..de54179 100644 (file)
@@ -1,8 +1,9 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org>
 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
 ;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net>
+;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
@@ -31,8 +32,7 @@
   #:use-module (rnrs bytevectors)
   #:use-module (rnrs io ports)
   #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!))
-  #:use-module ((guix build utils)
-                #:select (dump-port package-name->name+version))
+  #:use-module ((guix build utils) #:select (dump-port))
   #:use-module ((guix build syscalls) #:select (errno mkdtemp!))
   #:use-module (ice-9 vlist)
   #:use-module (ice-9 format)
@@ -42,7 +42,6 @@
   #:use-module (ice-9 match)
   #:use-module (ice-9 format)
   #:use-module (system foreign)
-  #:re-export (package-name->name+version)
   #:export (bytevector->base16-string
             base16-string->bytevector
 
@@ -52,6 +51,7 @@
             strip-keyword-arguments
             default-keyword-arguments
             substitute-keyword-arguments
+            ensure-keyword-arguments
 
             <location>
             location
@@ -65,6 +65,7 @@
             gnu-triplet->nix-system
             %current-system
             %current-target-system
+            package-name->name+version
             version-compare
             version>?
             version>=?
@@ -453,6 +454,45 @@ previous value of the keyword argument."
          (()
           (reverse before)))))))
 
+(define (delkw kw lst)
+  "Remove KW and its associated value from LST, a keyword/value list such
+as '(#:foo 1 #:bar 2)."
+  (let loop ((lst    lst)
+             (result '()))
+    (match lst
+      (()
+       (reverse result))
+      ((kw? value rest ...)
+       (if (eq? kw? kw)
+           (append (reverse result) rest)
+           (loop rest (cons* value kw? result)))))))
+
+(define (ensure-keyword-arguments args kw/values)
+  "Force the keywords arguments KW/VALUES in the keyword argument list ARGS.
+For instance:
+
+  (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
+  => (#:foo 2)
+
+  (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
+  => (#:foo 2 #:bar 3)
+
+  (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))
+  => (#:foo 42 #:bar 3)
+"
+  (let loop ((args      args)
+             (kw/values kw/values)
+             (result    '()))
+    (match args
+      (()
+       (append (reverse result) kw/values))
+      ((kw value rest ...)
+       (match (memq kw kw/values)
+         ((_ value . _)
+          (loop rest (delkw kw kw/values) (cons* value kw result)))
+         (#f
+          (loop rest kw/values (cons* value kw result))))))))
+
 (define* (nix-system->gnu-triplet
           #:optional (system (%current-system)) (vendor "unknown"))
   "Return a guess of the GNU triplet corresponding to Nix system
@@ -504,6 +544,15 @@ returned by `config.guess'."
   ;; cross-building to.
   (make-parameter #f))
 
+(define (package-name->name+version spec)
+  "Given SPEC, a package name like \"foo@0.9.1b\", return two values: \"foo\"
+and \"0.9.1b\".  When the version part is unavailable, SPEC and #f are
+returned.  Both parts must not contain any '@'."
+  (match (string-rindex spec #\@)
+    (#f  (values spec #f))
+    (idx (values (substring spec 0 idx)
+                 (substring spec (1+ idx))))))
+
 (define version-compare
   (let ((strverscmp
          (let ((sym (or (dynamic-func "strverscmp" (dynamic-link))