#!/usr/bin/env nomsu -V4.8.10 # Some file utilities for searching for files recursively and using package.nomsupath use "lib/os.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %_SPOOFED_FILES = {} %_FILE_CACHE = ({} with fallback % -> %_SPOOFED_FILES.%) %_BROWSE_CACHE = {} # Create a fake file and put it in the cache externally (spoof file %filename %contents) means: %_SPOOFED_FILES.%filename = %contents return %contents # Read a file's contents externally (read file %filename) means: %contents = %_FILE_CACHE.%filename if %contents: return %contents if (%filename == "stdin"): return (spoof file "stdin" (=lua "io.read('*a')")) %file = (=lua "io.open(\%filename)") unless %file: return (nil) %contents = (call %file.read with [%file, "*a"]) %file::close %_FILE_CACHE.%filename = %contents return %contents externally (%path sanitized) means: %path = (%path::with "\\" -> "\\\\") %path = (%path::with "`" -> "") %path = (%path::with "\"" -> "\\\"") %path = (%path::with "$" -> "") %path = (%path::with "%.%." -> "\\..") return %path try: %lfs = (=lua "require('lfs')") ..and if it succeeds: (filesystem has %filename) means: %mode = (call %lfs.attributes with [%filename, "mode"]) if %mode is: "file" "directory" "link" "char device": return (yes) else: return (no) externally (file %path exists) means: if (any of [%_SPOOFED_FILES.%path, %path == "stdin", filesystem has %path]): return (yes) for %nomsupath in (%package.nomsupath::all matches of "[^;]+"): if (filesystem has "\(%nomsupath)/\%path"): return (yes) return (no) externally (files in %path) means: unless %_BROWSE_CACHE.%path: if (%_SPOOFED_FILES.%path or (%filename == "stdin")): %_BROWSE_CACHE.%path = [%path] ..else: if (call %lfs.attributes with [%filename, "mode"]) is: "file" "char device": %_BROWSE_CACHE.%path = [%filename] "directory" "link": for %nomsupath in (%package.nomsupath::all matches of "[^;]+"): %files = [] for %member in (call %lfs.dir with ["\(%nomsupath)/\%filename"]): if ((%member == ".") or (%member == "..")): do next %member for % in (files in %member): %files::add % if ((size of %files) > 0): %_BROWSE_CACHE.%path = %files go to (Found Files) %_BROWSE_CACHE.%path = [] else: %_BROWSE_CACHE.%path = [] === (Found Files) === return %_BROWSE_CACHE.%filename ..or if it barfs: # LFS not found! Fall back to shell commands, if available. unless (sh> "find . -maxdepth 0"): barf "\ ..Could not find 'luafilesystem' module and couldn't run system command 'find' (this might happen on Windows). Please install \ ..'luafilesystem' (which can be found at \(..) "https://github.com/spacewander/luafilesystem" if %jit else "\ ..https://github.com/keplerproject/luafilesystem" .. or obtained through `luarocks install luafilesystem`)" externally (file %path exists) means: if (any of [%_SPOOFED_FILES.%path, %path == "stdin", sh> "ls \(%path sanitized)"]) \ ..: return (yes) for %nomsupath in (%package.nomsupath::all matches of "[^;]+"): if (sh> "ls \(%nomsupath)/\%path"): return (yes) return (no) externally (files in %path) means: unless %_BROWSE_CACHE.%path: if %_SPOOFED_FILES.%path: %_BROWSE_CACHE.%path = [%_SPOOFED_FILES.%path] ..else: for %nomsupath in (%package.nomsupath::all matches of "[^;]+"): %files = (sh> "find -L '\(%path)' -not -path '*/\\.*' -type f'") if %files: %_BROWSE_CACHE.%path = (%files::lines) go to (Found Files) %_BROWSE_CACHE.%path = [] === (Found Files) === return %_BROWSE_CACHE.%path