refresh.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. ;;; Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
  9. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  10. ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@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 scripts refresh)
  27. #:use-module (guix ui)
  28. #:use-module (gcrypt hash)
  29. #:use-module (guix scripts)
  30. #:use-module ((guix scripts build) #:select (%standard-build-options))
  31. #:use-module (guix store)
  32. #:use-module (guix utils)
  33. #:use-module (guix packages)
  34. #:use-module (guix profiles)
  35. #:use-module (guix upstream)
  36. #:use-module (guix graph)
  37. #:use-module (guix scripts graph)
  38. #:use-module (guix monads)
  39. #:use-module (guix gnupg)
  40. #:use-module (gnu packages)
  41. #:use-module ((gnu packages commencement) #:select (%final-inputs))
  42. #:use-module (ice-9 match)
  43. #:use-module (ice-9 regex)
  44. #:use-module (ice-9 vlist)
  45. #:use-module (ice-9 format)
  46. #:use-module (srfi srfi-1)
  47. #:use-module (srfi srfi-11)
  48. #:use-module (srfi srfi-26)
  49. #:use-module (srfi srfi-37)
  50. #:use-module (ice-9 binary-ports)
  51. #:export (guix-refresh))
  52. ;;;
  53. ;;; Command-line options.
  54. ;;;
  55. (define %default-options
  56. ;; Alist of default option values.
  57. '())
  58. (define %options
  59. ;; Specification of the command-line options.
  60. (list (option '(#\u "update") #f #f
  61. (lambda (opt name arg result)
  62. (alist-cons 'update? #t result)))
  63. (option '(#\s "select") #t #f
  64. (lambda (opt name arg result)
  65. (match arg
  66. ((or "core" "non-core")
  67. (alist-cons 'select (string->symbol arg)
  68. result))
  69. (x
  70. (leave (G_ "~a: invalid selection; expected `core' or `non-core'~%")
  71. arg)))))
  72. (option '(#\t "type") #t #f
  73. (lambda (opt name arg result)
  74. (let* ((not-comma (char-set-complement (char-set #\,)))
  75. (names (map string->symbol
  76. (string-tokenize arg not-comma))))
  77. (alist-cons 'updaters names result))))
  78. (option '(#\L "list-updaters") #f #f
  79. (lambda args
  80. (list-updaters-and-exit)))
  81. (option '(#\m "manifest") #t #f
  82. (lambda (opt name arg result)
  83. (alist-cons 'manifest arg result)))
  84. (option '(#\e "expression") #t #f
  85. (lambda (opt name arg result)
  86. (alist-cons 'expression arg result)))
  87. (option '(#\l "list-dependent") #f #f
  88. (lambda (opt name arg result)
  89. (alist-cons 'list-dependent? #t result)))
  90. (option '(#\r "recursive") #f #f
  91. (lambda (opt name arg result)
  92. (alist-cons 'recursive? #t result)))
  93. (option '("list-transitive") #f #f
  94. (lambda (opt name arg result)
  95. (alist-cons 'list-transitive? #t result)))
  96. (option '("keyring") #t #f
  97. (lambda (opt name arg result)
  98. (alist-cons 'keyring arg result)))
  99. (option '("key-server") #t #f
  100. (lambda (opt name arg result)
  101. (alist-cons 'key-server arg result)))
  102. (option '("gpg") #t #f
  103. (lambda (opt name arg result)
  104. (alist-cons 'gpg-command arg result)))
  105. (option '("key-download") #t #f
  106. (lambda (opt name arg result)
  107. (match arg
  108. ((or "interactive" "always" "never")
  109. (alist-cons 'key-download (string->symbol arg)
  110. result))
  111. (x
  112. (leave (G_ "unsupported policy: ~a~%")
  113. arg)))))
  114. ;; The short option -L is already used by --list-updaters, therefore
  115. ;; it needs to be removed from %standard-build-options.
  116. (let ((load-path-option (find (lambda (option)
  117. (member "load-path"
  118. (option-names option)))
  119. %standard-build-options)))
  120. (option
  121. (filter (lambda (name) (not (equal? #\L name)))
  122. (option-names load-path-option))
  123. (option-required-arg? load-path-option)
  124. (option-optional-arg? load-path-option)
  125. (option-processor load-path-option)))
  126. (option '(#\h "help") #f #f
  127. (lambda args
  128. (show-help)
  129. (exit 0)))
  130. (option '(#\V "version") #f #f
  131. (lambda args
  132. (show-version-and-exit "guix refresh")))))
  133. (define (show-help)
  134. (display (G_ "Usage: guix refresh [OPTION]... [PACKAGE]...
  135. Update package definitions to match the latest upstream version.
  136. When PACKAGE... is given, update only the specified packages. Otherwise
  137. update all the packages of the distribution, or the subset thereof
  138. specified with `--select'.\n"))
  139. (display (G_ "
  140. -e, --expression=EXPR consider the package EXPR evaluates to"))
  141. (display (G_ "
  142. -u, --update update source files in place"))
  143. (display (G_ "
  144. -s, --select=SUBSET select all the packages in SUBSET, one of
  145. `core' or `non-core'"))
  146. (display (G_ "
  147. -m, --manifest=FILE select all the packages from the manifest in FILE"))
  148. (display (G_ "
  149. -t, --type=UPDATER,... restrict to updates from the specified updaters
  150. (e.g., 'gnu')"))
  151. (display (G_ "
  152. -L, --list-updaters list available updaters and exit"))
  153. (display (G_ "
  154. -l, --list-dependent list top-level dependent packages that would need to
  155. be rebuilt as a result of upgrading PACKAGE..."))
  156. (display (G_ "
  157. -r, --recursive check the PACKAGE and its inputs for upgrades"))
  158. (display (G_ "
  159. --list-transitive list all the packages that PACKAGE depends on"))
  160. (newline)
  161. (display (G_ "
  162. --keyring=FILE use FILE as the keyring of upstream OpenPGP keys"))
  163. (display (G_ "
  164. --key-server=HOST use HOST as the OpenPGP key server"))
  165. (display (G_ "
  166. --gpg=COMMAND use COMMAND as the GnuPG 2.x command"))
  167. (display (G_ "
  168. --key-download=POLICY
  169. handle missing OpenPGP keys according to POLICY:
  170. 'always', 'never', and 'interactive', which is also
  171. used when 'key-download' is not specified"))
  172. (newline)
  173. (display (G_ "
  174. --load-path=DIR prepend DIR to the package module search path"))
  175. (newline)
  176. (display (G_ "
  177. -h, --help display this help and exit"))
  178. (display (G_ "
  179. -V, --version display version information and exit"))
  180. (newline)
  181. (show-bug-report-information))
  182. (define (options->packages opts)
  183. "Return the list of packages requested by OPTS, honoring options like
  184. '--recursive'."
  185. (define core-package?
  186. (let* ((input->package (match-lambda
  187. ((name (? package? package) _ ...) package)
  188. (_ #f)))
  189. (final-inputs (map input->package %final-inputs))
  190. (core (append final-inputs
  191. (append-map (compose (cut filter-map input->package <>)
  192. package-transitive-inputs)
  193. final-inputs)))
  194. (names (delete-duplicates (map package-name core))))
  195. (lambda (package)
  196. "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
  197. update would trigger a complete rebuild."
  198. ;; Compare by name because packages in base.scm basically inherit
  199. ;; other packages. So, even if those packages are not core packages
  200. ;; themselves, updating them would also update those who inherit from
  201. ;; them.
  202. ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
  203. (member (package-name package) names))))
  204. (define (keep-newest package lst)
  205. ;; If a newer version of PACKAGE is already in LST, return LST; otherwise
  206. ;; return LST minus the other version of PACKAGE in it, plus PACKAGE.
  207. (let ((name (package-name package)))
  208. (match (find (lambda (p)
  209. (string=? (package-name p) name))
  210. lst)
  211. ((? package? other)
  212. (if (version>? (package-version other) (package-version package))
  213. lst
  214. (cons package (delq other lst))))
  215. (_
  216. (cons package lst)))))
  217. (define args-packages
  218. ;; Packages explicitly passed as command-line arguments.
  219. (match (filter-map (match-lambda
  220. (('argument . spec)
  221. ;; Take either the specified version or the
  222. ;; latest one.
  223. (specification->package spec))
  224. (('expression . exp)
  225. (read/eval-package-expression exp))
  226. (_ #f))
  227. opts)
  228. (() ;default to all packages
  229. (let ((select? (match (assoc-ref opts 'select)
  230. ('core core-package?)
  231. ('non-core (negate core-package?))
  232. (_ (const #t)))))
  233. (fold-packages (lambda (package result)
  234. (if (select? package)
  235. (keep-newest package result)
  236. result))
  237. '())))
  238. (some ;user-specified packages
  239. some)))
  240. (define packages
  241. (match (assoc-ref opts 'manifest)
  242. (#f args-packages)
  243. ((? string? file) (packages-from-manifest file))))
  244. (if (assoc-ref opts 'recursive?)
  245. (mlet %store-monad ((edges (node-edges %bag-node-type
  246. (all-packages))))
  247. (return (node-transitive-edges packages edges)))
  248. (with-monad %store-monad
  249. (return packages))))
  250. ;;;
  251. ;;; Updates.
  252. ;;;
  253. (define (lookup-updater-by-name name)
  254. "Return the updater called NAME."
  255. (or (find (lambda (updater)
  256. (eq? name (upstream-updater-name updater)))
  257. (force %updaters))
  258. (leave (G_ "~a: no such updater~%") name)))
  259. (define (list-updaters-and-exit)
  260. "Display available updaters and exit."
  261. (format #t (G_ "Available updaters:~%"))
  262. (newline)
  263. (let* ((packages (fold-packages cons '()))
  264. (total (length packages)))
  265. (define uncovered
  266. (fold (lambda (updater uncovered)
  267. (let ((matches (filter (upstream-updater-predicate updater)
  268. packages)))
  269. ;; TRANSLATORS: The parenthetical expression here is rendered
  270. ;; like "(42% coverage)" and denotes the fraction of packages
  271. ;; covered by the given updater.
  272. (format #t (G_ " - ~a: ~a (~2,1f% coverage)~%")
  273. (upstream-updater-name updater)
  274. (G_ (upstream-updater-description updater))
  275. (* 100. (/ (length matches) total)))
  276. (lset-difference eq? uncovered matches)))
  277. packages
  278. (force %updaters)))
  279. (newline)
  280. (format #t (G_ "~2,1f% of the packages are covered by these updaters.~%")
  281. (* 100. (/ (- total (length uncovered)) total))))
  282. (exit 0))
  283. (define (warn-no-updater package)
  284. (warning (package-location package)
  285. (G_ "no updater for ~a~%")
  286. (package-name package)))
  287. (define* (update-package store package updaters
  288. #:key (key-download 'interactive) warn?)
  289. "Update the source file that defines PACKAGE with the new version.
  290. KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
  291. values: 'interactive' (default), 'always', and 'never'. When WARN? is true,
  292. warn about packages that have no matching updater."
  293. (if (lookup-updater package updaters)
  294. (let-values (((version tarball source)
  295. (package-update store package updaters
  296. #:key-download key-download))
  297. ((loc)
  298. (or (package-field-location package 'version)
  299. (package-location package))))
  300. (when version
  301. (if (and=> tarball file-exists?)
  302. (begin
  303. (info loc
  304. (G_ "~a: updating from version ~a to version ~a...~%")
  305. (package-name package)
  306. (package-version package) version)
  307. (for-each
  308. (lambda (change)
  309. (format (current-error-port)
  310. (match (list (upstream-input-change-action change)
  311. (upstream-input-change-type change))
  312. (('add 'regular)
  313. (G_ "~a: consider adding this input: ~a~%"))
  314. (('add 'native)
  315. (G_ "~a: consider adding this native input: ~a~%"))
  316. (('add 'propagated)
  317. (G_ "~a: consider adding this propagated input: ~a~%"))
  318. (('remove 'regular)
  319. (G_ "~a: consider removing this input: ~a~%"))
  320. (('remove 'native)
  321. (G_ "~a: consider removing this native input: ~a~%"))
  322. (('remove 'propagated)
  323. (G_ "~a: consider removing this propagated input: ~a~%")))
  324. (package-name package)
  325. (upstream-input-change-name change)))
  326. (upstream-source-input-changes source))
  327. (let ((hash (call-with-input-file tarball
  328. port-sha256)))
  329. (update-package-source package source hash)))
  330. (warning (G_ "~a: version ~a could not be \
  331. downloaded and authenticated; not updating~%")
  332. (package-name package) version))))
  333. (when warn?
  334. (warn-no-updater package))))
  335. (define* (check-for-package-update package updaters #:key warn?)
  336. "Check whether an update is available for PACKAGE and print a message. When
  337. WARN? is true and no updater exists for PACKAGE, print a warning."
  338. (match (package-latest-release package updaters)
  339. ((? upstream-source? source)
  340. (let ((loc (or (package-field-location package 'version)
  341. (package-location package))))
  342. (case (version-compare (upstream-source-version source)
  343. (package-version package))
  344. ((>)
  345. (info loc
  346. (G_ "~a would be upgraded from ~a to ~a~%")
  347. (package-name package) (package-version package)
  348. (upstream-source-version source)))
  349. ((=)
  350. (when warn?
  351. (info loc
  352. (G_ "~a is already the latest version of ~a~%")
  353. (package-version package)
  354. (package-name package))))
  355. (else
  356. (when warn?
  357. (warning loc
  358. (G_ "~a is greater than \
  359. the latest known version of ~a (~a)~%")
  360. (package-version package)
  361. (package-name package)
  362. (upstream-source-version source)))))))
  363. (#f
  364. (when warn?
  365. ;; Distinguish between "no updater" and "failing updater."
  366. (match (lookup-updater package updaters)
  367. ((? upstream-updater? updater)
  368. (warning (package-location package)
  369. (G_ "'~a' updater failed to determine available \
  370. releases for ~a~%")
  371. (upstream-updater-name updater)
  372. (package-name package)))
  373. (#f
  374. (warn-no-updater package)))))))
  375. ;;;
  376. ;;; Dependents.
  377. ;;;
  378. (define (all-packages)
  379. "Return the list of all the distro's packages."
  380. (fold-packages (lambda (package result)
  381. ;; Ignore deprecated packages.
  382. (if (package-superseded package)
  383. result
  384. (cons package result)))
  385. '()
  386. #:select? (const #t))) ;include hidden packages
  387. (define (list-dependents packages)
  388. "List all the things that would need to be rebuilt if PACKAGES are changed."
  389. ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
  390. ;; because it includes implicit dependencies.
  391. (define (full-name package)
  392. (string-append (package-name package) "@"
  393. (package-version package)))
  394. (mlet %store-monad ((edges (node-back-edges %bag-node-type
  395. (package-closure (all-packages)))))
  396. (let* ((dependents (node-transitive-edges packages edges))
  397. (covering (filter (lambda (node)
  398. (null? (edges node)))
  399. dependents)))
  400. (match dependents
  401. (()
  402. (format (current-output-port)
  403. (N_ "No dependents other than itself: ~{~a~}~%"
  404. "No dependents other than themselves: ~{~a~^ ~}~%"
  405. (length packages))
  406. (map full-name packages)))
  407. ((x)
  408. (format (current-output-port)
  409. (G_ "A single dependent package: ~a~%")
  410. (full-name x)))
  411. (lst
  412. (format (current-output-port)
  413. (N_ "Building the following ~d package would ensure ~d \
  414. dependent packages are rebuilt: ~{~a~^ ~}~%"
  415. "Building the following ~d packages would ensure ~d \
  416. dependent packages are rebuilt: ~{~a~^ ~}~%"
  417. (length covering))
  418. (length covering) (length dependents)
  419. (map full-name covering))))
  420. (return #t))))
  421. (define (list-transitive packages)
  422. "List all the packages that would cause PACKAGES to be rebuilt if they are changed."
  423. ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
  424. ;; because it includes implicit dependencies.
  425. (define (full-name package)
  426. (string-append (package-name package) "@"
  427. (package-version package)))
  428. (mlet %store-monad ((edges (node-edges %bag-node-type
  429. ;; Here we don't want the -boot0 packages.
  430. (fold-packages cons '()))))
  431. (let ((dependent (node-transitive-edges packages edges)))
  432. (match packages
  433. ((x)
  434. (format (current-output-port)
  435. (G_ "~a depends on the following ~d packages: ~{~a~^ ~}~%.")
  436. (full-name x) (length dependent) (map full-name dependent)))
  437. (lst
  438. (format (current-output-port)
  439. (G_ "The following ~d packages \
  440. all are dependent packages: ~{~a~^ ~}~%")
  441. (length dependent) (map full-name dependent))))
  442. (return #t))))
  443. ;;;
  444. ;;; Manifest.
  445. ;;;
  446. (define (manifest->packages manifest)
  447. "Return the list of packages in MANIFEST."
  448. (filter-map (lambda (entry)
  449. (let ((item (manifest-entry-item entry)))
  450. (if (package? item) item #f)))
  451. (manifest-entries manifest)))
  452. (define (packages-from-manifest manifest)
  453. "Return the list of packages in loaded MANIFEST."
  454. (let* ((user-module (make-user-module '((guix profiles) (gnu))))
  455. (manifest (load* manifest user-module)))
  456. (manifest->packages manifest)))
  457. ;;;
  458. ;;; Entry point.
  459. ;;;
  460. (define-command (guix-refresh . args)
  461. (category packaging)
  462. (synopsis "update existing package definitions")
  463. (define (parse-options)
  464. ;; Return the alist of option values.
  465. (parse-command-line args %options (list %default-options)
  466. #:build-options? #f))
  467. (define (options->updaters opts)
  468. ;; Return the list of updaters to use.
  469. (match (filter-map (match-lambda
  470. (('updaters . names)
  471. (map lookup-updater-by-name names))
  472. (_ #f))
  473. opts)
  474. (()
  475. ;; Use the default updaters.
  476. (force %updaters))
  477. (lists
  478. (concatenate lists))))
  479. (let* ((opts (parse-options))
  480. (update? (assoc-ref opts 'update?))
  481. (updaters (options->updaters opts))
  482. (recursive? (assoc-ref opts 'recursive?))
  483. (list-dependent? (assoc-ref opts 'list-dependent?))
  484. (list-transitive? (assoc-ref opts 'list-transitive?))
  485. (key-download (assoc-ref opts 'key-download))
  486. ;; Warn about missing updaters when a package is explicitly given on
  487. ;; the command line.
  488. (warn? (and (or (assoc-ref opts 'argument)
  489. (assoc-ref opts 'expression)
  490. (assoc-ref opts 'manifest))
  491. (not recursive?))))
  492. (with-error-handling
  493. (with-store store
  494. (run-with-store store
  495. (mlet %store-monad ((packages (options->packages opts)))
  496. (cond
  497. (list-dependent?
  498. (list-dependents packages))
  499. (list-transitive?
  500. (list-transitive packages))
  501. (update?
  502. (parameterize ((%openpgp-key-server
  503. (or (assoc-ref opts 'key-server)
  504. (%openpgp-key-server)))
  505. (%gpg-command
  506. (or (assoc-ref opts 'gpg-command)
  507. (%gpg-command)))
  508. (current-keyring
  509. (or (assoc-ref opts 'keyring)
  510. (string-append (config-directory)
  511. "/upstream/trustedkeys.kbx"))))
  512. (for-each
  513. (cut update-package store <> updaters
  514. #:key-download key-download
  515. #:warn? warn?)
  516. packages)
  517. (return #t)))
  518. (else
  519. (for-each (cut check-for-package-update <> updaters
  520. #:warn? warn?)
  521. packages)
  522. (return #t)))))))))