159 lines
4.3 KiB
Scheme
159 lines
4.3 KiB
Scheme
;;
|
|
;; brmsaptool.scm
|
|
;;
|
|
;; ISC License
|
|
;;
|
|
;; Copyright 2023 Brmlab, z.s.
|
|
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
|
|
;;
|
|
;; Permission to use, copy, modify, and/or distribute this software
|
|
;; for any purpose with or without fee is hereby granted, provided
|
|
;; that the above copyright notice and this permission notice appear
|
|
;; in all copies.
|
|
;;
|
|
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
;;
|
|
(import (chicken condition)
|
|
(chicken file)
|
|
(chicken pathname)
|
|
(chicken file posix)
|
|
|
|
(chicken process-context))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Static default configuration
|
|
|
|
(define *members-directory* (make-parameter "members"))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Testing
|
|
|
|
(define-syntax with-handler
|
|
(syntax-rules ()
|
|
((_ handler body ...)
|
|
(call-with-current-continuation
|
|
(lambda (k)
|
|
(with-exception-handler
|
|
(lambda (x) (k (handler x)))
|
|
(lambda () body ...)))))))
|
|
|
|
(define-syntax unit-test
|
|
(syntax-rules ()
|
|
((_ name condition)
|
|
(if (with-handler (lambda (x) #f)
|
|
(lambda () condition))
|
|
(display ".")
|
|
(error 'unit-test name)))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Dictionary
|
|
|
|
(define (make-dict)
|
|
'())
|
|
|
|
(define (dict-has-key? d k)
|
|
(if (assq k d) #t #f))
|
|
|
|
(define (dict-ref d k . r)
|
|
(let ((p (assq k d)))
|
|
(if p
|
|
(cdr p)
|
|
(if (null? r)
|
|
(error 'dict-ref "Key does not exist" k)
|
|
(car r)))))
|
|
|
|
(define (dict-remove d k)
|
|
(let loop ((s d)
|
|
(r '())
|
|
(e #t))
|
|
(if (null? s)
|
|
(if e
|
|
(error 'dict-remove "Key does not exist" k)
|
|
r)
|
|
(if (eq? (caar s) k)
|
|
(loop (cdr s) r #f)
|
|
(loop (cdr s) (cons (car s) r) e)))))
|
|
|
|
(define (dict-set d k v)
|
|
(let ((dr (let loop ((s d)
|
|
(r '()))
|
|
(if (null? s)
|
|
r
|
|
(if (eq? (caar s) k)
|
|
(loop (cdr s) r)
|
|
(loop (cdr s) (cons (car s) r)))))))
|
|
(cons (cons k v)
|
|
dr)))
|
|
|
|
(define (dict-keys d)
|
|
(map car d))
|
|
|
|
(define (dict-tests!)
|
|
(display "[test] dict ")
|
|
(unit-test 'make-dict (null? (make-dict)))
|
|
(unit-test 'dict-ref-nonexistent (with-handler (lambda (x) #t) (dict-ref (make-dict) 'nonexistent) #f))
|
|
(unit-test 'dict-ref-default (dict-ref (make-dict) 'nonexistent #t))
|
|
(unit-test 'dict-set-nonexistent (equal? (dict-set (make-dict) 'nonexistent 1) '((nonexistent . 1))))
|
|
(unit-test 'dict-set-existent (equal? (dict-set (dict-set (make-dict) 'existent 1) 'existent 2) '((existent . 2))))
|
|
(unit-test 'dict-remove-nonexistent (with-handler (lambda (x) #t) (dict-remove (make-dict) 'nonexistent) #f))
|
|
(unit-test 'dict-remove-existing (null? (dict-remove (dict-set (make-dict) 'existing 1) 'existing)))
|
|
(unit-test 'dict-keys (equal? (dict-keys (dict-set (make-dict) 'existing 1)) '(existing)))
|
|
(print " ok."))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Members index
|
|
|
|
(define (load-members-raw-index)
|
|
(let loop ((fns (directory (*members-directory*)))
|
|
(rs '()))
|
|
(if (null? fns)
|
|
rs
|
|
(let* ((fn (car fns))
|
|
(ffn (make-pathname (*members-directory*) fn))
|
|
(sl (if (symbolic-link? ffn) (read-symbolic-link ffn) #f)))
|
|
(loop (cdr fns)
|
|
(if sl
|
|
(cons (cons fn sl) rs)
|
|
rs))))))
|
|
|
|
(define (members-expand-raw-index ri)
|
|
(let loop ((ri ri)
|
|
(ds '()))
|
|
(if (null? ri)
|
|
ds
|
|
(let* ((mp (car ri))
|
|
(lnk (car mp))
|
|
(dfn (cdr mp))
|
|
(lnkn (string->number lnk))
|
|
(dfnn (string->number dfn))
|
|
(id (or dfnn lnkn))
|
|
(name (if lnkn dfn lnk)))
|
|
(loop (cdr ri)
|
|
(cons (list (cons 'id id)
|
|
(cons 'name name)
|
|
(cons 'file dfn))
|
|
ds))))))
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Run everything
|
|
|
|
;; Print banner
|
|
(print "brmsaptool 0.1 (c) 2023 Brmlab, z.s.")
|
|
(newline)
|
|
|
|
;; Run tests
|
|
(print "Running self-tests:")
|
|
(dict-tests!)
|
|
(print "All self-tests ok!")
|
|
(newline)
|
|
|
|
;; Perform requested action
|
|
(print (members-expand-raw-index (load-members-raw-index)))
|