forked from mario-goulart/salmonella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalmonella-epidemy.scm
175 lines (159 loc) · 6.78 KB
/
salmonella-epidemy.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(use posix salmonella salmonella-log-parser)
(include "salmonella-common.scm")
(include "salmonella-version.scm")
(define default-verbosity 0)
(define *verbosity* 0)
(define (split-eggs eggs slices)
(let loop ((eggs eggs)
(lists (make-list slices '())))
(if (null? eggs)
(map sort-eggs lists)
(let* ((egg (car eggs))
(first-list (car lists)))
(set! first-list (cons egg first-list))
(loop (cdr eggs)
(append (cdr lists) (list first-list)))))))
(define (run-salmonella instance
eggs
chicken-installation-prefix
salmonella-prefix
chicken-install-args
skip-eggs
eggs-doc-dir
keep-repo?
repo-dir
log-dir)
(let* ((instance-repo-dir (make-pathname repo-dir (number->string instance)))
(cmd
(string-intersperse
(filter
identity
(list
(make-pathname salmonella-prefix "salmonella")
(and chicken-installation-prefix
(string-append "--chicken-installation-prefix="
chicken-installation-prefix))
(and chicken-install-args
(qs (string-append "--chicken-install-args=" chicken-install-args)))
(and skip-eggs
(not (null? skip-eggs))
(string-append "--skip-eggs="
(string-intersperse (map symbol->string skip-eggs) ",")))
(and eggs-doc-dir
(string-append "--eggs-doc-dir=" eggs-doc-dir))
(and keep-repo?
"--keep-repo")
(string-append "--repo-dir=" instance-repo-dir)
(string-append "--log-file="
(make-pathname log-dir (number->string instance) "log"))
"--verbosity=1"
(conc "--instance-id=" instance)
(string-intersperse (map ->string eggs)))))))
(when (> *verbosity* 0) (print cmd))
(process-run cmd)))
(define (merge-logs salmonella-prefix log-dir log-file instances)
(let ((cmd (string-intersperse
(list (make-pathname salmonella-prefix "salmonella-log-merger")
(string-append "--log-file=" log-file)
(string-intersperse
(map (lambda (i)
(make-pathname log-dir (number->string i) "log"))
(iota instances 1)))))))
(when (> *verbosity* 0) (print cmd))
(system cmd)))
(let ((args (command-line-arguments)))
(when (or (member "-h" args)
(member "--help" args))
(usage exit-code: 0 epidemy?: #t))
(when (member "--version" args)
(print salmonella-version)
(exit 0))
(let* ((chicken-installation-prefix
(cmd-line-arg '--chicken-installation-prefix args))
(salmonella-prefix
(or (cmd-line-arg '--salmonella-prefix args)
(pathname-directory (program-name))))
(chicken-install-args
(cmd-line-arg '--chicken-install-args args))
(eggs-source-dir
(cmd-line-arg '--eggs-source-dir args))
(eggs-doc-dir
(cmd-line-arg '--eggs-doc-dir args))
(log-file (or (cmd-line-arg '--log-file args) "salmonella-epidemy.log"))
(skip-eggs (let ((skip (cmd-line-arg '--skip-eggs args)))
(if skip
(map string->symbol (string-split skip ","))
'())))
(keep-repo? (and (member "--keep-repo" args) #t))
(repo-dir (or (and-let* ((path (cmd-line-arg '--repo-dir args)))
(if (absolute-pathname? path)
path
(normalize-pathname
(make-pathname (current-directory) path))))
(mktempdir)))
(log-dir (or (and-let* ((path (cmd-line-arg '--log-dir args)))
(if (absolute-pathname? path)
path
(normalize-pathname
(make-pathname (current-directory) path))))
(mktempdir)))
(eggs (remove (lambda (egg)
(memq egg skip-eggs))
(map string->symbol
(remove (lambda (arg)
(string-prefix? "--" arg))
args))))
(total-eggs (length eggs))
(instances (or (and-let* ((i (cmd-line-arg '--instances args)))
(or (string->number i) 1))
1)))
(when (null? eggs)
(print "Nothing to do.")
(exit 0))
(when eggs-source-dir
(die (pathname-strip-directory (program-name))
" doesn't support --egg-sources-dir. Aborting."))
;; Remove the temporary directory if interrupted
(set-signal-handler! signal/int
(lambda (signal)
(delete-path repo-dir)
(delete-path log-dir)
(exit)))
(set! *verbosity*
(or (and-let* ((verbosity (cmd-line-arg '--verbosity args)))
(string->number verbosity))
default-verbosity))
;; Remove old log
(delete-file* log-file)
;; Run salmonellas
(let ((egg-slices (split-eggs eggs instances))
(salmonellas '())) ;; (pid . instance-id)
(let loop ((i instances))
(unless (zero? i)
(when (> *verbosity* 0) (print "Running instance " i "."))
(set! salmonellas
(cons
(cons (run-salmonella i
(list-ref egg-slices (- i 1))
chicken-installation-prefix
salmonella-prefix
chicken-install-args
skip-eggs
eggs-doc-dir
keep-repo?
repo-dir
log-dir)
i)
salmonellas))
(loop (- i 1))))
;; Wait for all salmonellas
(let loop ((i instances))
(unless (zero? i)
(let-values (((pid exit-normally? exit-status) (process-wait)))
(printf "### Instance ~a has finished.\n" (alist-ref pid salmonellas =))
(loop (- i 1))))))
;; Merge logs
(merge-logs salmonella-prefix log-dir log-file instances)
(delete-path log-dir)
(unless keep-repo?
(delete-path repo-dir))))