utils.scm 21 KB

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