You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
15 KiB

ui: Rename '_' to 'G_'. This avoids collisions with '_' when the latter is used as a 'match' pattern for instance. See <https://lists.gnu.org/archive/html/guix-devel/2017-04/msg00464.html>. * guix/ui.scm: Rename '_' to 'G_'. * po/guix/Makevars (XGETTEXT_OPTIONS): Adjust accordingly. * build-aux/compile-all.scm (warnings): Remove 'format'. * gnu/packages.scm, gnu/services.scm, gnu/services/shepherd.scm, gnu/system.scm, gnu/system/shadow.scm, guix/gnupg.scm, guix/http-client.scm, guix/import/cpan.scm, guix/import/elpa.scm, guix/import/pypi.scm, guix/nar.scm, guix/scripts.scm, guix/scripts/archive.scm, guix/scripts/authenticate.scm, guix/scripts/build.scm, guix/scripts/challenge.scm, guix/scripts/container.scm, guix/scripts/container/exec.scm, guix/scripts/copy.scm, guix/scripts/download.scm, guix/scripts/edit.scm, guix/scripts/environment.scm, guix/scripts/gc.scm, guix/scripts/graph.scm, guix/scripts/hash.scm, guix/scripts/import.scm, guix/scripts/import/cpan.scm, guix/scripts/import/cran.scm, guix/scripts/import/crate.scm, guix/scripts/import/elpa.scm, guix/scripts/import/gem.scm, guix/scripts/import/gnu.scm, guix/scripts/import/hackage.scm, guix/scripts/import/nix.scm, guix/scripts/import/pypi.scm, guix/scripts/import/stackage.scm, guix/scripts/lint.scm, guix/scripts/offload.scm, guix/scripts/pack.scm, guix/scripts/package.scm, guix/scripts/perform-download.scm, guix/scripts/publish.scm, guix/scripts/pull.scm, guix/scripts/refresh.scm, guix/scripts/size.scm, guix/scripts/substitute.scm, guix/scripts/system.scm, guix/ssh.scm, guix/upstream.scm: Use 'G_' instead of '_'. Most of this change was obtained by running: "sed -i -e's/(_ "/(G_ "/g' `find -name \*.scm`".
5 years ago
  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, 2017 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix import pypi)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 pretty-print)
  26. #:use-module (ice-9 regex)
  27. #:use-module (ice-9 receive)
  28. #:use-module ((ice-9 rdelim) #:select (read-line))
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-34)
  32. #:use-module (srfi srfi-35)
  33. #:use-module (rnrs bytevectors)
  34. #:use-module (json)
  35. #:use-module (web uri)
  36. #:use-module (guix ui)
  37. #:use-module (guix utils)
  38. #:use-module ((guix build utils)
  39. #:select ((package-name->name+version
  40. . hyphen-package-name->name+version)
  41. find-files))
  42. #:use-module (guix import utils)
  43. #:use-module ((guix download) #:prefix download:)
  44. #:use-module (guix import json)
  45. #:use-module (guix packages)
  46. #:use-module (guix upstream)
  47. #:use-module ((guix licenses) #:prefix license:)
  48. #:use-module (guix build-system python)
  49. #:export (guix-package->pypi-name
  50. pypi-recursive-import
  51. pypi->guix-package
  52. %pypi-updater))
  53. (define (pypi-fetch name)
  54. "Return an alist representation of the PyPI metadata for the package NAME,
  55. or #f on failure."
  56. (json-fetch-alist (string-append "https://pypi.org/pypi/" name "/json")))
  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. (define (latest-source-release pypi-package)
  62. "Return the latest source release for PYPI-PACKAGE."
  63. (let ((releases (assoc-ref* pypi-package "releases"
  64. (assoc-ref* pypi-package "info" "version"))))
  65. (or (find (lambda (release)
  66. (string=? "sdist" (assoc-ref release "packagetype")))
  67. releases)
  68. (raise (condition (&missing-source-error
  69. (package pypi-package)))))))
  70. (define (latest-wheel-release pypi-package)
  71. "Return the url of the wheel for the latest release of pypi-package,
  72. or #f if there isn't any."
  73. (let ((releases (assoc-ref* pypi-package "releases"
  74. (assoc-ref* pypi-package "info" "version"))))
  75. (or (find (lambda (release)
  76. (string=? "bdist_wheel" (assoc-ref release "packagetype")))
  77. releases)
  78. #f)))
  79. (define (python->package-name name)
  80. "Given the NAME of a package on PyPI, return a Guix-compliant name for the
  81. package."
  82. (if (string-prefix? "python-" name)
  83. (snake-case name)
  84. (string-append "python-" (snake-case name))))
  85. (define (guix-package->pypi-name package)
  86. "Given a Python PACKAGE built from pypi.org, return the name of the
  87. package on PyPI."
  88. (define (url->pypi-name url)
  89. (hyphen-package-name->name+version
  90. (basename (file-sans-extension url))))
  91. (match (and=> (package-source package) origin-uri)
  92. ((? string? url)
  93. (url->pypi-name url))
  94. ((lst ...)
  95. (any url->pypi-name lst))
  96. (#f #f)))
  97. (define (wheel-url->extracted-directory wheel-url)
  98. (match (string-split (basename wheel-url) #\-)
  99. ((name version _ ...)
  100. (string-append name "-" version ".dist-info"))))
  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. (define (guess-requirements source-url wheel-url tarball)
  110. "Given SOURCE-URL, WHEEL-URL and a TARBALL of the package, return a list
  111. of the required packages specified in the requirements.txt file. TARBALL will
  112. be extracted in a temporary directory."
  113. (define (tarball-directory url)
  114. ;; Given the URL of the package's tarball, return the name of the directory
  115. ;; that will be created upon decompressing it. If the filetype is not
  116. ;; supported, return #f.
  117. ;; TODO: Support more archive formats.
  118. (let ((basename (substring url (+ 1 (string-rindex url #\/)))))
  119. (cond
  120. ((string-suffix? ".tar.gz" basename)
  121. (string-drop-right basename 7))
  122. ((string-suffix? ".tar.bz2" basename)
  123. (string-drop-right basename 8))
  124. (else
  125. (begin
  126. (warning (G_ "Unsupported archive format: \
  127. cannot determine package dependencies"))
  128. #f)))))
  129. (define (clean-requirement s)
  130. ;; Given a requirement LINE, as can be found in a Python requirements.txt
  131. ;; file, remove everything other than the actual name of the required
  132. ;; package, and return it.
  133. (string-take s
  134. (or (string-index s (lambda (chr) (member chr '(#\space #\> #\= #\<))))
  135. (string-length s))))
  136. (define (comment? line)
  137. ;; Return #t if the given LINE is a comment, #f otherwise.
  138. (eq? (string-ref (string-trim line) 0) #\#))
  139. (define (read-requirements requirements-file)
  140. ;; Given REQUIREMENTS-FILE, a Python requirements.txt file, return a list
  141. ;; of name/variable pairs describing the requirements.
  142. (call-with-input-file requirements-file
  143. (lambda (port)
  144. (let loop ((result '()))
  145. (let ((line (read-line port)))
  146. (if (eof-object? line)
  147. result
  148. (cond
  149. ((or (string-null? line) (comment? line))
  150. (loop result))
  151. (else
  152. (loop (cons (clean-requirement line)
  153. result))))))))))
  154. (define (read-wheel-metadata wheel-archive)
  155. ;; Given WHEEL-ARCHIVE, a ZIP Python wheel archive, return the package's
  156. ;; requirements.
  157. (let* ((dirname (wheel-url->extracted-directory wheel-url))
  158. (json-file (string-append dirname "/metadata.json")))
  159. (and (zero? (system* "unzip" "-q" wheel-archive json-file))
  160. (dynamic-wind
  161. (const #t)
  162. (lambda ()
  163. (call-with-input-file json-file
  164. (lambda (port)
  165. (let* ((metadata (json->scm port))
  166. (run_requires (hash-ref metadata "run_requires"))
  167. (requirements (if run_requires
  168. (hash-ref (list-ref run_requires 0)
  169. "requires")
  170. '())))
  171. (map clean-requirement requirements)))))
  172. (lambda ()
  173. (delete-file json-file)
  174. (rmdir dirname))))))
  175. (define (guess-requirements-from-wheel)
  176. ;; Return the package's requirements using the wheel, or #f if an error
  177. ;; occurs.
  178. (call-with-temporary-output-file
  179. (lambda (temp port)
  180. (if wheel-url
  181. (and (url-fetch wheel-url temp)
  182. (read-wheel-metadata temp))
  183. #f))))
  184. (define (guess-requirements-from-source)
  185. ;; Return the package's requirements by guessing them from the source.
  186. (let ((dirname (tarball-directory source-url)))
  187. (if (string? dirname)
  188. (call-with-temporary-directory
  189. (lambda (dir)
  190. (let* ((pypi-name (string-take dirname (string-rindex dirname #\-)))
  191. (req-files (list (string-append dirname "/requirements.txt")
  192. (string-append dirname "/" pypi-name ".egg-info"
  193. "/requires.txt")))
  194. (exit-codes (map (lambda (file-name)
  195. (parameterize ((current-error-port (%make-void-port "rw+"))
  196. (current-output-port (%make-void-port "rw+")))
  197. (system* "tar" "xf" tarball "-C" dir file-name)))
  198. req-files)))
  199. ;; Only one of these files needs to exist.
  200. (if (any zero? exit-codes)
  201. (match (find-files dir)
  202. ((file . _)
  203. (read-requirements file))
  204. (()
  205. (warning (G_ "No requirements file found.\n"))))
  206. (begin
  207. (warning (G_ "Failed to extract requirements files\n"))
  208. '())))))
  209. '())))
  210. ;; First, try to compute the requirements using the wheel, since that is the
  211. ;; most reliable option. If a wheel is not provided for this package, try
  212. ;; getting them by reading either the "requirements.txt" file or the
  213. ;; "requires.txt" from the egg-info directory from the source tarball. Note
  214. ;; that "requirements.txt" is not mandatory, so this is likely to fail.
  215. (or (guess-requirements-from-wheel)
  216. (guess-requirements-from-source)))
  217. (define (compute-inputs source-url wheel-url tarball)
  218. "Given the SOURCE-URL of an already downloaded TARBALL, return a list of
  219. name/variable pairs describing the required inputs of this package. Also
  220. return the unaltered list of upstream dependency names."
  221. (let ((dependencies
  222. (remove (cut string=? "argparse" <>)
  223. (guess-requirements source-url wheel-url tarball))))
  224. (values (sort
  225. (map (lambda (input)
  226. (let ((guix-name (python->package-name input)))
  227. (list guix-name (list 'unquote (string->symbol guix-name)))))
  228. dependencies)
  229. (lambda args
  230. (match args
  231. (((a _ ...) (b _ ...))
  232. (string-ci<? a b)))))
  233. dependencies)))
  234. (define (make-pypi-sexp name version source-url wheel-url home-page synopsis
  235. description license)
  236. "Return the `package' s-expression for a python package with the given NAME,
  237. VERSION, SOURCE-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
  238. (call-with-temporary-output-file
  239. (lambda (temp port)
  240. (and (url-fetch source-url temp)
  241. (receive (input-package-names upstream-dependency-names)
  242. (compute-inputs source-url wheel-url temp)
  243. (values
  244. `(package
  245. (name ,(python->package-name name))
  246. (version ,version)
  247. (source (origin
  248. (method url-fetch)
  249. ;; Sometimes 'pypi-uri' doesn't quite work due to mixed
  250. ;; cases in NAME, for instance, as is the case with
  251. ;; "uwsgi". In that case, fall back to a full URL.
  252. (uri (pypi-uri ,(string-downcase name) version))
  253. (sha256
  254. (base32
  255. ,(guix-hash-url temp)))))
  256. (build-system python-build-system)
  257. ,@(maybe-inputs input-package-names)
  258. (home-page ,home-page)
  259. (synopsis ,synopsis)
  260. (description ,description)
  261. (license ,(license->symbol license)))
  262. upstream-dependency-names))))))
  263. (define pypi->guix-package
  264. (memoize
  265. (lambda* (package-name)
  266. "Fetch the metadata for PACKAGE-NAME from pypi.org, and return the
  267. `package' s-expression corresponding to that package, or #f on failure."
  268. (let ((package (pypi-fetch package-name)))
  269. (and package
  270. (guard (c ((missing-source-error? c)
  271. (let ((package (missing-source-error-package c)))
  272. (leave (G_ "no source release for pypi package ~a ~a~%")
  273. (assoc-ref* package "info" "name")
  274. (assoc-ref* package "info" "version")))))
  275. (let ((name (assoc-ref* package "info" "name"))
  276. (version (assoc-ref* package "info" "version"))
  277. (release (assoc-ref (latest-source-release package) "url"))
  278. (wheel (assoc-ref (latest-wheel-release package) "url"))
  279. (synopsis (assoc-ref* package "info" "summary"))
  280. (description (assoc-ref* package "info" "summary"))
  281. (home-page (assoc-ref* package "info" "home_page"))
  282. (license (string->license (assoc-ref* package "info" "license"))))
  283. (make-pypi-sexp name version release wheel home-page synopsis
  284. description license))))))))
  285. (define (pypi-recursive-import package-name)
  286. (recursive-import package-name #f
  287. #:repo->guix-package (lambda (name repo)
  288. (pypi->guix-package name))
  289. #:guix-name python->package-name))
  290. (define (string->license str)
  291. "Convert the string STR into a license object."
  292. (match str
  293. ("GNU LGPL" license:lgpl2.0)
  294. ("GPL" license:gpl3)
  295. ((or "BSD" "BSD License") license:bsd-3)
  296. ((or "MIT" "MIT license" "Expat license") license:expat)
  297. ("Public domain" license:public-domain)
  298. ((or "Apache License, Version 2.0" "Apache 2.0") license:asl2.0)
  299. (_ #f)))
  300. (define (pypi-package? package)
  301. "Return true if PACKAGE is a Python package from PyPI."
  302. (define (pypi-url? url)
  303. (or (string-prefix? "https://pypi.org/" url)
  304. (string-prefix? "https://pypi.python.org/" url)
  305. (string-prefix? "https://pypi.io/packages" url)))
  306. (let ((source-url (and=> (package-source package) origin-uri))
  307. (fetch-method (and=> (package-source package) origin-method)))
  308. (and (eq? fetch-method download:url-fetch)
  309. (match source-url
  310. ((? string?)
  311. (pypi-url? source-url))
  312. ((source-url ...)
  313. (any pypi-url? source-url))))))
  314. (define (latest-release package)
  315. "Return an <upstream-source> for the latest release of PACKAGE."
  316. (let* ((pypi-name (guix-package->pypi-name package))
  317. (pypi-package (pypi-fetch pypi-name)))
  318. (and pypi-package
  319. (guard (c ((missing-source-error? c) #f))
  320. (let* ((metadata pypi-package)
  321. (version (assoc-ref* metadata "info" "version"))
  322. (url (assoc-ref (latest-source-release metadata) "url")))
  323. (upstream-source
  324. (package (package-name package))
  325. (version version)
  326. (urls (list url))))))))
  327. (define %pypi-updater
  328. (upstream-updater
  329. (name 'pypi)
  330. (description "Updater for PyPI packages")
  331. (pred pypi-package?)
  332. (latest latest-release)))