update-NEWS.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;;
  20. ;;; This script updates the list of new and updated packages in 'NEWS'.
  21. ;;;
  22. ;;; Code:
  23. (use-modules (gnu) (guix)
  24. (guix build utils)
  25. ((guix ui) #:select (fill-paragraph))
  26. (srfi srfi-1)
  27. (srfi srfi-11)
  28. (ice-9 match)
  29. (ice-9 rdelim)
  30. (ice-9 regex)
  31. (ice-9 pretty-print))
  32. (define %header-rx
  33. (make-regexp "^\\* Changes in (version )?([0-9.]+) \\(since ([0-9.]+)\\)"))
  34. (define (NEWS->versions port)
  35. "Return two values: the previous version and the current version as read
  36. from PORT, which is an input port on the 'NEWS' file."
  37. (let loop ()
  38. (let ((line (read-line port)))
  39. (cond ((eof-object? line)
  40. (error "failed to determine previous and current version"
  41. port))
  42. ((regexp-exec %header-rx line)
  43. =>
  44. (lambda (match)
  45. (values (match:substring match 3)
  46. (match:substring match 2))))
  47. (else
  48. (loop))))))
  49. (define (skip-to-org-heading port)
  50. "Read from PORT until an Org heading is found."
  51. (let loop ()
  52. (let ((next (peek-char port)))
  53. (cond ((eqv? next #\*)
  54. #t)
  55. ((eof-object? next)
  56. (error "next heading could not be found"))
  57. (else
  58. (read-line port)
  59. (loop))))))
  60. (define (rewrite-org-section input output heading-rx proc)
  61. "Write to OUTPUT the text read from INPUT, but with the first Org section
  62. matching HEADING-RX replaced by NEW-HEADING and CONTENTS."
  63. (let loop ()
  64. (let ((line (read-line input)))
  65. (cond ((eof-object? line)
  66. (error "failed to match heading regexp" heading-rx))
  67. ((regexp-exec heading-rx line)
  68. =>
  69. (lambda (match)
  70. (proc match output)
  71. (skip-to-org-heading input)
  72. (dump-port input output)
  73. #t))
  74. (else
  75. (display line output)
  76. (newline output)
  77. (loop))))))
  78. (define (enumeration->paragraph lst)
  79. "Turn LST, a list of strings, into a single string that is a ready-to-print
  80. paragraph."
  81. (fill-paragraph (string-join (sort lst string<?) ", ")
  82. 75))
  83. (define (write-packages-added news-file old new)
  84. "Write to NEWS-FILE the list of packages added between OLD and NEW."
  85. (let ((added (lset-difference string=? (map car new) (map car old))))
  86. (with-atomic-file-replacement news-file
  87. (lambda (input output)
  88. (rewrite-org-section input output
  89. (make-regexp "^(\\*+) (.*) new packages")
  90. (lambda (match port)
  91. (let ((stars (match:substring match 1)))
  92. (format port
  93. "~a ~a new packages~%~%~a~%~%"
  94. stars (length added)
  95. (enumeration->paragraph added)))))))))
  96. (define (write-packages-updates news-file old new)
  97. "Write to NEWS-FILE the list of packages upgraded between OLD and NEW."
  98. (let ((upgraded (filter-map (match-lambda
  99. ((package . new-version)
  100. (match (assoc package old)
  101. ((_ . old-version)
  102. (and (version>? new-version old-version)
  103. (string-append package "@"
  104. new-version)))
  105. (_ #f))))
  106. new)))
  107. (with-atomic-file-replacement news-file
  108. (lambda (input output)
  109. (rewrite-org-section input output
  110. (make-regexp "^(\\*+) (.*) package updates")
  111. (lambda (match port)
  112. (let ((stars (match:substring match 1)))
  113. (format port
  114. "~a ~a package updates~%~%~a~%~%"
  115. stars (length upgraded)
  116. (enumeration->paragraph upgraded)))))))))
  117. (define (main . args)
  118. (match args
  119. ((news-file data-directory)
  120. ;; Don't browse things listed in the user's $GUIX_PACKAGE_PATH. Here we
  121. ;; assume that the last item in (%package-module-path) is the distro
  122. ;; directory.
  123. (parameterize ((%package-module-path
  124. (list (last (%package-module-path)))))
  125. (define (package-file version)
  126. (string-append data-directory "/packages-"
  127. version ".txt"))
  128. (let-values (((previous-version new-version)
  129. (call-with-input-file news-file NEWS->versions)))
  130. (let* ((old (call-with-input-file (package-file previous-version)
  131. read))
  132. (new (fold-packages (lambda (p r)
  133. (alist-cons (package-name p) (package-version p)
  134. r))
  135. '())))
  136. (call-with-output-file (package-file new-version)
  137. (lambda (port)
  138. (pretty-print new port)))
  139. (write-packages-added news-file old new)
  140. (write-packages-updates news-file old new)))))
  141. (x
  142. (format (current-error-port) "Usage: update-NEWS NEWS-FILE DATA-DIRECTORY
  143. Update the list of new and updated packages in NEWS-FILE using the
  144. previous-version package list from DATA-DIRECTORY.\n")
  145. (exit 1))))
  146. (apply main (cdr (command-line)))