1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016 Alex Sassmannshausen <alex@pompo.co>
5 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (guix import cpan)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 regex)
25 #:use-module ((ice-9 popen) #:select (open-pipe* close-pipe))
26 #:use-module ((ice-9 rdelim) #:select (read-line))
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
30 #:use-module (guix hash)
31 #:use-module (guix store)
32 #:use-module (guix utils)
33 #:use-module (guix base32)
34 #:use-module (guix ui)
35 #:use-module ((guix download) #:select (download-to-store url-fetch))
36 #:use-module ((guix import utils) #:select (factorize-uri
38 #:use-module (guix import json)
39 #:use-module (guix packages)
40 #:use-module (guix upstream)
41 #:use-module (guix derivations)
42 #:export (cpan->guix-package
47 ;;; Generate a package declaration template for the latest version of a CPAN
48 ;;; module, using meta-data from metacpan.org.
52 (define string->license
54 ;; List of valid values from https://metacpan.org/pod/CPAN::Meta::Spec.
55 ;; Some licenses are excluded based on their absense from (guix licenses).
58 ("apache_2_0" 'asl2.0)
60 ("artistic_2" 'artistic2.0)
72 ("mozilla_1_1" 'mpl1.1)
74 ("perl_5" 'perl-license) ;GPL1+ and Artistic 1
79 ((x) (string->license x))
80 ((lst ...) `(list ,@(map string->license lst)))
83 (define (module->name module)
84 "Transform a 'module' name into a 'release' name"
85 (regexp-substitute/global #f "::" module 'pre "-" 'post))
87 (define (module->dist-name module)
88 "Return the base distribution module for a given module. E.g. the 'ok'
89 module is distributed with 'Test::Simple', so (module->dist-name \"ok\") would
90 return \"Test-Simple\""
91 (assoc-ref (json-fetch (string-append "https://fastapi.metacpan.org/v1/module/"
93 "?fields=distribution"))
96 (define (package->upstream-name package)
97 "Return the CPAN name of PACKAGE."
98 (let* ((properties (package-properties package))
99 (upstream-name (and=> properties
100 (cut assoc-ref <> 'upstream-name))))
102 (match (package-source package)
104 (match (origin-uri origin)
105 ((or (? string? url) (url _ ...))
106 (match (string-match (string-append "([^/]*)-v?[0-9\\.]+") url)
108 (m (match:substring m 1))))
112 (define (cpan-fetch name)
113 "Return an alist representation of the CPAN metadata for the perl module MODULE,
114 or #f on failure. MODULE should be e.g. \"Test::Script\""
115 ;; This API always returns the latest release of the module.
116 (json-fetch (string-append "https://fastapi.metacpan.org/v1/release/" name)))
118 (define (cpan-home name)
119 (string-append "http://search.cpan.org/dist/" name "/"))
121 (define (cpan-source-url meta)
122 "Return the download URL for a module's source tarball."
123 (regexp-substitute/global #f "http[s]?://cpan.metacpan.org"
124 (assoc-ref meta "download_url")
125 'pre "mirror://cpan" 'post))
127 (define (cpan-version meta)
128 "Return the version number from META."
129 (match (assoc-ref meta "version")
131 ;; version is sometimes not quoted in the module json, so it gets
132 ;; imported into Guile as a number, so convert it to a string.
133 (number->string version))
136 (define (perl-package)
137 "Return the 'perl' package. This is a lazy reference so that we don't
138 depend on (gnu packages perl)."
139 (module-ref (resolve-interface '(gnu packages perl)) 'perl))
143 (let* ((perl (with-store store
144 (derivation->output-path
145 (package-derivation store (perl-package)))))
146 (core (string-append perl "/bin/corelist")))
147 (and (access? core X_OK)
151 (let ((rx (make-regexp
152 (string-append "released with perl v?([0-9\\.]*)"
153 "(.*and removed from v?([0-9\\.]*))?"))))
156 (package-version (perl-package)))
158 (define (version-between? lower version upper)
159 (and (version>=? version lower)
161 (version>? upper version))))
162 (and (force %corelist)
163 (parameterize ((current-error-port (%make-void-port "w")))
164 (let* ((corelist (open-pipe* OPEN_READ (force %corelist) name)))
166 (let ((line (read-line corelist)))
167 (if (eof-object? line)
168 (begin (close-pipe corelist) #f)
169 (or (and=> (regexp-exec rx line)
171 (let ((first (match:substring m 1))
172 (last (match:substring m 3)))
174 first perl-version last))))
177 (define (cpan-module->sexp meta)
178 "Return the `package' s-expression for a CPAN module from the metadata in
181 (assoc-ref meta "distribution"))
183 (define (guix-name name)
184 (if (string-prefix? "perl-" name)
185 (string-downcase name)
186 (string-append "perl-" (string-downcase name))))
188 (define version (cpan-version meta))
189 (define source-url (cpan-source-url meta))
191 (define (convert-inputs phases)
192 ;; Convert phase dependencies into a list of name/variable pairs.
195 (filter-map (lambda (t)
196 (assoc-ref* meta "metadata" "prereqs" ph t))
197 '("requires" "recommends" "suggests")))
204 ;; Listed dependencies may include core modules. Filter those out.
205 (filter-map (match-lambda
206 (("perl" . _) ;implicit dependency
209 (and (not (core-module? module))
210 (let ((name (guix-name (module->dist-name module))))
212 (list 'unquote (string->symbol name)))))))
216 (((a _ ...) (b _ ...))
217 (string<? a b))))))))
219 (define (maybe-inputs guix-name inputs)
224 (list (list guix-name
225 (list 'quasiquote inputs))))))
227 (let ((tarball (with-store store
228 (download-to-store store source-url))))
230 (name ,(guix-name name))
234 (uri (string-append ,@(factorize-uri source-url version)))
237 ,(bytevector->nix-base32-string (file-sha256 tarball))))))
238 (build-system perl-build-system)
239 ,@(maybe-inputs 'native-inputs
240 ;; "runtime" may also be needed here. See
241 ;; https://metacpan.org/pod/CPAN::Meta::Spec#Phases,
242 ;; which says they are required during building. We
243 ;; have not yet had a need for cross-compiled perl
244 ;; modules, however, so we leave it out.
245 (convert-inputs '("configure" "build" "test")))
246 ,@(maybe-inputs 'propagated-inputs
247 (convert-inputs '("runtime")))
248 (home-page ,(cpan-home name))
249 (synopsis ,(assoc-ref meta "abstract"))
250 (description fill-in-yourself!)
251 (license ,(string->license (assoc-ref meta "license"))))))
253 (define (cpan->guix-package module-name)
254 "Fetch the metadata for PACKAGE-NAME from metacpan.org, and return the
255 `package' s-expression corresponding to that package, or #f on failure."
256 (let ((module-meta (cpan-fetch (module->name module-name))))
257 (and=> module-meta cpan-module->sexp)))
259 (define (cpan-package? package)
260 "Return #t if PACKAGE is a package from CPAN."
262 (let ((cpan-rx (make-regexp (string-append "("
264 "https?://www.cpan.org" "|"
265 "https?://cpan.metacpan.org"
268 (regexp-exec cpan-rx url))))
270 (let ((source-url (and=> (package-source package) origin-uri))
271 (fetch-method (and=> (package-source package) origin-method)))
272 (and (eq? fetch-method url-fetch)
275 (cpan-url? source-url))
277 (any cpan-url? source-url))))))
279 (define (latest-release package)
280 "Return an <upstream-source> for the latest release of PACKAGE."
281 (match (cpan-fetch (package->upstream-name package))
285 (match (package-direct-inputs package)
286 (((_ inputs _ ...) ...)
287 (filter-map (match-lambda
290 (= package->upstream-name
291 (? core-module? name)))
295 ;; Warn about inputs that are part of perl's core
296 (unless (null? core-inputs)
297 (for-each (lambda (module)
298 (warning (G_ "input '~a' of ~a is in Perl core~%")
299 module (package-name package)))
301 (let ((version (cpan-version meta))
302 (url (cpan-source-url meta)))
304 (package (package-name package))
306 (urls (list url)))))))
308 (define %cpan-updater
311 (description "Updater for CPAN packages")
313 (latest latest-release)))