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, 2018 Tobias Geerinckx-Rice <me@tobias.gr>
6 ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (guix import cpan)
24 #:use-module (ice-9 match)
25 #:use-module (ice-9 regex)
26 #:use-module ((ice-9 popen) #:select (open-pipe* close-pipe))
27 #:use-module ((ice-9 rdelim) #:select (read-line))
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
31 #:use-module (gcrypt hash)
32 #:use-module (guix store)
33 #:use-module (guix utils)
34 #:use-module (guix base32)
35 #:use-module (guix ui)
36 #:use-module ((guix download) #:select (download-to-store url-fetch))
37 #: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-dependency?
43 cpan-dependency-relationship
45 cpan-dependency-module
46 cpan-dependency-version
53 cpan-release-distribution
54 cpan-release-download-url
56 cpan-release-home-page
57 cpan-release-dependencies
62 metacpan-url->mirror-url
69 ;;; Generate a package declaration template for the latest version of a CPAN
70 ;;; module, using meta-data from metacpan.org.
74 (define %metacpan-base-url
75 ;; Base URL of the MetaCPAN API.
76 (make-parameter "https://fastapi.metacpan.org/v1/"))
78 ;; Dependency of a "release".
79 (define-json-mapping <cpan-dependency> make-cpan-dependency cpan-dependency?
81 (relationship cpan-dependency-relationship "relationship"
82 string->symbol) ;requires | suggests
83 (phase cpan-dependency-phase "phase"
84 string->symbol) ;develop | configure | test | runtime
85 (module cpan-dependency-module) ;string
86 (version cpan-dependency-version)) ;string
88 ;; Release as returned by <https://fastapi.metacpan.org/v1/release/PKG>.
89 (define-json-mapping <cpan-release> make-cpan-release cpan-release?
91 (license cpan-release-license)
92 (author cpan-release-author)
93 (version cpan-release-version "version"
96 ;; Version is sometimes not quoted in the module json, so
97 ;; it gets imported into Guile as a number, so convert it
98 ;; to a string (example: "X11-Protocol-Other").
99 (number->string version))
101 ;; Sometimes we get a "v" prefix. Strip it.
102 (if (string-prefix? "v" version)
103 (string-drop version 1)
105 (module cpan-release-module "main_module") ;e.g., "Test::Script"
106 (distribution cpan-release-distribution) ;e.g., "Test-Script"
107 (download-url cpan-release-download-url "download_url")
108 (abstract cpan-release-abstract "abstract")
109 (home-page cpan-release-home-page "resources"
112 ((lst ...) (assoc-ref lst "homepage"))))
113 (dependencies cpan-release-dependencies "dependency"
115 (map json->cpan-dependency (vector->list vector)))))
117 (define string->license
119 ;; List of valid values from https://metacpan.org/pod/CPAN::Meta::Spec.
120 ;; Some licenses are excluded based on their absense from (guix licenses).
123 ("apache_2_0" 'asl2.0)
125 ("artistic_2" 'artistic2.0)
129 ("gfdl_1_3" 'fdl1.3+)
133 ("lgpl_2_1" 'lgpl2.1)
137 ("mozilla_1_1" 'mpl1.1)
139 ("perl_5" 'perl-license) ;GPL1+ and Artistic 1
144 (#(x) (string->license x))
145 (#(lst ...) `(list ,@(map string->license lst)))
148 (define (module->name module)
149 "Transform a 'module' name into a 'release' name"
150 (regexp-substitute/global #f "::" module 'pre "-" 'post))
152 (define (module->dist-name module)
153 "Return the base distribution module for a given module. E.g. the 'ok'
154 module is distributed with 'Test::Simple', so (module->dist-name \"ok\") would
155 return \"Test-Simple\""
156 (assoc-ref (json-fetch (string-append
157 (%metacpan-base-url) "/module/"
159 "?fields=distribution"))
162 (define (package->upstream-name package)
163 "Return the CPAN name of PACKAGE."
164 (let* ((properties (package-properties package))
165 (upstream-name (and=> properties
166 (cut assoc-ref <> 'upstream-name))))
168 (match (package-source package)
170 (match (origin-uri origin)
171 ((or (? string? url) (url _ ...))
172 (match (string-match (string-append "([^/]*)-v?[0-9\\.]+") url)
174 (m (match:substring m 1))))
178 (define (cpan-fetch name)
179 "Return a <cpan-release> record for Perl module MODULE,
180 or #f on failure. MODULE should be the distribution name, such as
181 \"Test-Script\" for the \"Test::Script\" module."
182 ;; This API always returns the latest release of the module.
183 (and=> (json-fetch (string-append (%metacpan-base-url) "/release/"
187 (define (cpan-home name)
188 (string-append "https://metacpan.org/release/" name))
190 (define (metacpan-url->mirror-url url)
191 "Replace 'https://cpan.metacpan.org' in URL with 'mirror://cpan'."
192 (regexp-substitute/global #f "http[s]?://cpan.metacpan.org"
194 'pre "mirror://cpan" 'post))
196 (define cpan-source-url
197 (compose metacpan-url->mirror-url cpan-release-download-url))
199 (define (perl-package)
200 "Return the 'perl' package. This is a lazy reference so that we don't
201 depend on (gnu packages perl)."
202 (module-ref (resolve-interface '(gnu packages perl)) 'perl))
206 (let* ((perl (with-store store
207 (derivation->output-path
208 (package-derivation store (perl-package)))))
209 (core (string-append perl "/bin/corelist")))
210 (and (access? core X_OK)
214 (let ((rx (make-regexp
215 (string-append "released with perl v?([0-9\\.]*)"
216 "(.*and removed from v?([0-9\\.]*))?"))))
219 (package-version (perl-package)))
221 (define (version-between? lower version upper)
222 (and (version>=? version lower)
224 (version>? upper version))))
225 (and (force %corelist)
226 (parameterize ((current-error-port (%make-void-port "w")))
227 (let* ((corelist (open-pipe* OPEN_READ (force %corelist) name)))
229 (let ((line (read-line corelist)))
230 (if (eof-object? line)
231 (begin (close-pipe corelist) #f)
232 (or (and=> (regexp-exec rx line)
234 (let ((first (match:substring m 1))
235 (last (match:substring m 3)))
237 first perl-version last))))
240 (define (cpan-module->sexp release)
241 "Return the 'package' s-expression for a CPAN module from the release data
242 in RELEASE, a <cpan-release> record."
244 (cpan-release-distribution release))
246 (define (guix-name name)
247 (if (string-prefix? "perl-" name)
248 (string-downcase name)
249 (string-append "perl-" (string-downcase name))))
251 (define version (cpan-release-version release))
252 (define source-url (cpan-source-url release))
254 (define (convert-inputs phases)
255 ;; Convert phase dependencies into a list of name/variable pairs.
256 (match (filter-map (lambda (dependency)
257 (and (memq (cpan-dependency-phase dependency)
259 (cpan-dependency-module dependency)))
260 (cpan-release-dependencies release))
264 ;; Listed dependencies may include core modules. Filter those out.
265 (filter-map (match-lambda
266 ("perl" #f) ;implicit dependency
267 ((? core-module?) #f)
269 (let ((name (guix-name (module->dist-name module))))
271 (list 'unquote (string->symbol name))))))
275 (((a _ ...) (b _ ...))
276 (string<? a b))))))))
278 (define (maybe-inputs guix-name inputs)
283 (list (list guix-name
284 (list 'quasiquote inputs))))))
286 (let ((tarball (with-store store
287 (download-to-store store source-url))))
289 (name ,(guix-name name))
293 (uri (string-append ,@(factorize-uri source-url version)))
296 ,(bytevector->nix-base32-string (file-sha256 tarball))))))
297 (build-system perl-build-system)
298 ,@(maybe-inputs 'native-inputs
299 ;; "runtime" may also be needed here. See
300 ;; https://metacpan.org/pod/CPAN::Meta::Spec#Phases,
301 ;; which says they are required during building. We
302 ;; have not yet had a need for cross-compiled perl
303 ;; modules, however, so we leave it out.
304 (convert-inputs '(configure build test)))
305 ,@(maybe-inputs 'propagated-inputs
306 (convert-inputs '(runtime)))
307 (home-page ,(cpan-home name))
308 (synopsis ,(cpan-release-abstract release))
309 (description fill-in-yourself!)
310 (license ,(string->license (cpan-release-license release))))))
312 (define (cpan->guix-package module-name)
313 "Fetch the metadata for PACKAGE-NAME from metacpan.org, and return the
314 `package' s-expression corresponding to that package, or #f on failure."
315 (let ((release (cpan-fetch (module->name module-name))))
316 (and=> release cpan-module->sexp)))
318 (define cpan-package?
319 (let ((cpan-rx (make-regexp (string-append "("
321 "https?://www.cpan.org" "|"
322 "https?://cpan.metacpan.org"
324 (url-predicate (cut regexp-exec cpan-rx <>))))
326 (define (latest-release package)
327 "Return an <upstream-source> for the latest release of PACKAGE."
328 (match (cpan-fetch (package->upstream-name package))
332 (match (package-direct-inputs package)
333 (((_ inputs _ ...) ...)
334 (filter-map (match-lambda
337 (= package->upstream-name
338 (? core-module? name)))
342 ;; Warn about inputs that are part of perl's core
343 (unless (null? core-inputs)
344 (for-each (lambda (module)
345 (warning (G_ "input '~a' of ~a is in Perl core~%")
346 module (package-name package)))
348 (let ((version (cpan-release-version release))
349 (url (cpan-source-url release)))
351 (package (package-name package))
353 (urls (list url)))))))
355 (define %cpan-updater
358 (description "Updater for CPAN packages")
360 (latest latest-release)))