unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob cd2201858901f5b826bd7aa9bb606de7b84b74fa 8341 bytes (raw)
name: guix/import/melpa.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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
;;; Copyright © 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
;;; Copyright © 2019 Carlo Zancanaro <carlo@zancanaro.id.au>
;;;
;;; 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 melpa)
  #:use-module (gcrypt hash)
  #:use-module (guix base32)
  #:use-module (guix git)
  #:use-module (guix http-client)
  #:use-module (guix import utils)
  #:use-module (guix serialization)
  #:use-module (guix store)
  #:use-module (ice-9 control)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-26)
  #:use-module (web uri)
  #:export (melpa->guix-package))

(define emacs-standard-library?
  (let ((libs '("emacs" "cl-lib")))
    (lambda (lib)
      "Return true if LIB is part of Emacs itself.  The check is not
exhaustive and only attempts to recognize a subset of packages which in the
past were distributed separately from Emacs."
      (member lib libs))))

(define* (download-git-repository url ref)
  (with-store store
    (latest-repository-commit store url #:ref ref)))

(define (package-name->recipe-url package-name)
  (string-append "https://raw.githubusercontent.com/melpa/melpa/master/recipes/"
                 package-name))

(define (package-name->recipe package-name)
  (define (data->recipe data)
    (match data
      (() '())
      ((key value . tail)
       (cons (cons key value) (data->recipe tail)))))

  (let* ((port (http-fetch/cached (string->uri (package-name->recipe-url package-name))
                                  #:ttl (* 6 3600)))
         (previous-keyword-mode (match (member 'keywords (read-options))
                                  ((_ value . _) value)))
         (data (begin
                 (read-set! keywords 'prefix)
                 (read port))))
    (read-set! keywords previous-keyword-mode)
    (close-port port)
    (data->recipe (cons #:name data))))

(define (github-repo->url repo)
  (string-append "https://github.com/" repo ".git"))

(define (gitlab-repo->url repo)
  (string-append "https://gitlab.com/" repo ".git"))

;; XXX adapted from (guix scripts hash)
(define (file-hash file select? recursive?)
  ;; Compute the hash of FILE.
  (if recursive?
      (let-values (((port get-hash) (open-sha256-port)))
        (write-file file port #:select? select?)
        (force-output port)
        (get-hash))
      (call-with-input-file file port-sha256)))

;; XXX taken from (guix scripts hash)
(define (vcs-file? file stat)
  (case (stat:type stat)
    ((directory)
     (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
    ((regular)
     ;; Git sub-modules have a '.git' file that is a regular text file.
     (string=? (basename file) ".git"))
    (else
     #f)))

(define (emacs-requires->inputs requires)
  (define (require-symbol->input-string require)
    (let ((require-string (symbol->string (if (pair? require)
                                              (car require)
                                              require))))
      (if (emacs-standard-library? require-string)
          #f
          (string-append "emacs-" require-string))))

  (package-names->package-inputs
   (filter identity (map require-symbol->input-string requires))))

;; This is a regular expression that will extract the package requirements
;; from a line of elisp.  See "(elisp) Library Headers" for more details about
;; this header.
(define package-depends-regexp
  (make-regexp "\\s*;+\\s**package-requires:(.*)" regexp/icase))

(define (find-package-inputs directory)
  (define (for-each-line f file)
    (call-with-input-file file
      (lambda (port)
        (let loop ()
          (let ((line (read-line port 'concat)))
            (unless (eof-object? line)
              (f line)
              (loop)))))))

  (emacs-requires->inputs
   (call/ec (lambda (return)
              (for-each
               (lambda (filename)
                 (for-each-line
                  (lambda (line)
                    (let ((match-data (regexp-exec package-depends-regexp line)))
                      (when match-data
                        (return (with-input-from-string (match:substring match-data 1)
                                  read)))))
                  (string-append directory "/" filename)))
               (scandir directory (cut string-suffix-ci? ".el" <>)))
              (return '())))))


(define (git-repository->package recipe url)
  (define ref
    (cond
     ((assoc-ref recipe #:branch)
      => (lambda (branch) (cons 'branch branch)))
     ((assoc-ref recipe #:commit)
      => (lambda (commit) (cons 'commit commit)))
     (else
      '(branch . "master"))))

  (define (maybe-inputs input-type inputs)
    (if (null? inputs)
        (list)
        (list (list input-type (list 'quasiquote inputs)))))

  (define (maybe-arguments files)
    (define (glob->regexp glob)
      (string-append
       "^"
       (regexp-substitute/global #f "\\*\\*?" glob
                                 'pre
                                 (lambda (m)
                                   (if (string= (match:substring m 0) "**")
                                       ".*"
                                       "[^/]+"))
                                 'post)
       "$"))

    (if files
        `((arguments '(#:include ',(map glob->regexp (remove pair? files))
                       #:exclude ',(map glob->regexp (apply append
                                                            (map (match-lambda
                                                                   ((#:exclude . values)
                                                                    values)
                                                                   (_ '()))
                                                                 files))))))
        '()))

  (let-values (((directory commit) (download-git-repository url ref)))
    (let ((inputs (find-package-inputs directory)))
      `(package
         (name ,(string-append "emacs-" (symbol->string (assoc-ref recipe #:name))))
         (version , (strftime "%Y%m%d" (gmtime (current-time))))
         (source (origin
                   (method git-fetch)
                   (uri (git-reference
                         (url ,url)
                         (commit ,commit)))
                   (sha256
                    (base32
                     ,(bytevector->nix-base32-string
                       (file-hash directory (negate vcs-file?) #t))))))
         (build-system emacs-build-system)
         ,@(maybe-inputs 'propagated-inputs
                         (find-package-inputs directory))
         ,@(maybe-arguments (assoc-ref recipe #:files))
         (home-page #f)
         (description #f)
         (synopsis #f)
         (license #f)))))

(define (melpa->guix-package package-name)
  "Construct a Guix package based on the MELPA recipe for PACKAGE-NAME."
  (let ((recipe (package-name->recipe package-name)))
    (match (assoc-ref recipe #:fetcher)
      ('github (git-repository->package recipe
                                        (github-repo->url (assoc-ref recipe #:repo))))
      ('gitlab (git-repository->package recipe
                                        (gitlab-repo->url (assoc-ref recipe #:repo))))
      ('git (git-repository->package recipe
                                     (assoc-ref recipe #:url)))
      (_ (leave (G_ "Only github, gitlab, and git repositories are currently supported"))))))

debug log:

solving cd22018589 ...
found cd22018589 in https://yhetil.org/guix-patches/87v9q1jjlf.fsf@zancanaro.id.au/

applying [1/1] https://yhetil.org/guix-patches/87v9q1jjlf.fsf@zancanaro.id.au/
diff --git a/guix/import/melpa.scm b/guix/import/melpa.scm
new file mode 100644
index 0000000000..cd22018589

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

index at:
100644 cd2201858901f5b826bd7aa9bb606de7b84b74fa	guix/import/melpa.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).