unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 5683369b7a68529895b6cee0633bc66508de2f72 11707 bytes (raw)
name: guix/import/crate.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016 David Craven <david@craven.ch>
;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2019 Martin Becze <mjbecze@riseup.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix import crate)
  #:use-module (guix base32)
  #:use-module (guix build-system cargo)
  #:use-module ((guix download) #:prefix download:)
  #:use-module (gcrypt hash)
  #:use-module (guix http-client)
  #:use-module (guix json)
  #:use-module (guix import json)
  #:use-module (guix import utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix monads)
  #:use-module (guix packages)
  #:use-module (guix upstream)
  #:use-module (guix utils)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (json)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-2)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-71)
  #:export (crate->guix-package
            guix-package->crate-name
            crate-recursive-import
            %crate-updater))

\f
;;;
;;; Interface to https://crates.io/api/v1.
;;;

;; Crates.  A crate is essentially a "package".  It can have several
;; "versions", each of which has its own set of dependencies, license,
;; etc.--see <crate-version> below.
(define-json-mapping <crate> make-crate crate?
  json->crate
  (name          crate-name)                      ;string
  (latest-version crate-latest-version "max_version") ;string
  (home-page     crate-home-page "homepage")      ;string | #nil
  (repository    crate-repository)                ;string
  (description   crate-description)               ;string
  (keywords      crate-keywords                   ;list of strings
                 "keywords" vector->list)
  (categories    crate-categories                 ;list of strings
                 "categories" vector->list)
  (versions      crate-versions "actual_versions" ;list of <crate-version>
                 (lambda (vector)
                   (map json->crate-version
                        (vector->list vector))))
  (links         crate-links))                    ;alist

;; Crate version.
(define-json-mapping <crate-version> make-crate-version crate-version?
  json->crate-version
  (id            crate-version-id)                ;integer
  (number        crate-version-number "num")      ;string
  (download-path crate-version-download-path "dl_path") ;string
  (readme-path   crate-version-readme-path "readme_path") ;string
  (license       crate-version-license "license") ;string
  (links         crate-version-links))            ;alist

;; Crate dependency.  Each dependency (each edge in the graph) is annotated as
;; being a "normal" dependency or a development dependency.  There also
;; information about the minimum required version, such as "^0.0.41".
(define-json-mapping <crate-dependency> make-crate-dependency
  crate-dependency?
  json->crate-dependency
  (id            crate-dependency-id "crate_id")  ;string
  (kind          crate-dependency-kind "kind"     ;'normal | 'dev | 'build
                 string->symbol)
  (requirement   crate-dependency-requirement "req")) ;string

