pypi.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  6. ;;; Copyright © 2022 Vivien Kraus <vivien@planete-kraus.eu>
  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 (test-pypi)
  23. #:use-module (guix import pypi)
  24. #:use-module (guix base32)
  25. #:use-module (guix memoization)
  26. #:use-module (guix utils)
  27. #:use-module ((guix base16) #:select (base16-string->bytevector))
  28. #:use-module (guix upstream)
  29. #:use-module (gcrypt hash)
  30. #:use-module (guix tests)
  31. #:use-module (guix tests http)
  32. #:use-module ((guix download) #:select (url-fetch))
  33. #:use-module (guix build-system python)
  34. #:use-module ((guix build utils)
  35. #:select (delete-file-recursively
  36. which mkdir-p dump-port
  37. with-directory-excursion))
  38. #:use-module ((guix diagnostics) #:select (guix-warning-port))
  39. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  40. #:use-module (json)
  41. #:use-module (srfi srfi-26)
  42. #:use-module (srfi srfi-34)
  43. #:use-module (srfi srfi-35)
  44. #:use-module (srfi srfi-64)
  45. #:use-module (ice-9 match)
  46. #:use-module (ice-9 optargs))
  47. (define default-sha256
  48. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
  49. (define default-sha256/base32
  50. (bytevector->nix-base32-string
  51. (base16-string->bytevector default-sha256)))
  52. (define* (foo-json #:key (name "foo") (name-in-url #f))
  53. "Create a JSON description of an example pypi package, named @var{name},
  54. optionally using a different @var{name in its URL}."
  55. (scm->json-string
  56. `((info
  57. . ((version . "1.0.0")
  58. (name . ,name)
  59. (license . "GNU LGPL")
  60. (summary . "summary")
  61. (home_page . "http://example.com")
  62. (classifiers . #())
  63. (download_url . "")))
  64. (urls . #())
  65. (releases
  66. . ((1.0.0
  67. . #(((url . ,(format #f "~a/~a-1.0.0.egg"
  68. (%local-url #:path "")
  69. (or name-in-url name)))
  70. (packagetype . "bdist_egg"))
  71. ((url . ,(format #f "~a/~a-1.0.0.tar.gz"
  72. (%local-url #:path "")
  73. (or name-in-url name)))
  74. (packagetype . "sdist")
  75. (digests . (("sha256" . ,default-sha256))))
  76. ((url . ,(format #f "~a/~a-1.0.0-py2.py3-none-any.whl"
  77. (%local-url #:path "")
  78. (or name-in-url name)))
  79. (packagetype . "bdist_wheel")))))))))
  80. (define test-specifications
  81. '("Fizzy [foo, bar]"
  82. "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
  83. "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
  84. "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
  85. "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
  86. sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
  87. (define test-requires.txt "\
  88. # A comment
  89. # A comment after a space
  90. foo ~= 3
  91. bar != 2
  92. [test]
  93. pytest (>=2.5.0)
  94. ")
  95. ;; Beaker contains only optional dependencies.
  96. (define test-requires.txt-beaker "\
  97. [crypto]
  98. pycryptopp>=0.5.12
  99. [cryptography]
  100. cryptography
  101. [testsuite]
  102. Mock
  103. coverage
  104. ")
  105. (define test-metadata "\
  106. Classifier: Programming Language :: Python :: 3.7
  107. Requires-Dist: baz ~= 3
  108. Requires-Dist: bar != 2
  109. Provides-Extra: test
  110. Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
  111. ")
  112. (define test-metadata-with-extras "
  113. Classifier: Programming Language :: Python :: 3.7
  114. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  115. Requires-Dist: wrapt (<2,>=1)
  116. Requires-Dist: bar
  117. Provides-Extra: dev
  118. Requires-Dist: tox ; extra == 'dev'
  119. Requires-Dist: bumpversion (<1) ; extra == 'dev'
  120. ")
  121. ;;; Provides-Extra can appear before Requires-Dist.
  122. (define test-metadata-with-extras-jedi "\
  123. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  124. Provides-Extra: testing
  125. Requires-Dist: parso (>=0.3.0)
  126. Provides-Extra: testing
  127. Requires-Dist: pytest (>=3.1.0); extra == 'testing'
  128. ")
  129. (define sample-directory
  130. ;; Directory containing tarballs and .whl files for this test.
  131. (let ((template (string-append (or (getenv "TMPDIR") "/tmp")
  132. "/guix-pypi-test-XXXXXX")))
  133. (mkdtemp! template)))
  134. (define (pypi-tarball name specs)
  135. "Return a PyPI tarball called NAME suffixed with '.tar.gz' and containing
  136. the files specified in SPECS. Return its file name."
  137. (let ((directory (in-vicinity sample-directory name))
  138. (tarball (in-vicinity sample-directory (string-append name ".tar.gz"))))
  139. (false-if-exception (delete-file tarball))
  140. (mkdir-p directory)
  141. (for-each (match-lambda
  142. ((file content)
  143. (mkdir-p (in-vicinity directory (dirname file)))
  144. (call-with-output-file (in-vicinity directory file)
  145. (lambda (port)
  146. (display content port)))))
  147. specs)
  148. (parameterize ((current-output-port (%make-void-port "w0")))
  149. (system* "tar" "-C" sample-directory "-czvf" tarball
  150. (basename directory)))
  151. (delete-file-recursively directory)
  152. tarball))
  153. (define (wheel-file name specs)
  154. "Return a Wheel file called NAME suffixed with '.whl' and containing the
  155. files specified by SPECS. Return its file name."
  156. (let* ((directory (in-vicinity sample-directory
  157. (string-append name ".dist-info")))
  158. (zip-file (in-vicinity sample-directory
  159. (string-append name ".zip")))
  160. (whl-file (in-vicinity sample-directory
  161. (string-append name ".whl"))))
  162. (false-if-exception (delete-file whl-file))
  163. (mkdir-p directory)
  164. (for-each (match-lambda
  165. ((file content)
  166. (mkdir-p (in-vicinity directory (dirname file)))
  167. (call-with-output-file (in-vicinity directory file)
  168. (lambda (port)
  169. (display content port)))))
  170. specs)
  171. ;; zip always adds a "zip" extension to the file it creates,
  172. ;; so we need to rename it.
  173. (with-directory-excursion (dirname directory)
  174. (system* "zip" "-qr" zip-file (basename directory)))
  175. (rename-file zip-file whl-file)
  176. (delete-file-recursively directory)
  177. whl-file))
  178. (define (file-dump file)
  179. "Return a procedure that dumps FILE to the given port."
  180. (lambda (output)
  181. (call-with-input-file file
  182. (lambda (input)
  183. (dump-port input output)))))
  184. (define-syntax-rule (with-pypi responses body ...)
  185. (with-http-server responses
  186. (parameterize ((%pypi-base-url (%local-url #:path "/")))
  187. body ...)))
  188. (test-begin "pypi")
  189. (test-equal "guix-package->pypi-name, old URL style"
  190. "psutil"
  191. (guix-package->pypi-name
  192. (dummy-package "foo"
  193. (source (dummy-origin
  194. (uri
  195. "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
  196. (test-equal "guix-package->pypi-name, new URL style"
  197. "certbot"
  198. (guix-package->pypi-name
  199. (dummy-package "foo"
  200. (source (dummy-origin
  201. (uri
  202. "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
  203. (test-equal "guix-package->pypi-name, several URLs"
  204. "cram"
  205. (guix-package->pypi-name
  206. (dummy-package "foo"
  207. (source
  208. (dummy-origin
  209. (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
  210. (pypi-uri "cram" "0.7"))))))))
  211. (test-equal "guix-package->pypi-name, honor 'upstream-name'"
  212. "bar-3"
  213. (guix-package->pypi-name
  214. (dummy-package "foo"
  215. (properties
  216. '((upstream-name . "bar-3"))))))
  217. (test-equal "specification->requirement-name"
  218. '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
  219. (map specification->requirement-name test-specifications))
  220. (test-equal "parse-requires.txt"
  221. (list '("foo" "bar") '("pytest"))
  222. (mock ((ice-9 ports) call-with-input-file
  223. call-with-input-string)
  224. (parse-requires.txt test-requires.txt)))
  225. (test-equal "parse-requires.txt - Beaker"
  226. (list '() '("Mock" "coverage"))
  227. (mock ((ice-9 ports) call-with-input-file
  228. call-with-input-string)
  229. (parse-requires.txt test-requires.txt-beaker)))
  230. (test-equal "parse-wheel-metadata, with extras"
  231. (list '("wrapt" "bar") '("tox" "bumpversion"))
  232. (mock ((ice-9 ports) call-with-input-file
  233. call-with-input-string)
  234. (parse-wheel-metadata test-metadata-with-extras)))
  235. (test-equal "parse-wheel-metadata, with extras - Jedi"
  236. (list '("parso") '("pytest"))
  237. (mock ((ice-9 ports) call-with-input-file
  238. call-with-input-string)
  239. (parse-wheel-metadata test-metadata-with-extras-jedi)))
  240. (test-equal "find-project-url, with numpy"
  241. "numpy"
  242. (find-project-url
  243. "numpy"
  244. "https://files.pythonhosted.org/packages/0a/c8/a62767a6b374a0dfb02d2a0456e5f56a372cdd1689dbc6ffb6bf1ddedbc0/numpy-1.22.1.zip"))
  245. (test-equal "find-project-url, uWSGI"
  246. "uwsgi"
  247. (find-project-url
  248. "uWSGI"
  249. "https://files.pythonhosted.org/packages/24/fd/93851e4a076719199868d4c918cc93a52742e68370188c1c570a6e42a54f/uwsgi-2.0.20.tar.gz"))
  250. (test-equal "find-project-url, flake8-array-spacing"
  251. "flake8_array_spacing"
  252. (find-project-url
  253. "flake8-array-spacing"
  254. "https://files.pythonhosted.org/packages/a4/21/ff29b901128b681b7de7a2787b3aeb3e1f3cba4a8c0cffa9712cbff016bc/flake8_array_spacing-0.2.0.tar.gz"))
  255. (test-equal "find-project-url, foo/goo"
  256. "foo"
  257. (find-project-url
  258. "foo"
  259. "https://files.pythonhosted.org/packages/f0/f00/goo-0.0.0.tar.gz"))
  260. (test-assert "pypi->guix-package, no wheel"
  261. (let ((tarball (pypi-tarball
  262. "foo-1.0.0"
  263. `(("src/bizarre.egg-info/requires.txt"
  264. ,test-requires.txt))))
  265. (twice (lambda (lst) (append lst lst))))
  266. (with-pypi (twice `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
  267. ("/foo-1.0.0-py2.py3-none-any.whl" 404 "")
  268. ("/foo/json" 200 ,(lambda (port)
  269. (display (foo-json) port)))))
  270. (match (pypi->guix-package "foo")
  271. (`(package
  272. (name "python-foo")
  273. (version "1.0.0")
  274. (source (origin
  275. (method url-fetch)
  276. (uri (pypi-uri "foo" version))
  277. (sha256
  278. (base32 ,(? string? hash)))))
  279. (build-system pyproject-build-system)
  280. (propagated-inputs (list python-bar python-foo))
  281. (native-inputs (list python-pytest))
  282. (home-page "http://example.com")
  283. (synopsis "summary")
  284. (description "summary")
  285. (license license:lgpl2.0))
  286. (and (string=? default-sha256/base32 hash)
  287. (equal? (pypi->guix-package "foo" #:version "1.0.0")
  288. (pypi->guix-package "foo"))
  289. (guard (c ((error? c) #t))
  290. (pypi->guix-package "foo" #:version "42"))))
  291. (x
  292. (pk 'fail x #f))))))
  293. (test-skip (if (which "zip") 0 1))
  294. (test-assert "pypi->guix-package, wheels"
  295. (let ((tarball (pypi-tarball
  296. "foo-1.0.0"
  297. '(("foo-1.0.0/foo.egg-info/requires.txt"
  298. "wrong data \
  299. to make sure we're testing wheels"))))
  300. (wheel (wheel-file "foo-1.0.0"
  301. `(("METADATA" ,test-metadata)))))
  302. (with-pypi `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
  303. ("/foo-1.0.0-py2.py3-none-any.whl"
  304. 200 ,(file-dump wheel))
  305. ("/foo/json" 200 ,(lambda (port)
  306. (display (foo-json) port))))
  307. ;; Not clearing the memoization cache here would mean returning the value
  308. ;; computed in the previous test.
  309. (invalidate-memoization! pypi->guix-package)
  310. (match (pypi->guix-package "foo")
  311. (`(package
  312. (name "python-foo")
  313. (version "1.0.0")
  314. (source (origin
  315. (method url-fetch)
  316. (uri (pypi-uri "foo" version))
  317. (sha256
  318. (base32 ,(? string? hash)))))
  319. (build-system pyproject-build-system)
  320. (propagated-inputs (list python-bar python-baz))
  321. (native-inputs (list python-pytest))
  322. (home-page "http://example.com")
  323. (synopsis "summary")
  324. (description "summary")
  325. (license license:lgpl2.0))
  326. (string=? default-sha256/base32 hash))
  327. (x
  328. (pk 'fail x #f))))))
  329. (test-assert "pypi->guix-package, no usable requirement file."
  330. (let ((tarball (pypi-tarball "foo-1.0.0"
  331. '(("foo.egg-info/.empty" "")))))
  332. (with-pypi `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
  333. ("/foo-1.0.0-py2.py3-none-any.whl" 404 "")
  334. ("/foo/json" 200 ,(lambda (port)
  335. (display (foo-json) port))))
  336. ;; Not clearing the memoization cache here would mean returning the
  337. ;; value computed in the previous test.
  338. (invalidate-memoization! pypi->guix-package)
  339. (match (pypi->guix-package "foo")
  340. (`(package
  341. (name "python-foo")
  342. (version "1.0.0")
  343. (source (origin
  344. (method url-fetch)
  345. (uri (pypi-uri "foo" version))
  346. (sha256
  347. (base32 ,(? string? hash)))))
  348. (build-system pyproject-build-system)
  349. (home-page "http://example.com")
  350. (synopsis "summary")
  351. (description "summary")
  352. (license license:lgpl2.0))
  353. (string=? default-sha256/base32 hash))
  354. (x
  355. (pk 'fail x #f))))))
  356. (test-assert "pypi->guix-package, package name contains \"-\" followed by digits"
  357. (let ((tarball (pypi-tarball "foo-99-1.0.0"
  358. `(("src/bizarre.egg-info/requires.txt"
  359. ,test-requires.txt)))))
  360. (with-pypi `(("/foo-99-1.0.0.tar.gz" 200 ,(file-dump tarball))
  361. ("/foo-99-1.0.0-py2.py3-none-any.whl" 404 "")
  362. ("/foo-99/json" 200 ,(lambda (port)
  363. (display (foo-json #:name "foo-99")
  364. port))))
  365. (match (pypi->guix-package "foo-99")
  366. (`(package
  367. (name "python-foo-99")
  368. (version "1.0.0")
  369. (source (origin
  370. (method url-fetch)
  371. (uri (pypi-uri "foo-99" version))
  372. (sha256
  373. (base32 ,(? string? hash)))))
  374. (properties (quote (("upstream-name" . "foo-99"))))
  375. (build-system pyproject-build-system)
  376. (propagated-inputs (list python-bar python-foo))
  377. (native-inputs (list python-pytest))
  378. (home-page "http://example.com")
  379. (synopsis "summary")
  380. (description "summary")
  381. (license license:lgpl2.0))
  382. (string=? default-sha256/base32 hash))
  383. (x
  384. (pk 'fail x #f))))))
  385. (test-equal "package-latest-release"
  386. (list '("foo-1.0.0.tar.gz")
  387. '("foo-1.0.0.tar.gz.asc")
  388. (list (upstream-input
  389. (name "bar")
  390. (downstream-name "python-bar")
  391. (type 'propagated))
  392. (upstream-input
  393. (name "foo")
  394. (downstream-name "python-foo")
  395. (type 'propagated))
  396. (upstream-input
  397. (name "pytest")
  398. (downstream-name "python-pytest")
  399. (type 'native))))
  400. (let ((tarball (pypi-tarball
  401. "foo-1.0.0"
  402. `(("src/bizarre.egg-info/requires.txt"
  403. ,test-requires.txt)))))
  404. (with-pypi `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
  405. ("/foo-1.0.0-py2.py3-none-any.whl" 404 "")
  406. ("/foo/json" 200 ,(lambda (port)
  407. (display (foo-json) port))))
  408. (define source
  409. (package-latest-release
  410. (dummy-package "python-foo"
  411. (version "0.1.2")
  412. (source (dummy-origin
  413. (method url-fetch)
  414. (uri (pypi-uri "foo" version))))
  415. (build-system python-build-system))
  416. (list %pypi-updater)))
  417. (list (map basename (upstream-source-urls source))
  418. (map basename (upstream-source-signature-urls source))
  419. (upstream-source-inputs source)))))
  420. (test-end "pypi")
  421. (delete-file-recursively sample-directory)
  422. ;; Local Variables:
  423. ;; eval: (put 'with-pypi 'scheme-indent-function 1)
  424. ;; End: