utils.scm 19 KB

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