Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / import / elpa.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
3 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix import elpa)
21 #:use-module (ice-9 match)
22 #:use-module (ice-9 rdelim)
23 #:use-module (web uri)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-9)
26 #:use-module (srfi srfi-9 gnu)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-26)
29 #:use-module ((guix download) #:select (download-to-store))
30 #:use-module (guix import utils)
31 #:use-module (guix http-client)
32 #:use-module (guix store)
33 #:use-module (guix ui)
34 #:use-module (guix hash)
35 #:use-module (guix base32)
36 #:use-module (guix upstream)
37 #:use-module (guix packages)
38 #:use-module ((guix utils) #:select (call-with-temporary-output-file))
39 #:export (elpa->guix-package
40 %elpa-updater))
41
42 (define (elpa-dependencies->names deps)
43 "Convert DEPS, a list of symbol/version pairs à la ELPA, to a list of
44 package names as strings"
45 (match deps
46 (((names _ ...) ...)
47 (map symbol->string names))))
48
49 (define emacs-standard-library?
50 (let ((libs '("emacs" "cl-lib")))
51 (lambda (lib)
52 "Return true if LIB is part of Emacs itself. The check is not
53 exhaustive and only attempts to recognize a subset of packages which in the
54 past were distributed separately from Emacs."
55 (member lib libs))))
56
57 (define (filter-dependencies names)
58 "Remove the package names included with Emacs from the list of
59 NAMES (strings)."
60 (filter (compose not emacs-standard-library?) names))
61
62 (define (elpa-name->package-name name)
63 "Given the NAME of an Emacs package, return the corresponding Guix name."
64 (let ((package-name-prefix "emacs-"))
65 (if (string-prefix? package-name-prefix name)
66 (string-downcase name)
67 (string-append package-name-prefix (string-downcase name)))))
68
69 (define* (elpa-url #:optional (repo 'gnu))
70 "Retrun the URL of REPO."
71 (let ((elpa-archives
72 '((gnu . "http://elpa.gnu.org/packages")
73 (melpa-stable . "http://stable.melpa.org/packages")
74 (melpa . "http://melpa.org/packages"))))
75 (assq-ref elpa-archives repo)))
76
77 (define* (elpa-fetch-archive #:optional (repo 'gnu))
78 "Retrive the archive with the list of packages available from REPO."
79 (let ((url (and=> (elpa-url repo)
80 (cut string-append <> "/archive-contents"))))
81 (if url
82 ;; Use a relatively small TTL for the archive itself.
83 (parameterize ((%http-cache-ttl (* 6 3600)))
84 (call-with-downloaded-file url read))
85 (leave (_ "~A: currently not supported~%") repo))))
86
87 (define* (call-with-downloaded-file url proc #:optional (error-thunk #f))
88 "Fetch URL, store the content in a temporary file and call PROC with that
89 file. Returns the value returned by PROC. On error call ERROR-THUNK and
90 return its value or leave if it's false."
91 (catch #t
92 (lambda ()
93 (proc (http-fetch/cached (string->uri url))))
94 (lambda (key . args)
95 (if error-thunk
96 (error-thunk)
97 (leave (_ "~A: download failed~%") url)))))
98
99 (define (is-elpa-package? name elpa-pkg-spec)
100 "Return true if the string NAME corresponds to the name of the package
101 defined by ELPA-PKG-SPEC, a package specification as in an archive
102 'archive-contents' file."
103 (eq? (first elpa-pkg-spec) (string->symbol name)))
104
105 (define* (elpa-package-info name #:optional (repo 'gnu))
106 "Extract the information about the package NAME from the package archieve of
107 REPO."
108 (let* ((archive (elpa-fetch-archive repo))
109 (pkgs (match archive ((version pkg-spec ...) pkg-spec)))
110 (info (filter (cut is-elpa-package? name <>) pkgs)))
111 (if (pair? info) (first info) #f)))
112
113 ;; Object to store information about an ELPA package.
114 (define-record-type <elpa-package>
115 (make-elpa-package name version inputs synopsis kind home-page description
116 source-url)
117 elpa-package?
118 (name elpa-package-name)
119 (version elpa-package-version)
120 (inputs elpa-package-inputs)
121 (synopsis elpa-package-synopsis)
122 (kind elpa-package-kind)
123 (home-page elpa-package-home-page)
124 (description elpa-package-description)
125 (source-url elpa-package-source-url))
126
127 (set-record-type-printer! <elpa-package>
128 (lambda (package port)
129 (format port "#<elpa-package ~a@~a>"
130 (elpa-package-name package)
131 (elpa-package-version package))))
132
133 (define (elpa-version->string elpa-version)
134 "Convert the package version as used in Emacs package files into a string."
135 (if (pair? elpa-version)
136 (let-values (((ms rest) (match elpa-version
137 ((ms . rest)
138 (values ms rest)))))
139 (fold (lambda (n s) (string-append s "." (number->string n)))
140 (number->string ms) rest))
141 #f))
142
143 (define (package-home-page alist)
144 "Extract the package home-page from ALIST."
145 (or (assq-ref alist ':url) "unspecified"))
146
147 (define (ensure-list alist)
148 "If ALIST is the symbol 'nil return the empty list. Otherwise, return ALIST."
149 (if (eq? alist 'nil)
150 '()
151 alist))
152
153 (define (package-source-url kind name version repo)
154 "Return the source URL of the package described the the strings NAME and
155 VERSION at REPO. KIND is either the symbol 'single or 'tar."
156 (case kind
157 ((single) (full-url repo name ".el" version))
158 ((tar) (full-url repo name ".tar" version))
159 (else
160 #f)))
161
162 (define* (full-url repo name suffix #:optional (version #f))
163 "Return the full URL of the package NAME at REPO and the SUFFIX. Maybe
164 include VERSION."
165 (if version
166 (string-append (elpa-url repo) "/" name "-" version suffix)
167 (string-append (elpa-url repo) "/" name suffix)))
168
169 (define (fetch-package-description kind name repo)
170 "Fetch the description of package NAME of type KIND from REPO."
171 (let ((url (full-url repo name "-readme.txt"))
172 (error-thunk (lambda () "No description available.")))
173 (call-with-downloaded-file url read-string error-thunk)))
174
175 (define* (fetch-elpa-package name #:optional (repo 'gnu))
176 "Fetch package NAME from REPO."
177 (let ((pkg (elpa-package-info name repo)))
178 (match pkg
179 ((name version reqs synopsis kind . rest)
180 (let* ((name (symbol->string name))
181 (ver (elpa-version->string version))
182 (url (package-source-url kind name ver repo)))
183 (make-elpa-package name ver
184 (ensure-list reqs) synopsis kind
185 (package-home-page (first rest))
186 (fetch-package-description kind name repo)
187 url)))
188 (_ #f))))
189
190 (define* (elpa-package->sexp pkg)
191 "Return the `package' S-expression for the Emacs package PKG, a record of
192 type '<elpa-package>'."
193
194 (define name (elpa-package-name pkg))
195
196 (define version (elpa-package-version pkg))
197
198 (define source-url (elpa-package-source-url pkg))
199
200 (define dependencies
201 (let* ((deps (elpa-package-inputs pkg))
202 (names (filter-dependencies (elpa-dependencies->names deps))))
203 (map (lambda (n)
204 (let ((new-n (elpa-name->package-name n)))
205 (list new-n (list 'unquote (string->symbol new-n)))))
206 names)))
207
208 (define (maybe-inputs input-type inputs)
209 (match inputs
210 (()
211 '())
212 ((inputs ...)
213 (list (list input-type
214 (list 'quasiquote inputs))))))
215
216 (let ((tarball (with-store store
217 (download-to-store store source-url))))
218 `(package
219 (name ,(elpa-name->package-name name))
220 (version ,version)
221 (source (origin
222 (method url-fetch)
223 (uri (string-append ,@(factorize-uri source-url version)))
224 (sha256
225 (base32
226 ,(if tarball
227 (bytevector->nix-base32-string (file-sha256 tarball))
228 "failed to download package")))))
229 (build-system emacs-build-system)
230 ,@(maybe-inputs 'propagated-inputs dependencies)
231 (home-page ,(elpa-package-home-page pkg))
232 (synopsis ,(elpa-package-synopsis pkg))
233 (description ,(elpa-package-description pkg))
234 (license license:gpl3+))))
235
236 (define* (elpa->guix-package name #:optional (repo 'gnu))
237 "Fetch the package NAME from REPO and produce a Guix package S-expression."
238 (let ((pkg (fetch-elpa-package name repo)))
239 (and=> pkg elpa-package->sexp)))
240
241 \f
242 ;;;
243 ;;; Updates.
244 ;;;
245
246 (define (latest-release package)
247 "Return an <upstream-release> for the latest release of PACKAGE."
248 (define name
249 (if (string-prefix? "emacs-" (package-name package))
250 (string-drop (package-name package) 6)
251 (package-name package)))
252
253 (let* ((repo 'gnu)
254 (info (elpa-package-info name repo))
255 (version (match info
256 ((name raw-version . _)
257 (elpa-version->string raw-version))))
258 (url (match info
259 ((_ raw-version reqs synopsis kind . rest)
260 (package-source-url kind name version repo)))))
261 (upstream-source
262 (package (package-name package))
263 (version version)
264 (urls (list url))
265 (signature-urls (list (string-append url ".sig"))))))
266
267 (define (package-from-gnu.org? package)
268 "Return true if PACKAGE is from elpa.gnu.org."
269 (match (and=> (package-source package) origin-uri)
270 ((? string? uri)
271 (let ((uri (string->uri uri)))
272 (and uri (string=? (uri-host uri) "elpa.gnu.org"))))
273 (_ #f)))
274
275 (define %elpa-updater
276 ;; The ELPA updater. We restrict it to packages hosted on elpa.gnu.org
277 ;; because for other repositories, we typically grab the source elsewhere.
278 (upstream-updater
279 (name 'elpa)
280 (description "Updater for ELPA packages")
281 (pred package-from-gnu.org?)
282 (latest latest-release)))
283
284 ;;; elpa.scm ends here