utils.scm 19 KB

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