source: project/release/4/salmonella/trunk/salmonella @ 15189

Last change on this file since 15189 was 15189, checked in by Ivan Raikov, 11 years ago

improved salmonella documentation

File size: 16.1 KB
Line 
1#!/bin/sh
2#| -*- scheme -*-
3exec csi -s $0 "$@"
4|#
5
6(use setup-download utils posix (srfi 1 13))
7
8(define major-version     4)
9(define chicken-install   (make-parameter "chicken-install"))
10(define lib-dir           (make-pathname '("lib" "chicken") (->string major-version)))
11(define repo-test-dir     (make-parameter (make-pathname (current-directory) "salmonella-repo")))
12(define repo-test-lib-dir (make-parameter (make-pathname (repo-test-dir) lib-dir)))
13(define chicken-eggs-dir  (make-parameter (make-pathname `(,(current-directory) "chicken-eggs" "release")
14                                                         (->string major-version))))
15(define chicken-install-args (make-parameter (lambda () (string-append " -prefix " (repo-test-dir) " -t local "
16                                                                       " -l " (chicken-eggs-dir)))))
17
18
19(define env-vars      (make-parameter (string-append
20                                       "CHICKEN_INSTALL_PREFIX=" (repo-test-dir)
21                                       " "
22                                       "CHICKEN_INCLUDE_PATH=" (repo-test-lib-dir))))
23(define skip-eggs     (make-parameter '()))
24(define verbose       (make-parameter #f))
25(define logfile       (make-parameter "salmonella.log"))
26(define system-deps   (make-parameter #f))
27(define pkg-manager   (make-parameter #f))
28(define resume        (make-parameter #f))
29(define dont-ask      (make-parameter #f))
30(define progress-file (make-parameter "salmonella.progress"))
31(define html-output   (make-parameter "salmonella-report"))
32(define css-file      (make-parameter #f))
33(define ->html        (make-parameter #f))
34
35(define (show . msg)
36  (when (verbose)
37    (print "  " (string-concatenate (map ->string msg)))))
38
39(define egg-information (make-parameter (lambda () #f)))
40
41(define eggs (make-parameter (lambda () #f)))
42
43(define (system:system . command)
44  ;; I'm afraid this thing won't work on windows...
45  (let* ((p (open-input-pipe (sprintf "~A 2>&1" (string-concatenate command))))
46         (output (read-all p)))
47    (values (close-input-pipe p) output)))
48
49(define (system:output . command)
50  (let-values (((status output) (system:system (string-concatenate command))))
51    output))
52
53(define (system:status . command)
54  (let-values (((status output) (system:system (string-concatenate command))))
55    status))
56
57(define usage #<#EOF
58Usage: salmonella -h | -help | --help | -?
59       salmonella <conf-file>
60       salmonella <options>
61
62<options> are Scheme expressions. <conf-file> is a file which may
63contain the same Scheme expressions as <options>'s. The following
64expressions are recognized:
65
66             == Expression ==                   == Default ==
67 (chicken-install \"<path to chicken-install>\")    chicken-install
68 (chicken-install-args \"<args>\"                 a procedure which returns chicken-install options
69 (env-vars \"<environment variables settings>\" CHICKEN_INCLUDE_PATH=(repo-test-lib-dir)
70                                                CHICKEN_INSTALL_PREFIX=(repo-test-dir)
71 (repo-test-dir \"<path to repo-test-dir>\")    `pwd`/salmonella-repo
72 (chicken-eggs-dir \"<path to SVN dir>\")       `pwd`/chicken-eggs/release/4
73 (skip-eggs (list \"egg1\" \"egg2\" ...))         '()
74 (verbose <bool>)                               ##f
75 (logfile \"filename\")                         salmonella.log
76 (egg-information (lambda () ...))              a procedure which returns egg information for all eggs found in (chicken-eggs-dir)
77 (eggs (lambda () ...))                         a procedure which returns the names of all eggs found in (chicken-eggs-dir)
78 (system-deps \"filename\")                     ##f
79 (pkg-manager \"system pkg manager & args\")    ##f
80 (resume <bool>)                                ##f
81 (dont-ask <bool>)                              ##f
82 (progress-file \"filename\")                   salmonella.progress
83 (dont-ask <bool>)                              ##f
84 (progress-file \"filename\")                   salmonella.progress
85 (html-output \"dirname\")                      salmonella-report.  If ##f, no HTML output
86 (css-file \"filename\")                        ##f
87 (->html \"filename\")                          ##f
88Example:
89
90 salmonella '(chicken-install \"/usr/local/chicken/bin/chicken-install\") (verbose ##t)'
91EOF
92)
93
94(define (delete-path . paths)
95  ;; mostly stolen from chicken-setup.scm
96  (define *windows*
97    (and (eq? (software-type) 'windows)
98         (build-platform) ) )
99
100  (define *windows-shell* (memq *windows* '(msvc mingw32)))
101  (let ((cmd (if *windows-shell* "del /Q /S" "rm -fr")))
102    (for-each (lambda (path) (system* "~a ~a" cmd path)) paths)))
103
104(define (clean-repo-test-dir)
105  (delete-path (make-pathname (repo-test-dir) "*")))
106
107(define (delete-eggs-source #!optional confirm)
108  (when confirm
109    (display (string-append "salmonella will delete all *.egg and *.egg-dir files and directories under "
110                            (current-directory) ".  Proceed? [y/n]: "))
111    (flush-output)
112    (unless (string=? (read-line) "y")
113      (print "Aborting.")
114      (exit 0)))
115  (delete-path (make-pathname (current-directory) "*.egg")
116               (make-pathname (current-directory) "*.egg-dir")))
117
118(define (install-egg egg)
119  (system:system (sprintf "~a ~a ~a ~a" (env-vars) (chicken-install) ((chicken-install-args)) egg)))
120
121(define (report egg status msg #!key (action 'egg-install))
122  (with-output-to-file (logfile)
123    (lambda () (pp (list egg action status msg))) append:))
124
125(define (set-installed egg)
126  (with-output-to-file (progress-file) (cut print egg) append:))
127
128(define (current-time) (seconds->string (current-seconds)))
129
130(define (report-env)
131  (when (logfile)
132    (with-output-to-file (logfile)
133      (lambda ()
134        (pp #<#EOF
135salmonella -- a tool for testing Chicken eggs (http://chicken.wiki.br/eggref/#(->string major-version)/salmonella)
136#(string-trim-both (current-time))
137#(system:output (chicken-install) " -version")
138Options:
139  chicken-install: #(chicken-install)
140  repo-test-dir: #(repo-test-dir)
141  chicken-install-args: #((chicken-install-args))
142  env-vars: #(env-vars)
143  skip-eggs:  #(skip-eggs)
144  verbose: #(verbose)
145  logfile: #(logfile)
146  pkg-manager: #(pkg-manager)
147  system-deps: #(system-deps)
148  resume: #(resume)
149  dont-ask: #(dont-ask)
150  progress-file: #(progress-file)
151  html-output: #(html-output)
152  css-file: #(css-file)
153  ->html: #(->html)
154EOF
155)))))
156
157(define (install-system-deps egg dep-table)
158  (let ((deps (assq (string->symbol egg) dep-table)))
159    (when deps
160      (for-each
161       (lambda (dep)
162         (show "Installing system dependency " dep)
163         (let-values (((status output) (system:system (pkg-manager) egg)))
164           (report egg status output action: 'osdep-install)))
165       (map ->string (cdr deps))))))
166
167(define (create-directory-recursively dir)
168  (let ((done '()))
169    (for-each (lambda (d)
170                (let ((to-create (make-pathname done d)))
171                  (if (directory? to-create)
172                      'skip
173                      (unless (string-null? to-create)
174                        (create-directory to-create)))
175                  (set! done (append done (list d)))))
176              (let* ((sep (->string ##sys#pathname-directory-separator))
177                     (dirs (string-split dir sep)))
178                (if (absolute-pathname? dir)
179                    (cons sep dirs)
180                    dirs)))))
181
182(define (create-repo-test-dir)
183  (create-directory-recursively (repo-test-lib-dir)))
184
185(define egg-dependencies
186  (let ((eggs/deps '()))
187    (lambda arg
188      (when (null? eggs/deps)
189        (set! eggs/deps (map (lambda (egg-data)
190                               (cons (car egg-data) (drop egg-data 3)))
191                             ((egg-information)))))
192      (if (null? arg)
193          eggs/deps
194          (let ((egg (car arg)))
195            (alist-ref (if (string? egg)
196                           (string->symbol egg)
197                           egg)
198                       eggs/deps))))))
199
200(define (main)
201  (let ((args (command-line-arguments)))
202   
203    ;; Check if user is asking for help
204    (unless (null? args)
205      (when (member (car args) '("-h" "-help" "--help" "-?"))
206        (print usage)
207        (exit 0)))
208   
209    ;; Eval command line options
210    (if (and (not (null? args)) (file-exists? (car args)))
211        (load (car args))
212        (for-each eval (with-input-from-string (string-intersperse args) read-file)))
213
214    (unless ((eggs))
215      (eggs (lambda ()
216              (show "Fetching eggs list...")
217              (map (compose ->string car) ((egg-information))))))
218
219    (unless ((egg-information))
220      (egg-information
221       (let ((egg-data #f))
222         (lambda () (if (not egg-data)
223                        (begin
224                          (set! egg-data (gather-egg-information (chicken-eggs-dir)))
225                          egg-data)
226                        egg-data)))))
227
228    (when (->html)
229      (logfile (->html))
230      (report->html)
231      (exit 0))
232         
233    (if (resume)
234        (if (file-exists? (progress-file))
235            (skip-eggs (append (skip-eggs) (read-lines (progress-file))))
236            (begin
237              (print "Could not find progress-file " (progress-file))
238              (exit 1)))
239        (when (file-exists? (progress-file))
240          (delete-file (progress-file))))
241   
242    (clean-repo-test-dir)
243    (unless (directory? (repo-test-dir))
244      (create-repo-test-dir))
245    (report-env)
246
247    (delete-eggs-source (not (dont-ask)))
248   
249    (let ((deps (and (pkg-manager) (system-deps) (read-file (system-deps))))
250          (successful '())
251          (fail '()))
252      (for-each (lambda (egg)
253                  (unless (member egg (skip-eggs))
254                    (install-system-deps egg deps)
255                    (show "Deleting " egg " egg source (if it exists)...")
256                    (delete-eggs-source)
257                    (display (string-append "Installing " egg))
258                    (flush-output)
259                    (let-values (((status output) (install-egg egg)))
260                      (print (string-pad
261                              (cond ((zero? status)
262                                     (set! successful (cons egg successful))
263                                     "[ok]")
264                                    (else (set! fail (cons egg fail))
265                                          "[error]"))
266                              (- 50 (string-length egg))))
267                      (when (logfile) (report egg status output)))
268                    (set-installed egg)
269                    (show "Cleaning up " (repo-test-dir) "...")
270                    (clean-repo-test-dir)))
271                ((eggs))))))
272
273(define css-data
274"body { font-size: 10pt; }
275#egg-even { background-color: #CCC; }
276#egg-fail { font-weight: bold; color: red; }
277#egg-ok { font-weight: bold; color: blue; }
278#egg-odd { background-color: #FFFFCC; }")
279 
280;;; A simple web-scheme replacement
281
282(define s+ string-append)
283
284(define (make-tag tagname)
285  (lambda (data #!key (id #f))
286    (conc "<" tagname (if id (conc " id=\"" id "\"") "") ">"
287          data
288          "</" tagname ">")))
289
290(define (a url text)
291  (s+ "<a 'href='" url "'>" text "</a>"))
292
293(define p (make-tag 'p))
294(define div (make-tag 'div))
295(define pre (make-tag 'pre))
296(define td (make-tag 'td))
297(define tr (make-tag 'tr))
298(define table (make-tag 'table))
299(define b (make-tag 'b))
300(define html (make-tag 'html))
301(define body (make-tag 'body))
302(define head (make-tag 'head))
303(define title (make-tag 'title))
304(define meta (make-tag 'meta))
305(define style (make-tag 'style))
306(define h1 (make-tag 'h1))
307(define h2 (make-tag 'h2))
308(define h3 (make-tag 'h3))
309
310(define (ws:page contents #!key (css #f) (page-title ""))
311  (html
312   (s+ (head (s+ (title page-title)
313                 (if css
314                     (s+ "<link rel=stylesheet href='" css "' type=text/css>")
315                     "")))
316       (body contents))))
317
318(define (tag-attribs->string attribs)
319  (string-intersperse (map (lambda (attrib)
320                             (conc (car attrib) "=\"" (cdr attrib) "\""))
321                           attribs)
322                      " "))
323
324(define alternate-odd/even
325  (let ((current #f))
326    (lambda ()
327      (set! current (not current))
328      (if current
329          'egg-even
330          'egg-odd))))
331   
332(define (ws:make-table alist)
333  (table (string-intersperse
334          (map (lambda (line)
335                 (tr (string-intersperse (map td line) "") id: (alternate-odd/even)))
336               alist) "")))
337
338(define (report->html)
339
340  (define (link-egg-doc egg)
341    (a (conc "http://chicken.wiki.br/eggref/" major-version "/" egg ".html")
342       "egg page"))
343
344  (define (link-egg-deps egg)
345    (a (conc "http://chicken.wiki.br/dep-graphs/" egg ".png")
346       "dependencies"))
347
348  (define (egg-fail? egg eggs)
349    (let ((egg-data (alist-ref (->string egg) eggs equal?)))
350      ;; some eggs are not explicitly tested, so have no status
351      (and egg-data (not (zero? (cadr egg-data))))))
352         
353  ;; if the optional argument FAIL is not #f, it indicates that all the EGGS failed
354  (define (tabularize-eggs eggs css #!optional fail)
355    (ws:make-table
356     (append
357      (list (map b (let ((h '("Egg" "Date" "Doc" "Dependencies")))
358                     (if fail
359                         (append h (list "Broken dependencies"))
360                         h))))
361      (map (lambda (egg-data)
362             (let ((egg (car egg-data))
363                   (status (caddr egg-data))
364                   (output (cadddr egg-data)))
365               ;; write the per-egg report
366               (with-output-to-file (make-pathname (html-output) egg ".html")
367                 (lambda ()
368                   (print (ws:page (string-append (h1 egg) (pre output))
369                                   css: css
370                                   page-title: egg))))
371               ;; generate the eggs table
372               (let ((version-str (lambda (egg-data)
373                                    (or (and egg-data (let ((x (alist-ref 'version egg-data)))
374                                                        (and x (car x)))) ""))))
375               (map (lambda (info) (a (make-pathname '() egg ".html") info))
376                    (let ((data
377                           (list egg
378                                 (let ((egg-data (alist-ref (string->symbol egg) ((egg-information)))))
379                                  (version-str egg-data))
380                                 (link-egg-doc egg)
381                                 (link-egg-deps egg))))
382                      (if fail
383                          (append data (list (string-intersperse
384                                              (map link-egg-doc
385                                                   (filter (lambda (egg)
386                                                             (egg-fail? egg eggs))
387                                                           (egg-dependencies egg)))
388                                              ",")))
389                          data))))))
390           (sort eggs (lambda (s1 s2) (string< (car s1) (car s2))))))))
391
392  (show "Generating HTML report")
393  (when (file-exists? (html-output))
394    (delete-path (html-output)))
395  (create-directory (html-output))
396  (let* ((data (with-input-from-file (logfile) read-file))
397         (env-report (car data))
398         (eggs-report (cdr data))
399         (css (or (css-file)
400                  (begin
401                    (with-output-to-file (make-pathname (html-output) "salmonella.css")
402                      (cut print css-data))
403                    "salmonella.css"))))
404    (with-output-to-file (make-pathname (html-output) "index.html")
405      (lambda ()
406        (print
407         (ws:page
408          (string-append
409           (h1 "Salmonella report")
410           (h2 "Summary")
411           (let* ((success (filter (lambda (l) (zero? (caddr l))) eggs-report))
412                  (fail (filter (lambda (l) (not (zero? (caddr l)))) eggs-report))
413                  (success-count (length success))
414                  (fail-count (length fail))
415                  (total (+ (length eggs-report) (length (skip-eggs)))))
416             (string-append
417              (ws:make-table
418               `(("Failed"   ,(div (number->string fail-count) id: "egg-fail"))
419                 ("Succeed"  ,(div (number->string success-count) id: "egg-ok"))
420                 ("Skipped"  ,(length (skip-eggs)))
421                 (,(b "Total")    ,(b (number->string total)))))
422              (h2 "Eggs")
423              (h3 "Failed") (tabularize-eggs fail css 'fail)
424              (h3 "Succeed") (tabularize-eggs success css)
425              (if (null? (skip-eggs))
426                  ""
427                  (string-append
428                   (h3 "Skipped")
429                   (ws:make-table (append `((,(b "Egg"))) (map list (skip-eggs))))))))
430           (h2 "Environment")
431           (pre env-report)
432           (p (conc "Finished at: " (current-time))))
433          css: css
434          page-title: "Salmonella report"))))))
435
436(main)
437(when (html-output) (report->html))
Note: See TracBrowser for help on using the repository browser.