]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/filesystem.lisp
feat(topic-maps): read CTM topic identities and default names
[topic-maps.git] / src / filesystem.lisp
1 (in-package #:topic-maps.test-foundation)
2
3 ;;; ANSI Common Lisp has no portable recursive-directory API. Keep the UIOP
4 ;;; dependency here. No SBCL-specific API is used by the library.
5 (defun directory-files-recursively (root)
6 (let ((directory (uiop:ensure-directory-pathname root)))
7 (when (uiop:directory-exists-p directory)
8 (append (uiop:directory-files directory)
9 (mapcan #'directory-files-recursively
10 (uiop:subdirectories directory))))))
11
12 (defun relative-file-name (pathname root)
13 (let ((relative (enough-namestring pathname
14 (uiop:ensure-directory-pathname root))))
15 (when (or (uiop:absolute-pathname-p relative)
16 (member :up (pathname-directory relative)))
17 (error "Path ~S is outside corpus root ~S." pathname root))
18 ;; UIOP's Unix namestring is stable across host pathname conventions.
19 (uiop:unix-namestring relative)))
20
21 (defun sorted-files (directory root)
22 (sort (directory-files-recursively directory) #'string<
23 :key (lambda (path) (relative-file-name path root))))
24
25 (defun file-type-p (pathname type)
26 (equal (pathname-type pathname) type))
27
28 (defun default-corpus-root ()
29 (asdf:system-relative-pathname "topic-maps" "vendor/cxtm/"))
30
31 (defun read-corpus-metadata ()
32 (let ((*read-eval* nil))
33 (with-open-file (stream (asdf:system-relative-pathname
34 "topic-maps" "references/corpus-lock.sexp"))
35 (let ((data (read stream)))
36 (unless (eq :eof (read stream nil :eof))
37 (error "Corpus metadata contains more than one form."))
38 data))))
39
40 (defun document-iri (base relative)
41 "Assign fixture IRIs, not dereference or resolve CTM identifiers."
42 (unless (and (stringp base) (> (length base) 0)
43 (char= #\/ (char base (1- (length base))))
44 (or (uiop:string-prefix-p "https://" base)
45 (uiop:string-prefix-p "http://" base))
46 (not (find #\# base)) (not (find #\? base))
47 (every (lambda (c) (and (> (char-code c) 32)
48 (< (char-code c) 127))) base))
49 (error "Document base must be an explicit absolute HTTP(S) directory IRI: ~S" base))
50 (unless (every (lambda (c)
51 (or (find c "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._/")
52 nil)) relative)
53 (error "Fixture path needs an explicit IRI encoding policy: ~S" relative))
54 (concatenate 'string base relative))
55
56 (defun read-clause-map ()
57 (let ((*read-eval* nil))
58 (with-open-file (stream (asdf:system-relative-pathname
59 "topic-maps" "references/reviewed-clauses.sexp"))
60 (read stream))))