;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2012, 2013, 2018, 2019 Ludovic Courtès ;;; Copyright © 2016 Jelle Licht ;;; Copyright © 2016 David Craven ;;; Copyright © 2017, 2019 Ricardo Wurmus ;;; Copyright © 2018 Oleg Pykhalov ;;; Copyright © 2019 Robert Vollmert ;;; Copyright © 2019 Martin Becze ;;; ;;; 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 . (define-module (guix import utils) #:use-module (guix base32) #:use-module ((guix build download) #:prefix build:) #:use-module (gcrypt hash) #:use-module (guix http-client) #:use-module ((guix licenses) #:prefix license:) #:use-module (guix utils) #:use-module (guix packages) #:use-module (guix discovery) #:use-module (guix build-system) #:use-module (guix gexp) #:use-module (guix memoization) #:use-module (guix store) #:use-module (guix download) #:use-module (gnu packages) #: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-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-41) #:use-module (semver) #:use-module (semver ranges) #:export (factorize-uri flatten assoc-ref* url-fetch guix-hash-url package-names->package-inputs maybe-inputs maybe-native-inputs package->definition spdx-string->license license->symbol snake-case beautify-description alist->package read-lines chunk-lines guix-name recursive-import recursive-import-semver)) (define (factorize-uri uri version) "Factorize URI, a package tarball URI as a string, such that any occurrences of the string VERSION is replaced by the symbol 'version." (let ((version-rx (make-regexp (regexp-quote version)))) (match (regexp-exec version-rx uri) (#f uri) (_ (let ((indices (fold-matches version-rx uri '((0)) (lambda (m result) (match result (((start) rest ...) `((,(match:end m)) (,start . ,(match:start m)) ,@rest))))))) (fold (lambda (index result) (match index ((start) (cons (substring uri start) result)) ((start . end) (cons* (substring uri start end) 'version result)))) '() indices)))))) (define (flatten lst) "Return a list that recursively concatenates all sub-lists of LST." (fold-right (match-lambda* (((sub-list ...) memo) (append (flatten sub-list) memo)) ((elem memo) (cons elem memo))) '() lst)) (define (assoc-ref* alist key . rest) "Return the value for KEY from ALIST. For each additional key specified, recursively apply the procedure to the sub-list." (if (null? rest) (assoc-ref alist key) (apply assoc-ref* (assoc-ref alist key) rest))) (define (url-fetch url file-name) "Save the contents of URL to FILE-NAME. Return #f on failure." (parameterize ((current-output-port (current-error-port))) (build:url-fetch url file-name))) (define (guix-hash-url filename) "Return the hash of FILENAME in nix-base32 format." (bytevector->nix-base32-string (file-sha256 filename))) (define (spdx-string->license str) "Convert STR, a SPDX formatted license identifier, to a license object. Return #f if STR does not match any known identifiers." ;; https://spdx.org/licenses/ ;; The psfl, gfl1.0, nmap, repoze ;; licenses doesn't have SPDX identifiers (match str ("AGPL-1.0" 'license:agpl-1.0) ("AGPL-3.0" 'license:agpl-3.0) ("Apache-1.1" 'license:asl1.1) ("Apache-2.0" 'license:asl2.0) ("BSL-1.0" 'license:boost1.0) ("BSD-2-Clause-FreeBSD" 'license:bsd-2) ("BSD-3-Clause" 'license:bsd-3) ("BSD-4-Clause" 'license:bsd-4) ("CC0-1.0" 'license:cc0) ("CC-BY-2.0" 'license:cc-by2.0) ("CC-BY-3.0" 'license:cc-by3.0) ("CC-BY-SA-2.0" 'license:cc-by-sa2.0) ("CC-BY-SA-3.0" 'license:cc-by-sa3.0) ("CC-BY-SA-4.0" 'license:cc-by-sa4.0) ("CDDL-1.0" 'license:cddl1.0) ("CECILL-C" 'license:cecill-c) ("Artistic-2.0" 'license:artistic2.0) ("ClArtistic" 'license:clarified-artistic) ("CPL-1.0" 'license:cpl1.0) ("EPL-1.0" 'license:epl1.0) ("MIT" 'license:expat) ("FTL" 'license:freetype) ("GFDL-1.1" 'license:fdl1.1+) ("GFDL-1.2" 'license:fdl1.2+) ("GFDL-1.3" 'license:fdl1.3+) ("Giftware" 'license:giftware) ("GPL-1.0" 'license:gpl1) ("GPL-1.0+" 'license:gpl1+) ("GPL-2.0" 'license:gpl2) ("GPL-2.0+" 'license:gpl2+) ("GPL-3.0" 'license:gpl3) ("GPL-3.0+" 'license:gpl3+) ("ISC" 'license:isc) ("IJG" 'license:ijg) ("Imlib2" 'license:imlib2) ("IPA" 'license:ipa) ("IPL-1.0" 'license:ibmpl1.0) ("LGPL-2.0" 'license:lgpl2.0) ("LGPL-2.0+" 'license:lgpl2.0+) ("LGPL-2.1" 'license:lgpl2.1) ("LGPL-2.1+" 'license:lgpl2.1+) ("LGPL-3.0" 'license:lgpl3.0) ("LGPL-3.0+" 'license:lgpl3.0+) ("MPL-1.0" 'license:mpl1.0) ("MPL-1.1" 'license:mpl1.1) ("MPL-2.0" 'license:mpl2.0) ("MS-PL" 'license:ms-pl) ("NCSA" 'license:ncsa) ("OpenSSL" 'license:openssl) ("OLDAP-2.8" 'license:openldap2.8) ("CUA-OPL-1.0" 'license:opl1.0) ("QPL-1.0" 'license:qpl) ("Ruby" 'license:ruby) ("SGI-B-2.0" 'license:sgifreeb2.0) ("OFL-1.1" 'license:silofl1.1) ("Sleepycat" 'license:sleepycat) ("TCL" 'license:tcl/tk) ("Unlicense" 'license:unlicense) ("Vim" 'license:vim) ("X11" 'license:x11) ("ZPL-2.1" 'license:zpl2.1) ("Zlib" 'license:zlib) (_ #f))) (define (license->symbol license) "Convert license to a symbol representing the variable the object is bound to in the (guix licenses) module, or #f if there is no such known license." (define licenses (module-map (lambda (sym var) `(,(variable-ref var) . ,sym)) (resolve-interface '(guix licenses) #:prefix 'license:))) (assoc-ref licenses license)) (define (snake-case str) "Return a downcased version of the string STR where underscores are replaced with dashes." (string-join (string-split (string-downcase str) #\_) "-")) (define (beautify-description description) "Improve the package DESCRIPTION by turning a beginning sentence fragment into a proper sentence and by using two spaces between sentences." (let ((cleaned (cond ((string-prefix? "A " description) (string-append "This package provides a" (substring description 1))) ((string-prefix? "Provides " description) (string-append "This package provides" (substring description (string-length "Provides")))) ((string-prefix? "Functions " description) (string-append "This package provides functions" (substring description (string-length "Functions")))) (else description)))) ;; Use double spacing between sentences (regexp-substitute/global #f "\\. \\b" cleaned 'pre ". " 'post))) (define* (package-names->package-inputs names #:optional (output #f)) "Given a list of PACKAGE-NAMES, and an optional OUTPUT, tries to generate a quoted list of inputs, as suitable to use in an 'inputs' field of a package definition." (map (lambda (input) (cons* input (list 'unquote (string->symbol input)) (or (and output (list output)) '()))) names)) (define* (maybe-inputs package-names #:optional (output #f)) "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a package definition." (match (package-names->package-inputs package-names output) (() '()) ((package-inputs ...) `((inputs (,'quasiquote ,package-inputs)))))) (define* (maybe-native-inputs package-names #:optional (output #f)) "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a package definition." (match (package-names->package-inputs package-names output) (() '()) ((package-inputs ...) `((native-inputs (,'quasiquote ,package-inputs)))))) (define* (package->definition guix-package #:optional (latest #t)) (match guix-package ((or ('package ('name name) ('version version) . rest) ('let _ ('package ('name name) ('version version) . rest))) `(define-public ,(string->symbol (if latest name (string-append name "-" version))) ,guix-package)))) (define (build-system-modules) (all-modules (map (lambda (entry) `(,entry . "guix/build-system")) %load-path))) (define (lookup-build-system-by-name name) "Return a value for the symbol NAME, representing the name of the build system." (fold-module-public-variables (lambda (obj result) (if (and (build-system? obj) (eq? name (build-system-name obj))) obj result)) #f (build-system-modules))) (define (specs->package-lists specs) "Convert each string in the SPECS list to a list of a package label and a package value." (map (lambda (spec) (let-values (((pkg out) (specification->package+output spec))) (match out ("out" (list (package-name pkg) pkg)) (_ (list (package-name pkg) pkg out))))) specs)) (define (source-spec->object source) "Generate an object from a SOURCE specification. The SOURCE can either be a simple URL string, #F, or an alist containing entries for each of the expected fields of an object." (match source ((? string? source-url) (let ((tarball (with-store store (download-to-store store source-url)))) (origin (method url-fetch) (uri source-url) (sha256 (base32 (guix-hash-url tarball)))))) (#f #f) (orig (let ((sha (match (assoc-ref orig "sha256") ((("base32" . value)) (base32 value)) (_ #f)))) (origin (method (match (assoc-ref orig "method") ("url-fetch" (@ (guix download) url-fetch)) ("git-fetch" (@ (guix git-download) git-fetch)) ("svn-fetch" (@ (guix svn-download) svn-fetch)) ("hg-fetch" (@ (guix hg-download) hg-fetch)) (_ #f))) (uri (assoc-ref orig "uri")) (sha256 sha)))))) (define (alist->package meta) (package (name (assoc-ref meta "name")) (version (assoc-ref meta "version")) (source (source-spec->object (assoc-ref meta "source"))) (build-system (lookup-build-system-by-name (string->symbol (assoc-ref meta "build-system")))) (native-inputs (specs->package-lists (vector->list (or (assoc-ref meta "native-inputs") '#())))) (inputs (specs->package-lists (vector->list (or (assoc-ref meta "inputs") '#())))) (propagated-inputs (specs->package-lists (vector->list (or (assoc-ref meta "propagated-inputs") '#())))) (home-page (assoc-ref meta "home-page")) (synopsis (assoc-ref meta "synopsis")) (description (assoc-ref meta "description")) (license (match (assoc-ref meta "license") (#f #f) (l (or (module-ref (resolve-interface '(guix licenses) #:prefix 'license:) (spdx-string->license l)) (license:fsdg-compatible l))))))) (define* (read-lines #:optional (port (current-input-port))) "Read lines from PORT and return them as a list." (let loop ((line (read-line port)) (lines '())) (if (eof-object? line) (reverse lines) (loop (read-line port) (cons line lines))))) (define* (chunk-lines lines #:optional (pred string-null?)) "Return a list of chunks, each of which is a list of lines. The chunks are separated by PRED." (let loop ((rest lines) (parts '())) (receive (before after) (break pred rest) (let ((res (cons before parts))) (if (null? after) (reverse res) (loop (cdr after) res)))))) (define (guix-name prefix name) "Return a Guix package name for a given package name." (string-append prefix (string-map (match-lambda (#\_ #\-) (#\. #\-) (chr (char-downcase chr))) name))) (define* (recursive-import package-name repo #:key repo->guix-package guix-name #:allow-other-keys) "Generate a stream of package expressions for PACKAGE-NAME and all its dependencies." (define (exists? dependency) (not (null? (find-packages-by-name (guix-name dependency))))) (define initial-state (list #f (list package-name) (list))) (define (step state) (match state ((prev (next . rest) done) (define (handle? dep) (and (not (equal? dep next)) (not (member dep done)) (not (exists? dep)))) (receive (package . dependencies) (repo->guix-package next repo) (list (if package package '()) ;; default #f on failure would interrupt (if package (lset-union equal? rest (filter handle? (car dependencies))) rest) (cons next done)))) ((prev '() done) (list #f '() done)))) ;; Generate a lazy stream of package expressions for all unknown ;; dependencies in the graph. (stream-unfold ;; map: produce a stream element (match-lambda ((latest queue done) latest)) ;; predicate (match-lambda ((latest queue done) latest)) ;; generator: update the queue step ;; initial state (step initial-state))) (define* (recursive-import-semver #:key name (range "*") name->metadata metadata->package metadata-versions package-dependencies dependency-name dependency-range guix-name make-sexp) "Generates a stream of package expressions for the dependencies of the given NAME and version RANGE. The dependencies will be resolved using semantic versioning. This procedure makes the assumption that most package repositories will, for a given package provide some on that package that includes what versions of the package that are available and a list of dependencies for each version. Dependencies are assumed to be composed of a NAME, a semantic RANGE and other data. This procedure takes the following keys: NAME - The name of the package to import RANGE - The version range of the package to import NAME->METADATA - A procedure that takes a NAME of a package and returns that package's METADATA->PACKAGE A procedure that takes a package's and VERSION and returns the for the given VERSION METADATA-VERSIONS A procedure that that takes a packages and returns a list of version as strings that are available for the given package PACKAGE-DEPENDENCIES a procedure that returns a list of given a DEPENDENCY-NAME A procedure that takes a and returns the its name DEPENDENCY-RANGE A procedure that takes a and returns that decency's range as a string GUIX-NAME A procedure that take a NAME and returns the Guix version of it MAKE-SEXP A procedure that takes , and a list of pairs containing (EXPORT-NAME ), returning the package expression as an s-expression" (define mem-name->metadata (memoize name->metadata)) (define (latest? versions version) (equal? (car versions) version)) (define (sorted-versions metadata) (sort (metadata-versions metadata) version>?)) (define (name->versions name) (sorted-versions (mem-name->metadata name))) (define (semver-range-contains-string? range version) (semver-range-contains? range (string->semver version))) ;; given a name of a package and a version number this returns the export ;; symbol that will be used (define (guix-export-name name version) (let ((versions (name->versions name)) (name (guix-name name))) (if (latest? versions version) name (string-append name "-" version)))) ;; checks to see if we already defined or want to define a dep (define (find-known name range known) (match (find (match-lambda ((dep-name version) (and (string=? dep-name name) (semver-range-contains-string? range version)))) known) (#f #f) ((name version) (list (guix-export-name name version) version #f))) ) ;; searches searches for a package in guix (define (find-locally name range) (match (find (match-lambda ((_ _ package) (semver-range-contains-string? range (package-version package)))) (find-packages-by-name*/direct (guix-name name))) (#f #f) ((_ export-symbol package) (list (symbol->string export-symbol) (package-version package) #f)))) ;; searches for a package in some external repo (define (find-remote name range) (let* ((versions (name->versions name)) (version (find (lambda (ver) (semver-range-contains-string? range ver)) versions)) (export-name (guix-export-name name version))) `(,export-name ,version #t))) (define (find-dep-version-by-name-range name range-string known-deps) (let ((range (string->semver-range range-string))) (or (find-known name range known-deps) (find-locally name range) (find-remote name range)))) (define (find-dep-version dep known-deps) (let* ((name (dependency-name dep)) (range (dependency-range dep)) (export-name-version-needed (find-dep-version-by-name-range name range known-deps))) `(,name ,@export-name-version-needed ,dep) )) (define (make-package-definition name version known-deps) (let* ((metadata (mem-name->metadata name)) (versions (sorted-versions metadata)) (package (metadata->package metadata version)) (deps (map (lambda (dep) (find-dep-version dep known-deps)) (package-dependencies package))) (deps-with-export-symbol (map (match-lambda ((_ export-symbol _ _ dep) (list export-symbol dep))) deps)) (sexp (make-sexp metadata package deps-with-export-symbol))) (values (package->definition sexp (latest? versions version)) (filter-map (match-lambda ((name _ version need? dep) (if need? (list name version) #f))) deps)))) (define (initial-state name version) `(#f ;; packages to find ,(list (list name version)) ;; packages that have been found ())) (define (step state) (match state ((prev ((next-name next-version) . rest) done) (receive (package dependencies) (make-package-definition next-name next-version (append done rest `((,next-name ,next-version)))) (list package (append rest dependencies) (cons (list next-name next-version) done)))) ((prev '() done) (list #f '() done)))) (define (create-stream initial-state) (stream-unfold ;; map: produce a stream element (match-lambda ((latest queue done) latest)) ;; predicate (match-lambda ((latest queue done) latest)) step (step initial-state))) (match (find-dep-version-by-name-range name range '()) ((_ version #t) (create-stream (initial-state name version))) ;; if the initial package alread exsits then just return its export symbol ((export-name _ #f) (list->stream (list export-name)))))