utils.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. ("AGPL-1.0" 'license:agpl1)
  122. ("AGPL-3.0" 'license:agpl3)
  123. ("Apache-1.1" 'license:asl1.1)
  124. ("Apache-2.0" 'license:asl2.0)
  125. ("BSL-1.0" 'license:boost1.0)
  126. ("0BSD" 'license:bsd-0)
  127. ("BSD-2-Clause-FreeBSD" 'license:bsd-2)
  128. ("BSD-3-Clause" 'license:bsd-3)
  129. ("BSD-4-Clause" 'license:bsd-4)
  130. ("CC0-1.0" 'license:cc0)
  131. ("CC-BY-2.0" 'license:cc-by2.0)
  132. ("CC-BY-3.0" 'license:cc-by3.0)
  133. ("CC-BY-SA-2.0" 'license:cc-by-sa2.0)
  134. ("CC-BY-SA-3.0" 'license:cc-by-sa3.0)
  135. ("CC-BY-SA-4.0" 'license:cc-by-sa4.0)
  136. ("CDDL-1.0" 'license:cddl1.0)
  137. ("CECILL-C" 'license:cecill-c)
  138. ("Artistic-2.0" 'license:artistic2.0)
  139. ("ClArtistic" 'license:clarified-artistic)
  140. ("CPL-1.0" 'license:cpl1.0)
  141. ("EPL-1.0" 'license:epl1.0)
  142. ("MIT" 'license:expat)
  143. ("FTL" 'license:freetype)
  144. ("GFDL-1.1" 'license:fdl1.1+)
  145. ("GFDL-1.2" 'license:fdl1.2+)
  146. ("GFDL-1.3" 'license:fdl1.3+)
  147. ("Giftware" 'license:giftware)
  148. ("GPL-1.0" 'license:gpl1)
  149. ("GPL-1.0+" 'license:gpl1+)
  150. ("GPL-2.0" 'license:gpl2)
  151. ("GPL-2.0+" 'license:gpl2+)
  152. ("GPL-3.0" 'license:gpl3)
  153. ("GPL-3.0+" 'license:gpl3+)
  154. ("ISC" 'license:isc)
  155. ("IJG" 'license:ijg)
  156. ("Imlib2" 'license:imlib2)
  157. ("IPA" 'license:ipa)
  158. ("IPL-1.0" 'license:ibmpl1.0)
  159. ("LAL-1.3" 'license:lal1.3)
  160. ("LGPL-2.0" 'license:lgpl2.0)
  161. ("LGPL-2.0+" 'license:lgpl2.0+)
  162. ("LGPL-2.1" 'license:lgpl2.1)
  163. ("LGPL-2.1+" 'license:lgpl2.1+)
  164. ("LGPL-3.0" 'license:lgpl3)
  165. ("LGPL-3.0+" 'license:lgpl3+)
  166. ("MPL-1.0" 'license:mpl1.0)
  167. ("MPL-1.1" 'license:mpl1.1)
  168. ("MPL-2.0" 'license:mpl2.0)
  169. ("MS-PL" 'license:ms-pl)
  170. ("NCSA" 'license:ncsa)
  171. ("OpenSSL" 'license:openssl)
  172. ("OLDAP-2.8" 'license:openldap2.8)
  173. ("CUA-OPL-1.0" 'license:cua-opl1.0)
  174. ("QPL-1.0" 'license:qpl)
  175. ("Ruby" 'license:ruby)
  176. ("SGI-B-2.0" 'license:sgifreeb2.0)
  177. ("OFL-1.1" 'license:silofl1.1)
  178. ("Sleepycat" 'license:sleepycat)
  179. ("TCL" 'license:tcl/tk)
  180. ("Unlicense" 'license:unlicense)
  181. ("Vim" 'license:vim)
  182. ("X11" 'license:x11)
  183. ("ZPL-2.1" 'license:zpl2.1)
  184. ("Zlib" 'license:zlib)
  185. (_ #f)))
  186. (define (license->symbol license)
  187. "Convert license to a symbol representing the variable the object is bound
  188. to in the (guix licenses) module, or #f if there is no such known license."
  189. (define licenses
  190. (module-map (lambda (sym var) `(,(variable-ref var) . ,sym))
  191. (resolve-interface '(guix licenses) #:prefix 'license:)))
  192. (assoc-ref licenses license))
  193. (define (snake-case str)
  194. "Return a downcased version of the string STR where underscores are replaced
  195. with dashes."
  196. (string-join (string-split (string-downcase str) #\_) "-"))
  197. (define (beautify-description description)
  198. "Improve the package DESCRIPTION by turning a beginning sentence fragment
  199. into a proper sentence and by using two spaces between sentences."
  200. (let ((cleaned (cond
  201. ((string-prefix? "A " description)
  202. (string-append "This package provides a"
  203. (substring description 1)))
  204. ((string-prefix? "Provides " description)
  205. (string-append "This package provides"
  206. (substring description
  207. (string-length "Provides"))))
  208. ((string-prefix? "Functions " description)
  209. (string-append "This package provides functions"
  210. (substring description
  211. (string-length "Functions"))))
  212. (else description))))
  213. ;; Use double spacing between sentences
  214. (regexp-substitute/global #f "\\. \\b"
  215. cleaned 'pre ". " 'post)))
  216. (define* (package-names->package-inputs names #:optional (output #f))
  217. "Given a list of PACKAGE-NAMES or (PACKAGE-NAME VERSION) pairs, and an
  218. optional OUTPUT, tries to generate a quoted list of inputs, as suitable to
  219. use in an 'inputs' field of a package definition."
  220. (define (make-input input version)
  221. (cons* input (list 'unquote (string->symbol
  222. (if version
  223. (string-append input "-" version)
  224. input)))
  225. (or (and output (list output))
  226. '())))
  227. (map (match-lambda
  228. ((input version) (make-input input version))
  229. (input (make-input input #f)))
  230. names))
  231. (define* (maybe-inputs package-names #:optional (output #f)
  232. #:key (type #f))
  233. "Given a list of PACKAGE-NAMES, tries to generate the 'inputs' field of a
  234. package definition. TYPE can be used to specify the type of the inputs;
  235. either the 'native or 'propagated symbols are accepted. Left unspecified, the
  236. snippet generated is for regular inputs."
  237. (let ((field-name (match type
  238. ('native 'native-inputs)
  239. ('propagated 'propagated-inputs)
  240. (_ 'inputs))))
  241. (match (package-names->package-inputs package-names output)
  242. (()
  243. '())
  244. ((package-inputs ...)
  245. `((,field-name (,'quasiquote ,package-inputs)))))))
  246. (define* (maybe-native-inputs package-names #:optional (output #f))
  247. "Same as MAYBE-INPUTS, but for native inputs."
  248. (maybe-inputs package-names output #:type 'native))
  249. (define* (maybe-propagated-inputs package-names #:optional (output #f))
  250. "Same as MAYBE-INPUTS, but for propagated inputs."
  251. (maybe-inputs package-names output #:type 'propagated))
  252. (define* (package->definition guix-package #:optional append-version?/string)
  253. "If APPEND-VERSION?/STRING is #t, append the package's major+minor version.
  254. If it is the symbol 'full, append the package's complete version. If
  255. APPEND-VERSION?/string is a string, append this string."
  256. (match guix-package
  257. ((or
  258. ('package ('name name) ('version version) . rest)
  259. ('let _ ('package ('name name) ('version version) . rest)))
  260. `(define-public ,(string->symbol
  261. (cond
  262. ((string? append-version?/string)
  263. (string-append name "-" append-version?/string))
  264. ((eq? append-version?/string #t)
  265. (string-append name "-" (version-major+minor version)))
  266. ((eq? 'full append-version?/string)
  267. (string-append name "-" version))
  268. (else name)))
  269. ,guix-package))))
  270. (define (build-system-modules)
  271. (all-modules (map (lambda (entry)
  272. `(,entry . "guix/build-system"))
  273. %load-path)))
  274. (define (lookup-build-system-by-name name)
  275. "Return a <build-system> value for the symbol NAME, representing the name of
  276. the build system."
  277. (fold-module-public-variables (lambda (obj result)
  278. (if (and (build-system? obj)
  279. (eq? name (build-system-name obj)))
  280. obj result))
  281. #f
  282. (build-system-modules)))
  283. (define (specs->package-lists specs)
  284. "Convert each string in the SPECS list to a list of a package label and a
  285. package value."
  286. (map (lambda (spec)
  287. (let-values (((pkg out) (specification->package+output spec)))
  288. (match out
  289. ("out" (list (package-name pkg) pkg))
  290. (_ (list (package-name pkg) pkg out)))))
  291. specs))
  292. (define (source-spec->object source)
  293. "Generate an <origin> object from a SOURCE specification. The SOURCE can
  294. either be a simple URL string, #F, or an alist containing entries for each of
  295. the expected fields of an <origin> object."
  296. (match source
  297. ((? string? source-url)
  298. (let ((tarball (with-store store (download-to-store store source-url))))
  299. (origin
  300. (method url-fetch)
  301. (uri source-url)
  302. (sha256 (base32 (guix-hash-url tarball))))))
  303. (#f #f)
  304. (orig (let ((sha (match (assoc-ref orig "sha256")
  305. ((("base32" . value))
  306. (base32 value))
  307. (_ #f))))
  308. (origin
  309. (method (match (assoc-ref orig "method")
  310. ("url-fetch" (@ (guix download) url-fetch))
  311. ("git-fetch" (@ (guix git-download) git-fetch))
  312. ("svn-fetch" (@ (guix svn-download) svn-fetch))
  313. ("hg-fetch" (@ (guix hg-download) hg-fetch))
  314. (_ #f)))
  315. (uri (assoc-ref orig "uri"))
  316. (sha256 sha))))))
  317. (define* (alist->package meta #:optional (known-inputs '()))
  318. "Return a package value generated from the alist META. If the list of
  319. strings KNOWN-INPUTS is provided, do not treat the mentioned inputs as
  320. specifications to look up and replace them with plain symbols instead."
  321. (define (process-inputs which)
  322. (let-values (((regular known)
  323. (lset-diff+intersection
  324. string=?
  325. (vector->list (or (assoc-ref meta which) #()))
  326. known-inputs)))
  327. (append (specs->package-lists regular)
  328. (map string->symbol known))))
  329. (define (process-arguments arguments)
  330. (append-map (match-lambda
  331. ((key . value)
  332. (list (symbol->keyword (string->symbol key)) value)))
  333. arguments))
  334. (package
  335. (name (assoc-ref meta "name"))
  336. (version (assoc-ref meta "version"))
  337. (source (source-spec->object (assoc-ref meta "source")))
  338. (build-system
  339. (lookup-build-system-by-name
  340. (string->symbol (assoc-ref meta "build-system"))))
  341. (arguments
  342. (or (and=> (assoc-ref meta "arguments")
  343. process-arguments)
  344. '()))
  345. (native-inputs (process-inputs "native-inputs"))
  346. (inputs (process-inputs "inputs"))
  347. (propagated-inputs (process-inputs "propagated-inputs"))
  348. (home-page
  349. (assoc-ref meta "home-page"))
  350. (synopsis
  351. (assoc-ref meta "synopsis"))
  352. (description
  353. (assoc-ref meta "description"))
  354. (license
  355. (match (assoc-ref meta "license")
  356. (#f #f)
  357. (l
  358. (or (false-if-exception
  359. (module-ref (resolve-interface '(guix licenses))
  360. (string->symbol l)))
  361. (false-if-exception
  362. (module-ref (resolve-interface '(guix licenses) #:prefix 'license:)
  363. (spdx-string->license l)))
  364. (license:fsdg-compatible l)))))))
  365. (define* (read-lines #:optional (port (current-input-port)))
  366. "Read lines from PORT and return them as a list."
  367. (let loop ((line (read-line port))
  368. (lines '()))
  369. (if (eof-object? line)
  370. (reverse lines)
  371. (loop (read-line port)
  372. (cons line lines)))))
  373. (define* (chunk-lines lines #:optional (pred string-null?))
  374. "Return a list of chunks, each of which is a list of lines. The chunks are
  375. separated by PRED."
  376. (let loop ((rest lines)
  377. (parts '()))
  378. (receive (before after)
  379. (break pred rest)
  380. (let ((res (cons before parts)))
  381. (if (null? after)
  382. (reverse res)
  383. (loop (cdr after) res))))))
  384. (define (guix-name prefix name)
  385. "Return a Guix package name for a given package name."
  386. (string-append prefix (string-map (match-lambda
  387. (#\_ #\-)
  388. (#\. #\-)
  389. (chr (char-downcase chr)))
  390. name)))
  391. (define (topological-sort nodes
  392. node-dependencies
  393. node-name)
  394. "Perform a breadth-first traversal of the graph rooted at NODES, a list of
  395. nodes, and return the list of nodes sorted in topological order. Call
  396. NODE-DEPENDENCIES to obtain the dependencies of a node, and NODE-NAME to
  397. obtain a node's uniquely identifying \"key\"."
  398. (let loop ((nodes nodes)
  399. (result '())
  400. (visited (set)))
  401. (match nodes
  402. (()
  403. result)
  404. ((head . tail)
  405. (if (set-contains? visited (node-name head))
  406. (loop tail result visited)
  407. (let ((dependencies (node-dependencies head)))
  408. (loop (append dependencies tail)
  409. (cons head result)
  410. (set-insert (node-name head) visited))))))))
  411. (define* (recursive-import package-name
  412. #:key repo->guix-package guix-name version repo
  413. #:allow-other-keys)
  414. "Return a list of package expressions for PACKAGE-NAME and all its
  415. dependencies, sorted in topological order. For each package,
  416. call (REPO->GUIX-PACKAGE NAME :KEYS version repo), which should return a
  417. package expression and a list of dependencies; call (GUIX-NAME PACKAGE-NAME)
  418. to obtain the Guix package name corresponding to the upstream name."
  419. (define-record-type <node>
  420. (make-node name version package dependencies)
  421. node?
  422. (name node-name)
  423. (version node-version)
  424. (package node-package)
  425. (dependencies node-dependencies))
  426. (define (exists? name version)
  427. (not (null? (find-packages-by-name (guix-name name) version))))
  428. (define (lookup-node name version)
  429. (let* ((package dependencies (repo->guix-package name
  430. #:version version
  431. #:repo repo))
  432. (normalized-deps (map (match-lambda
  433. ((name version) (list name version))
  434. (name (list name #f))) dependencies)))
  435. (make-node name version package normalized-deps)))
  436. (map node-package
  437. (topological-sort (list (lookup-node package-name version))
  438. (lambda (node)
  439. (map (lambda (name-version)
  440. (apply lookup-node name-version))
  441. (remove (lambda (name-version)
  442. (apply exists? name-version))
  443. (node-dependencies node))))
  444. (lambda (node)
  445. (string-append
  446. (node-name node)
  447. (or (node-version node) ""))))))