pypi.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-pypi)
  21. #:use-module (guix import pypi)
  22. #:use-module (guix base32)
  23. #:use-module (guix memoization)
  24. #:use-module (gcrypt hash)
  25. #:use-module (guix tests)
  26. #:use-module (guix build-system python)
  27. #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
  28. #:use-module (srfi srfi-64)
  29. #:use-module (ice-9 match))
  30. (define test-json
  31. "{
  32. \"info\": {
  33. \"version\": \"1.0.0\",
  34. \"name\": \"foo\",
  35. \"license\": \"GNU LGPL\",
  36. \"summary\": \"summary\",
  37. \"home_page\": \"http://example.com\",
  38. \"classifiers\": [],
  39. \"download_url\": \"\"
  40. },
  41. \"urls\": [],
  42. \"releases\": {
  43. \"1.0.0\": [
  44. {
  45. \"url\": \"https://example.com/foo-1.0.0.egg\",
  46. \"packagetype\": \"bdist_egg\"
  47. }, {
  48. \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
  49. \"packagetype\": \"sdist\"
  50. }, {
  51. \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
  52. \"packagetype\": \"bdist_wheel\"
  53. }
  54. ]
  55. }
  56. }")
  57. (define test-source-hash
  58. "")
  59. (define test-specifications
  60. '("Fizzy [foo, bar]"
  61. "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
  62. "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
  63. "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
  64. "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
  65. sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
  66. (define test-requires.txt "\
  67. # A comment
  68. # A comment after a space
  69. foo ~= 3
  70. bar != 2
  71. [test]
  72. pytest (>=2.5.0)
  73. ")
  74. ;; Beaker contains only optional dependencies.
  75. (define test-requires.txt-beaker "\
  76. [crypto]
  77. pycryptopp>=0.5.12
  78. [cryptography]
  79. cryptography
  80. [testsuite]
  81. Mock
  82. coverage
  83. ")
  84. (define test-metadata "\
  85. Classifier: Programming Language :: Python :: 3.7
  86. Requires-Dist: baz ~= 3
  87. Requires-Dist: bar != 2
  88. Provides-Extra: test
  89. Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
  90. ")
  91. (define test-metadata-with-extras "
  92. Classifier: Programming Language :: Python :: 3.7
  93. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  94. Requires-Dist: wrapt (<2,>=1)
  95. Requires-Dist: bar
  96. Provides-Extra: dev
  97. Requires-Dist: tox ; extra == 'dev'
  98. Requires-Dist: bumpversion (<1) ; extra == 'dev'
  99. ")
  100. ;;; Provides-Extra can appear before Requires-Dist.
  101. (define test-metadata-with-extras-jedi "\
  102. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  103. Provides-Extra: testing
  104. Requires-Dist: parso (>=0.3.0)
  105. Provides-Extra: testing
  106. Requires-Dist: pytest (>=3.1.0); extra == 'testing'
  107. ")
  108. (test-begin "pypi")
  109. (test-equal "guix-package->pypi-name, old URL style"
  110. "psutil"
  111. (guix-package->pypi-name
  112. (dummy-package "foo"
  113. (source (dummy-origin
  114. (uri
  115. "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
  116. (test-equal "guix-package->pypi-name, new URL style"
  117. "certbot"
  118. (guix-package->pypi-name
  119. (dummy-package "foo"
  120. (source (dummy-origin
  121. (uri
  122. "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
  123. (test-equal "guix-package->pypi-name, several URLs"
  124. "cram"
  125. (guix-package->pypi-name
  126. (dummy-package "foo"
  127. (source
  128. (dummy-origin
  129. (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
  130. (pypi-uri "cram" "0.7"))))))))
  131. (test-equal "specification->requirement-name"
  132. '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
  133. (map specification->requirement-name test-specifications))
  134. (test-equal "parse-requires.txt"
  135. (list '("foo" "bar") '("pytest"))
  136. (mock ((ice-9 ports) call-with-input-file
  137. call-with-input-string)
  138. (parse-requires.txt test-requires.txt)))
  139. (test-equal "parse-requires.txt - Beaker"
  140. (list '() '("Mock" "coverage"))
  141. (mock ((ice-9 ports) call-with-input-file
  142. call-with-input-string)
  143. (parse-requires.txt test-requires.txt-beaker)))
  144. (test-equal "parse-wheel-metadata, with extras"
  145. (list '("wrapt" "bar") '("tox" "bumpversion"))
  146. (mock ((ice-9 ports) call-with-input-file
  147. call-with-input-string)
  148. (parse-wheel-metadata test-metadata-with-extras)))
  149. (test-equal "parse-wheel-metadata, with extras - Jedi"
  150. (list '("parso") '("pytest"))
  151. (mock ((ice-9 ports) call-with-input-file
  152. call-with-input-string)
  153. (parse-wheel-metadata test-metadata-with-extras-jedi)))
  154. (test-assert "pypi->guix-package, no wheel"
  155. ;; Replace network resources with sample data.
  156. (mock ((guix import utils) url-fetch
  157. (lambda (url file-name)
  158. (match url
  159. ("https://example.com/foo-1.0.0.tar.gz"
  160. (begin
  161. ;; Unusual requires.txt location should still be found.
  162. (mkdir-p "foo-1.0.0/src/bizarre.egg-info")
  163. (with-output-to-file "foo-1.0.0/src/bizarre.egg-info/requires.txt"
  164. (lambda ()
  165. (display test-requires.txt)))
  166. (parameterize ((current-output-port (%make-void-port "rw+")))
  167. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  168. (delete-file-recursively "foo-1.0.0")
  169. (set! test-source-hash
  170. (call-with-input-file file-name port-sha256))))
  171. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  172. (_ (error "Unexpected URL: " url)))))
  173. (mock ((guix http-client) http-fetch
  174. (lambda (url . rest)
  175. (match url
  176. ("https://pypi.org/pypi/foo/json"
  177. (values (open-input-string test-json)
  178. (string-length test-json)))
  179. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  180. (_ (error "Unexpected URL: " url)))))
  181. (match (pypi->guix-package "foo")
  182. (('package
  183. ('name "python-foo")
  184. ('version "1.0.0")
  185. ('source ('origin
  186. ('method 'url-fetch)
  187. ('uri ('pypi-uri "foo" 'version))
  188. ('sha256
  189. ('base32
  190. (? string? hash)))))
  191. ('build-system 'python-build-system)
  192. ('propagated-inputs
  193. ('quasiquote
  194. (("python-bar" ('unquote 'python-bar))
  195. ("python-foo" ('unquote 'python-foo)))))
  196. ('native-inputs
  197. ('quasiquote
  198. (("python-pytest" ('unquote 'python-pytest)))))
  199. ('home-page "http://example.com")
  200. ('synopsis "summary")
  201. ('description "summary")
  202. ('license 'license:lgpl2.0))
  203. (string=? (bytevector->nix-base32-string
  204. test-source-hash)
  205. hash))
  206. (x
  207. (pk 'fail x #f))))))
  208. (test-skip (if (which "zip") 0 1))
  209. (test-assert "pypi->guix-package, wheels"
  210. ;; Replace network resources with sample data.
  211. (mock ((guix import utils) url-fetch
  212. (lambda (url file-name)
  213. (match url
  214. ("https://example.com/foo-1.0.0.tar.gz"
  215. (begin
  216. (mkdir-p "foo-1.0.0/foo.egg-info/")
  217. (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
  218. (lambda ()
  219. (display "wrong data to make sure we're testing wheels ")))
  220. (parameterize ((current-output-port (%make-void-port "rw+")))
  221. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  222. (delete-file-recursively "foo-1.0.0")
  223. (set! test-source-hash
  224. (call-with-input-file file-name port-sha256))))
  225. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
  226. (begin
  227. (mkdir "foo-1.0.0.dist-info")
  228. (with-output-to-file "foo-1.0.0.dist-info/METADATA"
  229. (lambda ()
  230. (display test-metadata)))
  231. (let ((zip-file (string-append file-name ".zip")))
  232. ;; zip always adds a "zip" extension to the file it creates,
  233. ;; so we need to rename it.
  234. (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
  235. (rename-file zip-file file-name))
  236. (delete-file-recursively "foo-1.0.0.dist-info")))
  237. (_ (error "Unexpected URL: " url)))))
  238. (mock ((guix http-client) http-fetch
  239. (lambda (url . rest)
  240. (match url
  241. ("https://pypi.org/pypi/foo/json"
  242. (values (open-input-string test-json)
  243. (string-length test-json)))
  244. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  245. (_ (error "Unexpected URL: " url)))))
  246. ;; Not clearing the memoization cache here would mean returning the value
  247. ;; computed in the previous test.
  248. (invalidate-memoization! pypi->guix-package)
  249. (match (pypi->guix-package "foo")
  250. (('package
  251. ('name "python-foo")
  252. ('version "1.0.0")
  253. ('source ('origin
  254. ('method 'url-fetch)
  255. ('uri ('pypi-uri "foo" 'version))
  256. ('sha256
  257. ('base32
  258. (? string? hash)))))
  259. ('build-system 'python-build-system)
  260. ('propagated-inputs
  261. ('quasiquote
  262. (("python-bar" ('unquote 'python-bar))
  263. ("python-baz" ('unquote 'python-baz)))))
  264. ('native-inputs
  265. ('quasiquote
  266. (("python-pytest" ('unquote 'python-pytest)))))
  267. ('home-page "http://example.com")
  268. ('synopsis "summary")
  269. ('description "summary")
  270. ('license 'license:lgpl2.0))
  271. (string=? (bytevector->nix-base32-string
  272. test-source-hash)
  273. hash))
  274. (x
  275. (pk 'fail x #f))))))
  276. (test-assert "pypi->guix-package, no usable requirement file."
  277. ;; Replace network resources with sample data.
  278. (mock ((guix import utils) url-fetch
  279. (lambda (url file-name)
  280. (match url
  281. ("https://example.com/foo-1.0.0.tar.gz"
  282. (mkdir-p "foo-1.0.0/foo.egg-info/")
  283. (parameterize ((current-output-port (%make-void-port "rw+")))
  284. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  285. (delete-file-recursively "foo-1.0.0")
  286. (set! test-source-hash
  287. (call-with-input-file file-name port-sha256)))
  288. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  289. (_ (error "Unexpected URL: " url)))))
  290. (mock ((guix http-client) http-fetch
  291. (lambda (url . rest)
  292. (match url
  293. ("https://pypi.org/pypi/foo/json"
  294. (values (open-input-string test-json)
  295. (string-length test-json)))
  296. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  297. (_ (error "Unexpected URL: " url)))))
  298. ;; Not clearing the memoization cache here would mean returning the value
  299. ;; computed in the previous test.
  300. (invalidate-memoization! pypi->guix-package)
  301. (match (pypi->guix-package "foo")
  302. (('package
  303. ('name "python-foo")
  304. ('version "1.0.0")
  305. ('source ('origin
  306. ('method 'url-fetch)
  307. ('uri ('pypi-uri "foo" 'version))
  308. ('sha256
  309. ('base32
  310. (? string? hash)))))
  311. ('build-system 'python-build-system)
  312. ('home-page "http://example.com")
  313. ('synopsis "summary")
  314. ('description "summary")
  315. ('license 'license:lgpl2.0))
  316. (string=? (bytevector->nix-base32-string
  317. test-source-hash)
  318. hash))
  319. (x
  320. (pk 'fail x #f))))))
  321. (test-end "pypi")