1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 Ricardo Wurmus <rekado@elephly.net>
3 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
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 (guix import cran)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 regex)
24 #:use-module ((ice-9 rdelim) #:select (read-string read-line))
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-34)
28 #:use-module (srfi srfi-41)
29 #:use-module (ice-9 receive)
30 #:use-module (web uri)
31 #:use-module (guix memoization)
32 #:use-module (guix http-client)
33 #:use-module (guix hash)
34 #:use-module (guix store)
35 #:use-module (guix base32)
36 #:use-module ((guix download) #:select (download-to-store))
37 #:use-module (guix import utils)
38 #:use-module ((guix build utils) #:select (find-files))
39 #:use-module (guix utils)
40 #:use-module ((guix build-system r) #:select (cran-uri bioconductor-uri))
41 #:use-module (guix upstream)
42 #:use-module (guix packages)
43 #:use-module (gnu packages)
44 #:export (cran->guix-package
45 bioconductor->guix-package
52 bioconductor-data-package?
53 bioconductor-experiment-package?))
57 ;;; Generate a package declaration template for the latest version of an R
58 ;;; package on CRAN, using the DESCRIPTION file downloaded from
59 ;;; cran.r-project.org.
63 (define string->license
66 ("Artistic-2.0" 'artistic2.0)
67 ("Apache License 2.0" 'asl2.0)
68 ("BSD_2_clause" 'bsd-2)
69 ("BSD_2_clause + file LICENSE" 'bsd-2)
70 ("BSD_3_clause" 'bsd-3)
71 ("BSD_3_clause + file LICENSE" 'bsd-3)
72 ("GPL" '(list gpl2+ gpl3+))
80 ("LGPL (>= 2)" 'lgpl2.0+)
81 ("LGPL (>= 3)" 'lgpl3+)
83 ("MIT + file LICENSE" 'expat)
84 ((x) (string->license x))
85 ((lst ...) `(list ,@(map string->license lst)))
89 (define (description->alist description)
90 "Convert a DESCRIPTION string into an alist."
91 (let ((lines (string-split description #\newline))
92 (parse (lambda (line acc)
93 (if (string-null? line) acc
94 ;; Keys usually start with a capital letter and end with
95 ;; ":". There are some exceptions, unfortunately (such
96 ;; as "biocViews"). There are no blanks in a key.
97 (if (string-match "^[A-Za-z][^ :]+:( |\n|$)" line)
99 (let* ((pos (string-index line #\:))
100 (key (string-take line pos))
101 (value (string-drop line (+ 1 pos))))
103 (string-trim-both value))
105 ;; This is a continuation of the previous pair
106 (match-let ((((key . value) . rest) acc))
107 (cons (cons key (string-join
109 (string-trim-both line))))
111 (fold parse '() lines)))
113 (define (format-inputs names)
114 "Generate a sorted list of package inputs from a list of package NAMES."
116 (list name (list 'unquote (string->symbol name))))
117 (sort names string-ci<?)))
119 (define* (maybe-inputs package-inputs #:optional (type 'inputs))
120 "Given a list of PACKAGE-INPUTS, tries to generate the TYPE field of a
122 (match package-inputs
125 ((package-inputs ...)
126 `((,type (,'quasiquote ,(format-inputs package-inputs)))))))
128 (define %cran-url "http://cran.r-project.org/web/packages/")
129 (define %bioconductor-url "https://bioconductor.org/packages/")
131 ;; The latest Bioconductor release is 3.6. Bioconductor packages should be
133 (define %bioconductor-version "3.6")
135 (define %bioconductor-packages-list-url
136 (string-append "https://bioconductor.org/packages/"
137 %bioconductor-version "/bioc/src/contrib/PACKAGES"))
139 (define (bioconductor-packages-list)
140 "Return the latest version of package NAME for the current bioconductor
142 (let ((url (string->uri %bioconductor-packages-list-url)))
143 (guard (c ((http-get-error? c)
144 (format (current-error-port)
145 "error: failed to retrieve list of packages from ~s: ~a (~s)~%"
146 (uri->string (http-get-error-uri c))
147 (http-get-error-code c)
148 (http-get-error-reason c))
150 ;; Split the big list on empty lines, then turn each chunk into an
151 ;; alist of attributes.
153 (description->alist (string-join chunk "\n")))
154 (chunk-lines (read-lines (http-fetch/cached url)))))))
156 (define (latest-bioconductor-package-version name)
157 "Return the version string corresponding to the latest release of the
158 bioconductor package NAME, or #F if the package is unknown."
159 (and=> (find (lambda (meta)
160 (string=? (assoc-ref meta "Package") name))
161 (bioconductor-packages-list))
162 (cut assoc-ref <> "Version")))
164 (define (fetch-description repository name)
165 "Return an alist of the contents of the DESCRIPTION file for the R package
166 NAME in the given REPOSITORY, or #f in case of failure. NAME is
170 (let ((url (string-append %cran-url name "/DESCRIPTION")))
171 (guard (c ((http-get-error? c)
172 (format (current-error-port)
173 "error: failed to retrieve package information \
175 (uri->string (http-get-error-uri c))
176 (http-get-error-code c)
177 (http-get-error-reason c))
179 (description->alist (read-string (http-fetch url))))))
181 ;; Currently, the bioconductor project does not offer a way to access a
182 ;; package's DESCRIPTION file over HTTP, so we determine the version,
183 ;; download the source tarball, and then extract the DESCRIPTION file.
184 (let* ((version (latest-bioconductor-package-version name))
185 (url (car (bioconductor-uri name version)))
186 (tarball (with-store store (download-to-store store url))))
187 (call-with-temporary-directory
189 (parameterize ((current-error-port (%make-void-port "rw+"))
190 (current-output-port (%make-void-port "rw+")))
191 (and (zero? (system* "tar" "--wildcards" "-x"
192 "--strip-components=1"
194 "-f" tarball "*/DESCRIPTION"))
195 (description->alist (with-input-from-file
196 (string-append dir "/DESCRIPTION") read-string))))))))))
198 (define (listify meta field)
199 "Look up FIELD in the alist META. If FIELD contains a comma-separated
200 string, turn it into a list and strip off parenthetic expressions. Return the
201 empty list when the FIELD cannot be found."
202 (let ((value (assoc-ref meta field)))
205 ;; Strip off parentheses
206 (let ((items (string-split (regexp-substitute/global
207 #f "( *\\([^\\)]+\\)) *"
210 (remove (lambda (item)
211 (or (string-null? item)
212 ;; When there is whitespace inside of items it is
213 ;; probably because this was not an actual list to
215 (string-any char-set:whitespace item)))
216 (map string-trim-both items))))))
218 (define default-r-packages
234 (define (guix-name name)
235 "Return a Guix package name for a given R package name."
236 (string-append "r-" (string-map (match-lambda
239 (chr (char-downcase chr)))
242 (define (needs-fortran? tarball)
243 "Check if the TARBALL contains Fortran source files."
244 (define (check pattern)
245 (parameterize ((current-error-port (%make-void-port "rw+"))
246 (current-output-port (%make-void-port "rw+")))
247 (zero? (system* "tar" "--wildcards" "--list" pattern "-f" tarball))))
252 (define (tarball-files-match-pattern? tarball regexp . file-patterns)
253 "Return #T if any of the files represented by FILE-PATTERNS in the TARBALL
254 match the given REGEXP."
255 (call-with-temporary-directory
257 (let ((pattern (make-regexp regexp)))
258 (parameterize ((current-error-port (%make-void-port "rw+")))
260 "xf" tarball "-C" dir
261 `("--wildcards" ,@file-patterns)))
263 (call-with-input-file file
266 (let ((line (read-line port)))
268 ((eof-object? line) #f)
269 ((regexp-exec pattern line) #t)
271 (find-files dir))))))
273 (define (needs-zlib? tarball)
274 "Return #T if any of the Makevars files in the src directory of the TARBALL
275 contain a zlib linker flag."
276 (tarball-files-match-pattern?
278 "*/src/Makevars*" "*/src/configure*" "*/configure*"))
280 (define (needs-pkg-config? tarball)
281 "Return #T if any of the Makevars files in the src directory of the TARBALL
282 reference the pkg-config tool."
283 (tarball-files-match-pattern?
285 "*/src/Makevars*" "*/src/configure*" "*/configure*"))
287 (define (description->package repository meta)
288 "Return the `package' s-expression for an R package published on REPOSITORY
289 from the alist META, which was derived from the R package's DESCRIPTION file."
290 (let* ((base-url (case repository
292 ((bioconductor) %bioconductor-url)))
293 (uri-helper (case repository
295 ((bioconductor) bioconductor-uri)))
296 (name (assoc-ref meta "Package"))
297 (synopsis (assoc-ref meta "Title"))
298 (version (assoc-ref meta "Version"))
299 (license (string->license (assoc-ref meta "License")))
300 ;; Some packages have multiple home pages. Some have none.
301 (home-page (match (listify meta "URL")
303 (_ (string-append base-url name))))
304 (source-url (match (uri-helper name version)
306 ((? string? url) url)
308 (tarball (with-store store (download-to-store store source-url)))
310 (if (needs-zlib? tarball) '("zlib") '())
311 (map string-downcase (listify meta "SystemRequirements"))))
312 (propagate (filter (lambda (name)
313 (not (member name default-r-packages)))
315 (listify meta "Imports")
316 (listify meta "LinkingTo")
318 (listify meta "Depends"))))))
321 (name ,(guix-name name))
325 (uri (,(procedure-name uri-helper) ,name version))
328 ,(bytevector->nix-base32-string (file-sha256 tarball))))))
329 ,@(if (not (equal? (string-append "r-" name)
331 `((properties ,`(,'quasiquote ((,'upstream-name . ,name)))))
333 (build-system r-build-system)
334 ,@(maybe-inputs sysdepends)
335 ,@(maybe-inputs (map guix-name propagate) 'propagated-inputs)
337 `(,@(if (needs-fortran? tarball)
339 ,@(if (needs-pkg-config? tarball)
340 '("pkg-config") '()))
342 (home-page ,(if (string-null? home-page)
343 (string-append base-url name)
346 (description ,(beautify-description (or (assoc-ref meta "Description")
351 (define cran->guix-package
353 (lambda* (package-name #:optional (repo 'cran))
354 "Fetch the metadata for PACKAGE-NAME from REPO and return the `package'
355 s-expression corresponding to that package, or #f on failure."
356 (and=> (fetch-description repo package-name)
357 (cut description->package repo <>)))))
359 (define* (recursive-import package-name #:optional (repo 'cran))
360 "Generate a stream of package expressions for PACKAGE-NAME and all its
362 (receive (package . dependencies)
363 (cran->guix-package package-name repo)
367 ;; Generate a lazy stream of package expressions for all unknown
368 ;; dependencies in the graph.
369 (let* ((make-state (lambda (queue done)
372 (((next . rest) . done) next)))
373 (imported (match-lambda
374 ((queue . done) done)))
377 (zero? (length queue)))))
378 (unknown? (lambda* (dependency #:optional (done '()))
379 (and (not (member dependency
381 (null? (find-packages-by-name
382 (guix-name dependency))))))
383 (update (lambda (state new-queue)
385 (((head . tail) . done)
386 (make-state (lset-difference
388 (lset-union equal? new-queue tail)
390 (cons head done)))))))
394 ;; map: produce a stream element
396 (cran->guix-package (next state) repo))
401 ;; generator: update the queue
403 (receive (package . dependencies)
404 (cran->guix-package (next state) repo)
406 (update state (filter (cut unknown? <>
410 ;; TODO: Try the other archives before giving up
411 (update state (imported state)))))
414 (make-state (filter unknown? (car dependencies))
415 (list package-name))))))))
422 (define (package->upstream-name package)
423 "Return the upstream name of the PACKAGE."
424 (let* ((properties (package-properties package))
425 (upstream-name (and=> properties
426 (cut assoc-ref <> 'upstream-name))))
429 (match (package-source package)
431 (match (origin-uri origin)
432 ((or (? string? url) (url _ ...))
433 (let ((end (string-rindex url #\_))
434 (start (string-rindex url #\/)))
436 ;; (string-append "/" name "_" version ".tar.gz")
437 (and start end (substring url (+ start 1) end))))
441 (define (latest-cran-release package)
442 "Return an <upstream-source> for the latest release of PACKAGE."
444 (define upstream-name
445 (package->upstream-name package))
448 (fetch-description 'cran upstream-name))
451 (let ((version (assoc-ref meta "Version")))
452 ;; CRAN does not provide signatures.
454 (package (package-name package))
456 (urls (cran-uri upstream-name version))))))
458 (define (latest-bioconductor-release package)
459 "Return an <upstream-source> for the latest release of PACKAGE."
461 (define upstream-name
462 (package->upstream-name package))
465 (latest-bioconductor-package-version upstream-name))
468 ;; Bioconductor does not provide signatures.
470 (package (package-name package))
472 (urls (bioconductor-uri upstream-name version)))))
474 (define (cran-package? package)
475 "Return true if PACKAGE is an R package from CRAN."
476 (and (string-prefix? "r-" (package-name package))
477 ;; Check if the upstream name can be extracted from package uri.
478 (package->upstream-name package)
479 ;; Check if package uri(s) are prefixed by "mirror://cran".
480 (match (and=> (package-source package) origin-uri)
482 (string-prefix? "mirror://cran" uri))
484 (any (cut string-prefix? "mirror://cran" <>) uris))
487 (define (bioconductor-package? package)
488 "Return true if PACKAGE is an R package from Bioconductor."
489 (let ((predicate (lambda (uri)
490 (and (string-prefix? "https://bioconductor.org" uri)
491 ;; Data packages are neither listed in SVN nor on
492 ;; the Github mirror, so we have to exclude them
493 ;; from the set of bioconductor packages that can be
494 ;; updated automatically.
495 (not (string-contains uri "/data/annotation/"))
496 ;; Experiment packages are in a separate repository.
497 (not (string-contains uri "/data/experiment/"))))))
498 (and (string-prefix? "r-" (package-name package))
499 (match (and=> (package-source package) origin-uri)
503 (any predicate uris))
506 (define (bioconductor-data-package? package)
507 "Return true if PACKAGE is an R data package from Bioconductor."
508 (let ((predicate (lambda (uri)
509 (and (string-prefix? "https://bioconductor.org" uri)
510 (string-contains uri "/data/annotation/")))))
511 (and (string-prefix? "r-" (package-name package))
512 (match (and=> (package-source package) origin-uri)
516 (any predicate uris))
519 (define (bioconductor-experiment-package? package)
520 "Return true if PACKAGE is an R experiment package from Bioconductor."
521 (let ((predicate (lambda (uri)
522 (and (string-prefix? "https://bioconductor.org" uri)
523 (string-contains uri "/data/experiment/")))))
524 (and (string-prefix? "r-" (package-name package))
525 (match (and=> (package-source package) origin-uri)
529 (any predicate uris))
532 (define %cran-updater
535 (description "Updater for CRAN packages")
537 (latest latest-cran-release)))
539 (define %bioconductor-updater
542 (description "Updater for Bioconductor packages")
543 (pred bioconductor-package?)
544 (latest latest-bioconductor-release)))
546 ;;; cran.scm ends here