aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-02-02 15:48:28 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-02-02 15:49:42 -0800
commit505fec2a61d2571317cc4bbc36ec0f4822a63f9d (patch)
treec2b37e9db8e2f958fbca0caa0a9c4924912a37a9 /lib/file_hash.nom
parent513c721198b2256235a95c98d161ab1bb51e6671 (diff)
Restructured the nomsu files to group all the essentials into core/ and
all the optionals into lib/. lib/core.nom and tests/all.nom are no longer needed now.
Diffstat (limited to 'lib/file_hash.nom')
-rw-r--r--lib/file_hash.nom46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/file_hash.nom b/lib/file_hash.nom
new file mode 100644
index 0000000..4d99e39
--- /dev/null
+++ b/lib/file_hash.nom
@@ -0,0 +1,46 @@
+use "core"
+
+%hash_to_filename <- {}
+
+lua> ".."
+ local Hash = require("openssl.digest");
+ local function sha1(x)
+ local hash = Hash.new("sha1"):final(x);
+ local hex = hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end)
+ return hex
+ end
+ local lfs = require('lfs');
+ local function attrdir(path)
+ for filename in lfs.dir(path) do
+ if filename ~= "." and filename ~= ".." and filename:sub(1,1) ~= "." then
+ local filename = path..'/'..filename
+ local attr = lfs.attributes(filename);
+ if attr.mode == "directory" then
+ attrdir(filename);
+ elseif filename:match(".*%.nom") then
+ local file = io.open(filename);
+ local hash = sha1(file:read("*a"));
+ file:close();
+ \%hash_to_filename[hash] = filename
+ end
+ end
+ end
+ end
+ attrdir(".");
+
+action [sha1 %]:
+ lua> "return sha1(\%);"
+
+action [file with hash %hash]:
+ %file <- (%hash in %hash_to_filename)
+ assume %file or barf "File with SHA1 hash \%hash not found!"
+ return %file
+
+action [hash of file %filename]:
+ lua> ".."
+ local f = io.open(\%filename);
+ local hash = sha1(f:read("*a"));
+ f:close();
+ return hash;
+
+parse [use file with hash %hash] as: use (file with hash %hash)