api-diff.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; api-diff --- diff guile-api.alist files
  2. ;; Copyright (C) 2002, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
  19. ;;; Commentary:
  20. ;; Usage: api-diff [-d GROUPS] ALIST-FILE-A ALIST-FILE-B
  21. ;;
  22. ;; Read in the alists from files ALIST-FILE-A and ALIST-FILE-B
  23. ;; and display a (count) summary of the groups defined therein.
  24. ;; Optional arg "--details" (or "-d") specifies a comma-separated
  25. ;; list of groups, in which case api-diff displays instead the
  26. ;; elements added and deleted for each of the specified groups.
  27. ;;
  28. ;; For scheme programming, this module exports the proc:
  29. ;; (api-diff A-file B-file)
  30. ;;
  31. ;; Note that the convention is that the "older" alist/file is
  32. ;; specified first.
  33. ;;
  34. ;; TODO: Develop scheme interface.
  35. ;;; Code:
  36. (define-module (scripts api-diff)
  37. :use-module (ice-9 common-list)
  38. :use-module (ice-9 format)
  39. :use-module (ice-9 getopt-long)
  40. :autoload (srfi srfi-13) (string-tokenize)
  41. :export (api-diff))
  42. (define (read-alist-file file)
  43. (with-input-from-file file
  44. (lambda () (read))))
  45. (define put set-object-property!)
  46. (define get object-property)
  47. (define (read-api-alist-file file)
  48. (let* ((alist (read-alist-file file))
  49. (meta (assq-ref alist 'meta))
  50. (interface (assq-ref alist 'interface)))
  51. (put interface 'meta meta)
  52. (put interface 'groups (let ((ht (make-hash-table 31)))
  53. (for-each (lambda (group)
  54. (hashq-set! ht group '()))
  55. (assq-ref meta 'groups))
  56. ht))
  57. interface))
  58. (define (hang-by-the-roots interface)
  59. (let ((ht (get interface 'groups)))
  60. (for-each (lambda (x)
  61. (for-each (lambda (group)
  62. (hashq-set! ht group
  63. (cons (car x)
  64. (hashq-ref ht group))))
  65. (assq-ref x 'groups)))
  66. interface))
  67. interface)
  68. (define (diff? a b)
  69. (let ((result (set-difference a b)))
  70. (if (null? result)
  71. #f ; CL weenies bite me
  72. result)))
  73. (define (diff+note! a b note-removals note-additions note-same)
  74. (let ((same? #t))
  75. (cond ((diff? a b) => (lambda (x) (note-removals x) (set! same? #f))))
  76. (cond ((diff? b a) => (lambda (x) (note-additions x) (set! same? #f))))
  77. (and same? (note-same))))
  78. (define (group-diff i-old i-new . options)
  79. (let* ((i-old (hang-by-the-roots i-old))
  80. (g-old (hash-fold acons '() (get i-old 'groups)))
  81. (g-old-names (map car g-old))
  82. (i-new (hang-by-the-roots i-new))
  83. (g-new (hash-fold acons '() (get i-new 'groups)))
  84. (g-new-names (map car g-new)))
  85. (cond ((null? options)
  86. (diff+note! g-old-names g-new-names
  87. (lambda (removals)
  88. (format #t "groups-removed: ~A\n" removals))
  89. (lambda (additions)
  90. (format #t "groups-added: ~A\n" additions))
  91. (lambda () #t))
  92. (for-each (lambda (group)
  93. (let* ((old (assq-ref g-old group))
  94. (new (assq-ref g-new group))
  95. (old-count (and old (length old)))
  96. (new-count (and new (length new)))
  97. (delta (and old new (- new-count old-count))))
  98. (format #t " ~5@A ~5@A : "
  99. (or old-count "-")
  100. (or new-count "-"))
  101. (cond ((and old new)
  102. (let ((add-count 0) (sub-count 0))
  103. (diff+note!
  104. old new
  105. (lambda (subs)
  106. (set! sub-count (length subs)))
  107. (lambda (adds)
  108. (set! add-count (length adds)))
  109. (lambda () #t))
  110. (format #t "~5@D ~5@D : ~5@D"
  111. add-count (- sub-count) delta)))
  112. (else
  113. (format #t "~5@A ~5@A : ~5@A" "-" "-" "-")))
  114. (format #t " ~A\n" group)))
  115. (sort (union g-old-names g-new-names)
  116. (lambda (a b)
  117. (string<? (symbol->string a)
  118. (symbol->string b))))))
  119. ((assq-ref options 'details)
  120. => (lambda (groups)
  121. (for-each (lambda (group)
  122. (let* ((old (or (assq-ref g-old group) '()))
  123. (new (or (assq-ref g-new group) '()))
  124. (>>! (lambda (label ls)
  125. (format #t "~A ~A:\n" group label)
  126. (for-each (lambda (x)
  127. (format #t " ~A\n" x))
  128. ls))))
  129. (diff+note! old new
  130. (lambda (removals)
  131. (>>! 'removals removals))
  132. (lambda (additions)
  133. (>>! 'additions additions))
  134. (lambda ()
  135. (format #t "~A: no changes\n"
  136. group)))))
  137. groups)))
  138. (else
  139. (error "api-diff: group-diff: bad options")))))
  140. (define (api-diff . args)
  141. (let* ((p (getopt-long (cons 'api-diff args)
  142. '((details (single-char #\d)
  143. (value #t))
  144. ;; Add options here.
  145. )))
  146. (rest (option-ref p '() '("/dev/null" "/dev/null")))
  147. (i-old (read-api-alist-file (car rest)))
  148. (i-new (read-api-alist-file (cadr rest)))
  149. (options '()))
  150. (cond ((option-ref p 'details #f)
  151. => (lambda (groups)
  152. (set! options (cons (cons 'details
  153. (map string->symbol
  154. (string-tokenize
  155. groups
  156. #\,)))
  157. options)))))
  158. (apply group-diff i-old i-new options)))
  159. (define main api-diff)
  160. ;;; api-diff ends here