import: pypi: Match new 'pypi-uri' domain in updater.
[jackhill/guix/guix.git] / guix / import / pypi.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2015 Cyril Roelandt <tipecaml@gmail.com>
4 ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
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 pypi)
22 #:use-module (ice-9 binary-ports)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 pretty-print)
25 #:use-module (ice-9 regex)
26 #:use-module ((ice-9 rdelim) #:select (read-line))
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
31 #:use-module (rnrs bytevectors)
32 #:use-module (json)
33 #:use-module (web uri)
34 #:use-module (guix ui)
35 #:use-module (guix utils)
36 #:use-module ((guix build utils)
37 #:select ((package-name->name+version
38 . hyphen-package-name->name+version)))
39 #:use-module (guix import utils)
40 #:use-module ((guix download) #:prefix download:)
41 #:use-module (guix import json)
42 #:use-module (guix packages)
43 #:use-module (guix upstream)
44 #:use-module ((guix licenses) #:prefix license:)
45 #:use-module (guix build-system python)
46 #:use-module (gnu packages python)
47 #:export (guix-package->pypi-name
48 pypi->guix-package
49 %pypi-updater))
50
51 (define (pypi-fetch name)
52 "Return an alist representation of the PyPI metadata for the package NAME,
53 or #f on failure."
54 (json-fetch (string-append "https://pypi.python.org/pypi/"
55 name "/json")))
56
57 ;; For packages found on PyPI that lack a source distribution.
58 (define-condition-type &missing-source-error &error
59 missing-source-error?
60 (package missing-source-error-package))
61
62 (define (latest-source-release pypi-package)
63 "Return the latest source release for PYPI-PACKAGE."
64 (let ((releases (assoc-ref* pypi-package "releases"
65 (assoc-ref* pypi-package "info" "version"))))
66 (or (find (lambda (release)
67 (string=? "sdist" (assoc-ref release "packagetype")))
68 releases)
69 (raise (condition (&missing-source-error
70 (package pypi-package)))))))
71
72 (define (latest-wheel-release pypi-package)
73 "Return the url of the wheel for the latest release of pypi-package,
74 or #f if there isn't any."
75 (let ((releases (assoc-ref* pypi-package "releases"
76 (assoc-ref* pypi-package "info" "version"))))
77 (or (find (lambda (release)
78 (string=? "bdist_wheel" (assoc-ref release "packagetype")))
79 releases)
80 #f)))
81
82 (define (python->package-name name)
83 "Given the NAME of a package on PyPI, return a Guix-compliant name for the
84 package."
85 (if (string-prefix? "python-" name)
86 (snake-case name)
87 (string-append "python-" (snake-case name))))
88
89 (define (guix-package->pypi-name package)
90 "Given a Python PACKAGE built from pypi.python.org, return the name of the
91 package on PyPI."
92 (let ((source-url (and=> (package-source package) origin-uri)))
93 (hyphen-package-name->name+version
94 (basename (file-sans-extension source-url)))))
95
96 (define (wheel-url->extracted-directory wheel-url)
97 (match (string-split (basename wheel-url) #\-)
98 ((name version _ ...)
99 (string-append name "-" version ".dist-info"))))
100
101 (define (maybe-inputs package-inputs)
102 "Given a list of PACKAGE-INPUTS, tries to generate the 'inputs' field of a
103 package definition."
104 (match package-inputs
105 (()
106 '())
107 ((package-inputs ...)
108 `((propagated-inputs (,'quasiquote ,package-inputs))))))
109
110 (define (guess-requirements source-url wheel-url tarball)
111 "Given SOURCE-URL, WHEEL-URL and a TARBALL of the package, return a list of
112 the required packages specified in the requirements.txt file. TARBALL will be
113 extracted in the current directory, and will be deleted."
114
115 (define (tarball-directory url)
116 ;; Given the URL of the package's tarball, return the name of the directory
117 ;; that will be created upon decompressing it. If the filetype is not
118 ;; supported, return #f.
119 ;; TODO: Support more archive formats.
120 (let ((basename (substring url (+ 1 (string-rindex url #\/)))))
121 (cond
122 ((string-suffix? ".tar.gz" basename)
123 (string-drop-right basename 7))
124 ((string-suffix? ".tar.bz2" basename)
125 (string-drop-right basename 8))
126 (else
127 (begin
128 (warning (_ "Unsupported archive format: \
129 cannot determine package dependencies"))
130 #f)))))
131
132 (define (clean-requirement s)
133 ;; Given a requirement LINE, as can be found in a Python requirements.txt
134 ;; file, remove everything other than the actual name of the required
135 ;; package, and return it.
136 (string-take s
137 (or (string-index s #\space)
138 (string-length s))))
139
140 (define (comment? line)
141 ;; Return #t if the given LINE is a comment, #f otherwise.
142 (eq? (string-ref (string-trim line) 0) #\#))
143
144 (define (read-requirements requirements-file)
145 ;; Given REQUIREMENTS-FILE, a Python requirements.txt file, return a list
146 ;; of name/variable pairs describing the requirements.
147 (call-with-input-file requirements-file
148 (lambda (port)
149 (let loop ((result '()))
150 (let ((line (read-line port)))
151 (if (eof-object? line)
152 result
153 (cond
154 ((or (string-null? line) (comment? line))
155 (loop result))
156 (else
157 (loop (cons (python->package-name (clean-requirement line))
158 result))))))))))
159
160 (define (read-wheel-metadata wheel-archive)
161 ;; Given WHEEL-ARCHIVE, a ZIP Python wheel archive, return the package's
162 ;; requirements.
163 (let* ((dirname (wheel-url->extracted-directory wheel-url))
164 (json-file (string-append dirname "/metadata.json")))
165 (and (zero? (system* "unzip" "-q" wheel-archive json-file))
166 (dynamic-wind
167 (const #t)
168 (lambda ()
169 (call-with-input-file json-file
170 (lambda (port)
171 (let* ((metadata (json->scm port))
172 (run_requires (hash-ref metadata "run_requires"))
173 (requirements (if run_requires
174 (hash-ref (list-ref run_requires 0)
175 "requires")
176 '())))
177 (map (lambda (r)
178 (python->package-name (clean-requirement r)))
179 requirements)))))
180 (lambda ()
181 (delete-file json-file)
182 (rmdir dirname))))))
183
184 (define (guess-requirements-from-wheel)
185 ;; Return the package's requirements using the wheel, or #f if an error
186 ;; occurs.
187 (call-with-temporary-output-file
188 (lambda (temp port)
189 (if wheel-url
190 (and (url-fetch wheel-url temp)
191 (read-wheel-metadata temp))
192 #f))))
193
194
195 (define (guess-requirements-from-source)
196 ;; Return the package's requirements by guessing them from the source.
197 (let ((dirname (tarball-directory source-url)))
198 (if (string? dirname)
199 (let* ((req-file (string-append dirname "/requirements.txt"))
200 (exit-code (system* "tar" "xf" tarball req-file)))
201 ;; TODO: support more formats.
202 (if (zero? exit-code)
203 (dynamic-wind
204 (const #t)
205 (lambda ()
206 (read-requirements req-file))
207 (lambda ()
208 (delete-file req-file)
209 (rmdir dirname)))
210 (begin
211 (warning (_ "'tar xf' failed with exit code ~a\n")
212 exit-code)
213 '())))
214 '())))
215
216 ;; First, try to compute the requirements using the wheel, since that is the
217 ;; most reliable option. If a wheel is not provided for this package, try
218 ;; getting them by reading the "requirements.txt" file from the source. Note
219 ;; that "requirements.txt" is not mandatory, so this is likely to fail.
220 (or (guess-requirements-from-wheel)
221 (guess-requirements-from-source)))
222
223
224 (define (compute-inputs source-url wheel-url tarball)
225 "Given the SOURCE-URL of an already downloaded TARBALL, return a list of
226 name/variable pairs describing the required inputs of this package."
227 (sort
228 (map (lambda (input)
229 (list input (list 'unquote (string->symbol input))))
230 (append '("python-setuptools")
231 ;; Argparse has been part of Python since 2.7.
232 (remove (cut string=? "python-argparse" <>)
233 (guess-requirements source-url wheel-url tarball))))
234 (lambda args
235 (match args
236 (((a _ ...) (b _ ...))
237 (string-ci<? a b))))))
238
239 (define (make-pypi-sexp name version source-url wheel-url home-page synopsis
240 description license)
241 "Return the `package' s-expression for a python package with the given NAME,
242 VERSION, SOURCE-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
243 (call-with-temporary-output-file
244 (lambda (temp port)
245 (and (url-fetch source-url temp)
246 `(package
247 (name ,(python->package-name name))
248 (version ,version)
249 (source (origin
250 (method url-fetch)
251
252 ;; Sometimes 'pypi-uri' doesn't quite work due to mixed
253 ;; cases in NAME, for instance, as is the case with
254 ;; "uwsgi". In that case, fall back to a full URL.
255 (uri ,(if (equal? (pypi-uri name version) source-url)
256 `(pypi-uri ,name version)
257 `(string-append
258 ,@(factorize-uri source-url version))))
259
260 (sha256
261 (base32
262 ,(guix-hash-url temp)))))
263 (build-system python-build-system)
264 ,@(maybe-inputs (compute-inputs source-url wheel-url temp))
265 (home-page ,home-page)
266 (synopsis ,synopsis)
267 (description ,description)
268 (license ,(license->symbol license)))))))
269
270 (define (pypi->guix-package package-name)
271 "Fetch the metadata for PACKAGE-NAME from pypi.python.org, and return the
272 `package' s-expression corresponding to that package, or #f on failure."
273 (let ((package (pypi-fetch package-name)))
274 (and package
275 (guard (c ((missing-source-error? c)
276 (let ((package (missing-source-error-package c)))
277 (leave (_ "no source release for pypi package ~a ~a~%")
278 (assoc-ref* package "info" "name")
279 (assoc-ref* package "info" "version")))))
280 (let ((name (assoc-ref* package "info" "name"))
281 (version (assoc-ref* package "info" "version"))
282 (release (assoc-ref (latest-source-release package) "url"))
283 (wheel (assoc-ref (latest-wheel-release package) "url"))
284 (synopsis (assoc-ref* package "info" "summary"))
285 (description (assoc-ref* package "info" "summary"))
286 (home-page (assoc-ref* package "info" "home_page"))
287 (license (string->license (assoc-ref* package "info" "license"))))
288 (make-pypi-sexp name version release wheel home-page synopsis
289 description license))))))
290
291 (define (string->license str)
292 "Convert the string STR into a license object."
293 (match str
294 ("GNU LGPL" license:lgpl2.0)
295 ("GPL" license:gpl3)
296 ((or "BSD" "BSD License") license:bsd-3)
297 ((or "MIT" "MIT license" "Expat license") license:expat)
298 ("Public domain" license:public-domain)
299 ((or "Apache License, Version 2.0" "Apache 2.0") license:asl2.0)
300 (_ #f)))
301
302 (define (pypi-package? package)
303 "Return true if PACKAGE is a Python package from PyPI."
304
305 (define (pypi-url? url)
306 (or (string-prefix? "https://pypi.python.org/" url)
307 (string-prefix? "https://pypi.io/packages" url)))
308
309 (let ((source-url (and=> (package-source package) origin-uri))
310 (fetch-method (and=> (package-source package) origin-method)))
311 (and (eq? fetch-method download:url-fetch)
312 (match source-url
313 ((? string?)
314 (pypi-url? source-url))
315 ((source-url ...)
316 (any pypi-url? source-url))))))
317
318 (define (latest-release package)
319 "Return an <upstream-source> for the latest release of PACKAGE."
320 (guard (c ((missing-source-error? c) #f))
321 (let* ((pypi-name (guix-package->pypi-name package))
322 (metadata (pypi-fetch pypi-name))
323 (version (assoc-ref* metadata "info" "version"))
324 (url (assoc-ref (latest-source-release metadata) "url")))
325 (upstream-source
326 (package (package-name package))
327 (version version)
328 (urls (list url))))))
329
330 (define %pypi-updater
331 (upstream-updater
332 (name 'pypi)
333 (description "Updater for PyPI packages")
334 (pred pypi-package?)
335 (latest latest-release)))