utils.scm 21 KB

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