Megatest

commonmod.scm at [22eaf9c6fa]
Login

File commonmod.scm artifact f39c7864aa part of check-in 22eaf9c6fa


;;======================================================================
;; Copyright 2017, Matthew Welland.
;; 
;; This file is part of Megatest.
;; 
;;     Megatest is free software: you can redistribute it and/or modify
;;     it under the terms of the GNU General Public License as published by
;;     the Free Software Foundation, either version 3 of the License, or
;;     (at your option) any later version.
;; 
;;     Megatest is distributed in the hope that it will be useful,
;;     but WITHOUT ANY WARRANTY; without even the implied warranty of
;;     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;     GNU General Public License for more details.
;; 
;;     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 configfmod))

(module commonmod
	*
	
(import scheme chicken data-structures extras)
(import (prefix sqlite3 sqlite3:) posix typed-records srfi-18 srfi-69 ports srfi-1 files format)

(import configfmod)
(include "common_records.scm")

(define (db:dbdat-get-path dbdat)
  (if (pair? dbdat)
      (cdr dbdat)
      #f))

(define (common:get-area-name alldat #!optional (areapath-in #f))
  (let* ((configdat (alldat-mtconfig alldat))
	 (areapath  (or (alldat-areapath alldat)
			(get-environment-variable "MT_RUN_AREA_HOME")
			areapath-in)))
    (or (configf:lookup configdat "setup" "area-name") ;; megatest is a flexible tool, testsuite is too limiting a description.
	(configf:lookup configdat "setup" "testsuite" )
	(get-environment-variable "MT_TESTSUITENAME") ;; circulat?
	(if (string? areapath )
	    (pathname-file areapath)
	    #f)))) ;; (pathname-file (current-directory)))))

;; return first path that can be created or already exists and is writable
;;
(define (common:get-create-writeable-dir dirs)
  (if (null? dirs)
      #f
      (let loop ((hed (car dirs))
		 (tal (cdr dirs)))
	(let ((res (or (and (directory? hed)
			    (file-write-access? hed)
			    hed)
		       (handle-exceptions
			   exn
			   (begin
			     ;; TODO add print of exception here
			     ;; (debug:print-info 0 *default-log-port* "could not create " hed ", this might cause problems down the road.")
			     #f)
			(create-directory hed #t)))))
	  (if (and (string? res)
		   (directory? res))
	      res
	      (if (null? tal)
		  #f
		  (loop (car tal)(cdr tal))))))))

;; (define common:get-area-name common:get-area-name)

(define (common:get-db-tmp-area alldat)
  (let* ((dbdir #f)
	 (log-port (alldat-log-port alldat)))
    (if (alldat-tmppath alldat)
	(alldat-tmppath alldat)
	(if (alldat-areapath alldat) ;; common:get-create-writeable-dir
	    (handle-exceptions
	     exn
	     (begin
	       (print-call-chain)
	       (print ((condition-property-accessor 'exn 'message) exn))
	       (debug:print-error 0 log-port "Couldn't create path to " dbdir)
	       (exit 1))
	     (let ((dbpath (common:get-create-writeable-dir
			    (list (conc "/tmp/" (current-user-name)
					"/megatest_localdb/"
					(common:get-area-name alldat) "/"
					(string-translate (alldat-areapath alldat) "/" ".")))))) ;;  #t))))
	       (set! dbdir dbpath) 
	       (alldat-tmppath-set! alldat dbpath)
	       dbpath))
	    #f))))

(define (common:low-noise-print alldat waitval . keys)
  (let* ((key      (string-intersperse (map conc keys) "-" ))
	 (lasttime (hash-table-ref/default (alldat-denoise alldat) key 0))
	 (currtime (current-seconds)))
    (if (> (- currtime lasttime) waitval)
	(begin
	  (hash-table-set! (alldat-denoise alldat) key currtime)
	  #t)
	#f)))

(define (common:version-signature alldat)
  (conc (alldat-megatest-version alldat)
	"-" (substring (alldat-megatest-fossil-hash alldat) 0 4)))

;; (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))

)