pypi.scm 25 KB

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