pypi.scm 18 KB

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