streamlet examples: hash v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 11 Jan 2020 22:11:01 +0100
branchv_0
changeset 34 0b9e4af08cc8
parent 33 f9cada1d46a4
child 35 926eb93c302f
streamlet examples: hash
streamlet-examples/__relpipe_in_filesystem_script_hash
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/__relpipe_in_filesystem_script_hash	Sat Jan 11 22:11:01 2020 +0100
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program 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, version 3 of the License.
+#
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# This streamlet computes hashes of given files.
+# Default algorithm is sha256.
+#
+# Any supported hash algorithm can be specified by e.g. --option "attribute" "sha512"
+# The hash command is derived from the algorithm name by adding "sum" suffix and must be available at $PATH.
+#
+# Multiple algorithms can be specified (just repeat the --option).
+# But single streamlet instance will run them sequentially.
+# When parallell processing is needed (usually faster) then multiple scriptlet instances should be used:
+#   --scriptlet hash --option "sha1" --as "sha1" --scriptlet hash --option "sha256" --as "sha256"
+# instead of:
+#   --scriptlet hash --option "sha1" --as "sha1" --option "sha256" --as "sha256"
+
+
+. "$(dirname $0)/streamlet-common.sh"
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
+	hashTypes=()
+	hashCommands=()
+
+	for (( i=0; i<${#optionNames[@]}; i++)); do
+		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
+			if type "${optionValues[$i]}sum" > /dev/null; then
+				hashTypes+=("${optionValues[$i]}");
+				hashCommands+=("${optionValues[$i]}sum");
+			else
+				echo "Unsupported attribute: ${optionValues[$i]}" >&2
+			fi
+		else
+			echo "Unsupported option: ${optionNames[$i]}" >&2
+		fi
+	done
+
+	if [[ -z "$hashTypes" ]]; then
+		hashTypes=("sha256")
+		hashCommands=("sha256sum")
+	fi
+
+	for (( i=0; i<${#hashTypes[@]}; i++)); do
+		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-${hashTypes[$i]}}"    "string"
+	done
+
+	send WAITING_FOR_INPUT_ATTRIBUTES
+}
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
+	for (( i=0; i<${#hashTypes[@]}; i++)); do
+		value=$("${hashCommands[$i]}" "$currentFile" | cut -d" " -f1) 2>/dev/null;
+		if [[ -z "$value" ]]; then isNull="true"; else isNull="false"; fi
+		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
+	done
+	
+	send WAITING_FOR_INPUT_ATTRIBUTES;
+}
+
+initialize
+processMessages