(define (lookup-crate name)
  "Look up NAME on https://crates.io and return the corresopnding <crate>
record or #f if it was not found."
  (let ((json (json-fetch (string-append (%crate-base-url) "/api/v1/crates/"
                                         name))))
    (and=> (and json (assoc-ref json "crate"))
           (lambda (alist)
             ;; The "versions" field of ALIST is simply a list of version IDs
             ;; (integers).  Here, we squeeze in the actual version
             ;; dictionaries that are not part of ALIST but are just more
             ;; convenient handled this way.
             (let ((versions (or (assoc-ref json "versions") '#())))
               (json->crate `(,@alist
                              ("actual_versions" . ,versions))))))))

(define (crate-version-dependencies version)
  "Return the list of <crate-dependency> records of VERSION, a
<crate-version>."
  (let* ((path (assoc-ref (crate-version-links version) "dependencies"))
         (url  (string-append (%crate-base-url) path)))
    (match (assoc-ref (or (json-fetch url) '()) "dependencies")
      ((? vector? vector)
       (filter (lambda (dep)
                 (not (eq? (crate-dependency-kind dep) 'dev)))
               (map json->crate-dependency (vector->list vector))))
      (_
       '()))))

\f
;;;
;;; Converting crates to Guix packages.
;;;

(define (maybe-cargo-inputs package-names)
  (match (package-names->package-inputs package-names)
    (()
     '())
    ((package-inputs ...)
     `(#:cargo-inputs ,package-inputs))))

(define (maybe-cargo-development-inputs package-names)
  (match (package-names->package-inputs package-names)
    (()
     '())
    ((package-inputs ...)
     `(#:cargo-development-inputs ,package-inputs))))

(define (maybe-arguments arguments)
  (match arguments
    (()
     '())
    ((args ...)
     `((arguments (,'quasiquote ,args))))))

(define (make-crate-sexp crate version* dependencies)
  "Return the `package' s-expression for a rust package given <crate>,
 <crate-version> and a list of <crate-dependency>"
  (define normal-dependency?
    (match-lambda ((_ dep) (not (eq? (crate-dependency-kind dep) 'dev)))))

  (define (string->license string)
    (match (regexp-exec %dual-license-rx string)
      (#f (list (spdx-string->license string)))
      (m  (list (spdx-string->license (match:substring m 1))
                (spdx-string->license (match:substring m 2))))))

  (let* ((dep-crates dev-dep-crates (partition normal-dependency? dependencies))
         (cargo-inputs (sort (unzip1 dep-crates)
                             string-ci<?))
         (cargo-development-inputs
          (sort (unzip1 dev-dep-crates)
                string-ci<?))
         (name (crate-name crate))
         (version (crate-version-number version*))
         (home-page (or (crate-home-page crate)
                        (crate-repository crate)))
         (synopsis (crate-description crate))
         (description (crate-description crate))
         (license (and=> (crate-version-license version*)
                         string->license))
         (port (http-fetch (crate-uri name version)) )
         (guix-name (crate-name->package-name name))
         (pkg `(package
                 (name ,guix-name)
                 (version ,version)
                 (source (origin
                           (method url-fetch)
                           (uri (crate-uri ,name version))
                           (file-name (string-append name "-" version ".crate"))
                           (sha256
                            (base32
                             ,(bytevector->nix-base32-string (port-sha256 port))))))
                 (build-system cargo-build-system)
                 ,@(maybe-arguments (append `(#:skip-build? #t)
                                            (maybe-cargo-inputs cargo-inputs)
                                            (maybe-cargo-development-inputs
                                             cargo-development-inputs)))
                 (home-page ,(match home-page
                               (() "")
                               (_ home-page)))
                 (synopsis ,synopsis)
                 (description ,(beautify-description description))
                 (license ,(match license
                             (() #f)
                             ((license) license)
                             (_ `(list ,@license)))))))

    (close-port port)
    pkg))

(define %dual-license-rx
  ;; Dual licensing is represented by a string such as "MIT OR Apache-2.0".
  ;; This regexp matches that.
  (make-regexp "^(.*) OR (.*)$"))

(define (crate->crate-version crate version-number)
  "returns the <crate-version> for a given CRATE and VERSION-NUMBER"
  (find (lambda (version)
            (string=? (crate-version-number version)
                      version-number))
          (crate-versions crate)))

(define (crate->versions crate)
  "Returns a list of versions for a given CRATE"
  (map (lambda (version)
         (crate-version-number version))
       (crate-versions crate)))

(define* (crate->guix-package crate-name #:optional version)
  "Fetch the metadata for CRATE-NAME from crates.io, and return the
`package' s-expression corresponding to that package, or #f on failure.
When VERSION is specified, attempt to fetch that version; otherwise fetch the
latest version of CRATE-NAME."
  (define crate
    (lookup-crate crate-name))

  (define version-number
    (or version
        (crate-latest-version crate)))

  (define version*
    (crate->crate-version crate version-number))

  (define dependencies (map
                        (lambda (dep)
                          (list (crate-name->package-name
                           (crate-dependency-id dep)) dep))
                        (crate-version-dependencies version*)))
  (make-crate-sexp crate version* dependencies))

(define* (crate-recursive-import name #:optional version)
  (recursive-import-semver
   #:name name
   #:version version
   #:name->metadata lookup-crate
   #:metadata->package crate->crate-version
   #:metadata-versions crate->versions
   #:package-dependencies crate-version-dependencies
   #:dependency-name crate-dependency-id
   #:dependency-range crate-dependency-requirement
   #:guix-name crate-name->package-name
   #:make-sexp make-crate-sexp))

(define (guix-package->crate-name package)
  "Return the crate name of PACKAGE."
  (and-let* ((origin (package-source package))
             (uri (origin-uri origin))
             (crate-url? uri)
             (len (string-length crate-url))
             (path (xsubstring uri len))
             (parts (string-split path #\/)))
    (match parts
      ((name _ ...) name))))

(define (crate-name->package-name name)
  (string-append "rust-" (string-join (string-split name #\_) "-")))

\f
;;;
;;; Updater
;;;

(define (crate-package? package)
  "Return true if PACKAGE is a Rust crate from crates.io."
  (let ((source-url (and=> (package-source package) origin-uri))
        (fetch-method (and=> (package-source package) origin-method)))
    (and (eq? fetch-method download:url-fetch)
         (match source-url
           ((? string?)
            (crate-url? source-url))
           ((source-url ...)
            (any crate-url? source-url))))))

(define (latest-release package)
  "Return an <upstream-source> for the latest release of PACKAGE."
  (let* ((crate-name (guix-package->crate-name package))
         (crate      (lookup-crate crate-name))
         (version    (crate-latest-version crate))
         (url        (crate-uri crate-name version)))
    (upstream-source
     (package (package-name package))
     (version version)
     (urls (list url)))))

(define %crate-updater
  (upstream-updater
   (name 'crates)
   (description "Updater for crates.io packages")
   (pred crate-package?)
   (latest latest-release)))

debug log:

solving 5683369b7a ...
found 5683369b7a in https://yhetil.org/guix-patches/a734de722430cdd288cb0ed21dc8515fc89fdd3a.1576005195.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/bf327fb427eba14f6d4d8d324c5c75135f39e79b.1575656092.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/bf327fb427eba14f6d4d8d324c5c75135f39e79b.1575575779.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/42cb010759c8355943b9e2cb71a66b93@riseup.net/
found da92c43b8c in https://yhetil.org/guix-patches/583af30cbbd946af96c7f9cb856df9d1170287c7.1576005195.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/7cccd4231b9c1327d59ff39daf2aa7dd47a2e765.1575656092.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/7cccd4231b9c1327d59ff39daf2aa7dd47a2e765.1575575779.git.mjbecze@riseup.net/ ||
	https://yhetil.org/guix-patches/052524339786cd4c0db5fda81547239c8bee6003.1574897905.git.mjbecze@riseup.net/
found 8dc014d232 in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 8dc014d23234d8eb88a631f8e27fd4edf5a5b40c	guix/import/crate.scm

applying [1/2] https://yhetil.org/guix-patches/583af30cbbd946af96c7f9cb856df9d1170287c7.1576005195.git.mjbecze@riseup.net/
diff --git a/guix/import/crate.scm b/guix/import/crate.scm
index 8dc014d232..da92c43b8c 100644

Checking patch guix/import/crate.scm...
Applied patch guix/import/crate.scm cleanly.

skipping https://yhetil.org/guix-patches/7cccd4231b9c1327d59ff39daf2aa7dd47a2e765.1575656092.git.mjbecze@riseup.net/ for da92c43b8c
skipping https://yhetil.org/guix-patches/7cccd4231b9c1327d59ff39daf2aa7dd47a2e765.1575575779.git.mjbecze@riseup.net/ for da92c43b8c
skipping https://yhetil.org/guix-patches/052524339786cd4c0db5fda81547239c8bee6003.1574897905.git.mjbecze@riseup.net/ for da92c43b8c
index at:
100644 da92c43b8cedbaad430e1d3c6870772856814088	guix/import/crate.scm

applying [2/2] https://yhetil.org/guix-patches/a734de722430cdd288cb0ed21dc8515fc89fdd3a.1576005195.git.mjbecze@riseup.net/
diff --git a/guix/import/crate.scm b/guix/import/crate.scm
index da92c43b8c..5683369b7a 100644

Checking patch guix/import/crate.scm...
Applied patch guix/import/crate.scm cleanly.

skipping https://yhetil.org/guix-patches/bf327fb427eba14f6d4d8d324c5c75135f39e79b.1575656092.git.mjbecze@riseup.net/ for 5683369b7a
skipping https://yhetil.org/guix-patches/bf327fb427eba14f6d4d8d324c5c75135f39e79b.1575575779.git.mjbecze@riseup.net/ for 5683369b7a
skipping https://yhetil.org/guix-patches/42cb010759c8355943b9e2cb71a66b93@riseup.net/ for 5683369b7a
index at:
100644 5683369b7a68529895b6cee0633bc66508de2f72	guix/import/crate.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).