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.

193 lines
7.5 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  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 opam)
  19. #:use-module (ice-9 match)
  20. #:use-module (ice-9 vlist)
  21. #:use-module ((ice-9 rdelim) #:select (read-line))
  22. #:use-module (srfi srfi-1)
  23. #:use-module (web uri)
  24. #:use-module (guix http-client)
  25. #:use-module (guix utils)
  26. #:use-module (guix import utils)
  27. #:use-module ((guix licenses) #:prefix license:)
  28. #:export (opam->guix-package))
  29. (define (opam-urls)
  30. "Fetch the urls.txt file from the opam repository and returns the list of
  31. URLs it contains."
  32. (let ((port (http-fetch/cached (string->uri "https://opam.ocaml.org/urls.txt"))))
  33. (let loop ((result '()))
  34. (let ((line (read-line port)))
  35. (if (eof-object? line)
  36. (begin
  37. (close port)
  38. result)
  39. (loop (cons line result)))))))
  40. (define (vhash-ref hashtable key default)
  41. (match (vhash-assoc key hashtable)
  42. (#f default)
  43. ((_ . x) x)))
  44. (define (hashtable-update hashtable line)
  45. "Parse @var{line} to get the name and version of the package and adds them
  46. to the hashtable."
  47. (let* ((line (string-split line #\ )))
  48. (match line
  49. ((url foo ...)
  50. (if (equal? url "repo")
  51. hashtable
  52. (match (string-split url #\/)
  53. ((type name1 versionstr foo ...)
  54. (if (equal? type "packages")
  55. (match (string-split versionstr #\.)
  56. ((name2 versions ...)
  57. (let ((version (string-join versions ".")))
  58. (if (equal? name1 name2)
  59. (let ((curr (vhash-ref hashtable name1 '())))
  60. (vhash-cons name1 (cons version curr) hashtable))
  61. hashtable)))
  62. (_ hashtable))
  63. hashtable))
  64. (_ hashtable))))
  65. (_ hashtable))))
  66. (define (urls->hashtable urls)
  67. "Transform urls.txt in a hashtable whose keys are package names and values
  68. the list of available versions."
  69. (let ((hashtable vlist-null))
  70. (let loop ((urls urls) (hashtable hashtable))
  71. (match urls
  72. (() hashtable)
  73. ((url rest ...) (loop rest (hashtable-update hashtable url)))))))
  74. (define (latest-version versions)
  75. "Find the most recent version from a list of versions."
  76. (match versions
  77. ((first rest ...)
  78. (let loop ((versions rest) (m first))
  79. (match versions
  80. (() m)
  81. ((first rest ...)
  82. (loop rest (if (version>? m first) m first))))))))
  83. (define (fetch-package-url uri)
  84. "Fetch and parse the url file. Return the URL the package can be downloaded
  85. from."
  86. (let ((port (http-fetch uri)))
  87. (let loop ((result #f))
  88. (let ((line (read-line port)))
  89. (if (eof-object? line)
  90. (begin
  91. (close port)
  92. result)
  93. (let* ((line (string-split line #\ )))
  94. (match line
  95. ((key value rest ...)
  96. (if (member key '("archive:" "http:"))
  97. (loop (string-trim-both value #\"))
  98. (loop result))))))))))
  99. (define (fetch-package-metadata uri)
  100. "Fetch and parse the opam file. Return an association list containing the
  101. homepage, the license and the list of inputs."
  102. (let ((port (http-fetch uri)))
  103. (let loop ((result '()) (dependencies? #f))
  104. (let ((line (read-line port)))
  105. (if (eof-object? line)
  106. (begin
  107. (close port)
  108. result)
  109. (let* ((line (string-split line #\ )))
  110. (match line
  111. ((key value ...)
  112. (let ((dependencies?
  113. (if dependencies?
  114. (not (equal? key "]"))
  115. (equal? key "depends:")))
  116. (val (string-trim-both (string-join value "") #\")))
  117. (cond
  118. ((equal? key "homepage:")
  119. (loop (cons `("homepage" . ,val) result) dependencies?))
  120. ((equal? key "license:")
  121. (loop (cons `("license" . ,val) result) dependencies?))
  122. ((and dependencies? (not (equal? val "[")))
  123. (match (string-split val #\{)
  124. ((val rest ...)
  125. (let ((curr (assoc-ref result "inputs"))
  126. (new (string-trim-both
  127. val (list->char-set '(#\] #\[ #\")))))
  128. (loop (cons `("inputs" . ,(cons new (if curr curr '()))) result)
  129. (if (string-contains val "]") #f dependencies?))))))
  130. (else (loop result dependencies?))))))))))))
  131. (define (string->license str)
  132. (cond
  133. ((equal? str "MIT") '(license:expat))
  134. ((equal? str "GPL2") '(license:gpl2))
  135. ((equal? str "LGPLv2") '(license:lgpl2))
  136. (else `())))
  137. (define (ocaml-name->guix-name name)
  138. (cond
  139. ((equal? name "ocamlfind") "ocaml-findlib")
  140. ((string-prefix? "ocaml" name) name)
  141. ((string-prefix? "conf-" name) (substring name 5))
  142. (else (string-append "ocaml-" name))))
  143. (define (dependencies->inputs dependencies)
  144. "Transform the list of dependencies in a list of inputs."
  145. (if (not dependencies)
  146. '()
  147. (map (lambda (input)
  148. (list input (list 'unquote (string->symbol input))))
  149. (map ocaml-name->guix-name dependencies))))
  150. (define (opam->guix-package name)
  151. (let* ((hashtable (urls->hashtable (opam-urls)))
  152. (versions (vhash-ref hashtable name #f)))
  153. (unless (eq? versions #f)
  154. (let* ((version (latest-version versions))
  155. (package-url (string-append "https://opam.ocaml.org/packages/" name
  156. "/" name "." version "/"))
  157. (url-url (string-append package-url "url"))
  158. (opam-url (string-append package-url "opam"))
  159. (source-url (fetch-package-url url-url))
  160. (metadata (fetch-package-metadata opam-url))
  161. (dependencies (assoc-ref metadata "inputs"))
  162. (inputs (dependencies->inputs dependencies)))
  163. (call-with-temporary-output-file
  164. (lambda (temp port)
  165. (and (url-fetch source-url temp)
  166. `(package
  167. (name ,(ocaml-name->guix-name name))
  168. (version ,version)
  169. (source
  170. (origin
  171. (method url-fetch)
  172. (uri ,source-url)
  173. (sha256 (base32 ,(guix-hash-url temp)))))
  174. (build-system ocaml-build-system)
  175. ,@(if (null? inputs)
  176. '()
  177. `((inputs ,(list 'quasiquote inputs))))
  178. (home-page ,(assoc-ref metadata "homepage"))
  179. (synopsis "")
  180. (description "")
  181. (license ,@(string->license (assoc-ref metadata "license")))))))))))