Megatest

Diff
Login

Differences From Artifact [9423abd515]:

To Artifact [b5e3523a1c]:


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
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







+




-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+










-


+
+
-
+
-
-
-
+
+
-
-
-
+
+
-
-
+
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
+
+
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+

-
-
-
-
+
-
-
-
-
-
+
+
+
-
-
-
+
+
-
-
-
+
-
-
-
+
+
-
-
-
-
-
-
+

-
-
-
+
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-


;; 
;;     You should have received a copy of the GNU General Public License
;;     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.

;;======================================================================

(declare (unit commonmod))
(declare (uses mtver))

(module commonmod
	*
	
(import scheme chicken data-structures extras files)
(import (prefix sqlite3 sqlite3:) posix typed-records srfi-18 srfi-69
	md5 message-digest
	regex srfi-1)
(import scheme
	chicken.base
	chicken.condition
	chicken.file
	chicken.time
	chicken.file.posix
	chicken.process-context.posix
	chicken.io
	chicken.string
	
	(prefix sqlite3 sqlite3:)
	
	system-information
	typed-records
	md5
	message-digest
	regex
	srfi-1
	srfi-18
	srfi-69

	mtver
	)

;;======================================================================
;; CONTENTS
;;
;;  config file utils
;;  misc conversion, data manipulation functions
;;  testsuite and area utilites
;;
;;======================================================================

(include "megatest-version.scm")
(include "megatest-fossil-hash.scm")

;; dot-locking egg seems not to work, using this for now
;; if lock is older than expire-time then remove it and try again
(define (get-full-version)
;; to get the lock
  (conc megatest-version "-" megatest-fossil-hash))

(define (version-signature)
;;
(define (common:simple-file-lock fname #!key (expire-time 300))
  (conc megatest-version "-" (substring megatest-fossil-hash 0 4)))


  (let ((fmod-time (handle-exceptions
		       ext
;;======================================================================
;; config file utils
		     (current-seconds)
;;======================================================================

		     (file-modification-time fname))))
(define (lookup cfgdat section var)
  (if (hash-table? cfgdat)
      (let ((sectdat (hash-table-ref/default cfgdat section '())))
	(if (null? sectdat)
    (if (file-exists? fname)
	    #f
	    (let ((match (assoc var sectdat)))
	      (if match ;; (and match (list? match)(> (length match) 1))
		  (cadr match)
		  #f))
	    ))
      #f))

	(if (> (- (current-seconds) fmod-time) expire-time)
;; returns var key1=val1; key2=val2 ... as alist
(define (get-key-list cfgdat section var)
  ;; convert string a=1; b=2; c=a silly thing; d=
  (let ((valstr (lookup cfgdat section var)))
    (if valstr
	    (begin
	(val->alist valstr)
	'()))) ;; should it return empty list or #f to indicate not set?


	      (handle-exceptions exn #f (delete-file* fname))	
	      (common:simple-file-lock fname expire-time: expire-time))
(define (get-section cfgdat section)
  (hash-table-ref/default cfgdat section '()))

	    #f)
;;======================================================================
;; misc conversion, data manipulation functions
;;======================================================================

	(let ((key-string (conc (get-host-name) "-" (current-process-id))))
;; if it looks like a number -> convert it to a number, else return it
;;
(define (lazy-convert inval)
  (let* ((as-num (if (string? inval)(string->number inval) #f)))
    (or as-num inval)))

	  (with-output-to-file fname
;; to '((a . 1)(b . 2)(c . "a silly thing")(d . ""))
;;
(define (val->alist val #!key (convert #f))
  (let ((val-list (string-split-fields ";\\s*" val #:infix)))
    (if val-list
	(map (lambda (x)
	       (let ((f (string-split-fields "\\s*=\\s*" x #:infix)))
		 (case (length f)
	    (lambda ()
	      (print key-string)))
	  (thread-sleep! 0.251)
		   ((0) `(,#f))  ;; null string case
		   ((1) `(,(string->symbol (car f))))
		   ((2) `(,(string->symbol (car f)) .
			  ,(let ((inval (cadr f)))
			     (if convert (lazy-convert inval) inval))))
		   (else f))))
	     (filter (lambda (x)
		       (not (string-match "^\\s*" x)))
	  (if (file-exists? fname)
	      (handle-exceptions exn
                #f 
                (with-input-from-file fname
	  	  (lambda ()
		    (equal? key-string (read-line)))))
		     val-list))
	'())))
	      #f)))))

;;======================================================================
;; testsuite and area utilites
;;======================================================================

(define (common:simple-file-lock-and-wait fname #!key (expire-time 300))
(define (get-testsuite-name toppath configdat)
  (or (lookup configdat "setup" "area-name")
      (lookup configdat "setup" "testsuite")
      (get-environment-variable "MT_TESTSUITE_NAME")
      (if (string? toppath)
  (let ((end-time (+ expire-time (current-seconds))))
    (let loop ((got-lock (common:simple-file-lock fname expire-time: expire-time)))
      (if got-lock
          (pathname-file toppath)
          #f)))

	  #t
	  (if (> end-time (current-seconds))
(define (get-area-path-signature toppath #!optional (short #f))
  (let ((res (message-digest-string (md5-primitive) toppath)))
    (if short
	      (begin
	(substring res 0 4)
	res)))

		(thread-sleep! 3)
		(loop (common:simple-file-lock fname expire-time: expire-time)))
(define (get-area-name configdat toppath #!optional (short #f))
  ;; look up my area name in areas table (future)
  ;; generate auto name
  (conc (get-area-path-signature toppath short)
	"-"
	(get-testsuite-name toppath configdat)))
	      #f)))))

;; need generic find-record-with-var-nmatching-val
;;
(define (path->area-record cfgdat path)
(define (common:simple-file-release-lock fname)
  (let* ((areadat (get-cfg-areas cfgdat))
	 (all     (filter (lambda (x)
			    (let* ((keyvals (cdr x))
				   (pth     (alist-ref 'path keyvals)))
			      (equal? path pth)))
			  areadat)))
  (handle-exceptions
      exn
    (if (null? all)
	#f
	(car all)))) ;; return first match

      #f ;; I don't really care why this failed (at least for now)
;; given a config return an alist of alists
;;   area-name => data
;;
(define (get-cfg-areas cfgdat)
  (let ((adat (get-section cfgdat "areas")))
    (map (lambda (entry)
	   `(,(car entry) . 
	     ,(val->alist (cadr entry))))
	 adat)))
	 
    (delete-file* fname)))

;; (define (debug:print . params) #f)
;; (define (debug:print-info . params) #f)
;; 
;; (define (set-functions dbgp dbgpinfo)
;;   (set! debug:print dbgp)
;;   (set! debug:print-info dbgpinfo))

)