unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 61009f356518e8f8e8ab0ca5f42e34f33a3622f6 11482 bytes (raw)
name: guix/import/go.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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Katherine Cox-Buday <cox.katherine.e@gmail.com>
;;;
;;; 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 go)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 regex)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (guix json)
  #:use-module ((guix download) #:prefix download:)
  #:use-module (guix import utils)
  #:use-module (guix import json)
  #:use-module (guix packages)
  #:use-module (guix upstream)
  #:use-module (guix utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix base16)
  #:use-module (guix base32)
  #:use-module (guix build download)
  #:use-module (web uri)

  #:export (go-module->guix-package
            go-module-recursive-import
            infer-module-root))

(define (escape-capital-letters s)
  "To avoid ambiguity when serving from case-insensitive file systems, the
$module and $version elements are case-encoded by replacing every uppercase
letter with an exclamation mark followed by the corresponding lower-case
letter."
  (let ((escaped-string (string)))
    (string-for-each-index
     (lambda (i)
       (let ((c (string-ref s i)))
         (set! escaped-string
           (string-concatenate
            (list escaped-string
                  (if (char-upper-case? c) "!" "")
                  (string (char-downcase c)))))))
     s)
    escaped-string))

(define (fetch-latest-version goproxy-url module-path)
  "Fetches the version number of the latest version for MODULE-PATH from the
given GOPROXY-URL server."
  (assoc-ref
   (json-fetch (format #f "~a/~a/@latest" goproxy-url
                       (escape-capital-letters module-path)))
   "Version"))

