pypi.scm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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-2017, 2019-2023 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2018, 2023 Ricardo Wurmus <rekado@elephly.net>
  7. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  8. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  9. ;;; Copyright © 2020 Lars-Dominik Braun <ldb@leibniz-psychology.org>
  10. ;;; Copyright © 2020 Arun Isaac <arunisaac@systemreboot.net>
  11. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  12. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  13. ;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
  14. ;;; Copyright © 2022 Vivien Kraus <vivien@planete-kraus.eu>
  15. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  16. ;;; Copyright © 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  17. ;;;
  18. ;;; This file is part of GNU Guix.
  19. ;;;
  20. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  21. ;;; under the terms of the GNU General Public License as published by
  22. ;;; the Free Software Foundation; either version 3 of the License, or (at
  23. ;;; your option) any later version.
  24. ;;;
  25. ;;; GNU Guix is distributed in the hope that it will be useful, but
  26. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. ;;; GNU General Public License for more details.
  29. ;;;
  30. ;;; You should have received a copy of the GNU General Public License
  31. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  32. (define-module (guix import pypi)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 regex)
  35. #:use-module ((ice-9 rdelim) #:select (read-line))
  36. #:use-module (srfi srfi-1)
  37. #:use-module (srfi srfi-26)
  38. #:use-module (srfi srfi-34)
  39. #:use-module (srfi srfi-35)
  40. #:use-module (srfi srfi-71)
  41. #:autoload (gcrypt hash) (port-sha256)
  42. #:autoload (guix base16) (base16-string->bytevector)
  43. #:autoload (guix base32) (bytevector->nix-base32-string)
  44. #:autoload (guix http-client) (http-fetch)
  45. #:use-module (guix utils)
  46. #:use-module (guix memoization)
  47. #:use-module (guix diagnostics)
  48. #:use-module (guix i18n)
  49. #:use-module ((guix ui) #:select (display-hint))
  50. #:use-module ((guix build utils)
  51. #:select ((package-name->name+version
  52. . hyphen-package-name->name+version)
  53. find-files
  54. invoke))
  55. #:use-module (guix import utils)
  56. #:use-module (guix import json)
  57. #:use-module (json)
  58. #:use-module (guix packages)
  59. #:use-module (guix upstream)
  60. #:use-module ((guix licenses) #:prefix license:)
  61. #:export (%pypi-base-url
  62. parse-requires.txt
  63. parse-wheel-metadata
  64. specification->requirement-name
  65. guix-package->pypi-name
  66. pypi-recursive-import
  67. find-project-url
  68. pypi->guix-package
  69. %pypi-updater))
  70. ;; The PyPI API (notice the rhyme) is "documented" at:
  71. ;; <https://warehouse.readthedocs.io/api-reference/json/>.
  72. (define %pypi-base-url
  73. ;; Base URL of the PyPI API.
  74. (make-parameter "https://pypi.org/pypi/"))
  75. (define non-empty-string-or-false
  76. (match-lambda
  77. ("" #f)
  78. ((? string? str) str)
  79. ((or 'null #f) #f)))
  80. ;; PyPI project.
  81. (define-json-mapping <pypi-project> make-pypi-project pypi-project?
  82. json->pypi-project
  83. (info pypi-project-info "info" json->project-info) ;<project-info>
  84. (last-serial pypi-project-last-serial "last_serial") ;integer
  85. (releases pypi-project-releases "releases" ;string/<distribution>* pairs
  86. (match-lambda
  87. (((versions . dictionaries) ...)
  88. (map (lambda (version vector)
  89. (cons version
  90. (map json->distribution
  91. (vector->list vector))))
  92. versions dictionaries))))
  93. (distributions pypi-project-distributions "urls" ;<distribution>*
  94. (lambda (vector)
  95. (map json->distribution (vector->list vector)))))
  96. ;; Project metadata.
  97. (define-json-mapping <project-info> make-project-info project-info?
  98. json->project-info
  99. (name project-info-name) ;string
  100. (author project-info-author) ;string
  101. (maintainer project-info-maintainer) ;string
  102. (classifiers project-info-classifiers ;list of strings
  103. "classifiers" vector->list)
  104. (description project-info-description) ;string
  105. (summary project-info-summary) ;string
  106. (keywords project-info-keywords) ;string
  107. (license project-info-license) ;string
  108. (download-url project-info-download-url ;string | #f
  109. "download_url" non-empty-string-or-false)
  110. (home-page project-info-home-page ;string
  111. "home_page")
  112. (url project-info-url "project_url") ;string
  113. (release-url project-info-release-url "release_url") ;string
  114. (version project-info-version)) ;string
  115. ;; Distribution: a URL along with cryptographic hashes and metadata.
  116. (define-json-mapping <distribution> make-distribution distribution?
  117. json->distribution
  118. (url distribution-url) ;string
  119. (digests distribution-digests) ;list of string pairs
  120. (file-name distribution-file-name "filename") ;string
  121. (has-signature? distribution-has-signature? "has_sig") ;Boolean
  122. (package-type distribution-package-type "packagetype") ;"bdist_wheel" | ...
  123. (python-version distribution-package-python-version
  124. "python_version"))
  125. (define (distribution-sha256 distribution)
  126. "Return the SHA256 hash of DISTRIBUTION as a bytevector, or #f."
  127. (match (assoc-ref (distribution-digests distribution) "sha256")
  128. (#f #f)
  129. (str (base16-string->bytevector str))))
  130. (define (pypi-fetch name)
  131. "Return a <pypi-project> record for package NAME, or #f on failure."
  132. (and=> (json-fetch (string-append (%pypi-base-url) name "/json"))
  133. json->pypi-project))
  134. ;; For packages found on PyPI that lack a source distribution.
  135. (define-condition-type &missing-source-error &error
  136. missing-source-error?
  137. (package missing-source-error-package))
  138. (define (latest-version project)
  139. "Return the latest version of PROJECT, a <pypi-project> record."
  140. (project-info-version (pypi-project-info project)))
  141. (define* (source-release pypi-package
  142. #:optional (version (latest-version pypi-package)))
  143. "Return the source release of VERSION for PYPI-PACKAGE, a <pypi-project>
  144. record, by default the latest version."
  145. (let ((releases (or (assoc-ref (pypi-project-releases pypi-package) version)
  146. '())))
  147. (or (find (lambda (release)
  148. (string=? "sdist" (distribution-package-type release)))
  149. releases)
  150. (raise (condition (&missing-source-error
  151. (package pypi-package)))))))
  152. (define* (wheel-release pypi-package
  153. #:optional (version (latest-version pypi-package)))
  154. "Return the url of the wheel for the latest release of pypi-package,
  155. or #f if there isn't any."
  156. (let ((releases (assoc-ref (pypi-project-releases pypi-package) version)))
  157. (find (lambda (release)
  158. (string=? "bdist_wheel" (distribution-package-type release)))
  159. releases)))
  160. (define (python->package-name name)
  161. "Given the NAME of a package on PyPI, return a Guix-compliant name for the
  162. package."
  163. (cond
  164. ((string-prefix? "python-" name) (snake-case name))
  165. ((or (string=? "trytond" name)
  166. (string-prefix? "trytond-" name)) (snake-case name))
  167. (else (string-append "python-" (snake-case name)))))
  168. (define (guix-package->pypi-name package)
  169. "Given a Python PACKAGE built from pypi.org, return the name of the
  170. package on PyPI."
  171. (define (url->pypi-name url)
  172. (hyphen-package-name->name+version
  173. (basename (file-sans-extension url))))
  174. (or (assoc-ref (package-properties package) 'upstream-name)
  175. (match (and=> (package-source package) origin-uri)
  176. ((? string? url)
  177. (url->pypi-name url))
  178. ((lst ...)
  179. (any url->pypi-name lst))
  180. (#f #f))))
  181. (define (wheel-url->extracted-directory wheel-url)
  182. (match (string-split (basename wheel-url) #\-)
  183. ((name version _ ...)
  184. (string-append name "-" version ".dist-info"))))
  185. (define (maybe-inputs package-inputs input-type)
  186. "Given a list of PACKAGE-INPUTS, tries to generate the 'inputs' field of a
  187. package definition. INPUT-TYPE, a symbol, is used to populate the name of
  188. the input field."
  189. (match package-inputs
  190. (()
  191. '())
  192. ((package-inputs ...)
  193. `((,input-type (list ,@(map (compose string->symbol
  194. upstream-input-downstream-name)
  195. package-inputs)))))))
  196. (define %requirement-name-regexp
  197. ;; Regexp to match the requirement name in a requirement specification.
  198. ;; Some grammar, taken from PEP-0508 (see:
  199. ;; https://www.python.org/dev/peps/pep-0508/).
  200. ;; Using this grammar makes the PEP-0508 regexp easier to understand for
  201. ;; humans. The use of a regexp is preferred to more primitive string
  202. ;; manipulations because we can more directly match what upstream uses
  203. ;; (again, per PEP-0508). The regexp approach is also easier to extend,
  204. ;; should we want to implement more completely the grammar of PEP-0508.
  205. ;; The unified rule can be expressed as:
  206. ;; specification = wsp* ( url_req | name_req ) wsp*
  207. ;; where url_req is:
  208. ;; url_req = name wsp* extras? wsp* urlspec wsp+ quoted_marker?
  209. ;; and where name_req is:
  210. ;; name_req = name wsp* extras? wsp* versionspec? wsp* quoted_marker?
  211. ;; Thus, we need only matching NAME, which is expressed as:
  212. ;; identifer_end = letterOrDigit | (('-' | '_' | '.' )* letterOrDigit)
  213. ;; identifier = letterOrDigit identifier_end*
  214. ;; name = identifier
  215. (let* ((letter-or-digit "[A-Za-z0-9]")
  216. (identifier-end (string-append "(" letter-or-digit "|"
  217. "[-_.]*" letter-or-digit ")"))
  218. (identifier (string-append "^" letter-or-digit identifier-end "*"))
  219. (name identifier))
  220. (make-regexp name)))
  221. (define (specification->requirement-name spec)
  222. "Given a specification SPEC, return the requirement name."
  223. (match:substring
  224. (or (regexp-exec %requirement-name-regexp spec)
  225. (error (G_ "Could not extract requirement name in spec:") spec))))
  226. (define (test-section? name)
  227. "Return #t if the section name contains 'test' or 'dev'."
  228. (any (cut string-contains-ci name <>)
  229. '("test" "dev")))
  230. (define (parse-requires.txt requires.txt)
  231. "Given REQUIRES.TXT, a path to a Setuptools requires.txt file, return a list
  232. of lists of requirements.
  233. The first list contains the required dependencies while the second the
  234. optional test dependencies. Note that currently, optional, non-test
  235. dependencies are omitted since these can be difficult or expensive to
  236. satisfy."
  237. (define (comment? line)
  238. ;; Return #t if the given LINE is a comment, #f otherwise.
  239. (string-prefix? "#" (string-trim line)))
  240. (define (section-header? line)
  241. ;; Return #t if the given LINE is a section header, #f otherwise.
  242. (string-prefix? "[" (string-trim line)))
  243. (call-with-input-file requires.txt
  244. (lambda (port)
  245. (let loop ((required-deps '())
  246. (test-deps '())
  247. (inside-test-section? #f)
  248. (optional? #f))
  249. (let ((line (read-line port)))
  250. (cond
  251. ((eof-object? line)
  252. ;; Duplicates can occur, since the same requirement can be
  253. ;; listed multiple times with different conditional markers, e.g.
  254. ;; pytest >= 3 ; python_version >= "3.3"
  255. ;; pytest < 3 ; python_version < "3.3"
  256. (map (compose reverse delete-duplicates)
  257. (list required-deps test-deps)))
  258. ((or (string-null? line) (comment? line))
  259. (loop required-deps test-deps inside-test-section? optional?))
  260. ((section-header? line)
  261. ;; Encountering a section means that all the requirements
  262. ;; listed below are optional. Since we want to pick only the
  263. ;; test dependencies from the optional dependencies, we must
  264. ;; track those separately.
  265. (loop required-deps test-deps (test-section? line) #t))
  266. (inside-test-section?
  267. (loop required-deps
  268. (cons (specification->requirement-name line)
  269. test-deps)
  270. inside-test-section? optional?))
  271. ((not optional?)
  272. (loop (cons (specification->requirement-name line)
  273. required-deps)
  274. test-deps inside-test-section? optional?))
  275. (optional?
  276. ;; Skip optional items.
  277. (loop required-deps test-deps inside-test-section? optional?))
  278. (else
  279. (warning (G_ "parse-requires.txt reached an unexpected \
  280. condition on line ~a~%") line))))))))
  281. (define (parse-wheel-metadata metadata)
  282. "Given METADATA, a Wheel metadata file, return a list of lists of
  283. requirements.
  284. Refer to the documentation of PARSE-REQUIRES.TXT for a description of the
  285. returned value."
  286. ;; METADATA is a RFC-2822-like, header based file.
  287. (define (requires-dist-header? line)
  288. ;; Return #t if the given LINE is a Requires-Dist header.
  289. (string-match "^Requires-Dist: " line))
  290. (define (requires-dist-value line)
  291. (string-drop line (string-length "Requires-Dist: ")))
  292. (define (extra? line)
  293. ;; Return #t if the given LINE is an "extra" requirement.
  294. (string-match "extra == '(.*)'" line))
  295. (define (test-requirement? line)
  296. (and=> (match:substring (extra? line) 1) test-section?))
  297. (call-with-input-file metadata
  298. (lambda (port)
  299. (let loop ((required-deps '())
  300. (test-deps '()))
  301. (let ((line (read-line port)))
  302. (cond
  303. ((eof-object? line)
  304. (map (compose reverse delete-duplicates)
  305. (list required-deps test-deps)))
  306. ((and (requires-dist-header? line) (not (extra? line)))
  307. (loop (cons (specification->requirement-name
  308. (requires-dist-value line))
  309. required-deps)
  310. test-deps))
  311. ((and (requires-dist-header? line) (test-requirement? line))
  312. (loop required-deps
  313. (cons (specification->requirement-name (requires-dist-value line))
  314. test-deps)))
  315. (else
  316. (loop required-deps test-deps)))))))) ;skip line
  317. (define (guess-requirements source-url wheel-url archive)
  318. "Given SOURCE-URL, WHEEL-URL and an ARCHIVE of the package, return a list
  319. of the required packages specified in the requirements.txt file. ARCHIVE will
  320. be extracted in a temporary directory."
  321. (define (read-wheel-metadata wheel-archive)
  322. ;; Given WHEEL-ARCHIVE, a ZIP Python wheel archive, return the package's
  323. ;; requirements, or #f if the metadata file contained therein couldn't be
  324. ;; extracted.
  325. (let* ((dirname (wheel-url->extracted-directory wheel-url))
  326. (metadata (string-append dirname "/METADATA")))
  327. (call-with-temporary-directory
  328. (lambda (dir)
  329. (if (zero?
  330. (parameterize ((current-error-port (%make-void-port "rw+"))
  331. (current-output-port (%make-void-port "rw+")))
  332. (system* "unzip" wheel-archive "-d" dir metadata)))
  333. (parse-wheel-metadata (string-append dir "/" metadata))
  334. (begin
  335. (warning
  336. (G_ "Failed to extract file: ~a from wheel.~%") metadata)
  337. #f))))))
  338. (define (guess-requirements-from-wheel)
  339. ;; Return the package's requirements using the wheel, or #f if an error
  340. ;; occurs.
  341. (call-with-temporary-output-file
  342. (lambda (temp port)
  343. (if wheel-url
  344. (and (url-fetch wheel-url temp)
  345. (read-wheel-metadata temp))
  346. #f))))
  347. (define (guess-requirements-from-source)
  348. ;; Return the package's requirements by guessing them from the source.
  349. (if (compressed-file? source-url)
  350. (call-with-temporary-directory
  351. (lambda (dir)
  352. (parameterize ((current-error-port (%make-void-port "rw+"))
  353. (current-output-port (%make-void-port "rw+")))
  354. (if (string=? "zip" (file-extension source-url))
  355. (invoke "unzip" archive "-d" dir)
  356. (invoke "tar" "xf" archive "-C" dir)))
  357. (let ((requires.txt-files
  358. (find-files dir (lambda (abs-file-name _)
  359. (string-match "\\.egg-info/requires.txt$"
  360. abs-file-name)))))
  361. (match requires.txt-files
  362. (()
  363. (warning (G_ "Cannot guess requirements from source archive:\
  364. no requires.txt file found.~%"))
  365. (list '() '()))
  366. (else (parse-requires.txt (first requires.txt-files)))))))
  367. (begin
  368. (warning (G_ "Unsupported archive format; \
  369. cannot determine package dependencies from source archive: ~a~%")
  370. (basename source-url))
  371. (list '() '()))))
  372. ;; First, try to compute the requirements using the wheel, else, fallback to
  373. ;; reading the "requires.txt" from the egg-info directory from the source
  374. ;; archive.
  375. (or (guess-requirements-from-wheel)
  376. (guess-requirements-from-source)))
  377. (define (compute-inputs source-url wheel-url archive)
  378. "Given the SOURCE-URL and WHEEL-URL of an already downloaded ARCHIVE, return
  379. the corresponding list of <upstream-input> records."
  380. (define (requirements->upstream-inputs deps type)
  381. (filter-map (match-lambda
  382. ("argparse" #f)
  383. (name (upstream-input
  384. (name name)
  385. (downstream-name (python->package-name name))
  386. (type type))))
  387. (sort deps string-ci<?)))
  388. ;; TODO: Record version number ranges in <upstream-input>.
  389. (let ((dependencies (guess-requirements source-url wheel-url archive)))
  390. (match dependencies
  391. ((propagated native)
  392. (append (requirements->upstream-inputs propagated 'propagated)
  393. (requirements->upstream-inputs native 'native))))))
  394. (define* (pypi-package-inputs pypi-package #:optional version)
  395. "Return the list of <upstream-input> for PYPI-PACKAGE. This procedure
  396. downloads the source and possibly the wheel of PYPI-PACKAGE."
  397. (let* ((info (pypi-project-info pypi-package))
  398. (version (or version (project-info-version info)))
  399. (dist (source-release pypi-package version))
  400. (source-url (distribution-url dist))
  401. (wheel-url (and=> (wheel-release pypi-package version)
  402. distribution-url)))
  403. (call-with-temporary-output-file
  404. (lambda (archive port)
  405. (and (url-fetch source-url archive)
  406. (compute-inputs source-url wheel-url archive))))))
  407. (define (find-project-url name pypi-url)
  408. "Try different project name substitution until the result is found in
  409. pypi-uri. Downcase is required for \"uWSGI\", and
  410. underscores are required for flake8-array-spacing."
  411. (or (find (cut string-contains pypi-url <>)
  412. (list name
  413. (string-downcase name)
  414. (string-replace-substring name "-" "_")))
  415. (begin
  416. (warning
  417. (G_ "project name ~a does not appear verbatim in the PyPI URI~%")
  418. name)
  419. (display-hint
  420. (format #f (G_ "The PyPI URI is: @url{~a}. You should review the
  421. pypi-uri declaration in the generated package. You may need to replace ~s with
  422. a substring of the PyPI URI that identifies the package.") pypi-url name))
  423. name)))
  424. (define* (pypi-package->upstream-source pypi-package #:optional version)
  425. "Return the upstream source for the given VERSION of PYPI-PACKAGE, a
  426. <pypi-project> record. If VERSION is omitted or #f, use the latest version."
  427. (let* ((info (pypi-project-info pypi-package))
  428. (version (or version (project-info-version info)))
  429. (dist (source-release pypi-package version))
  430. (source-url (distribution-url dist))
  431. (wheel-url (and=> (wheel-release pypi-package version)
  432. distribution-url)))
  433. (let ((extra-inputs (if (string-suffix? ".zip" source-url)
  434. (list (upstream-input
  435. (name "zip")
  436. (downstream-name "zip")
  437. (type 'native)))
  438. '())))
  439. (upstream-source
  440. (urls (list source-url))
  441. (signature-urls
  442. (if (distribution-has-signature? dist)
  443. (list (string-append source-url ".asc"))
  444. #f))
  445. (inputs (append (pypi-package-inputs pypi-package)
  446. extra-inputs))
  447. (package (project-info-name info))
  448. (version version)))))
  449. (define* (make-pypi-sexp pypi-package
  450. #:optional (version (latest-version pypi-package)))
  451. "Return the `package' s-expression the given VERSION of PYPI-PACKAGE, a
  452. <pypi-project> record."
  453. (define (maybe-upstream-name name)
  454. (if (string-match ".*\\-[0-9]+" name)
  455. `((properties ,`'(("upstream-name" . ,name))))
  456. '()))
  457. (let* ((info (pypi-project-info pypi-package))
  458. (name (project-info-name info))
  459. (source-url (and=> (source-release pypi-package version)
  460. distribution-url))
  461. (sha256 (and=> (source-release pypi-package version)
  462. distribution-sha256))
  463. (source (pypi-package->upstream-source pypi-package version)))
  464. (values
  465. `(package
  466. (name ,(python->package-name name))
  467. (version ,version)
  468. (source
  469. (origin
  470. (method url-fetch)
  471. (uri (pypi-uri
  472. ,(find-project-url name source-url)
  473. version
  474. ;; Some packages have been released as `.zip`
  475. ;; instead of the more common `.tar.gz`. For
  476. ;; example, see "path-and-address".
  477. ,@(if (string-suffix? ".zip" source-url)
  478. '(".zip")
  479. '())))
  480. (sha256 (base32
  481. ,(and=> (or sha256
  482. (let* ((port (http-fetch source-url))
  483. (hash (port-sha256 port)))
  484. (close-port port)
  485. hash))
  486. bytevector->nix-base32-string)))))
  487. ,@(maybe-upstream-name name)
  488. (build-system pyproject-build-system)
  489. ,@(maybe-inputs (upstream-source-propagated-inputs source)
  490. 'propagated-inputs)
  491. ,@(maybe-inputs (upstream-source-native-inputs source)
  492. 'native-inputs)
  493. (home-page ,(project-info-home-page info))
  494. (synopsis ,(project-info-summary info))
  495. (description ,(beautify-description
  496. (project-info-summary info)))
  497. (license ,(license->symbol
  498. (string->license
  499. (project-info-license info)))))
  500. (map upstream-input-name (upstream-source-inputs source)))))
  501. (define pypi->guix-package
  502. (memoize
  503. (lambda* (package-name #:key version #:allow-other-keys)
  504. "Fetch the metadata for PACKAGE-NAME from pypi.org, and return the
  505. `package' s-expression corresponding to that package, or #f on failure."
  506. (let* ((project (pypi-fetch package-name))
  507. (info (and=> project pypi-project-info))
  508. (version (or version (and=> project latest-version))))
  509. (if project
  510. (guard (c ((missing-source-error? c)
  511. (let ((package (missing-source-error-package c)))
  512. (raise
  513. (apply
  514. make-compound-condition
  515. (formatted-message
  516. (G_ "no source release for pypi package ~a ~a~%")
  517. (project-info-name info) version)
  518. (match (project-info-home-page info)
  519. ((or #f "") '())
  520. (url
  521. (list
  522. (condition
  523. (&fix-hint
  524. (hint (format #f (G_ "This indicates that the
  525. package is available on PyPI, but only as a \"wheel\" containing binaries, not
  526. source. To build it from source, refer to the upstream repository at
  527. @uref{~a}.")
  528. url))))))))))))
  529. (make-pypi-sexp project version))
  530. (values #f '()))))))
  531. (define* (pypi-recursive-import package-name #:optional version)
  532. (recursive-import package-name
  533. #:version version
  534. #:repo->guix-package pypi->guix-package
  535. #:guix-name python->package-name))
  536. (define (string->license str)
  537. "Convert the string STR into a license object."
  538. (match str
  539. ("GNU LGPL" license:lgpl2.0)
  540. ("GPL" license:gpl3)
  541. ((or "BSD" "BSD-3" "BSD License") license:bsd-3)
  542. ("BSD-2-Clause" license:bsd-2)
  543. ((or "MIT" "MIT license" "MIT License" "Expat license") license:expat)
  544. ("Public domain" license:public-domain)
  545. ((or "Apache License, Version 2.0" "Apache 2.0") license:asl2.0)
  546. ("MPL 2.0" license:mpl2.0)
  547. (_ #f)))
  548. (define pypi-package?
  549. (url-predicate
  550. (lambda (url)
  551. (or (string-prefix? "https://pypi.org/" url)
  552. (string-prefix? "https://pypi.python.org/" url)
  553. (string-prefix? "https://pypi.org/packages" url)
  554. (string-prefix? "https://files.pythonhosted.org/packages" url)))))
  555. (define* (import-release package #:key (version #f))
  556. "Return an <upstream-source> for the latest release of PACKAGE. Optionally
  557. include a VERSION string to fetch a specific version."
  558. (let* ((pypi-name (guix-package->pypi-name package))
  559. (pypi-package (pypi-fetch pypi-name)))
  560. (and pypi-package
  561. (guard (c ((missing-source-error? c) #f))
  562. (pypi-package->upstream-source pypi-package version)))))
  563. (define %pypi-updater
  564. (upstream-updater
  565. (name 'pypi)
  566. (description "Updater for PyPI packages")
  567. (pred pypi-package?)
  568. (import import-release)))