utils.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2018, 2019, 2020 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. ;;;
  16. ;;; This file is part of GNU Guix.
  17. ;;;
  18. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  19. ;;; under the terms of the GNU General Public License as published by
  20. ;;; the Free Software Foundation; either version 3 of the License, or (at
  21. ;;; your option) any later version.
  22. ;;;
  23. ;;; GNU Guix is distributed in the hope that it will be useful, but
  24. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. ;;; GNU General Public License for more details.
  27. ;;;
  28. ;;; You should have received a copy of the GNU General Public License
  29. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  30. (define-module (guix import utils)
  31. #:use-module (guix base32)
  32. #:use-module ((guix build download) #:prefix build:)
  33. #:use-module ((gcrypt hash) #:hide (sha256))
  34. #:use-module (guix http-client)
  35. #:use-module ((guix licenses) #:prefix license:)
  36. #:use-module (guix utils)
  37. #:use-module (guix packages)
  38. #:use-module (guix discovery)
  39. #:use-module (guix build-system)
  40. #:use-module (guix gexp)
  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. #:use-module (ice-9 match)
  48. #:use-module (ice-9 rdelim)
  49. #:use-module (ice-9 receive)
  50. #:use-module (ice-9 regex)
  51. #:use-module (srfi srfi-1)
  52. #:use-module (srfi srfi-9)
  53. #:use-module (srfi srfi-11)
  54. #:use-module (srfi srfi-26)
  55. #:use-module (srfi srfi-71)
  56. #:export (factorize-uri
  57. flatten
  58. url-fetch
  59. guix-hash-url
  60. package-names->package-inputs
  61. maybe-inputs
  62. maybe-native-inputs
  63. maybe-propagated-inputs
  64. package->definition
  65. spdx-string->license
  66. license->symbol
  67. snake-case
  68. beautify-description
  69. beautify-synopsis
  70. alist->package
  71. read-lines
  72. chunk-lines
  73. guix-name
  74. recursive-import))
  75. (define (factorize-uri uri version)
  76. "Factorize URI, a package tarball URI as a string, such that any occurrences
  77. of the string VERSION is replaced by the symbol 'version."
  78. (let ((version-rx (make-regexp (regexp-quote version))))
  79. (match (regexp-exec version-rx uri)
  80. (#f
  81. uri)
  82. (_
  83. (let ((indices (fold-matches version-rx uri
  84. '((0))
  85. (lambda (m result)
  86. (match result
  87. (((start) rest ...)
  88. `((,(match:end m))
  89. (,start . ,(match:start m))
  90. ,@rest)))))))
  91. (fold (lambda (index result)
  92. (match index
  93. ((start)
  94. (cons (substring uri start)
  95. result))
  96. ((start . end)
  97. (cons* (substring uri start end)
  98. 'version
  99. result))))
  100. '()
  101. indices))))))
  102. (define (flatten lst)
  103. "Return a list that recursively concatenates all sub-lists of LST."
  104. (fold-right
  105. (match-lambda*
  106. (((sub-list ...) memo)
  107. (append (flatten sub-list) memo))
  108. ((elem memo)
  109. (cons elem memo)))
  110. '() lst))
  111. (define (url-fetch url file-name)
  112. "Save the contents of URL to FILE-NAME. Return #f on failure."
  113. (parameterize ((current-output-port (current-error-port)))
  114. (build:url-fetch url file-name)))
  115. (define (guix-hash-url filename)
  116. "Return the hash of FILENAME in nix-base32 format."
  117. (bytevector->nix-base32-string (file-sha256 filename)))
  118. (define (spdx-string->license str)
  119. "Convert STR, a SPDX formatted license identifier, to a license object.
  120. Return #f if STR does not match any known identifiers."
  121. ;; https://spdx.org/licenses/
  122. ;; The gfl1.0, nmap, repoze
  123. ;; licenses doesn't have SPDX identifiers
  124. ;;
  125. ;; Please update guix/licenses.scm when modifying
  126. ;; this list to avoid mismatches.
  127. (match str
  128. ;; "GPL-N+" has been deprecated in favour of "GPL-N-or-later".
  129. ;; "GPL-N" has been deprecated in favour of "GPL-N-only"
  130. ;; or "GPL-N-or-later" as appropriate. Likewise for LGPL
  131. ;; and AGPL
  132. ("AGPL-1.0" 'license:agpl1)
  133. ("AGPL-1.0-only" 'license:agpl1)
  134. ("AGPL-3.0" 'license:agpl3)
  135. ("AGPL-3.0-only" 'license:agpl3)
  136. ("AGPL-3.0-or-later" 'license:agpl3+)
  137. ("Apache-1.1" 'license:asl1.1)
  138. ("Apache-2.0" 'license:asl2.0)
  139. ("APSL-2.0" 'license:apsl2)
  140. ("BSL-1.0" 'license:boost1.0)
  141. ("0BSD" 'license:bsd-0)
  142. ("BSD-2-Clause" 'license:bsd-2)
  143. ("BSD-2-Clause-FreeBSD" 'license:bsd-2) ;flagged as deprecated on spdx
  144. ("BSD-3-Clause" 'license:bsd-3)
  145. ("BSD-4-Clause" 'license:bsd-4)
  146. ("CC0-1.0" 'license:cc0)
  147. ("CC-BY-2.0" 'license:cc-by2.0)
  148. ("CC-BY-3.0" 'license:cc-by3.0)
  149. ("CC-BY-4.0" 'license:cc-by4.0)
  150. ("CC-BY-SA-2.0" 'license:cc-by-sa2.0)
  151. ("CC-BY-SA-3.0" 'license:cc-by-sa3.0)
  152. ("CC-BY-SA-4.0" 'license:cc-by-sa4.0)
  153. ("CDDL-1.0" 'license:cddl1.0)
  154. ("CDDL-1.1" 'license:cddl1.1)
  155. ("CECILL-2.1" 'license:cecill)
  156. ("CECILL-B" 'license:cecill-b)
  157. ("CECILL-C" 'license:cecill-c)
  158. ("Artistic-2.0" 'license:artistic2.0)
  159. ("ClArtistic" 'license:clarified-artistic)
  160. ("copyleft-next-0.3.0" 'license:copyleft-next)
  161. ("CPL-1.0" 'license:cpl1.0)
  162. ("EPL-1.0" 'license:epl1.0)
  163. ("EPL-2.0" 'license:epl2.0)
  164. ("EUPL-1.2" 'license:eupl1.2)
  165. ("MIT" 'license:expat)
  166. ("MIT-0" 'license:expat-0)
  167. ("FTL" 'license:freetype)
  168. ("FreeBSD-DOC" 'license:freebsd-doc)
  169. ("Freetype" 'license:freetype)
  170. ("FSFAP" 'license:fsf-free)
  171. ("FSFUL" 'license:fsf-free)
  172. ("GFDL-1.1" 'license:fdl1.1+)
  173. ("GFDL-1.1-or-later" 'license:fdl1.1+)
  174. ("GFDL-1.2" 'license:fdl1.2+)
  175. ("GFDL-1.2-or-later" 'license:fdl1.2+)
  176. ("GFDL-1.3" 'license:fdl1.3+)
  177. ("GFDL-1.3-or-later" 'license:fdl1.3+)
  178. ("Giftware" 'license:giftware)
  179. ("GPL-1.0" 'license:gpl1)
  180. ("GPL-1.0-only" 'license:gpl1)
  181. ("GPL-1.0+" 'license:gpl1+)
  182. ("GPL-1.0-or-later" 'license:gpl1+)
  183. ("GPL-2.0" 'license:gpl2)
  184. ("GPL-2.0-only" 'license:gpl2)
  185. ("GPL-2.0+" 'license:gpl2+)
  186. ("GPL-2.0-or-later" 'license:gpl2+)
  187. ("GPL-3.0" 'license:gpl3)
  188. ("GPL-3.0-only" 'license:gpl3)
  189. ("GPL-3.0+" 'license:gpl3+)
  190. ("GPL-3.0-or-later" 'license:gpl3+)
  191. ("HPND" 'license:hpnd)
  192. ("ISC" 'license:isc)
  193. ("IJG" 'license:ijg)
  194. ("Imlib2" 'license:imlib2)
  195. ("IPA" 'license:ipa)
  196. ("IPL-1.0" 'license:ibmpl1.0)
  197. ("LAL-1.3" 'license:lal1.3)
  198. ("LGPL-2.0" 'license:lgpl2.0)
  199. ("LGPL-2.0-only" 'license:lgpl2.0)
  200. ("LGPL-2.0+" 'license:lgpl2.0+)
  201. ("LGPL-2.0-or-later" 'license:lgpl2.0+)
  202. ("LGPL-2.1" 'license:lgpl2.1)
  203. ("LGPL-2.1-only" 'license:lgpl2.1)
  204. ("LGPL-2.1+" 'license:lgpl2.1+)
  205. ("LGPL-2.1-or-later" 'license:lgpl2.1+)
  206. ("LGPL-3.0" 'license:lgpl3)
  207. ("LGPL-3.0-only" 'license:lgpl3)
  208. ("LGPL-3.0+" 'license:lgpl3+)
  209. ("LGPL-3.0-or-later" 'license:lgpl3+)
  210. ("LPPL-1.0" 'license:lppl)
  211. ("LPPL-1.1" 'license:lppl)
  212. ("LPPL-1.2" 'license:lppl1.2)
  213. ("LPPL-1.3a" 'license:lppl1.3a)
  214. ("LPPL-1.3c" 'license:lppl1.3c)
  215. ("MirOS" 'license:miros)
  216. ("MPL-1.0" 'license:mpl1.0)
  217. ("MPL-1.1" 'license:mpl1.1)
  218. ("MPL-2.0" 'license:mpl2.0)
  219. ("MS-PL" 'license:ms-pl)
  220. ("NCSA" 'license:ncsa)
  221. ("OGL-UK-1.0" 'license:ogl-psi1.0)
  222. ("OpenSSL" 'license:openssl)
  223. ("OLDAP-2.8" 'license:openldap2.8)
  224. ("OPL-1.0" 'license:opl1.0+)
  225. ("CUA-OPL-1.0" 'license:cua-opl1.0)
  226. ("PSF-2.0" 'license:psfl)
  227. ("OSL-2.1" 'license:osl2.1)
  228. ("QPL-1.0" 'license:qpl)
  229. ("Ruby" 'license:ruby)
  230. ("SGI-B-2.0" 'license:sgifreeb2.0)
  231. ("OFL-1.1" 'license:silofl1.1)
  232. ("Sleepycat" 'license:sleepycat)
  233. ("TCL" 'license:tcl/tk)
  234. ("Unlicense" 'license:unlicense)
  235. ("Vim" 'license:vim)
  236. ("W3C" 'license:w3c)
  237. ("WTFPL" 'license:wtfpl2)
  238. ("wxWindow" 'license:wxwindows3.1+) ;flagged as deprecated on spdx
  239. ("X11" 'license:x11)
  240. ("ZPL-2.1" 'license:zpl2.1)
  241. ("Zlib" 'license:zlib)
  242. (_ #f)))
  243. (define (license->symbol license)
  244. "Convert license to a symbol representing the variable the object is bound
  245. to in the (guix licenses) module, or #f if there is no such known license."
  246. (define licenses
  247. (module-map (lambda (sym var) `(,(variable-ref var) . ,sym))
  248. (resolve-interface '(guix licenses) #:prefix 'license:)))
  249. (assoc-ref licenses license))
  250. (define (snake-case str)
  251. "Return a downcased version of the string STR where underscores are replaced
  252. with dashes."
  253. (string-join (string-split (string-downcase str) #\_) "-"))
  254. (define* (beautify-description description #:optional (length 80))
  255. "Improve the package DESCRIPTION by turning a beginning sentence fragment into
  256. a proper sentence and by using two spaces between sentences, and wrap lines at
  257. LENGTH characters."
  258. (let ((cleaned (cond
  259. ((not (string? description))
  260. (G_ "This package lacks a description. Run \
  261. \"info '(guix) Synopses and Descriptions'\" for more information."))
  262. ((string-prefix? "A " description)
  263. (string-append "This package provides a"
  264. (substring description 1)))
  265. ((string-prefix? "Provides " description)
  266. (string-append "This package provides"
  267. (substring description
  268. (string-length "Provides"))))
  269. ((string-prefix? "Implements " description)
  270. (string-append "This package implements"
  271. (substring description
  272. (string-length "Implements"))))
  273. ((string-prefix? "Functions " description)
  274. (string-append "This package provides functions"
  275. (substring description
  276. (string-length "Functions"))))
  277. (else description))))
  278. ;; Use double spacing between sentences
  279. (fill-paragraph (regexp-substitute/global #f "\\. \\b"
  280. cleaned 'pre ". " 'post)
  281. length)))
  282. (define (beautify-synopsis synopsis)
  283. "Improve the package SYNOPSIS."
  284. (let ((cleaned (cond
  285. ((not (string? synopsis))
  286. (G_ "This package lacks a synopsis. Run \
  287. \"info '(guix) Synopses and Descriptions'\" for more information."))
  288. ((string-prefix? "A " synopsis)
  289. (substring synopsis 1))
  290. ;; Remove trailing period.
  291. ((string-suffix? "." synopsis)
  292. (substring synopsis 0
  293. (1- (string-length synopsis))))
  294. (else synopsis))))
  295. (string-trim-both cleaned)))
  296. (define* (package-names->package-inputs names #:optional (output #f))
  297. "Given a list of PACKAGE-NAMES or (PACKAGE-NAME VERSION) pairs, and an
  298. optional OUTPUT, tries to generate a quoted list of inputs, as suitable to
  299. use in an 'inputs' field of a package definition."
  300. (define (make-input input version)
  301. (cons* input (list 'unquote (string->symbol
  302. (if version
  303. (string-append input "-" version)
  304. input)))
  305. (or (and output (list output))
  306. '())))
  307. (map (match-lambda
  308. ((input version) (make-input input version))
  309. (input (make-input input #f)))
  310. names))
  311. (define* (maybe-inputs package-names #:optional (output #f)
  312. #:key (type #f))
  313. "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a
  314. package definition. TYPE can be used to specify the type of the inputs;
  315. either the 'native or 'propagated symbols are accepted. Left unspecified, the
  316. snippet generated is for regular inputs."
  317. (let ((field-name (match type
  318. ('native 'native-inputs)
  319. ('propagated 'propagated-inputs)
  320. (_ 'inputs))))
  321. (match (package-names->package-inputs package-names output)
  322. (()
  323. '())
  324. ((package-inputs ...)
  325. `((,field-name (,'quasiquote ,package-inputs)))))))
  326. (define* (maybe-native-inputs package-names #:optional (output #f))
  327. "Same as MAYBE-INPUTS, but for native inputs."
  328. (maybe-inputs package-names output #:type 'native))
  329. (define* (maybe-propagated-inputs package-names #:optional (output #f))
  330. "Same as MAYBE-INPUTS, but for propagated inputs."
  331. (maybe-inputs package-names output #:type 'propagated))
  332. (define* (package->definition guix-package #:optional append-version?/string)
  333. "If APPEND-VERSION?/STRING is #t, append the package's major+minor version.
  334. If it is the symbol 'full, append the package's complete version. If
  335. APPEND-VERSION?/string is a string, append this string."
  336. (match guix-package
  337. ((or
  338. ('package ('name name) ('version version) . rest)
  339. ('package ('inherit ('simple-texlive-package name . _))
  340. ('version version) . rest)
  341. ('let _ ('package ('name name) ('version version) . rest)))
  342. `(define-public ,(string->symbol
  343. (cond
  344. ((string? append-version?/string)
  345. (string-append name "-" append-version?/string))
  346. ((eq? append-version?/string #t)
  347. (string-append name "-" (version-major+minor version)))
  348. ((eq? 'full append-version?/string)
  349. (string-append name "-" version))
  350. (else name)))
  351. ,guix-package))))
  352. (define (build-system-modules)
  353. (all-modules (map (lambda (entry)
  354. `(,entry . "guix/build-system"))
  355. %load-path)))
  356. (define (lookup-build-system-by-name name)
  357. "Return a <build-system> value for the symbol NAME, representing the name of
  358. the build system."
  359. (fold-module-public-variables (lambda (obj result)
  360. (if (and (build-system? obj)
  361. (eq? name (build-system-name obj)))
  362. obj result))
  363. #f
  364. (build-system-modules)))
  365. (define (specs->package-lists specs)
  366. "Convert each string in the SPECS list to a list of a package label and a
  367. package value."
  368. (map (lambda (spec)
  369. (let-values (((pkg out) (specification->package+output spec)))
  370. (match out
  371. ("out" (list (package-name pkg) pkg))
  372. (_ (list (package-name pkg) pkg out)))))
  373. specs))
  374. (define (source-spec->object source)
  375. "Generate an <origin> object from a SOURCE specification. The SOURCE can
  376. either be a simple URL string, #F, or an alist containing entries for each of
  377. the expected fields of an <origin> object."
  378. (match source
  379. ((? string? source-url)
  380. (let ((tarball (with-store store (download-to-store store source-url))))
  381. (origin
  382. (method url-fetch)
  383. (uri source-url)
  384. (sha256 (base32 (guix-hash-url tarball))))))
  385. (#f #f)
  386. (orig (let ((sha (match (assoc-ref orig "sha256")
  387. ((("base32" . value))
  388. (base32 value))
  389. (_ #f))))
  390. (origin
  391. (method (match (assoc-ref orig "method")
  392. ("url-fetch" (@ (guix download) url-fetch))
  393. ("git-fetch" (@ (guix git-download) git-fetch))
  394. ("svn-fetch" (@ (guix svn-download) svn-fetch))
  395. ("hg-fetch" (@ (guix hg-download) hg-fetch))
  396. (_ #f)))
  397. (uri (assoc-ref orig "uri"))
  398. (sha256 sha))))))
  399. (define* (alist->package meta #:optional (known-inputs '()))
  400. "Return a package value generated from the alist META. If the list of
  401. strings KNOWN-INPUTS is provided, do not treat the mentioned inputs as
  402. specifications to look up and replace them with plain symbols instead."
  403. (define (process-inputs which)
  404. (let-values (((regular known)
  405. (lset-diff+intersection
  406. string=?
  407. (vector->list (or (assoc-ref meta which) #()))
  408. known-inputs)))
  409. (append (specs->package-lists regular)
  410. (map string->symbol known))))
  411. (define (process-arguments arguments)
  412. (append-map (match-lambda
  413. ((key . value)
  414. (list (symbol->keyword (string->symbol key)) value)))
  415. arguments))
  416. (define (process-properties properties)
  417. (map (match-lambda
  418. ((key . value)
  419. (cons (string->symbol key) value)))
  420. properties))
  421. (package
  422. (name (assoc-ref meta "name"))
  423. (version (assoc-ref meta "version"))
  424. (source (source-spec->object (assoc-ref meta "source")))
  425. (properties
  426. (or (and=> (assoc-ref meta "properties")
  427. process-properties)
  428. '()))
  429. (build-system
  430. (lookup-build-system-by-name
  431. (string->symbol (assoc-ref meta "build-system"))))
  432. (arguments
  433. (or (and=> (assoc-ref meta "arguments")
  434. process-arguments)
  435. '()))
  436. (native-inputs (process-inputs "native-inputs"))
  437. (inputs (process-inputs "inputs"))
  438. (propagated-inputs (process-inputs "propagated-inputs"))
  439. (home-page
  440. (assoc-ref meta "home-page"))
  441. (synopsis
  442. (assoc-ref meta "synopsis"))
  443. (description
  444. (assoc-ref meta "description"))
  445. (license
  446. (match (assoc-ref meta "license")
  447. (#f #f)
  448. (l
  449. (or (false-if-exception
  450. (module-ref (resolve-interface '(guix licenses))
  451. (string->symbol l)))
  452. (false-if-exception
  453. (module-ref (resolve-interface '(guix licenses) #:prefix 'license:)
  454. (spdx-string->license l)))
  455. (license:fsdg-compatible l)))))))
  456. (define* (read-lines #:optional (port (current-input-port)))
  457. "Read lines from PORT and return them as a list."
  458. (let loop ((line (read-line port))
  459. (lines '()))
  460. (if (eof-object? line)
  461. (reverse lines)
  462. (loop (read-line port)
  463. (cons line lines)))))
  464. (define* (chunk-lines lines #:optional (pred string-null?))
  465. "Return a list of chunks, each of which is a list of lines. The chunks are
  466. separated by PRED."
  467. (let loop ((rest lines)
  468. (parts '()))
  469. (receive (before after)
  470. (break pred rest)
  471. (let ((res (cons before parts)))
  472. (if (null? after)
  473. (reverse res)
  474. (loop (cdr after) res))))))
  475. (define (guix-name prefix name)
  476. "Return a Guix package name for a given package name."
  477. (string-append prefix (string-map (match-lambda
  478. (#\_ #\-)
  479. (#\. #\-)
  480. (chr (char-downcase chr)))
  481. name)))
  482. (define (topological-sort nodes
  483. node-dependencies
  484. node-name)
  485. "Perform a breadth-first traversal of the graph rooted at NODES, a list of
  486. nodes, and return the list of nodes sorted in topological order. Call
  487. NODE-DEPENDENCIES to obtain the dependencies of a node, and NODE-NAME to
  488. obtain a node's uniquely identifying \"key\"."
  489. (let loop ((nodes nodes)
  490. (result '())
  491. (visited (set)))
  492. (match nodes
  493. (()
  494. result)
  495. ((head . tail)
  496. (if (set-contains? visited (node-name head))
  497. (loop tail result visited)
  498. (let ((dependencies (node-dependencies head)))
  499. (loop (append dependencies tail)
  500. (cons head result)
  501. (set-insert (node-name head) visited))))))))
  502. (define* (recursive-import package-name
  503. #:key repo->guix-package guix-name version repo
  504. #:allow-other-keys)
  505. "Return a list of package expressions for PACKAGE-NAME and all its
  506. dependencies, sorted in topological order. For each package,
  507. call (REPO->GUIX-PACKAGE NAME :KEYS version repo), which should return a
  508. package expression and a list of dependencies; call (GUIX-NAME PACKAGE-NAME)
  509. to obtain the Guix package name corresponding to the upstream name."
  510. (define-record-type <node>
  511. (make-node name version package dependencies)
  512. node?
  513. (name node-name)
  514. (version node-version)
  515. (package node-package)
  516. (dependencies node-dependencies))
  517. (define (exists? name version)
  518. (not (null? (find-packages-by-name (guix-name name) version))))
  519. (define (lookup-node name version)
  520. (let* ((package dependencies (repo->guix-package name
  521. #:version version
  522. #:repo repo))
  523. (normalized-deps (map (match-lambda
  524. ((name version) (list name version))
  525. (name (list name #f))) dependencies)))
  526. (make-node name version package normalized-deps)))
  527. (filter-map
  528. node-package
  529. (topological-sort (list (lookup-node package-name version))
  530. (lambda (node)
  531. (map (lambda (name-version)
  532. (apply lookup-node name-version))
  533. (remove (lambda (name-version)
  534. (apply exists? name-version))
  535. (node-dependencies node))))
  536. (lambda (node)
  537. (string-append
  538. (node-name node)
  539. (or (node-version node) ""))))))