utils.scm 21 KB

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