You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
6.2 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix import pypi)
  19. #:use-module (ice-9 binary-ports)
  20. #:use-module (ice-9 match)
  21. #:use-module (ice-9 pretty-print)
  22. #:use-module (ice-9 regex)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (json)
  26. #:use-module (web uri)
  27. #:use-module (guix utils)
  28. #:use-module (guix import utils)
  29. #:use-module (guix base32)
  30. #:use-module (guix hash)
  31. #:use-module (guix packages)
  32. #:use-module (guix licenses)
  33. #:use-module (guix build-system python)
  34. #:use-module ((guix build download) #:prefix build:)
  35. #:use-module (gnu packages python)
  36. #:export (pypi->guix-package))
  37. (define (hash-table->alist table)
  38. "Return an alist represenation of TABLE."
  39. (map (match-lambda
  40. ((key . (lst ...))
  41. (cons key
  42. (map (lambda (x)
  43. (if (hash-table? x)
  44. (hash-table->alist x)
  45. x))
  46. lst)))
  47. ((key . (? hash-table? table))
  48. (cons key (hash-table->alist table)))
  49. (pair pair))
  50. (hash-map->list cons table)))
  51. (define (flatten lst)
  52. "Return a list that recursively concatenates all sub-lists of LIST."
  53. (fold-right
  54. (match-lambda*
  55. (((sub-list ...) memo)
  56. (append (flatten sub-list) memo))
  57. ((elem memo)
  58. (cons elem memo)))
  59. '() lst))
  60. (define (join lst delimiter)
  61. "Return a list that contains the elements of LST, each separated by
  62. DELIMETER."
  63. (match lst
  64. (() '())
  65. ((elem)
  66. (list elem))
  67. ((elem . rest)
  68. (cons* elem delimiter (join rest delimiter)))))
  69. (define (assoc-ref* alist key . rest)
  70. "Return the value for KEY from ALIST. For each additional key specified,
  71. recursively apply the procedure to the sub-list."
  72. (if (null? rest)
  73. (assoc-ref alist key)
  74. (apply assoc-ref* (assoc-ref alist key) rest)))
  75. (define string->license
  76. (match-lambda
  77. ("GNU LGPL" lgpl2.0)
  78. ("GPL" gpl3)
  79. ((or "BSD" "BSD License") bsd-3)
  80. ((or "MIT" "MIT license" "Expat license") expat)
  81. ("Public domain" public-domain)
  82. (_ #f)))
  83. (define (url-fetch url file-name)
  84. "Save the contents of URL to FILE-NAME."
  85. (parameterize ((current-output-port (current-error-port)))
  86. (build:url-fetch url file-name)))
  87. (define (json-fetch url)
  88. "Return an alist representation of the JSON resource URL."
  89. (call-with-temporary-output-file
  90. (lambda (temp port)
  91. (and (url-fetch url temp)
  92. (hash-table->alist
  93. (call-with-input-file temp json->scm))))))
  94. (define (pypi-fetch name)
  95. "Return an alist representation of the PyPI metadata for the package NAME."
  96. (json-fetch (string-append "https://pypi.python.org/pypi/" name "/json")))
  97. (define (latest-source-release pypi-package)
  98. "Return the latest source release for PYPI-PACKAGE."
  99. (let ((releases (assoc-ref* pypi-package "releases"
  100. (assoc-ref* pypi-package "info" "version"))))
  101. (or (find (lambda (release)
  102. (string=? "sdist" (assoc-ref release "packagetype")))
  103. releases)
  104. (error "No source release found for pypi package: "
  105. (assoc-ref* pypi-package "info" "name")
  106. (assoc-ref* pypi-package "info" "version")))))
  107. (define (snake-case str)
  108. "Return a downcased version of the string STR where dashes are replaced with
  109. underscores."
  110. (string-join (string-split (string-downcase str) #\_) "-"))
  111. (define (guix-hash-url url)
  112. "Download the resource at URL and return the hash in nix-base32 format."
  113. (call-with-temporary-output-file
  114. (lambda (temp port)
  115. (and (url-fetch url temp)
  116. (bytevector->nix-base32-string
  117. (call-with-input-file temp port-sha256))))))
  118. (define (make-pypi-sexp name version source-url home-page synopsis
  119. description license)
  120. "Return the `package' s-expression for a python package with the given NAME,
  121. VERSION, SOURCE-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
  122. `(package
  123. (name ,(string-append "python-" (snake-case name)))
  124. (version ,version)
  125. (source (origin
  126. (method url-fetch)
  127. (uri (string-append ,@(factorize-uri source-url version)))
  128. (sha256
  129. (base32
  130. ,(guix-hash-url source-url)))))
  131. (build-system python-build-system)
  132. (inputs
  133. `(("python-setuptools" ,python-setuptools)))
  134. (home-page ,home-page)
  135. (synopsis ,synopsis)
  136. (description ,description)
  137. (license ,(assoc-ref `((,lgpl2.0 . lgpl2.0)
  138. (,gpl3 . gpl3)
  139. (,bsd-3 . bsd-3)
  140. (,expat . expat)
  141. (,public-domain . public-domain))
  142. license))))
  143. (define (pypi->guix-package package-name)
  144. "Fetch the metadata for PACKAGE-NAME from pypi.python.org, and return the
  145. `package' s-expression corresponding to that package."
  146. (let ((package (pypi-fetch package-name)))
  147. (let ((name (assoc-ref* package "info" "name"))
  148. (version (assoc-ref* package "info" "version"))
  149. (release (assoc-ref (latest-source-release package) "url"))
  150. (synopsis (assoc-ref* package "info" "summary"))
  151. (description (assoc-ref* package "info" "summary"))
  152. (home-page (assoc-ref* package "info" "home_page"))
  153. (license (string->license (assoc-ref* package "info" "license"))))
  154. (make-pypi-sexp name version release home-page synopsis
  155. description license))))