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

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

fixes to salmonella's handling of egg repository

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