You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

454 lines
18 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  5. ;;; Copyright © 2015 Alex Kost <alezost@gmail.com>
  6. ;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
  7. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  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 scripts refresh)
  24. #:use-module (guix ui)
  25. #:use-module (gcrypt hash)
  26. #:use-module (guix scripts)
  27. #:use-module (guix store)
  28. #:use-module (guix utils)
  29. #:use-module (guix packages)
  30. #:use-module (guix profiles)
  31. #:use-module (guix upstream)
  32. #:use-module (guix graph)
  33. #:use-module (guix scripts graph)
  34. #:use-module (guix monads)
  35. #:use-module (guix gnupg)
  36. #:use-module (gnu packages)
  37. #:use-module ((gnu packages commencement) #:select (%final-inputs))
  38. #:use-module (ice-9 match)
  39. #:use-module (ice-9 regex)
  40. #:use-module (ice-9 vlist)
  41. #:use-module (ice-9 format)
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-11)
  44. #:use-module (srfi srfi-26)
  45. #:use-module (srfi srfi-37)
  46. #:use-module (ice-9 binary-ports)
  47. #:export (guix-refresh))
  48. ;;;
  49. ;;; Command-line options.
  50. ;;;
  51. (define %default-options
  52. ;; Alist of default option values.
  53. '())
  54. (define %options
  55. ;; Specification of the command-line options.
  56. (list (option '(#\u "update") #f #f
  57. (lambda (opt name arg result)
  58. (alist-cons 'update? #t result)))
  59. (option '(#\s "select") #t #f
  60. (lambda (opt name arg result)
  61. (match arg
  62. ((or "core" "non-core")
  63. (alist-cons 'select (string->symbol arg)
  64. result))
  65. (x
  66. (leave (G_ "~a: invalid selection; expected `core' or `non-core'~%")
  67. arg)))))
  68. (option '(#\t "type") #t #f
  69. (lambda (opt name arg result)
  70. (let* ((not-comma (char-set-complement (char-set #\,)))
  71. (names (map string->symbol
  72. (string-tokenize arg not-comma))))
  73. (alist-cons 'updaters names result))))
  74. (option '(#\L "list-updaters") #f #f
  75. (lambda args
  76. (list-updaters-and-exit)))
  77. (option '(#\m "manifest") #t #f
  78. (lambda (opt name arg result)
  79. (alist-cons 'manifest arg result)))
  80. (option '(#\e "expression") #t #f
  81. (lambda (opt name arg result)
  82. (alist-cons 'expression arg result)))
  83. (option '(#\l "list-dependent") #f #f
  84. (lambda (opt name arg result)
  85. (alist-cons 'list-dependent? #t result)))
  86. (option '("key-server") #t #f
  87. (lambda (opt name arg result)
  88. (alist-cons 'key-server arg result)))
  89. (option '("gpg") #t #f
  90. (lambda (opt name arg result)
  91. (alist-cons 'gpg-command arg result)))
  92. (option '("key-download") #t #f
  93. (lambda (opt name arg result)
  94. (match arg
  95. ((or "interactive" "always" "never")
  96. (alist-cons 'key-download (string->symbol arg)
  97. result))
  98. (x
  99. (leave (G_ "unsupported policy: ~a~%")
  100. arg)))))
  101. (option '(#\h "help") #f #f
  102. (lambda args
  103. (show-help)
  104. (exit 0)))
  105. (option '(#\V "version") #f #f
  106. (lambda args
  107. (show-version-and-exit "guix refresh")))))
  108. (define (show-help)
  109. (display (G_ "Usage: guix refresh [OPTION]... [PACKAGE]...
  110. Update package definitions to match the latest upstream version.
  111. When PACKAGE... is given, update only the specified packages. Otherwise
  112. update all the packages of the distribution, or the subset thereof
  113. specified with `--select'.\n"))
  114. (display (G_ "
  115. -e, --expression=EXPR consider the package EXPR evaluates to"))
  116. (display (G_ "
  117. -u, --update update source files in place"))
  118. (display (G_ "
  119. -s, --select=SUBSET select all the packages in SUBSET, one of
  120. `core' or `non-core'"))
  121. (display (G_ "
  122. -m, --manifest=FILE select all the packages from the manifest in FILE"))
  123. (display (G_ "
  124. -t, --type=UPDATER,... restrict to updates from the specified updaters
  125. (e.g., 'gnu')"))
  126. (display (G_ "
  127. -L, --list-updaters list available updaters and exit"))
  128. (display (G_ "
  129. -l, --list-dependent list top-level dependent packages that would need to
  130. be rebuilt as a result of upgrading PACKAGE..."))
  131. (newline)
  132. (display (G_ "
  133. --key-server=HOST use HOST as the OpenPGP key server"))
  134. (display (G_ "
  135. --gpg=COMMAND use COMMAND as the GnuPG 2.x command"))
  136. (display (G_ "
  137. --key-download=POLICY
  138. handle missing OpenPGP keys according to POLICY:
  139. 'always', 'never', and 'interactive', which is also
  140. used when 'key-download' is not specified"))
  141. (newline)
  142. (display (G_ "
  143. -h, --help display this help and exit"))
  144. (display (G_ "
  145. -V, --version display version information and exit"))
  146. (newline)
  147. (show-bug-report-information))
  148. ;;;
  149. ;;; Updates.
  150. ;;;
  151. (define (lookup-updater-by-name name)
  152. "Return the updater called NAME."
  153. (or (find (lambda (updater)
  154. (eq? name (upstream-updater-name updater)))
  155. (force %updaters))
  156. (leave (G_ "~a: no such updater~%") name)))
  157. (define (list-updaters-and-exit)
  158. "Display available updaters and exit."
  159. (format #t (G_ "Available updaters:~%"))
  160. (newline)
  161. (let* ((packages (fold-packages cons '()))
  162. (total (length packages)))
  163. (define covered
  164. (fold (lambda (updater covered)
  165. (let ((matches (count (upstream-updater-predicate updater)
  166. packages)))
  167. ;; TRANSLATORS: The parenthetical expression here is rendered
  168. ;; like "(42% coverage)" and denotes the fraction of packages
  169. ;; covered by the given updater.
  170. (format #t (G_ " - ~a: ~a (~2,1f% coverage)~%")
  171. (upstream-updater-name updater)
  172. (G_ (upstream-updater-description updater))
  173. (* 100. (/ matches total)))
  174. (+ covered matches)))
  175. 0
  176. (force %updaters)))
  177. (newline)
  178. (format #t (G_ "~2,1f% of the packages are covered by these updaters.~%")
  179. (* 100. (/ covered total))))
  180. (exit 0))
  181. (define (warn-no-updater package)
  182. (format (current-error-port)
  183. (G_ "~a: warning: no updater for ~a~%")
  184. (location->string (package-location package))
  185. (package-name package)))
  186. (define* (update-package store package updaters
  187. #:key (key-download 'interactive) warn?)
  188. "Update the source file that defines PACKAGE with the new version.
  189. KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
  190. values: 'interactive' (default), 'always', and 'never'. When WARN? is true,
  191. warn about packages that have no matching updater."
  192. (if (lookup-updater package updaters)
  193. (let-values (((version tarball)
  194. (package-update store package updaters
  195. #:key-download key-download))
  196. ((loc)
  197. (or (package-field-location package 'version)
  198. (package-location package))))
  199. (when version
  200. (if (and=> tarball file-exists?)
  201. (begin
  202. (format (current-error-port)
  203. (G_ "~a: ~a: updating from version ~a to version ~a...~%")
  204. (location->string loc)
  205. (package-name package)
  206. (package-version package) version)
  207. (let ((hash (call-with-input-file tarball
  208. port-sha256)))
  209. (update-package-source package version hash)))
  210. (warning (G_ "~a: version ~a could not be \
  211. downloaded and authenticated; not updating~%")
  212. (package-name package) version))))
  213. (when warn?
  214. (warn-no-updater package))))
  215. (define* (check-for-package-update package updaters #:key warn?)
  216. "Check whether an update is available for PACKAGE and print a message. When
  217. WARN? is true and no updater exists for PACKAGE, print a warning."
  218. (match (package-latest-release package updaters)
  219. ((? upstream-source? source)
  220. (let ((loc (or (package-field-location package 'version)
  221. (package-location package))))
  222. (case (version-compare (upstream-source-version source)
  223. (package-version package))
  224. ((>)
  225. (format (current-error-port)
  226. (G_ "~a: ~a would be upgraded from ~a to ~a~%")
  227. (location->string loc)
  228. (package-name package) (package-version package)
  229. (upstream-source-version source)))
  230. ((=)
  231. (when warn?
  232. (format (current-error-port)
  233. (G_ "~a: info: ~a is already the latest version of ~a~%")
  234. (location->string loc)
  235. (package-version package)
  236. (package-name package))))
  237. (else
  238. (when warn?
  239. (format (current-error-port)
  240. (G_ "~a: warning: ~a is greater than \
  241. the latest known version of ~a (~a)~%")
  242. (location->string loc)
  243. (package-version package)
  244. (package-name package)
  245. (upstream-source-version source)))))))
  246. (#f
  247. (when warn?
  248. (warn-no-updater package)))))
  249. ;;;
  250. ;;; Dependents.
  251. ;;;
  252. (define (all-packages)
  253. "Return the list of all the distro's packages."
  254. (fold-packages cons '()
  255. #:select? (const #t))) ;include hidden packages
  256. (define (list-dependents packages)
  257. "List all the things that would need to be rebuilt if PACKAGES are changed."
  258. ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
  259. ;; because it includes implicit dependencies.
  260. (define (full-name package)
  261. (string-append (package-name package) "@"
  262. (package-version package)))
  263. (mlet %store-monad ((edges (node-back-edges %bag-node-type
  264. (all-packages))))
  265. (let* ((dependents (node-transitive-edges packages edges))
  266. (covering (filter (lambda (node)
  267. (null? (edges node)))
  268. dependents)))
  269. (match dependents
  270. (()
  271. (format (current-output-port)
  272. (N_ "No dependents other than itself: ~{~a~}~%"
  273. "No dependents other than themselves: ~{~a~^ ~}~%"
  274. (length packages))
  275. (map full-name packages)))
  276. ((x)
  277. (format (current-output-port)
  278. (G_ "A single dependent package: ~a~%")
  279. (full-name x)))
  280. (lst
  281. (format (current-output-port)
  282. (N_ "Building the following package would ensure ~d \
  283. dependent packages are rebuilt: ~*~{~a~^ ~}~%"
  284. "Building the following ~d packages would ensure ~d \
  285. dependent packages are rebuilt: ~{~a~^ ~}~%"
  286. (length covering))
  287. (length covering) (length dependents)
  288. (map full-name covering))))
  289. (return #t))))
  290. ;;;
  291. ;;; Manifest.
  292. ;;;
  293. (define (manifest->packages manifest)
  294. "Return the list of packages in MANIFEST."
  295. (filter-map (lambda (entry)
  296. (let ((item (manifest-entry-item entry)))
  297. (if (package? item) item #f)))
  298. (manifest-entries manifest)))
  299. (define (packages-from-manifest manifest)
  300. "Return the list of packages in loaded MANIFEST."
  301. (let* ((user-module (make-user-module '((guix profiles) (gnu))))
  302. (manifest (load* manifest user-module)))
  303. (manifest->packages manifest)))
  304. ;;;
  305. ;;; Entry point.
  306. ;;;
  307. (define (guix-refresh . args)
  308. (define (parse-options)
  309. ;; Return the alist of option values.
  310. (parse-command-line args %options (list %default-options)
  311. #:build-options? #f))
  312. (define (options->updaters opts)
  313. ;; Return the list of updaters to use.
  314. (match (filter-map (match-lambda
  315. (('updaters . names)
  316. (map lookup-updater-by-name names))
  317. (_ #f))
  318. opts)
  319. (()
  320. ;; Use the default updaters.
  321. (force %updaters))
  322. (lists
  323. (concatenate lists))))
  324. (define (keep-newest package lst)
  325. ;; If a newer version of PACKAGE is already in LST, return LST; otherwise
  326. ;; return LST minus the other version of PACKAGE in it, plus PACKAGE.
  327. (let ((name (package-name package)))
  328. (match (find (lambda (p)
  329. (string=? (package-name p) name))
  330. lst)
  331. ((? package? other)
  332. (if (version>? (package-version other) (package-version package))
  333. lst
  334. (cons package (delq other lst))))
  335. (_
  336. (cons package lst)))))
  337. (define core-package?
  338. (let* ((input->package (match-lambda
  339. ((name (? package? package) _ ...) package)
  340. (_ #f)))
  341. (final-inputs (map input->package %final-inputs))
  342. (core (append final-inputs
  343. (append-map (compose (cut filter-map input->package <>)
  344. package-transitive-inputs)
  345. final-inputs)))
  346. (names (delete-duplicates (map package-name core))))
  347. (lambda (package)
  348. "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
  349. update would trigger a complete rebuild."
  350. ;; Compare by name because packages in base.scm basically inherit
  351. ;; other packages. So, even if those packages are not core packages
  352. ;; themselves, updating them would also update those who inherit from
  353. ;; them.
  354. ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
  355. (member (package-name package) names))))
  356. (let* ((opts (parse-options))
  357. (update? (assoc-ref opts 'update?))
  358. (updaters (options->updaters opts))
  359. (list-dependent? (assoc-ref opts 'list-dependent?))
  360. (key-download (assoc-ref opts 'key-download))
  361. ;; Warn about missing updaters when a package is explicitly given on
  362. ;; the command line.
  363. (warn? (or (assoc-ref opts 'argument)
  364. (assoc-ref opts 'expression)))
  365. (args-packages
  366. (match (filter-map (match-lambda
  367. (('argument . spec)
  368. ;; Take either the specified version or the
  369. ;; latest one.
  370. (specification->package spec))
  371. (('expression . exp)
  372. (read/eval-package-expression exp))
  373. (_ #f))
  374. opts)
  375. (() ; default to all packages
  376. (let ((select? (match (assoc-ref opts 'select)
  377. ('core core-package?)
  378. ('non-core (negate core-package?))
  379. (_ (const #t)))))
  380. (fold-packages (lambda (package result)
  381. (if (select? package)
  382. (keep-newest package result)
  383. result))
  384. '())))
  385. (some ; user-specified packages
  386. some)))
  387. (packages
  388. (match (assoc-ref opts 'manifest)
  389. (#f args-packages)
  390. ((? string? file) (packages-from-manifest file)))))
  391. (with-error-handling
  392. (with-store store
  393. (run-with-store store
  394. (cond
  395. (list-dependent?
  396. (list-dependents packages))
  397. (update?
  398. (parameterize ((%openpgp-key-server
  399. (or (assoc-ref opts 'key-server)
  400. (%openpgp-key-server)))
  401. (%gpg-command
  402. (or (assoc-ref opts 'gpg-command)
  403. (%gpg-command))))
  404. (for-each
  405. (cut update-package store <> updaters
  406. #:key-download key-download
  407. #:warn? warn?)
  408. packages)
  409. (with-monad %store-monad
  410. (return #t))))
  411. (else
  412. (for-each (cut check-for-package-update <> updaters
  413. #:warn? warn?)
  414. packages)
  415. (with-monad %store-monad
  416. (return #t)))))))))