utils.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2018, 2019, 2020, 2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.org>
  4. ;;; Copyright © 2016 David Craven <david@craven.ch>
  5. ;;; Copyright © 2017, 2019, 2020, 2022 Ricardo Wurmus <rekado@elephly.net>
  6. ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
  7. ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
  8. ;;; Copyright © 2020 Helio Machado <0x2b3bfa0+guix@googlemail.com>
  9. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  10. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  11. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  12. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  13. ;;; Copyright © 2022 Alice Brenon <alice.brenon@ens-lyon.fr>
  14. ;;; Copyright © 2022 Kyle Meyer <kyle@kyleam.com>
  15. ;;; Copyright © 2022 Philip McGrath <philip@philipmcgrath.com>
  16. ;;;
  17. ;;; This file is part of GNU Guix.
  18. ;;;
  19. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  20. ;;; under the terms of the GNU General Public License as published by
  21. ;;; the Free Software Foundation; either version 3 of the License, or (at
  22. ;;; your option) any later version.
  23. ;;;
  24. ;;; GNU Guix is distributed in the hope that it will be useful, but
  25. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  26. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. ;;; GNU General Public License for more details.
  28. ;;;
  29. ;;; You should have received a copy of the GNU General Public License
  30. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  31. (define-module (guix import utils)
  32. #:use-module (guix base32)
  33. #:use-module ((guix build download) #:prefix build:)
  34. #:use-module ((gcrypt hash) #:hide (sha256))
  35. #:use-module (guix http-client)
  36. #:use-module ((guix licenses) #:prefix license:)
  37. #:use-module (guix utils)
  38. #:use-module (guix packages)
  39. #:use-module (guix discovery)
  40. #:use-module (guix build-system)
  41. #:use-module ((guix i18n) #:select (G_))
  42. #:use-module (guix store)
  43. #:use-module (guix download)
  44. #:use-module (guix sets)
  45. #:use-module ((guix ui) #:select (fill-paragraph))
  46. #:use-module (gnu packages)
  47. #:autoload (ice-9 control) (let/ec)
  48. #:use-module (ice-9 match)
  49. #:use-module (ice-9 rdelim)
  50. #:use-module (ice-9 receive)
  51. #:use-module (ice-9 regex)
  52. #:use-module (srfi srfi-1)
  53. #:use-module (srfi srfi-9)
  54. #:use-module (srfi srfi-11)
  55. #:use-module (srfi srfi-26)
  56. #:use-module (srfi srfi-34)
  57. #:use-module (srfi srfi-71)
  58. #:export (factorize-uri
  59. flatten
  60. false-if-networking-error
  61. url-fetch
  62. guix-hash-url
  63. package-names->package-inputs
  64. maybe-inputs
  65. maybe-native-inputs
  66. maybe-propagated-inputs
  67. package->definition
  68. spdx-string->license
  69. license->symbol
  70. snake-case
  71. beautify-description
  72. beautify-synopsis
  73. alist->package
  74. read-lines
  75. chunk-lines
  76. guix-name
  77. recursive-import))
  78. (define (factorize-uri uri version)
  79. "Factorize URI, a package tarball URI as a string, such that any occurrences
  80. of the string VERSION is replaced by the symbol 'version."
  81. (let ((version-rx (make-regexp (regexp-quote version))))
  82. (match (regexp-exec version-rx uri)
  83. (#f
  84. uri)
  85. (_
  86. (let ((indices (fold-matches version-rx uri
  87. '((0))
  88. (lambda (m result)
  89. (match result
  90. (((start) rest ...)
  91. `((,(match:end m))
  92. (,start . ,(match:start m))
  93. ,@rest)))))))
  94. (fold (lambda (index result)
  95. (match index
  96. ((start)
  97. (cons (substring uri start)
  98. result))
  99. ((start . end)
  100. (cons* (substring uri start end)
  101. 'version
  102. result))))
  103. '()
  104. indices))))))
  105. (define (flatten lst)
  106. "Return a list that recursively concatenates all sub-lists of LST."
  107. (fold-right
  108. (match-lambda*
  109. (((sub-list ...) memo)
  110. (append (flatten sub-list) memo))
  111. ((elem memo)
  112. (cons elem memo)))
  113. '() lst))
  114. (define (call-with-networking-exception-handler thunk)
  115. "Invoke THUNK, returning #f if one of the usual networking exception is
  116. thrown."
  117. (let/ec return
  118. (with-exception-handler
  119. (lambda (exception)
  120. (cond ((http-get-error? exception)
  121. (return #f))
  122. (((exception-predicate &exception-with-kind-and-args) exception)
  123. ;; Return false and move on upon connection failures and bogus
  124. ;; HTTP servers.
  125. (if (memq (exception-kind exception)
  126. '(gnutls-error tls-certificate-error
  127. system-error getaddrinfo-error
  128. bad-header bad-header-component))
  129. (return #f)
  130. (raise-exception exception)))
  131. (else
  132. (raise-exception exception))))
  133. thunk
  134. ;; Do not unwind to preserve meaningful backtraces.
  135. #:unwind? #f)))
  136. (define-syntax-rule (false-if-networking-error exp)
  137. "Evaluate EXP, returning #f if a networking-related exception is thrown."
  138. (call-with-networking-exception-handler (lambda () exp)))
  139. (define (url-fetch url file-name)
  140. "Save the contents of URL to FILE-NAME. Return #f on failure."
  141. (parameterize ((current-output-port (current-error-port)))
  142. (build:url-fetch url file-name)))
  143. (define (guix-hash-url filename)
  144. "Return the hash of FILENAME in nix-base32 format."
  145. (bytevector->nix-base32-string (file-sha256 filename)))
  146. (define %spdx-license-identifiers
  147. ;; https://spdx.org/licenses/
  148. ;; The gfl1.0, nmap, repoze
  149. ;; licenses doesn't have SPDX identifiers
  150. ;;
  151. ;; Please update guix/licenses.scm when modifying
  152. ;; this list to avoid mismatches.
  153. ;;
  154. ;; "GPL-N+" has been deprecated in favour of "GPL-N-or-later". "GPL-N" has
  155. ;; been deprecated in favour of "GPL-N-only" or "GPL-N-or-later" as
  156. ;; appropriate. Likewise for LGPL and AGPL. However, we list the
  157. ;; deprecated forms here (with and without the "+" operator) to get better
  158. ;; results from old license expressions.
  159. '(("AGPL-1.0" . license:agpl1)
  160. ("AGPL-1.0-only" . license:agpl1)
  161. ("AGPL-3.0" . license:agpl3)
  162. ("AGPL-3.0-only" . license:agpl3)
  163. ("AGPL-3.0-or-later" . license:agpl3+)
  164. ("Apache-1.1" . license:asl1.1)
  165. ("Apache-2.0" . license:asl2.0)
  166. ("APSL-2.0" . license:apsl2)
  167. ("BSL-1.0" . license:boost1.0)
  168. ("0BSD" . license:bsd-0)
  169. ("BSD-2-Clause" . license:bsd-2)
  170. ("BSD-2-Clause-FreeBSD" . license:bsd-2) ;flagged as deprecated on spdx
  171. ("BSD-3-Clause" . license:bsd-3)
  172. ("BSD-4-Clause" . license:bsd-4)
  173. ("CC0-1.0" . license:cc0)
  174. ("CC-BY-2.0" . license:cc-by2.0)
  175. ("CC-BY-3.0" . license:cc-by3.0)
  176. ("CC-BY-4.0" . license:cc-by4.0)
  177. ("CC-BY-SA-2.0" . license:cc-by-sa2.0)
  178. ("CC-BY-SA-3.0" . license:cc-by-sa3.0)
  179. ("CC-BY-SA-4.0" . license:cc-by-sa4.0)
  180. ("CDDL-1.0" . license:cddl1.0)
  181. ("CDDL-1.1" . license:cddl1.1)
  182. ("CECILL-2.1" . license:cecill)
  183. ("CECILL-B" . license:cecill-b)
  184. ("CECILL-C" . license:cecill-c)
  185. ("Artistic-2.0" . license:artistic2.0)
  186. ("ClArtistic" . license:clarified-artistic)
  187. ("copyleft-next-0.3.0" . license:copyleft-next)
  188. ("CPL-1.0" . license:cpl1.0)
  189. ("EPL-1.0" . license:epl1.0)
  190. ("EPL-2.0" . license:epl2.0)
  191. ("EUPL-1.1" . license:eupl1.1)
  192. ("EUPL-1.2" . license:eupl1.2)
  193. ("MIT" . license:expat)
  194. ("MIT-0" . license:expat-0)
  195. ("FTL" . license:freetype)
  196. ("FreeBSD-DOC" . license:freebsd-doc)
  197. ("Freetype" . license:freetype)
  198. ("FSFAP" . license:fsf-free)
  199. ("FSFUL" . license:fsf-free)
  200. ("GFDL-1.1" . license:fdl1.1+)
  201. ("GFDL-1.1-or-later" . license:fdl1.1+)
  202. ("GFDL-1.2" . license:fdl1.2+)
  203. ("GFDL-1.2-or-later" . license:fdl1.2+)
  204. ("GFDL-1.3" . license:fdl1.3+)
  205. ("GFDL-1.3-or-later" . license:fdl1.3+)
  206. ("Giftware" . license:giftware)
  207. ("GPL-1.0" . license:gpl1)
  208. ("GPL-1.0-only" . license:gpl1)
  209. ("GPL-1.0+" . license:gpl1+)
  210. ("GPL-1.0-or-later" . license:gpl1+)
  211. ("GPL-2.0" . license:gpl2)
  212. ("GPL-2.0-only" . license:gpl2)
  213. ("GPL-2.0+" . license:gpl2+)
  214. ("GPL-2.0-or-later" . license:gpl2+)
  215. ("GPL-3.0" . license:gpl3)
  216. ("GPL-3.0-only" . license:gpl3)
  217. ("GPL-3.0+" . license:gpl3+)
  218. ("GPL-3.0-or-later" . license:gpl3+)
  219. ("HPND" . license:hpnd)
  220. ("ISC" . license:isc)
  221. ("IJG" . license:ijg)
  222. ("Imlib2" . license:imlib2)
  223. ("IPA" . license:ipa)
  224. ("IPL-1.0" . license:ibmpl1.0)
  225. ("LAL-1.3" . license:lal1.3)
  226. ("LGPL-2.0" . license:lgpl2.0)
  227. ("LGPL-2.0-only" . license:lgpl2.0)
  228. ("LGPL-2.0+" . license:lgpl2.0+)
  229. ("LGPL-2.0-or-later" . license:lgpl2.0+)
  230. ("LGPL-2.1" . license:lgpl2.1)
  231. ("LGPL-2.1-only" . license:lgpl2.1)
  232. ("LGPL-2.1+" . license:lgpl2.1+)
  233. ("LGPL-2.1-or-later" . license:lgpl2.1+)
  234. ("LGPL-3.0" . license:lgpl3)
  235. ("LGPL-3.0-only" . license:lgpl3)
  236. ("LGPL-3.0+" . license:lgpl3+)
  237. ("LGPL-3.0-or-later" . license:lgpl3+)
  238. ("LPL-1.02" . license:lpl1.02)
  239. ("LPPL-1.0" . license:lppl)
  240. ("LPPL-1.1" . license:lppl)
  241. ("LPPL-1.2" . license:lppl1.2)
  242. ("LPPL-1.3a" . license:lppl1.3a)
  243. ("LPPL-1.3c" . license:lppl1.3c)
  244. ("MirOS" . license:miros)
  245. ("MPL-1.0" . license:mpl1.0)
  246. ("MPL-1.1" . license:mpl1.1)
  247. ("MPL-2.0" . license:mpl2.0)
  248. ("MS-PL" . license:ms-pl)
  249. ("NCSA" . license:ncsa)
  250. ("OGL-UK-1.0" . license:ogl-psi1.0)
  251. ("OpenSSL" . license:openssl)
  252. ("OLDAP-2.8" . license:openldap2.8)
  253. ("OPL-1.0" . license:opl1.0+)
  254. ("CUA-OPL-1.0" . license:cua-opl1.0)
  255. ("PSF-2.0" . license:psfl)
  256. ("OSL-2.1" . license:osl2.1)
  257. ("QPL-1.0" . license:qpl)
  258. ("Ruby" . license:ruby)
  259. ("SGI-B-2.0" . license:sgifreeb2.0)
  260. ("OFL-1.1" . license:silofl1.1)
  261. ("Sleepycat" . license:sleepycat)
  262. ("TCL" . license:tcl/tk)
  263. ("Unlicense" . license:unlicense)
  264. ("Vim" . license:vim)
  265. ("W3C" . license:w3c)
  266. ("WTFPL" . license:wtfpl2)
  267. ("wxWindow" . license:wxwindows3.1+) ;flagged as deprecated on spdx
  268. ("X11" . license:x11)
  269. ("ZPL-2.1" . license:zpl2.1)
  270. ("Zlib" . license:zlib)))
  271. (define (spdx-string->license str)
  272. "Convert STR, an SPDX license identifier (possibly with a postfix +
  273. operator), to a symbol like 'license:gpl3+ giving the prefixed name of a
  274. license object exported from (guix licenses). Return #f if STR does not match
  275. any known SPDX license identifiers. Per the SPDX specification, license
  276. identifiers are compared case-insensitively."
  277. ;; https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/#d2-case-sensitivity
  278. ;; Operators AND, OR, and WITH are case-sensitive, but identifiers are
  279. ;; case-insensitive for matching, though the canonical case is used in URIs.
  280. (match (assoc str %spdx-license-identifiers string-ci=?)
  281. ((_ . license)
  282. license)
  283. (#f
  284. (and (string-suffix? "+" str)
  285. ;; We try the form with the + to support deprecated identifiers for
  286. ;; GNU licenses (see above). Here, we handle other uses of +.
  287. (spdx-string->license (string-drop-right str 1))))))
  288. (define (license->symbol license)
  289. "Convert LICENSE object to a prefixed symbol representing the variable the
  290. object is bound to in the (guix licenses) module, such as 'license:gpl3+, or
  291. #f if there is no such known license."
  292. (define licenses
  293. (module-map (lambda (sym var) `(,(variable-ref var) . ,sym))
  294. (resolve-interface '(guix licenses) #:prefix 'license:)))
  295. (assoc-ref licenses license))
  296. (define (snake-case str)
  297. "Return a downcased version of the string STR where underscores are replaced
  298. with dashes."
  299. (string-join (string-split (string-downcase str) #\_) "-"))
  300. (define* (beautify-description description #:optional (length 80))
  301. "Improve the package DESCRIPTION by turning a beginning sentence fragment into
  302. a proper sentence and by using two spaces between sentences, and wrap lines at
  303. LENGTH characters."
  304. (unless (string? description)
  305. (G_ "This package lacks a description. Run \
  306. \"info '(guix) Synopses and Descriptions'\" for more information."))
  307. (let* ((fix-word
  308. (lambda (word)
  309. (fold (lambda (proc acc) (proc acc)) word
  310. (list
  311. ;; Remove wrapping in single quotes, common in R packages.
  312. (cut string-trim-both <> #\')
  313. ;; Escape single @ to prevent it from being understood as
  314. ;; invalid Texinfo syntax.
  315. (cut regexp-substitute/global #f "@" <> 'pre "@@" 'post)))))
  316. (words
  317. (string-tokenize (string-trim-both description)
  318. (char-set-complement
  319. (char-set #\space #\newline))))
  320. (new-words
  321. (match words
  322. (((and (or "A" "Functions" "Methods") first) . rest)
  323. (cons* "This" "package" "provides"
  324. (string-downcase first) rest))
  325. (((and (or "Contains"
  326. "Creates"
  327. "Performs"
  328. "Provides"
  329. "Produces"
  330. "Implements"
  331. "Infers") first) . rest)
  332. (cons* "This" "package"
  333. (string-downcase first) rest))
  334. (_ words)))
  335. (cleaned
  336. (string-join (map fix-word new-words))))
  337. ;; Use double spacing between sentences
  338. (fill-paragraph (regexp-substitute/global #f "\\. \\b"
  339. cleaned 'pre
  340. (lambda (m)
  341. (let ((pre (match:prefix m))
  342. (abbrevs '("Dr" "Mr" "Mrs"
  343. "Ms" "Prof" "vs"
  344. "e.g")))
  345. (if (or (any (cut string-suffix? <> pre) abbrevs)
  346. (char-upper-case?
  347. (string-ref pre (1- (string-length pre)))))
  348. ". "
  349. ". ")))
  350. 'post)
  351. length)))
  352. (define (beautify-synopsis synopsis)
  353. "Improve the package SYNOPSIS."
  354. (let ((cleaned (cond
  355. ((not (string? synopsis))
  356. (G_ "This package lacks a synopsis. Run \
  357. \"info '(guix) Synopses and Descriptions'\" for more information."))
  358. ((string-prefix? "A " synopsis)
  359. (substring synopsis 1))
  360. ;; Remove trailing period.
  361. ((string-suffix? "." synopsis)
  362. (substring synopsis 0
  363. (1- (string-length synopsis))))
  364. (else synopsis))))
  365. (string-trim-both cleaned)))
  366. (define* (package-names->package-inputs names #:optional (output #f))
  367. "Given a list of PACKAGE-NAMES or (PACKAGE-NAME VERSION) pairs, and an
  368. optional OUTPUT, tries to generate a quoted list of inputs, as suitable to
  369. use in an 'inputs' field of a package definition."
  370. (define (make-input input version)
  371. (cons* input (list 'unquote (string->symbol
  372. (if version
  373. (string-append input "-" version)
  374. input)))
  375. (or (and output (list output))
  376. '())))
  377. (map (match-lambda
  378. ((input version) (make-input input version))
  379. (input (make-input input #f)))
  380. names))
  381. (define* (maybe-inputs package-names #:optional (output #f)
  382. #:key (type #f))
  383. "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a
  384. package definition. TYPE can be used to specify the type of the inputs;
  385. either the 'native or 'propagated symbols are accepted. Left unspecified, the
  386. snippet generated is for regular inputs."
  387. (let ((field-name (match type
  388. ('native 'native-inputs)
  389. ('propagated 'propagated-inputs)
  390. (_ 'inputs))))
  391. (match (package-names->package-inputs package-names output)
  392. (()
  393. '())
  394. ((package-inputs ...)
  395. `((,field-name (,'quasiquote ,package-inputs)))))))
  396. (define* (maybe-native-inputs package-names #:optional (output #f))
  397. "Same as MAYBE-INPUTS, but for native inputs."
  398. (maybe-inputs package-names output #:type 'native))
  399. (define* (maybe-propagated-inputs package-names #:optional (output #f))
  400. "Same as MAYBE-INPUTS, but for propagated inputs."
  401. (maybe-inputs package-names output #:type 'propagated))
  402. (define* (package->definition guix-package #:optional append-version?/string)
  403. "If APPEND-VERSION?/STRING is #t, append the package's major+minor version.
  404. If it is the symbol 'full, append the package's complete version. If
  405. APPEND-VERSION?/string is a string, append this string."
  406. (match guix-package
  407. ((or
  408. ('package ('name name) ('version version) . rest)
  409. ('package ('inherit ('simple-texlive-package name . _))
  410. ('version version) . rest)
  411. ('let _ ('package ('name name) ('version version) . rest)))
  412. `(define-public ,(string->symbol
  413. (cond
  414. ((string? append-version?/string)
  415. (string-append name "-" append-version?/string))
  416. ((eq? append-version?/string #t)
  417. (string-append name "-" (version-major+minor version)))
  418. ((eq? 'full append-version?/string)
  419. (string-append name "-" version))
  420. (else name)))
  421. ,guix-package))))
  422. (define (build-system-modules)
  423. (all-modules (map (lambda (entry)
  424. `(,entry . "guix/build-system"))
  425. %load-path)))
  426. (define (lookup-build-system-by-name name)
  427. "Return a <build-system> value for the symbol NAME, representing the name of
  428. the build system."
  429. (fold-module-public-variables (lambda (obj result)
  430. (if (and (build-system? obj)
  431. (eq? name (build-system-name obj)))
  432. obj result))
  433. #f
  434. (build-system-modules)))
  435. (define (specs->package-lists specs)
  436. "Convert each string in the SPECS list to a list of a package label and a
  437. package value."
  438. (map (lambda (spec)
  439. (let-values (((pkg out) (specification->package+output spec)))
  440. (match out
  441. ("out" (list (package-name pkg) pkg))
  442. (_ (list (package-name pkg) pkg out)))))
  443. specs))
  444. (define (source-spec->object source)
  445. "Generate an <origin> object from a SOURCE specification. The SOURCE can
  446. either be a simple URL string, #F, or an alist containing entries for each of
  447. the expected fields of an <origin> object."
  448. (match source
  449. ((? string? source-url)
  450. (let ((tarball (with-store store (download-to-store store source-url))))
  451. (origin
  452. (method url-fetch)
  453. (uri source-url)
  454. (sha256 (base32 (guix-hash-url tarball))))))
  455. (#f #f)
  456. (orig (let ((sha (match (assoc-ref orig "sha256")
  457. ((("base32" . value))
  458. (base32 value))
  459. (_ #f))))
  460. (origin
  461. (method (match (assoc-ref orig "method")
  462. ("url-fetch" (@ (guix download) url-fetch))
  463. ("git-fetch" (@ (guix git-download) git-fetch))
  464. ("svn-fetch" (@ (guix svn-download) svn-fetch))
  465. ("hg-fetch" (@ (guix hg-download) hg-fetch))
  466. (_ #f)))
  467. (uri (assoc-ref orig "uri"))
  468. (sha256 sha))))))
  469. (define* (alist->package meta #:optional (known-inputs '()))
  470. "Return a package value generated from the alist META. If the list of
  471. strings KNOWN-INPUTS is provided, do not treat the mentioned inputs as
  472. specifications to look up and replace them with plain symbols instead."
  473. (define (process-inputs which)
  474. (let-values (((regular known)
  475. (lset-diff+intersection
  476. string=?
  477. (vector->list (or (assoc-ref meta which) #()))
  478. known-inputs)))
  479. (append (specs->package-lists regular)
  480. (map string->symbol known))))
  481. (define (process-arguments arguments)
  482. (append-map (match-lambda
  483. ((key . value)
  484. (list (symbol->keyword (string->symbol key)) value)))
  485. arguments))
  486. (define (process-properties properties)
  487. (map (match-lambda
  488. ((key . value)
  489. (cons (string->symbol key) value)))
  490. properties))
  491. (package
  492. (name (assoc-ref meta "name"))
  493. (version (assoc-ref meta "version"))
  494. (source (source-spec->object (assoc-ref meta "source")))
  495. (properties
  496. (or (and=> (assoc-ref meta "properties")
  497. process-properties)
  498. '()))
  499. (build-system
  500. (lookup-build-system-by-name
  501. (string->symbol (assoc-ref meta "build-system"))))
  502. (arguments
  503. (or (and=> (assoc-ref meta "arguments")
  504. process-arguments)
  505. '()))
  506. (native-inputs (process-inputs "native-inputs"))
  507. (inputs (process-inputs "inputs"))
  508. (propagated-inputs (process-inputs "propagated-inputs"))
  509. (home-page
  510. (assoc-ref meta "home-page"))
  511. (synopsis
  512. (assoc-ref meta "synopsis"))
  513. (description
  514. (assoc-ref meta "description"))
  515. (license
  516. (match (assoc-ref meta "license")
  517. (#f #f)
  518. (l
  519. (or (false-if-exception
  520. (module-ref (resolve-interface '(guix licenses))
  521. (string->symbol l)))
  522. (false-if-exception
  523. (module-ref (resolve-interface '(guix licenses) #:prefix 'license:)
  524. (spdx-string->license l)))
  525. (license:fsdg-compatible l)))))))
  526. (define* (read-lines #:optional (port (current-input-port)))
  527. "Read lines from PORT and return them as a list."
  528. (let loop ((line (read-line port))
  529. (lines '()))
  530. (if (eof-object? line)
  531. (reverse lines)
  532. (loop (read-line port)
  533. (cons line lines)))))
  534. (define* (chunk-lines lines #:optional (pred string-null?))
  535. "Return a list of chunks, each of which is a list of lines. The chunks are
  536. separated by PRED."
  537. (let loop ((rest lines)
  538. (parts '()))
  539. (receive (before after)
  540. (break pred rest)
  541. (let ((res (cons before parts)))
  542. (if (null? after)
  543. (reverse res)
  544. (loop (cdr after) res))))))
  545. (define (guix-name prefix name)
  546. "Return a Guix package name for a given package name."
  547. (string-append prefix (string-map (match-lambda
  548. (#\_ #\-)
  549. (#\. #\-)
  550. (chr (char-downcase chr)))
  551. name)))
  552. (define (topological-sort nodes
  553. node-dependencies
  554. node-name)
  555. "Perform a breadth-first traversal of the graph rooted at NODES, a list of
  556. nodes, and return the list of nodes sorted in topological order. Call
  557. NODE-DEPENDENCIES to obtain the dependencies of a node, and NODE-NAME to
  558. obtain a node's uniquely identifying \"key\"."
  559. (let loop ((nodes nodes)
  560. (result '())
  561. (visited (set)))
  562. (match nodes
  563. (()
  564. result)
  565. ((head . tail)
  566. (if (set-contains? visited (node-name head))
  567. (loop tail result visited)
  568. (let ((dependencies (node-dependencies head)))
  569. (loop (append dependencies tail)
  570. (cons head result)
  571. (set-insert (node-name head) visited))))))))
  572. (define* (recursive-import package-name
  573. #:key repo->guix-package guix-name version
  574. #:allow-other-keys #:rest rest)
  575. "Return a list of package expressions for PACKAGE-NAME and all its
  576. dependencies, sorted in topological order. For each package,
  577. call (REPO->GUIX-PACKAGE NAME #:version V), which should return a
  578. package expression and a list of dependencies; call (GUIX-NAME PACKAGE-NAME)
  579. to obtain the Guix package name corresponding to the upstream name."
  580. (define-record-type <node>
  581. (make-node name version package dependencies)
  582. node?
  583. (name node-name)
  584. (version node-version)
  585. (package node-package)
  586. (dependencies node-dependencies))
  587. (define (exists? name version)
  588. (not (null? (find-packages-by-name (guix-name name) version))))
  589. (define (lookup-node name version)
  590. (let* ((pre post (break (cut eq? #:version <>) rest))
  591. (post* (match post
  592. ((#:version v . more) more)
  593. (_ post)))
  594. (args (append pre (list #:version version) post*))
  595. (package dependencies (apply repo->guix-package name args))
  596. (normalized-deps (map (match-lambda
  597. ((name version) (list name version))
  598. (name (list name #f))) dependencies)))
  599. (make-node name version package normalized-deps)))
  600. (filter-map
  601. node-package
  602. (topological-sort (list (lookup-node package-name version))
  603. (lambda (node)
  604. (map (lambda (name-version)
  605. (apply lookup-node name-version))
  606. (remove (lambda (name-version)
  607. (apply exists? name-version))
  608. (node-dependencies node))))
  609. (lambda (node)
  610. (string-append
  611. (node-name node)
  612. (or (node-version node) ""))))))