Megatest

Diff
Login

Differences From Artifact [ee6645e059]:

To Artifact [6303547c57]:


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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
















































































































































































































































275
276
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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fi-69
	format ports srfi-1 matchable regex directory-utils)
;; (import commonmod)
;; (use (prefix ulex ulex:))

(include "common_records.scm")



;; accept an alist or hash table containing envvar/env value pairs (value of #f causes unset) 
;;   execute thunk in context of environment modified as per this list
;;   restore env to prior state then return value of eval'd thunk.
;;   ** this is not thread safe **
(define (common:with-env-vars delta-env-alist-or-hash-table thunk)
  (let* ((delta-env-alist (if (hash-table? delta-env-alist-or-hash-table)
                              (hash-table->alist delta-env-alist-or-hash-table)
                              delta-env-alist-or-hash-table))
         (restore-thunks
          (filter
           identity
           (map (lambda (env-pair)
                  (let* ((env-var     (car env-pair))
                         (new-val     (let ((tmp (cdr env-pair)))
                                        (if (list? tmp) (car tmp) tmp)))
                         (current-val (get-environment-variable env-var))
                         (restore-thunk
                          (cond
                           ((not current-val) (lambda () (unsetenv env-var)))
                           ((not (string? new-val)) #f)
                           ((eq? current-val new-val) #f)
                           (else 
                            (lambda () (setenv env-var current-val))))))
                    ;;(when (not (string? new-val))
                    ;;    (debug:print 0 *default-log-port* " PROBLEM: not a string: "new-val"\n from env-alist:\n"delta-env-alist)
                    ;;    (pp delta-env-alist)
                    ;;    (exit 1))
                        
                    
                    (cond
                     ((not new-val)  ;; modify env here
                      (unsetenv env-var))
                     ((string? new-val)
                      (setenv env-var new-val)))
                    restore-thunk))
                delta-env-alist))))
    (let ((rv (thunk)))
      (for-each (lambda (x) (x)) restore-thunks) ;; restore env to original state
      rv)))

(define (process:conservative-read port)
  (let loop ((res ""))
    (if (not (eof-object? (peek-char port)))
	(loop (conc res (read-char port)))
	res)))

(define (process:cmd-run-with-stderr->list cmd . params)
  ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
;;  (handle-exceptions
;;   exn
;;   (begin
;;     (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
;;     (print "       " ((condition-property-accessor 'exn 'message) exn))
;;     #f)
   (let-values (((fh fho pid fhe) (if (null? params)
				      (process* cmd)
				      (process* cmd params))))
       (let loop ((curr (read-line fh))
		  (result  '()))
	 (let ((errstr (process:conservative-read fhe)))
	   (if (not (string=? errstr ""))
	       (set! result (append result (list errstr)))))
       (if (not (eof-object? curr))
	   (loop (read-line fh)
		 (append result (list curr)))
	   (begin
	     (close-input-port fh)
	     (close-input-port fhe)
	     (close-output-port fho)
	     result))))) ;; )

(define (process:cmd-run-with-stderr-and-exitcode->list cmd . params)
  ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
;;  (handle-exceptions
;;   exn
;;   (begin
;;     (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
;;     (print "       " ((condition-property-accessor 'exn 'message) exn))
;;     #f)
   (let-values (((fh fho pid fhe) (if (null? params)
				      (process* cmd)
				      (process* cmd params))))
       (let loop ((curr (read-line fh))
		  (result  '()))
	 (let ((errstr (process:conservative-read fhe)))
	   (if (not (string=? errstr ""))
	       (set! result (append result (list errstr)))))
       (if (not (eof-object? curr))
	   (loop (read-line fh)
		 (append result (list curr)))
	   (begin
             (let-values (((anotherpid normalexit? exitstatus)  (process-wait pid)))
	     (close-input-port fh)
	     (close-input-port fhe)
	     (close-output-port fho)
               (list result (if normalexit? exitstatus -1))))))))

(define (process:cmd-run-proc-each-line cmd proc . params)
  ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
  (handle-exceptions
   exn
   (begin
     (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
     (debug:print 0 *default-log-port* " message: " ((condition-property-accessor 'exn 'message) exn))
     (debug:print 5 *default-log-port* "exn=" (condition->list exn))
     #f)
   (let-values (((fh fho pid) (if (null? params)
				  (process cmd)
				  (process cmd params))))
       (let loop ((curr (read-line fh))
		(result  '()))
       (if (not (eof-object? curr))
	   (loop (read-line fh)
		 (append result (list (proc curr))))
	   (begin
	     (close-input-port fh)
	     ;; (close-input-port fhe)
	     (close-output-port fho)
	     result))))))

(define (process:cmd-run-proc-each-line-alt cmd proc)
  (let* ((fh (open-input-pipe cmd))
         (res (port-proc->list fh proc))
         (status (close-input-pipe fh)))
    (if (eq? status 0) res #f)))

(define (process:cmd-run->list cmd #!key (delta-env-alist-or-hash-table '()))
  (common:with-env-vars
   delta-env-alist-or-hash-table
   (lambda ()
     (let* ((fh (open-input-pipe cmd))
            (res (port->list fh))
            (status (close-input-pipe fh)))
       (list res status)))))
   
(define (port->list fh)
  (if (eof-object? fh) #f
      (let loop ((curr (read-line fh))
                 (result '()))
        (if (not (eof-object? curr))
            (loop (read-line fh)
                  (append result (list curr)))
            result))))

(define (port-proc->list fh proc)
  (if (eof-object? fh) #f
      (let loop ((curr (proc (read-line fh)))
                 (result '()))
        (if (not (eof-object? curr))
            (loop (let ((l (read-line fh)))
                    (if (eof-object? l) l (proc l)))
                  (append result (list curr)))
            result))))

;; here is an example line where the shell is sh or bash
;; "find / -print 2&>1 > findall.log"
(define (run-n-wait cmdline #!key (params #f)(print-cmd #f)(run-dir #f))
  (if print-cmd 
      (debug:print 0 *default-log-port* 
		   (if (string? print-cmd)
		       print-cmd
		       "")
		   (if run-dir (conc "Run in " run-dir ";") "")
		   cmdline
		   (if params
		       (conc " " (string-intersperse params " "))
		       "")))
  (if (and run-dir
	   (directory-exists? run-dir))
      (push-directory run-dir))
  (let ((pid (if params
		 (process-run cmdline params)
		 (process-run cmdline))))
    (let loop ((i 0))
      (let-values (((pid-val exit-status exit-code) (process-wait pid #t)))
         (if (eq? pid-val 0)
	     (begin
	       (thread-sleep! 2)
	       (loop (+ i 1)))
	     (begin
	       (if (and run-dir
			(directory-exists? run-dir))
		   (pop-directory))
	       (values pid-val exit-status exit-code)))))))
  
;;======================================================================
;; MISC PROCESS RELATED STUFF
;;======================================================================

(define (process:children proc)
  (with-input-from-pipe
   (conc "ps h --ppid " (current-process-id) " -o pid")
   (lambda ()
     (let loop ((inl (read-line))
		(res '()))
       (if (eof-object? inl)
	   (reverse res)
	   (let ((pid (string->number inl)))
	     (if proc (proc pid))
	     (loop (read-line) (cons pid res))))))))

(define (process:alive? pid)
  (handle-exceptions
   exn
   ;; possibly pid is a process not a child, look in /proc to see if it is running still
   (file-exists? (conc "/proc/" pid))
   (let-values (((rpid exit-type exit-signal)(process-wait pid #t)))
       (and (number? rpid)
	    (equal? rpid pid)))))

(define (process:alive-on-host? host pid)
  (let ((cmd (conc "ssh " host " ps -o pid= -p " pid)))
    (handle-exceptions
     exn
     #f ;; anything goes wrong - assume the process in NOT running.
     (with-input-from-pipe 
      cmd
      (lambda ()
	(let loop ((inl (read-line)))
	  (if (eof-object? inl)
	      #f
	      (let* ((clean-str (string-substitute "^[^\\d]*([0-9]+)[^\\d]*$" "\\1" inl))
		     (innum     (string->number clean-str)))
		(and innum
		     (eq? pid innum))))))))))

(define (process:get-sub-pids pid)
  (with-input-from-pipe
   (conc "pstree -A -p " pid) ;; | tr 'a-z\\-+`()\\.' ' ' " pid)
   (lambda ()
     (let loop ((inl (read-line))
		(res '()))
       (if (eof-object? inl)
	   (reverse res)
	   (let ((nums (map string->number
			    (string-split-fields "\\d+" inl))))
	     (loop (read-line)
		   (append res nums))))))))
;; 
;; 
;; ;; accept an alist or hash table containing envvar/env value pairs (value of #f causes unset) 
;; ;;   execute thunk in context of environment modified as per this list
;; ;;   restore env to prior state then return value of eval'd thunk.
;; ;;   ** this is not thread safe **
;; (define (common:with-env-vars delta-env-alist-or-hash-table thunk)
;;   (let* ((delta-env-alist (if (hash-table? delta-env-alist-or-hash-table)
;;                               (hash-table->alist delta-env-alist-or-hash-table)
;;                               delta-env-alist-or-hash-table))
;;          (restore-thunks
;;           (filter
;;            identity
;;            (map (lambda (env-pair)
;;                   (let* ((env-var     (car env-pair))
;;                          (new-val     (let ((tmp (cdr env-pair)))
;;                                         (if (list? tmp) (car tmp) tmp)))
;;                          (current-val (get-environment-variable env-var))
;;                          (restore-thunk
;;                           (cond
;;                            ((not current-val) (lambda () (unsetenv env-var)))
;;                            ((not (string? new-val)) #f)
;;                            ((eq? current-val new-val) #f)
;;                            (else 
;;                             (lambda () (setenv env-var current-val))))))
;;                     ;;(when (not (string? new-val))
;;                     ;;    (debug:print 0 *default-log-port* " PROBLEM: not a string: "new-val"\n from env-alist:\n"delta-env-alist)
;;                     ;;    (pp delta-env-alist)
;;                     ;;    (exit 1))
;;                         
;;                     
;;                     (cond
;;                      ((not new-val)  ;; modify env here
;;                       (unsetenv env-var))
;;                      ((string? new-val)
;;                       (setenv env-var new-val)))
;;                     restore-thunk))
;;                 delta-env-alist))))
;;     (let ((rv (thunk)))
;;       (for-each (lambda (x) (x)) restore-thunks) ;; restore env to original state
;;       rv)))
;; 
;; (define (process:conservative-read port)
;;   (let loop ((res ""))
;;     (if (not (eof-object? (peek-char port)))
;; 	(loop (conc res (read-char port)))
;; 	res)))
;; 
;; (define (process:cmd-run-with-stderr->list cmd . params)
;;   ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
;; ;;  (handle-exceptions
;; ;;   exn
;; ;;   (begin
;; ;;     (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
;; ;;     (print "       " ((condition-property-accessor 'exn 'message) exn))
;; ;;     #f)
;;    (let-values (((fh fho pid fhe) (if (null? params)
;; 				      (process* cmd)
;; 				      (process* cmd params))))
;;        (let loop ((curr (read-line fh))
;; 		  (result  '()))
;; 	 (let ((errstr (process:conservative-read fhe)))
;; 	   (if (not (string=? errstr ""))
;; 	       (set! result (append result (list errstr)))))
;;        (if (not (eof-object? curr))
;; 	   (loop (read-line fh)
;; 		 (append result (list curr)))
;; 	   (begin
;; 	     (close-input-port fh)
;; 	     (close-input-port fhe)
;; 	     (close-output-port fho)
;; 	     result))))) ;; )
;; 
;; (define (process:cmd-run-with-stderr-and-exitcode->list cmd . params)
;;   ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
;; ;;  (handle-exceptions
;; ;;   exn
;; ;;   (begin
;; ;;     (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
;; ;;     (print "       " ((condition-property-accessor 'exn 'message) exn))
;; ;;     #f)
;;    (let-values (((fh fho pid fhe) (if (null? params)
;; 				      (process* cmd)
;; 				      (process* cmd params))))
;;        (let loop ((curr (read-line fh))
;; 		  (result  '()))
;; 	 (let ((errstr (process:conservative-read fhe)))
;; 	   (if (not (string=? errstr ""))
;; 	       (set! result (append result (list errstr)))))
;;        (if (not (eof-object? curr))
;; 	   (loop (read-line fh)
;; 		 (append result (list curr)))
;; 	   (begin
;;              (let-values (((anotherpid normalexit? exitstatus)  (process-wait pid)))
;; 	     (close-input-port fh)
;; 	     (close-input-port fhe)
;; 	     (close-output-port fho)
;;                (list result (if normalexit? exitstatus -1))))))))
;; 
;; (define (process:cmd-run-proc-each-line cmd proc . params)
;;   ;; (print "Called with cmd=" cmd ", proc=" proc ", params=" params)
;;   (handle-exceptions
;;    exn
;;    (begin
;;      (print "ERROR:  Failed to run command: " cmd " " (string-intersperse params " "))
;;      (debug:print 0 *default-log-port* " message: " ((condition-property-accessor 'exn 'message) exn))
;;      (debug:print 5 *default-log-port* "exn=" (condition->list exn))
;;      #f)
;;    (let-values (((fh fho pid) (if (null? params)
;; 				  (process cmd)
;; 				  (process cmd params))))
;;        (let loop ((curr (read-line fh))
;; 		(result  '()))
;;        (if (not (eof-object? curr))
;; 	   (loop (read-line fh)
;; 		 (append result (list (proc curr))))
;; 	   (begin
;; 	     (close-input-port fh)
;; 	     ;; (close-input-port fhe)
;; 	     (close-output-port fho)
;; 	     result))))))
;; 
;; (define (process:cmd-run-proc-each-line-alt cmd proc)
;;   (let* ((fh (open-input-pipe cmd))
;;          (res (port-proc->list fh proc))
;;          (status (close-input-pipe fh)))
;;     (if (eq? status 0) res #f)))
;; 
;; (define (process:cmd-run->list cmd #!key (delta-env-alist-or-hash-table '()))
;;   (common:with-env-vars
;;    delta-env-alist-or-hash-table
;;    (lambda ()
;;      (let* ((fh (open-input-pipe cmd))
;;             (res (port->list fh))
;;             (status (close-input-pipe fh)))
;;        (list res status)))))
;;    
;; (define (port->list fh)
;;   (if (eof-object? fh) #f
;;       (let loop ((curr (read-line fh))
;;                  (result '()))
;;         (if (not (eof-object? curr))
;;             (loop (read-line fh)
;;                   (append result (list curr)))
;;             result))))
;; 
;; (define (port-proc->list fh proc)
;;   (if (eof-object? fh) #f
;;       (let loop ((curr (proc (read-line fh)))
;;                  (result '()))
;;         (if (not (eof-object? curr))
;;             (loop (let ((l (read-line fh)))
;;                     (if (eof-object? l) l (proc l)))
;;                   (append result (list curr)))
;;             result))))
;; 
;; ;; here is an example line where the shell is sh or bash
;; ;; "find / -print 2&>1 > findall.log"
;; (define (run-n-wait cmdline #!key (params #f)(print-cmd #f)(run-dir #f))
;;   (if print-cmd 
;;       (debug:print 0 *default-log-port* 
;; 		   (if (string? print-cmd)
;; 		       print-cmd
;; 		       "")
;; 		   (if run-dir (conc "Run in " run-dir ";") "")
;; 		   cmdline
;; 		   (if params
;; 		       (conc " " (string-intersperse params " "))
;; 		       "")))
;;   (if (and run-dir
;; 	   (directory-exists? run-dir))
;;       (push-directory run-dir))
;;   (let ((pid (if params
;; 		 (process-run cmdline params)
;; 		 (process-run cmdline))))
;;     (let loop ((i 0))
;;       (let-values (((pid-val exit-status exit-code) (process-wait pid #t)))
;;          (if (eq? pid-val 0)
;; 	     (begin
;; 	       (thread-sleep! 2)
;; 	       (loop (+ i 1)))
;; 	     (begin
;; 	       (if (and run-dir
;; 			(directory-exists? run-dir))
;; 		   (pop-directory))
;; 	       (values pid-val exit-status exit-code)))))))
;;   
;; ;;======================================================================
;; ;; MISC PROCESS RELATED STUFF
;; ;;======================================================================
;; 
;; (define (process:children proc)
;;   (with-input-from-pipe
;;    (conc "ps h --ppid " (current-process-id) " -o pid")
;;    (lambda ()
;;      (let loop ((inl (read-line))
;; 		(res '()))
;;        (if (eof-object? inl)
;; 	   (reverse res)
;; 	   (let ((pid (string->number inl)))
;; 	     (if proc (proc pid))
;; 	     (loop (read-line) (cons pid res))))))))
;; 
;; (define (process:alive? pid)
;;   (handle-exceptions
;;    exn
;;    ;; possibly pid is a process not a child, look in /proc to see if it is running still
;;    (file-exists? (conc "/proc/" pid))
;;    (let-values (((rpid exit-type exit-signal)(process-wait pid #t)))
;;        (and (number? rpid)
;; 	    (equal? rpid pid)))))
;; 
;; (define (process:alive-on-host? host pid)
;;   (let ((cmd (conc "ssh " host " ps -o pid= -p " pid)))
;;     (handle-exceptions
;;      exn
;;      #f ;; anything goes wrong - assume the process in NOT running.
;;      (with-input-from-pipe 
;;       cmd
;;       (lambda ()
;; 	(let loop ((inl (read-line)))
;; 	  (if (eof-object? inl)
;; 	      #f
;; 	      (let* ((clean-str (string-substitute "^[^\\d]*([0-9]+)[^\\d]*$" "\\1" inl))
;; 		     (innum     (string->number clean-str)))
;; 		(and innum
;; 		     (eq? pid innum))))))))))
;; 
;; (define (process:get-sub-pids pid)
;;   (with-input-from-pipe
;;    (conc "pstree -A -p " pid) ;; | tr 'a-z\\-+`()\\.' ' ' " pid)
;;    (lambda ()
;;      (let loop ((inl (read-line))
;; 		(res '()))
;;        (if (eof-object? inl)
;; 	   (reverse res)
;; 	   (let ((nums (map string->number
;; 			    (string-split-fields "\\d+" inl))))
;; 	     (loop (read-line)
;; 		   (append res nums))))))))

)