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
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
|
(module ducttape-lib
(
runs-ok
glib-debug-level
glib-debug-regex-filter
glib-silent-mode
glib-quiet-mode
glib-log-file
glib-color-mode
ducttape-debug-level
ducttape-debug-regex-filter
ducttape-silent-mode
ducttape-quiet-mode
ducttape-log-file
ducttape-color-mode
iputs-preamble
script-name
idbg
ierr
iwarn
inote
iputs
re-match?
; launch-repl
keyword-skim
skim-cmdline-opts-noarg-by-regex
skim-cmdline-opts-withargs-by-regex
concat-lists
process-command-line
glib-append-logfile
glib-activate-logfile
ducttape-append-logfile
ducttape-activate-logfile
isys
do-or-die
counter-maker
dir-is-writable?
mktemp
get-tmpdir
sendmail
|
︙ | | |
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
|
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
|
-
+
-
+
|
(use posix regex ansi-escape-sequences test srfi-1 irregex slice srfi-13 rfc3339 scsh-process directory-utils uuid-lib filepath srfi-19 ) ; linenoise
(include "mimetypes.scm") ; provides ext->mimetype
(include "workweekdate.scm")
(define ducttape-lib-version 1.00)
(define (toplevel-command sym proc) (lambda () #f))
;;;; utility procedures
; begin credit: megatest's process.scm
;; begin credit: megatest's process.scm
(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 (conservative-read port)
(let loop ((res ""))
(if (not (eof-object? (peek-char port)))
(loop (conc res (read-char port)))
res)))
; end credit: megatest's process.scm
;; end credit: megatest's process.scm
(define (counter-maker)
(let ((acc 0))
(lambda ( #!optional (increment 1) )
(set! acc (+ increment acc))
acc)))
|
︙ | | |
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
-
-
+
+
|
)
(begin
;;http://bugs.call-cc.org/ticket/766
;;close-[input|output]-port implicitly calling process-wait on process pipe ports. This leads to errors like
;;Error: (process-wait) waiting for child process failed - No child processes: 10872
(close-output-port outport)
#f))))
; weird - alist-ref changes signature csc vs. csi... explitly defining.
;; weird - alist-ref arg order changes signature csc vs. csi... explitly defining.
(define (my-alist-ref key alist)
(let ((res (assoc key alist)))
(if res (cdr res) #f)))
(define (keyword-skim-alist args alist)
(let loop ((result-alist '()) (result-args args) (rest-alist alist))
(cond
|
︙ | | |
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
277
278
279
280
281
282
283
284
285
286
287
|
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
277
278
279
280
281
282
283
284
285
286
287
|
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
|
(define (concat-lists lista listb) ;; ok, I just reimplemented append...
(foldr cons listb lista))
;;; setup general_lib env var parameters
;; show warning/note/error/debug prefixes using ansi colors
(define glib-color-mode
(make-parameter (get-environment-variable "GLIB_COLORIZE")))
(define ducttape-color-mode
(make-parameter (get-environment-variable "DUCTTAPE_COLORIZE")))
;; if defined, has number value. if number value > 0, show debug messages
;; value should be decremented in subshells -- idea is raising debug level will show debug messages deeper and deeper in process call stack
(define glib-debug-level
(define ducttape-debug-level
(make-parameter
(let ( (raw-debug-level (get-environment-variable "GLIB_DEBUG_LEVEL")) )
(let ( (raw-debug-level (get-environment-variable "DUCTTAPE_DEBUG_LEVEL")) )
(if raw-debug-level
(let ((num-debug-level (runs-ok (string->number raw-debug-level))))
(if (integer? num-debug-level)
(begin
(let ((new-num-debug-level (- num-debug-level 1)))
(if (> new-num-debug-level 0) ;; decrement
(setenv "GLIB_DEBUG_LEVEL" (number->string new-num-debug-level))
(unsetenv "GLIB_DEBUG_LEVEL")))
(setenv "DUCTTAPE_DEBUG_LEVEL" (number->string new-num-debug-level))
(unsetenv "DUCTTAPE_DEBUG_LEVEL")))
num-debug-level) ; it was set and > 0, mode is value
(begin
(unsetenv "GLIB_DEBUG_LEVEL") ;; value was invalid, unset it
(unsetenv "DUCTTAPE_DEBUG_LEVEL") ;; value was invalid, unset it
#f))) ; value was invalid, mode is f
#f)))) ; var not set, mode is f
(define glib-debug-mode (if (glib-debug-level) #t #f))
(define ducttape-debug-mode (if (ducttape-debug-level) #t #f))
;; glib-debug-regex-filter suppresses non-matching debug messages
(define glib-debug-regex-filter
;; ducttape-debug-regex-filter suppresses non-matching debug messages
(define ducttape-debug-regex-filter
(make-parameter
(let ((raw-debug-pattern (get-environment-variable "GLIB_DEBUG_PATTERN")))
(let ((raw-debug-pattern (get-environment-variable "DUCTTAPE_DEBUG_PATTERN")))
(if raw-debug-pattern
raw-debug-pattern
"."))))
;; silent mode suppresses Note and Warning type messages
(define glib-silent-mode
(make-parameter (get-environment-variable "GLIB_SILENT_MODE")))
(define ducttape-silent-mode
(make-parameter (get-environment-variable "DUCTTAPE_SILENT_MODE")))
;; quiet mode suppresses Note type messages
(define glib-quiet-mode
(make-parameter (get-environment-variable "GLIB_QUIET_MODE")))
(define ducttape-quiet-mode
(make-parameter (get-environment-variable "DUCTTAPE_QUIET_MODE")))
;; if log file is defined, warning/note/error/debug messages are appended
;; to named logfile.
(define glib-log-file
(make-parameter (get-environment-variable "GLIB_LOG_FILE")))
(define ducttape-log-file
(make-parameter (get-environment-variable "DUCTTAPE_LOG_FILE")))
;;; standard messages printing implementation
; get the name of the current script/binary being run
(define (script-name)
(car (reverse (string-split (car (argv)) "/"))))
(define (glib-timestamp)
(define (ducttape-timestamp)
(rfc3339->string (time->rfc3339 (seconds->local-time))))
(define (iputs-preamble msg-type #!optional (suppress-color #f))
(let ((do-color (and
(not suppress-color)
(glib-color-mode)
(ducttape-color-mode)
(terminal-port? (current-error-port)))))
(case msg-type
((note)
(if do-color
(set-text (list 'fg-green 'bg-black 'bold) "Note:")
"Note:"
))
|
︙ | | |
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
-
+
-
+
-
+
-
+
-
+
-
-
-
+
+
+
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
-
+
+
|
))
((dbg)
(if do-color
(set-text (list 'fg-blue 'bg-magenta) "Debug:")
"Debug:"
)))))
(define (glib-append-logfile msg-type message #!optional (suppress-preamble #f))
(define (ducttape-append-logfile msg-type message #!optional (suppress-preamble #f))
(let
((txt
(string-join
(list
(glib-timestamp)
(ducttape-timestamp)
(script-name)
(if suppress-preamble
message
(string-join (list (iputs-preamble msg-type #t) message) " ")))
" | ")))
(if (glib-log-file)
(if (ducttape-log-file)
(runs-ok
(call-with-output-file (glib-log-file)
(call-with-output-file (ducttape-log-file)
(lambda (output-port)
(format output-port "~A ~%" txt)
)
#:append))
#t)))
(define (glib-activate-logfile #!optional (logfile #f))
(define (ducttape-activate-logfile #!optional (logfile #f))
;; from python ducttape-lib.py
; message = "START - pid=%d ppid=%d argv=(%s) pwd=%s user=%s host=%s"%(pid,ppid," ".join("'"+x+"'" for x in sys.argv),os.environ['PWD'],os.getenv('USER','nouser'),os.getenv('HOST','nohost') )
(let ((pid (number->string (current-process-id)))
(ppid (number->string (parent-process-id)))
(argv
(string-join
(map
(lambda (x)
(string-join (list "\"" x "\"") "" ))
(argv))
" "))
(pwd (or (get-environment-variable "PWD") "nopwd"))
(user (or (get-environment-variable "USER") "nouser"))
(host (or (get-environment-variable "HOST") "nohost")))
(if logfile
(begin
(glib-log-file logfile)
(setenv "GLIB_LOG_FILE" (glib-log-file))))
(glib-append-logfile 'note (format #f "START - pid=~A ppid=~A argv=(~A) pwd=~A user=~A host=~A" pid ppid argv pwd user host) #t)))
(ducttape-log-file logfile)
(setenv "DUCTTAPE_LOG_FILE" (ducttape-log-file))))
(ducttape-append-logfile 'note (format #f "START - pid=~A ppid=~A argv=(~A) pwd=~A user=~A host=~A" pid ppid argv pwd user host) #t)))
;; immediately activate logfile (will be noop if logfile disabled)
(glib-activate-logfile)
(ducttape-activate-logfile)
;; log exit code
(define (set-exit-handler)
(let ((orig-exit-handler (exit-handler)))
(exit-handler
(lambda (exitcode)
(glib-append-logfile 'note (format #f "Exit ~A by sys.exit" exitcode) #t)
(ducttape-append-logfile 'note (format #f "Exit ~A by sys.exit" exitcode) #t)
(orig-exit-handler exitcode)))))
(set-exit-handler)
;; TODO: hook exception handler so we can log exception before we sign off.
(define (idbg first-message . rest-args)
(let* ((debug-level-threshold
(if (> (length rest-args) 0) (car rest-args) 1))
(message-list
(if (> (length rest-args) 1)
(cons first-message (cdr rest-args))
(list first-message)) )
(message (apply conc
(map ->string message-list))))
(glib-append-logfile 'dbg message)
(if (glib-debug-level)
(if (<= debug-level-threshold (glib-debug-level))
(if (string-search (glib-debug-regex-filter) message)
(ducttape-append-logfile 'dbg message)
(if (ducttape-debug-level)
(if (<= debug-level-threshold (ducttape-debug-level))
(if (string-search (ducttape-debug-regex-filter) message)
(begin
(format (current-error-port) "~A ~A (~A)~%" (iputs-preamble 'dbg) message (script-name))))))))
(define (ierr message-first . message-rest)
(let* ((message
(apply conc
(map ->string (cons message-first message-rest)))))
(glib-append-logfile 'err message)
(ducttape-append-logfile 'err message)
(format (current-error-port) "~A ~A (~A)~%" (iputs-preamble 'err) message (script-name))))
(define (iwarn message-first . message-rest)
(let* ((message
(apply conc
(map ->string (cons message-first message-rest)))))
(glib-append-logfile 'warn message)
(if (not (glib-silent-mode))
(ducttape-append-logfile 'warn message)
(if (not (ducttape-silent-mode))
(begin
(format (current-error-port) "~A ~A (~A)~%" (iputs-preamble 'warn) message (script-name))))))
(define (inote message-first . message-rest)
(let* ((message
(apply conc
(map ->string (cons message-first message-rest)))))
(glib-append-logfile 'note message)
(if (not (or (glib-silent-mode) (glib-quiet-mode)))
(ducttape-append-logfile 'note message)
(if (not (or (ducttape-silent-mode) (ducttape-quiet-mode)))
(begin
(format (current-error-port) "~A ~A (~A)~%" (iputs-preamble 'note) message (script-name))))))
(define (iputs kind message #!optional (debug-level-threshold 1))
(cond
((member kind (string-split "NOTE/Note/note/n/N" "/")) (inote message))
|
︙ | | |
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
|
)))
(command-line-arguments new-cmdline-args)
result))
;; recognize ducttape-lib command line switches (--quiet, --silent, --color, -d.., -dp.., -logfile)
;; - reset parameters; reset GLIB_* env vars to match user specified intent
;; - reset parameters; reset DUCTTAPE_* env vars to match user specified intent
;; - mutate (command-line-arguments) parameter to subtract these recognized and handled switches
;; * beware -- now (argv) and (command-line-arguments) are inconsistent... cannot mutate (argv) alas. Use (command-line-arguments)
(define (process-command-line)
;; --quiet
(let ((quiet-opts (skim-cmdline-opts-noarg-by-regex "--?quiet")))
(if (not (null? quiet-opts))
(begin
(setenv "GLIB_QUIET_MODE" "1")
(glib-quiet-mode "1"))))
(setenv "DUCTTAPE_QUIET_MODE" "1")
(ducttape-quiet-mode "1"))))
;; --silent
(let ((silent-opts (skim-cmdline-opts-noarg-by-regex "--?silent")))
(if (not (null? silent-opts))
(begin
(setenv "GLIB_SILENT_MODE" "1")
(glib-silent-mode "1"))))
(setenv "DUCTTAPE_SILENT_MODE" "1")
(ducttape-silent-mode "1"))))
;; -color
(let ((color-opts (skim-cmdline-opts-noarg-by-regex "--?colou?r(ize)?")))
(if (not (null? color-opts))
(begin
(setenv "GLIB_COLORIZE" "1")
(glib-color-mode "1"))))
(setenv "DUCTTAPE_COLORIZE" "1")
(ducttape-color-mode "1"))))
;; -nocolor
(let ((nocolor-opts (skim-cmdline-opts-noarg-by-regex "--?nocolou?r(ize)?")))
(if (not (null? nocolor-opts))
(begin
(unsetenv "GLIB_COLORIZE" )
(glib-color-mode #f))))
(unsetenv "DUCTTAPE_COLORIZE" )
(ducttape-color-mode #f))))
;; -logfile
(let ((logfile-opts (skim-cmdline-opts-withargs-by-regex "--?log(-?file)?")))
(if (not (null? logfile-opts))
(begin
(glib-log-file (car (reverse logfile-opts)))
(setenv "GLIB_LOG_FILE" (glib-log-file)))))
(ducttape-log-file (car (reverse logfile-opts)))
(setenv "DUCTTAPE_LOG_FILE" (ducttape-log-file)))))
;; -d -dd -d#
(let ((debug-opts (skim-cmdline-opts-noarg-by-regex "-d(d*|\\d+)"))
(initial-debuglevel (if (glib-debug-level) (glib-debug-level) 0) ))
(initial-debuglevel (if (ducttape-debug-level) (ducttape-debug-level) 0) ))
(if (not (null? debug-opts))
(begin
(glib-debug-level
(ducttape-debug-level
(let loop ((opts debug-opts) (debuglevel initial-debuglevel))
(if (null? opts)
debuglevel
(let*
( (curopt (car opts))
(restopts (cdr opts))
(ds (string-match "-(d+)" curopt))
(dnum (string-match "-d(\\d+)" curopt)))
(cond
(ds (loop restopts (+ debuglevel (string-length (cadr ds)))))
(dnum (loop restopts (string->number (cadr dnum)))))))))
(setenv "GLIB_DEBUG_LEVEL" (number->string (glib-debug-level))))))
(setenv "DUCTTAPE_DEBUG_LEVEL" (number->string (ducttape-debug-level))))))
;; -dp <pat> / --debug-pattern <pat>
(let ((debugpat-opts (skim-cmdline-opts-withargs-by-regex "--?(debug-pattern|dp)")))
(if (not (null? debugpat-opts))
(begin
(glib-debug-regex-filter (string-join debugpat-opts "|"))
(setenv "GLIB_DEBUG_PATTERN" (glib-debug-regex-filter))))))
(ducttape-debug-regex-filter (string-join debugpat-opts "|"))
(setenv "DUCTTAPE_DEBUG_PATTERN" (ducttape-debug-regex-filter))))))
;; handle command line immediately;
(process-command-line)
) ; end module
|