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