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.
 
 
 
 
 
 

168 lines
6.8 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  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 crate)
  19. #:use-module (guix base32)
  20. #:use-module (guix build-system cargo)
  21. #:use-module ((guix download) #:prefix download:)
  22. #:use-module (gcrypt hash)
  23. #:use-module (guix http-client)
  24. #:use-module (guix import json)
  25. #:use-module (guix import utils)
  26. #:use-module ((guix licenses) #:prefix license:)
  27. #:use-module (guix monads)
  28. #:use-module (guix packages)
  29. #:use-module (guix upstream)
  30. #:use-module (guix utils)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 pretty-print) ; recursive
  33. #:use-module (json)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-2)
  36. #:use-module (srfi srfi-26)
  37. #:export (crate->guix-package
  38. guix-package->crate-name
  39. %crate-updater))
  40. (define (crate-fetch crate-name callback)
  41. "Fetch the metadata for CRATE-NAME from crates.io and call the callback."
  42. (define (crates->inputs crates)
  43. (sort (map (cut assoc-ref <> "crate_id") crates) string-ci<?))
  44. (define (string->license string)
  45. (map spdx-string->license (string-split string #\/)))
  46. (define (crate-kind-predicate kind)
  47. (lambda (dep) (string=? (assoc-ref dep "kind") kind)))
  48. (and-let* ((crate-json (json-fetch-alist (string-append crate-url crate-name)))
  49. (crate (assoc-ref crate-json "crate"))
  50. (name (assoc-ref crate "name"))
  51. (version (assoc-ref crate "max_version"))
  52. (homepage (assoc-ref crate "homepage"))
  53. (repository (assoc-ref crate "repository"))
  54. (synopsis (assoc-ref crate "description"))
  55. (description (assoc-ref crate "description"))
  56. (license (or (and=> (assoc-ref crate "license")
  57. string->license)
  58. '())) ;missing license info
  59. (path (string-append "/" version "/dependencies"))
  60. (deps-json (json-fetch-alist (string-append crate-url name path)))
  61. (deps (assoc-ref deps-json "dependencies"))
  62. (input-crates (filter (crate-kind-predicate "normal") deps))
  63. (native-input-crates
  64. (filter (lambda (dep)
  65. (not ((crate-kind-predicate "normal") dep))) deps))
  66. (inputs (crates->inputs input-crates))
  67. (native-inputs (crates->inputs native-input-crates))
  68. (home-page (match homepage
  69. (() repository)
  70. (_ homepage))))
  71. (callback #:name name #:version version
  72. #:inputs inputs #:native-inputs native-inputs
  73. #:home-page home-page #:synopsis synopsis
  74. #:description description #:license license)))
  75. (define* (make-crate-sexp #:key name version inputs native-inputs
  76. home-page synopsis description license
  77. #:allow-other-keys)
  78. "Return the `package' s-expression for a rust package with the given NAME,
  79. VERSION, INPUTS, NATIVE-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
  80. (let* ((port (http-fetch (crate-uri name version)))
  81. (guix-name (crate-name->package-name name))
  82. (inputs (map crate-name->package-name inputs))
  83. (native-inputs (map crate-name->package-name native-inputs))
  84. (pkg `(package
  85. (name ,guix-name)
  86. (version ,version)
  87. (source (origin
  88. (method url-fetch)
  89. (uri (crate-uri ,name version))
  90. (file-name (string-append name "-" version ".tar.gz"))
  91. (sha256
  92. (base32
  93. ,(bytevector->nix-base32-string (port-sha256 port))))))
  94. (build-system cargo-build-system)
  95. ,@(maybe-native-inputs native-inputs "src")
  96. ,@(maybe-inputs inputs "src")
  97. (home-page ,(match home-page
  98. (() "")
  99. (_ home-page)))
  100. (synopsis ,synopsis)
  101. (description ,(beautify-description description))
  102. (license ,(match license
  103. (() #f)
  104. ((license) license)
  105. (_ `(list ,@license)))))))
  106. (close-port port)
  107. pkg))
  108. (define (crate->guix-package crate-name)
  109. "Fetch the metadata for CRATE-NAME from crates.io, and return the
  110. `package' s-expression corresponding to that package, or #f on failure."
  111. (crate-fetch crate-name make-crate-sexp))
  112. (define (guix-package->crate-name package)
  113. "Return the crate name of PACKAGE."
  114. (and-let* ((origin (package-source package))
  115. (uri (origin-uri origin))
  116. (crate-url? uri)
  117. (len (string-length crate-url))
  118. (path (xsubstring uri len))
  119. (parts (string-split path #\/)))
  120. (match parts
  121. ((name _ ...) name))))
  122. (define (crate-name->package-name name)
  123. (string-append "rust-" (string-join (string-split name #\_) "-")))
  124. ;;;
  125. ;;; Updater
  126. ;;;
  127. (define (crate-package? package)
  128. "Return true if PACKAGE is a Rust crate from crates.io."
  129. (let ((source-url (and=> (package-source package) origin-uri))
  130. (fetch-method (and=> (package-source package) origin-method)))
  131. (and (eq? fetch-method download:url-fetch)
  132. (match source-url
  133. ((? string?)
  134. (crate-url? source-url))
  135. ((source-url ...)
  136. (any crate-url? source-url))))))
  137. (define (latest-release package)
  138. "Return an <upstream-source> for the latest release of PACKAGE."
  139. (let* ((crate-name (guix-package->crate-name package))
  140. (callback (lambda* (#:key version #:allow-other-keys) version))
  141. (version (crate-fetch crate-name callback))
  142. (url (crate-uri crate-name version)))
  143. (upstream-source
  144. (package (package-name package))
  145. (version version)
  146. (urls (list url)))))
  147. (define %crate-updater
  148. (upstream-updater
  149. (name 'crates)
  150. (description "Updater for crates.io packages")
  151. (pred crate-package?)
  152. (latest latest-release)))