(define (fetch-go.mod goproxy-url module-path version file)
  "Fetches go.mod from the given GOPROXY-URL server for the given MODULE-PATH
and VERSION."
  (url-fetch (format #f "~a/~a/@v/~a.mod" goproxy-url
                     (escape-capital-letters module-path)
                     (escape-capital-letters version))
             file
             #:print-build-trace? #f))

(define (parse-go.mod go.mod-path)
  "Parses a go.mod file and returns an alist of module path to version."
  (with-input-from-file go.mod-path
    (lambda ()
      (let ((in-require? #f)
            (requirements (list)))
        (do ((line (read-line) (read-line)))
            ((eof-object? line))
          (set! line (string-trim line))
          ;; The parser is either entering, within, exiting, or after the
          ;; require block. The Go toolchain is trustworthy so edge-cases like
          ;; double-entry, etc. need not complect the parser.
          (cond
           ((string=? line "require (")
            (set! in-require? #t))
           ((and in-require? (string=? line ")"))
            (set! in-require? #f))
           (in-require?
            (let* ((requirement (string-split line #\space))
                   ;; Modules should be unquoted
                   (module-path (string-delete #\" (car requirement)))
                   (version (list-ref requirement 1)))
              (set! requirements (acons module-path version requirements))))
           ((string-prefix? "replace" line)
            (let* ((requirement (string-split line #\space))
                   (module-path (list-ref requirement 1))
                   (new-module-path (list-ref requirement 3))
                   (version (list-ref requirement 4)))
              (set! requirements (assoc-remove! requirements module-path))
              (set! requirements (acons new-module-path version requirements))))))
        requirements))))

(define (module-path-without-major-version module-path)
  "Go modules can be appended with a major version indicator,
e.g. /v3. Sometimes it is desirable to work with the root module path. For
instance, for a module path github.com/foo/bar/v3 this function returns
github.com/foo/bar."
  (let ((m (string-match "(.*)\\/v[0-9]+$" module-path)))
    (if m
        (match:substring m 1)
        module-path)))

(define (infer-module-root module-path)
  "Go modules can be defined at any level of a repository's tree, but querying
for the meta tag usually can only be done at the webpage at the root of the
repository. Therefore, it is sometimes necessary to try and derive a module's
root path from its path. For a set of well-known forges, the pattern of what
consists of a module's root page is known before hand."
  ;; See the following URL for the official Go equivalent:
  ;; https://github.com/golang/go/blob/846dce9d05f19a1f53465e62a304dea21b99f910/src/cmd/go/internal/vcs/vcs.go#L1026-L1087
  (define-record-type <scs>
    (make-scs url-prefix root-regex type)
    scs?
    (url-prefix	scs-url-prefix)
    (root-regex scs-root-regex)
    (type	scs-type))
  (let* ((known-scs
          (list
           (make-scs
            "github.com"
            "^(github\\.com/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(/[A-Za-z0-9_.\\-]+)*$"
            'git)
           (make-scs
            "bitbucket.org"
            "^(bitbucket\\.org/([A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+))(/[A-Za-z0-9_.\\-]+)*$`"
            'unknown)
           (make-scs
            "hub.jazz.net/git/"
            "^(hub\\.jazz\\.net/git/[a-z0-9]+/[A-Za-z0-9_.\\-]+)(/[A-Za-z0-9_.\\-]+)*$"
            'git)
           (make-scs
            "git.apache.org"
            "^(git\\.apache\\.org/[a-z0-9_.\\-]+\\.git)(/[A-Za-z0-9_.\\-]+)*$"
            'git)
           (make-scs
            "git.openstack.org"
            "^(git\\.openstack\\.org/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(\\.git)?(/[A-Za-z0-9_.\\-]+)*$"
            'git)))
         (scs (find (lambda (scs) (string-prefix? (scs-url-prefix scs) module-path))
                    known-scs)))
    (if scs
        (match:substring (string-match (scs-root-regex scs) module-path) 1)
        module-path)))

(define (to-guix-package-name module-path)
  "Converts a module's path to the canonical Guix format for Go packages."
  (string-downcase
   (string-append "go-"
                  (string-replace-substring
                   (string-replace-substring
                    ;; Guix has its own field for version
                    (module-path-without-major-version module-path)
                    "." "-")
                   "/" "-"))))

(define (fetch-module-meta-data module-path)
  "Fetches module meta-data from a module's landing page. This is necessary
because goproxy servers don't currently provide all the information needed to
build a package."
  (let* ((port (http-fetch (string->uri (format #f "https://~a?go-get=1" module-path))))
         (module-metadata #f)
         (meta-tag-prefix "<meta name=\"go-import\" content=\"")
         (meta-tag-prefix-length (string-length meta-tag-prefix)))
    (do ((line (read-line port) (read-line port)))
        ((or (eof-object? line)
             module-metadata))
      (let ((meta-tag-index (string-contains line meta-tag-prefix)))
        (when meta-tag-index
          (let* ((start (+ meta-tag-index meta-tag-prefix-length))
                 (end (string-index line #\" start)))
            (set! module-metadata
              (string-split (substring/shared line start end) #\space))))))
    (close-port port)
    module-metadata))

(define (module-meta-data-scs meta-data)
  "Return the source control system specified by a module's meta-data."
  (string->symbol (list-ref meta-data 1)))

(define (module-meta-data-repo-url meta-data goproxy-url)
  "Return the URL where the fetcher which will be used can download the source
control."
  (if (member (module-meta-data-scs meta-data) '(fossil mod))
      goproxy-url
      (list-ref meta-data 2)))

(define (source-uri scs-type scs-repo-url file)
  "Generate the `origin' block of a package depending on what type of source
control system is being used."
  (case scs-type
    ((git)
     `(origin
        (method git-fetch)
        (uri (git-reference
              (url ,scs-repo-url)
              (commit (string-append "v" version))))
        (file-name (git-file-name name version))
        (sha256
         (base32
          ,(guix-hash-url file)))))
    ((hg)
     `(origin
        (method hg-fetch)
        (uri (hg-reference
              (url ,scs-repo-url)
              (changeset ,version)))
        (file-name (format #f "~a-~a-checkout" name version))))
    ((svn)
     `(origin
        (method svn-fetch)
        (uri (svn-reference
              (url ,scs-repo-url)
              (revision (string->number version))
              (recursive? #f)))
        (file-name (format #f "~a-~a-checkout" name version))
        (sha256
         (base32
          ,(guix-hash-url file)))))
    (else
     (raise-exception (format #f "unsupported scs type: ~a" scs-type)))))

(define* (go-module->guix-package module-path #:key (goproxy-url "https://proxy.golang.org"))
  (call-with-temporary-output-file
   (lambda (temp port)
     (let* ((latest-version (fetch-latest-version goproxy-url module-path))
            (go.mod-path (fetch-go.mod goproxy-url module-path latest-version
                                       temp))
            (dependencies (map car (parse-go.mod temp)))
            (guix-name (to-guix-package-name module-path))
            (root-module-path (infer-module-root module-path))
            ;; SCS type and URL are not included in goproxy information. For
            ;; this we need to fetch it from the official module page.
            (meta-data (fetch-module-meta-data root-module-path))
            (scs-type (module-meta-data-scs meta-data))
            (scs-repo-url (module-meta-data-repo-url meta-data goproxy-url)))
       (values
        `(package
           (name ,guix-name)
           ;; Elide the "v" prefix Go uses
           (version ,(string-trim latest-version #\v))
           (source
            ,(source-uri scs-type scs-repo-url temp))
           (build-system go-build-system)
           ,@(maybe-inputs (map to-guix-package-name dependencies))
           ;; TODO(katco): It would be nice to make an effort to fetch this
           ;; from known forges, e.g. GitHub
           (home-page ,(format #f "https://~a" root-module-path))
           (synopsis "A Go package")
           (description ,(format #f "~a is a Go package." guix-name))
           (license #f))
        dependencies)))))

(define* (go-module-recursive-import package-name
                                     #:key (goproxy-url "https://proxy.golang.org"))
  (recursive-import package-name #f
                    #:repo->guix-package
                    (lambda (name _)
                      (go-module->guix-package name
                                               #:goproxy-url goproxy-url))
                    #:guix-name to-guix-package-name))

debug log:

solving 61009f3565 ...
found 61009f3565 in https://yhetil.org/guix-patches/87sga5kpdp.fsf@gmail.com/

applying [1/1] https://yhetil.org/guix-patches/87sga5kpdp.fsf@gmail.com/
diff --git a/guix/import/go.scm b/guix/import/go.scm
new file mode 100644
index 0000000000..61009f3565

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

index at:
100644 61009f356518e8f8e8ab0ca5f42e34f33a3622f6	guix/import/go.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).