api-diff 7.6 KB

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