gnu: youtube-dl: Update to 2015.06.25.
[jackhill/guix/guix.git] / gnu / packages.scm
index ddabacd..6e46a89 100644 (file)
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
 ;;;
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-34)
+  #:use-module (srfi srfi-35)
   #:use-module (srfi srfi-39)
   #:export (search-patch
             search-bootstrap-binary
-            %patch-directory
+            %patch-path
             %bootstrap-binaries-path
             %package-module-path
 
@@ -49,7 +51,8 @@
 
             check-package-freshness
 
-            specification->package))
+            specification->package
+            specification->package+output))
 
 ;;; Commentary:
 ;;;
 ;; and an extra environment variable.  One advantage of this setup is
 ;; that everything just works in an auto-compilation setting.
 
-(define %patch-path
-  (make-parameter
-   (map (cut string-append <> "/gnu/packages/patches")
-        %load-path)))
-
 (define %bootstrap-binaries-path
   (make-parameter
    (map (cut string-append <> "/gnu/packages/bootstrap")
         %load-path)))
 
 (define (search-patch file-name)
-  "Search the patch FILE-NAME."
-  (search-path (%patch-path) file-name))
+  "Search the patch FILE-NAME.  Raise an error if not found."
+  (or (search-path (%patch-path) file-name)
+      (raise (condition
+              (&message (message (format #f (_ "~a: patch not found")
+                                         file-name)))))))
 
 (define (search-bootstrap-binary file-name system)
-  "Search the bootstrap binary FILE-NAME for SYSTEM."
-  (search-path (%bootstrap-binaries-path)
-               (string-append system "/" file-name)))
+  "Search the bootstrap binary FILE-NAME for SYSTEM.  Raise an error if not
+found."
+  (or (search-path (%bootstrap-binaries-path)
+                   (string-append system "/" file-name))
+      (raise (condition
+              (&message
+               (message
+                (format #f (_ "could not find bootstrap binary '~a' \
+for system '~a'")
+                        file-name system)))))))
 
 (define %distro-root-directory
   ;; Absolute file name of the module hierarchy.
   ;; Search path for package modules.  Each item must be either a directory
   ;; name or a pair whose car is a directory and whose cdr is a sub-directory
   ;; to narrow the search.
-  (list (cons %distro-root-directory "gnu/packages")))
+  (let* ((not-colon   (char-set-complement (char-set #\:)))
+         (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
+                                       not-colon)))
+    ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
+    (for-each (lambda (directory)
+                (set! %load-path (cons directory %load-path))
+                (set! %load-compiled-path
+                      (cons directory %load-compiled-path)))
+              environment)
+
+    (make-parameter
+     (append environment `((,%distro-root-directory . "gnu/packages"))))))
+
+(define %patch-path
+  ;; Define it after '%package-module-path' so that '%load-path' contains user
+  ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
+  (make-parameter
+   (map (lambda (directory)
+          (if (string=? directory %distro-root-directory)
+              (string-append directory "/gnu/packages/patches")
+              directory))
+        %load-path)))
 
 (define* (scheme-files directory)
-  "Return the list of Scheme files found under DIRECTORY."
-  (file-system-fold (const #t)                    ; enter?
-                    (lambda (path stat result)    ; leaf
-                      (if (string-suffix? ".scm" path)
-                          (cons path result)
-                          result))
-                    (lambda (path stat result)    ; down
-                      result)
-                    (lambda (path stat result)    ; up
-                      result)
-                    (const #f)                    ; skip
-                    (lambda (path stat errno result)
-                      (warning (_ "cannot access `~a': ~a~%")
-                               path (strerror errno))
-                      result)
-                    '()
-                    directory
-                    stat))
+  "Return the list of Scheme files found under DIRECTORY, recursively.  The
+returned list is sorted in alphabetical order."
+
+  ;; Sort entries so that 'fold-packages' works in a deterministic fashion
+  ;; regardless of details of the underlying file system.
+  (sort (file-system-fold (const #t)                   ; enter?
+                          (lambda (path stat result)   ; leaf
+                            (if (string-suffix? ".scm" path)
+                                (cons path result)
+                                result))
+                          (lambda (path stat result)   ; down
+                            result)
+                          (lambda (path stat result)   ; up
+                            result)
+                          (const #f)                   ; skip
+                          (lambda (path stat errno result)
+                            (warning (_ "cannot access `~a': ~a~%")
+                                     path (strerror errno))
+                            result)
+                          '()
+                          directory
+                          stat)
+        string<?))
 
 (define file-name->module-name
   (let ((not-slash (char-set-complement (char-set #\/))))
@@ -127,9 +161,15 @@ Optionally, narrow the search to SUB-DIRECTORY."
     (string-length directory))
 
   (filter-map (lambda (file)
-                (let ((file (substring file prefix-len)))
-                  (false-if-exception
-                   (resolve-interface (file-name->module-name file)))))
+                (let* ((file   (substring file prefix-len))
+                       (module (file-name->module-name file)))
+                  (catch #t
+                    (lambda ()
+                      (resolve-interface module))
+                    (lambda args
+                      ;; Report the error, but keep going.
+                      (warn-about-load-error module args)
+                      #f))))
               (scheme-files (if sub-directory
                                 (string-append directory "/" sub-directory)
                                 directory))))
@@ -168,22 +208,24 @@ same package twice."
           vlist-null
           (all-package-modules))))
 
-(define* (find-packages-by-name name #:optional version)
-  "Return the list of packages with the given NAME.  If VERSION is not #f,
-then only return packages whose version is equal to VERSION."
-  (define right-package?
-    (if version
-        (lambda (p)
-          (and (string=? (package-name p) name)
-               (string=? (package-version p) version)))
-        (lambda (p)
-          (string=? (package-name p) name))))
-
-  (fold-packages (lambda (package result)
-                   (if (right-package? package)
-                       (cons package result)
-                       result))
-                 '()))
+(define find-packages-by-name
+  (let ((packages (delay
+                    (fold-packages (lambda (p r)
+                                     (vhash-cons (package-name p) p r))
+                                   vlist-null)))
+        (version>? (lambda (p1 p2)
+                     (version>? (package-version p1) (package-version p2)))))
+    (lambda* (name #:optional version)
+      "Return the list of packages with the given NAME.  If VERSION is not #f,
+then only return packages whose version is prefixed by VERSION, sorted in
+decreasing version order."
+      (let ((matching (sort (vhash-fold* cons '() name (force packages))
+                            version>?)))
+        (if version
+            (filter (lambda (package)
+                      (string-prefix? version (package-version package)))
+                    matching)
+            matching)))))
 
 (define find-newest-available-packages
   (memoize
@@ -339,13 +381,16 @@ it."
                                           #:ftp-open ftp-open*
                                           #:ftp-close (const #f))
                           (_ "looking for the latest release of GNU ~a...") name)
-            ((latest-version . _)
-             (when (version>? latest-version full-name)
-               (format (current-error-port)
-                       (_ "~a: note: using ~a \
+            ((? gnu-release? release)
+             (let ((latest-version
+                    (string-append (gnu-release-package release) "-"
+                                   (gnu-release-version release))))
+              (when (version>? latest-version full-name)
+                (format (current-error-port)
+                        (_ "~a: note: using ~a \
 but ~a is available upstream~%")
-                       (location->string (package-location package))
-                       full-name latest-version)))
+                        (location->string (package-location package))
+                        full-name latest-version))))
             (_ #t)))))
     (lambda (key . args)
       ;; Silently ignore networking errors rather than preventing
@@ -374,3 +419,36 @@ present, return the preferred newest version."
            (leave (_ "~A: package not found for version ~a~%")
                   name version)
            (leave (_ "~A: unknown package~%") name))))))
+
+(define* (specification->package+output spec #:optional (output "out"))
+  "Return the package and output specified by SPEC, or #f and #f; SPEC may
+optionally contain a version number and an output name, as in these examples:
+
+  guile
+  guile-2.0.9
+  guile:debug
+  guile-2.0.9:debug
+
+If SPEC does not specify a version number, return the preferred newest
+version; if SPEC does not specify an output, return OUTPUT."
+  (define (ensure-output p sub-drv)
+    (if (member sub-drv (package-outputs p))
+        sub-drv
+        (leave (_ "package `~a' lacks output `~a'~%")
+               (package-full-name p)
+               sub-drv)))
+
+  (let-values (((name version sub-drv)
+                (package-specification->name+version+output spec output)))
+    (match (find-best-packages-by-name name version)
+      ((p)
+       (values p (ensure-output p sub-drv)))
+      ((p p* ...)
+       (warning (_ "ambiguous package specification `~a'~%")
+                spec)
+       (warning (_ "choosing ~a from ~a~%")
+                (package-full-name p)
+                (location->string (package-location p)))
+       (values p (ensure-output p sub-drv)))
+      (()
+       (leave (_ "~a: package not found~%") spec)))))