1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu packages)
22 #:use-module (guix packages)
23 #:use-module (guix ui)
24 #:use-module (guix utils)
25 #:use-module ((guix ftp-client) #:select (ftp-open))
26 #:use-module (guix gnu-maintenance)
27 #:use-module (ice-9 ftw)
28 #:use-module (ice-9 vlist)
29 #:use-module (ice-9 match)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-39)
33 #:export (search-patch
34 search-bootstrap-binary
36 %bootstrap-binaries-path
41 find-best-packages-by-name
42 find-newest-available-packages
44 package-direct-dependents
45 package-transitive-dependents
46 package-covering-dependents
48 check-package-freshness))
52 ;;; General utilities for the software distribution---i.e., the modules under
53 ;;; (gnu packages ...).
57 ;; By default, we store patches and bootstrap binaries alongside Guile
58 ;; modules. This is so that these extra files can be found without
59 ;; requiring a special setup, such as a specific installation directory
60 ;; and an extra environment variable. One advantage of this setup is
61 ;; that everything just works in an auto-compilation setting.
65 (map (cut string-append <> "/gnu/packages/patches")
68 (define %bootstrap-binaries-path
70 (map (cut string-append <> "/gnu/packages/bootstrap")
73 (define (search-patch file-name)
74 "Search the patch FILE-NAME."
75 (search-path (%patch-path) file-name))
77 (define (search-bootstrap-binary file-name system)
78 "Search the bootstrap binary FILE-NAME for SYSTEM."
79 (search-path (%bootstrap-binaries-path)
80 (string-append system "/" file-name)))
82 (define %distro-module-directory
83 ;; Absolute path of the (gnu packages ...) module root.
84 (string-append (dirname (search-path %load-path "gnu/packages.scm"))
87 (define (package-files)
88 "Return the list of files that implement distro modules."
91 (dirname (dirname (search-path %load-path "gnu/packages.scm")))))
93 (file-system-fold (const #t) ; enter?
94 (lambda (path stat result) ; leaf
95 (if (string-suffix? ".scm" path)
96 (cons (substring path prefix-len) result)
98 (lambda (path stat result) ; down
100 (lambda (path stat result) ; up
103 (lambda (path stat errno result)
104 (warning (_ "cannot access `~a': ~a~%")
105 path (strerror errno))
108 %distro-module-directory
111 (define (package-modules)
112 "Return the list of modules that provide packages for the distribution."
114 (char-set-complement (char-set #\/)))
116 (filter-map (lambda (path)
117 (let ((name (map string->symbol
118 (string-tokenize (string-drop-right path 4)
120 (false-if-exception (resolve-interface name))))
123 (define (fold-packages proc init)
124 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
125 the initial value of RESULT. It is guaranteed to never traverse the
127 (identity ; discard second return value
128 (fold2 (lambda (module result seen)
129 (fold2 (lambda (var result seen)
130 (if (and (package? var)
131 (not (vhash-assq var seen)))
132 (values (proc var result)
133 (vhash-consq var #t seen))
134 (values result seen)))
137 (module-map (lambda (sym var)
138 (false-if-exception (variable-ref var)))
144 (define* (find-packages-by-name name #:optional version)
145 "Return the list of packages with the given NAME. If VERSION is not #f,
146 then only return packages whose version is equal to VERSION."
147 (define right-package?
150 (and (string=? (package-name p) name)
151 (string=? (package-version p) version)))
153 (string=? (package-name p) name))))
155 (fold-packages (lambda (package result)
156 (if (right-package? package)
157 (cons package result)
161 (define find-newest-available-packages
164 "Return a vhash keyed by package names, and with
165 associated values of the form
167 (newest-version newest-package ...)
169 where the preferred package is listed first."
171 ;; FIXME: Currently, the preferred package is whichever one
172 ;; was found last by 'fold-packages'. Find a better solution.
173 (fold-packages (lambda (p r)
174 (let ((name (package-name p))
175 (version (package-version p)))
176 (match (vhash-assoc name r)
177 ((_ newest-so-far . pkgs)
178 (case (version-compare version newest-so-far)
179 ((>) (vhash-cons name `(,version ,p) r))
180 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
182 (#f (vhash-cons name `(,version ,p) r)))))
185 (define (find-best-packages-by-name name version)
186 "If version is #f, return the list of packages named NAME with the highest
187 version numbers; otherwise, return the list of packages named NAME and at
190 (find-packages-by-name name version)
191 (match (vhash-assoc name (find-newest-available-packages))
192 ((_ version pkgs ...) pkgs)
196 (define* (vhash-refq vhash key #:optional (dflt #f))
197 "Look up KEY in the vhash VHASH, and return the value (if any) associated
198 with it. If KEY is not found, return DFLT (or `#f' if no DFLT argument is
199 supplied). Uses `eq?' for equality testing."
200 (or (and=> (vhash-assq key vhash) cdr)
203 (define package-dependencies
206 "Return a vhash keyed by package, and with associated values that are a
207 list of packages that depend on that package."
209 (lambda (package dag)
212 ;; Insert a graph edge from each of package's inputs to package.
214 (cons package (vhash-refq d in '()))
217 (match (package-direct-inputs package)
218 (((labels packages . _) ...)
222 (define (package-direct-dependents packages)
223 "Return a list of packages from the distribution that directly depend on the
224 packages in PACKAGES."
228 (vhash-refq (package-dependencies) p '()))
231 (define (package-transitive-dependents packages)
232 "Return the transitive dependent packages of the distribution packages in
233 PACKAGES---i.e. the dependents of those packages, plus their dependents,
235 (let ((dependency-dag (package-dependencies)))
238 (lambda (node) (vhash-refq dependency-dag node))
239 ;; Start with the dependents to avoid including PACKAGES in the result.
240 (package-direct-dependents packages))))
242 (define (package-covering-dependents packages)
243 "Return a minimal list of packages from the distribution whose dependencies
244 include all of PACKAGES and all packages that depend on PACKAGES."
245 (let ((dependency-dag (package-dependencies)))
248 (lambda (node) (vhash-refq dependency-dag node))
249 ;; Start with the dependents to avoid including PACKAGES in the result.
250 (package-direct-dependents packages))))
253 (define %sigint-prompt
254 ;; The prompt to jump to upon SIGINT.
255 (make-prompt-tag "interruptible"))
257 (define (call-with-sigint-handler thunk handler)
258 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
259 number in the context of the continuation of the call to this function, and
260 return its return value."
261 (call-with-prompt %sigint-prompt
265 (sigaction SIGINT SIG_DFL)
266 (abort-to-prompt %sigint-prompt signum)))
270 (cut sigaction SIGINT SIG_DFL)))
274 (define-syntax-rule (waiting exp fmt rest ...)
275 "Display the given message while EXP is being evaluated."
276 (let* ((message (format #f fmt rest ...))
277 (blank (make-string (string-length message) #\space)))
278 (display message (current-error-port))
279 (force-output (current-error-port))
280 (call-with-sigint-handler
287 (display #\cr (current-error-port))
288 (display blank (current-error-port))
289 (display #\cr (current-error-port))
290 (force-output (current-error-port)))))
292 (format (current-error-port) " interrupted by signal ~a~%" SIGINT)
296 ;; Memoizing version of `ftp-open'. The goal is to avoid initiating a new
297 ;; FTP connection for each package, esp. since most of them are to the same
298 ;; server. This has a noticeable impact when doing "guix upgrade -u".
301 (define (check-package-freshness package)
302 "Check whether PACKAGE has a newer version available upstream, and report
304 ;; TODO: Automatically inject the upstream version when desired.
308 (when (false-if-exception (gnu-package? package))
309 (let ((name (package-name package))
310 (full-name (package-full-name package)))
311 (match (waiting (latest-release name
313 #:ftp-close (const #f))
314 (_ "looking for the latest release of GNU ~a...") name)
315 ((latest-version . _)
316 (when (version>? latest-version full-name)
317 (format (current-error-port)
318 (_ "~a: note: using ~a \
319 but ~a is available upstream~%")
320 (location->string (package-location package))
321 full-name latest-version)))
324 ;; Silently ignore networking errors rather than preventing
327 ((getaddrinfo-error ftp-error) #f)
328 (else (apply throw key args))))))