utils.scm 17 KB

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