1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017, 2018, 2019 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-2)
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-34)
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 (gcrypt 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 "https://cran.r-project.org/web/packages/")
129 (define %bioconductor-url "https://bioconductor.org/packages/")
131 ;; The latest Bioconductor release is 3.9. Bioconductor packages should be
133 (define %bioconductor-version "3.9")
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 ;; Little helper to download URLs only once.
168 (with-store store (download-to-store store url)))))
170 (define (fetch-description repository name)
171 "Return an alist of the contents of the DESCRIPTION file for the R package
172 NAME in the given REPOSITORY, or #f in case of failure. NAME is
176 (let ((url (string-append %cran-url name "/DESCRIPTION")))
177 (guard (c ((http-get-error? c)
178 (format (current-error-port)
179 "error: failed to retrieve package information \
181 (uri->string (http-get-error-uri c))
182 (http-get-error-code c)
183 (http-get-error-reason c))
185 (description->alist (read-string (http-fetch url))))))
187 ;; Currently, the bioconductor project does not offer a way to access a
188 ;; package's DESCRIPTION file over HTTP, so we determine the version,
189 ;; download the source tarball, and then extract the DESCRIPTION file.
190 (and-let* ((version (latest-bioconductor-package-version name))
191 (url (car (bioconductor-uri name version)))
192 (tarball (download url)))
193 (call-with-temporary-directory
195 (parameterize ((current-error-port (%make-void-port "rw+"))
196 (current-output-port (%make-void-port "rw+")))
197 (and (zero? (system* "tar" "--wildcards" "-x"
198 "--strip-components=1"
200 "-f" tarball "*/DESCRIPTION"))
201 (description->alist (with-input-from-file
202 (string-append dir "/DESCRIPTION") read-string))))))))))
204 (define (listify meta field)
205 "Look up FIELD in the alist META. If FIELD contains a comma-separated
206 string, turn it into a list and strip off parenthetic expressions. Return the
207 empty list when the FIELD cannot be found."
208 (let ((value (assoc-ref meta field)))
211 ;; Strip off parentheses
212 (let ((items (string-split (regexp-substitute/global
213 #f "( *\\([^\\)]+\\)) *"
216 (remove (lambda (item)
217 (or (string-null? item)
218 ;; When there is whitespace inside of items it is
219 ;; probably because this was not an actual list to
221 (string-any char-set:whitespace item)))
222 (map string-trim-both items))))))
224 (define default-r-packages
240 (define cran-guix-name (cut guix-name "r-" <>))
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 (download 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 ,(cran-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)
330 (cran-guix-name name)))
331 `((properties ,`(,'quasiquote ((,'upstream-name . ,name)))))
333 (build-system r-build-system)
334 ,@(maybe-inputs sysdepends)
335 ,@(maybe-inputs (map cran-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 (let ((description (fetch-description repo package-name)))
357 (if (and (not description)
358 (eq? repo 'bioconductor))
359 ;; Retry import from CRAN
360 (cran->guix-package package-name 'cran)
362 (description->package repo description)))))))
364 (define* (cran-recursive-import package-name #:optional (repo 'cran))
365 (recursive-import package-name repo
366 #:repo->guix-package cran->guix-package
367 #:guix-name cran-guix-name))
374 (define (package->upstream-name package)
375 "Return the upstream name of the PACKAGE."
376 (let* ((properties (package-properties package))
377 (upstream-name (and=> properties
378 (cut assoc-ref <> 'upstream-name))))
381 (match (package-source package)
383 (match (origin-uri origin)
384 ((or (? string? url) (url _ ...))
385 (let ((end (string-rindex url #\_))
386 (start (string-rindex url #\/)))
388 ;; (string-append "/" name "_" version ".tar.gz")
389 (and start end (substring url (+ start 1) end))))
393 (define (latest-cran-release pkg)
394 "Return an <upstream-source> for the latest release of the package PKG."
396 (define upstream-name
397 (package->upstream-name pkg))
400 (fetch-description 'cran upstream-name))
403 (let ((version (assoc-ref meta "Version")))
404 ;; CRAN does not provide signatures.
406 (package (package-name pkg))
408 (urls (cran-uri upstream-name version))
411 (description->package 'cran meta)))))))
413 (define (latest-bioconductor-release pkg)
414 "Return an <upstream-source> for the latest release of the package PKG."
416 (define upstream-name
417 (package->upstream-name pkg))
420 (latest-bioconductor-package-version upstream-name))
423 ;; Bioconductor does not provide signatures.
425 (package (package-name pkg))
427 (urls (bioconductor-uri upstream-name version))
431 (cran->guix-package upstream-name 'bioconductor))))))
433 (define (cran-package? package)
434 "Return true if PACKAGE is an R package from CRAN."
435 (and (string-prefix? "r-" (package-name package))
436 ;; Check if the upstream name can be extracted from package uri.
437 (package->upstream-name package)
438 ;; Check if package uri(s) are prefixed by "mirror://cran".
439 (match (and=> (package-source package) origin-uri)
441 (string-prefix? "mirror://cran" uri))
443 (any (cut string-prefix? "mirror://cran" <>) uris))
446 (define (bioconductor-package? package)
447 "Return true if PACKAGE is an R package from Bioconductor."
448 (let ((predicate (lambda (uri)
449 (and (string-prefix? "https://bioconductor.org" uri)
450 ;; Data packages are neither listed in SVN nor on
451 ;; the Github mirror, so we have to exclude them
452 ;; from the set of bioconductor packages that can be
453 ;; updated automatically.
454 (not (string-contains uri "/data/annotation/"))
455 ;; Experiment packages are in a separate repository.
456 (not (string-contains uri "/data/experiment/"))))))
457 (and (string-prefix? "r-" (package-name package))
458 (match (and=> (package-source package) origin-uri)
462 (any predicate uris))
465 (define (bioconductor-data-package? package)
466 "Return true if PACKAGE is an R data package from Bioconductor."
467 (let ((predicate (lambda (uri)
468 (and (string-prefix? "https://bioconductor.org" uri)
469 (string-contains uri "/data/annotation/")))))
470 (and (string-prefix? "r-" (package-name package))
471 (match (and=> (package-source package) origin-uri)
475 (any predicate uris))
478 (define (bioconductor-experiment-package? package)
479 "Return true if PACKAGE is an R experiment package from Bioconductor."
480 (let ((predicate (lambda (uri)
481 (and (string-prefix? "https://bioconductor.org" uri)
482 (string-contains uri "/data/experiment/")))))
483 (and (string-prefix? "r-" (package-name package))
484 (match (and=> (package-source package) origin-uri)
488 (any predicate uris))
491 (define %cran-updater
494 (description "Updater for CRAN packages")
496 (latest latest-cran-release)))
498 (define %bioconductor-updater
501 (description "Updater for Bioconductor packages")
502 (pred bioconductor-package?)
503 (latest latest-bioconductor-release)))
505 ;;; cran.scm ends here