streamlet-examples/hash
author František Kučera <franta-hg@frantovo.cz>
Sat, 18 Jan 2020 20:09:34 +0100
branchv_0
changeset 45 f466b4c7d9b1
parent 34 streamlet-examples/__relpipe_in_filesystem_script_hash@0b9e4af08cc8
child 49 ab48ad4ecb91
permissions -rwxr-xr-x
streamlets: use $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH variable instead of __relpipe_in_filesystem_script_ prefix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, version 3 of the License.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# GNU General Public License for more details.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    19
# This streamlet computes hashes of given files.
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    20
# Default algorithm is sha256.
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    21
#
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    22
# Any supported hash algorithm can be specified by e.g. --option "attribute" "sha512"
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    23
# The hash command is derived from the algorithm name by adding "sum" suffix and must be available at $PATH.
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    24
#
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    25
# Multiple algorithms can be specified (just repeat the --option).
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    26
# But single streamlet instance will run them sequentially.
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    27
# When parallell processing is needed (usually faster) then multiple scriptlet instances should be used:
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    28
#   --scriptlet hash --option "sha1" --as "sha1" --scriptlet hash --option "sha256" --as "sha256"
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    29
# instead of:
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    30
#   --scriptlet hash --option "sha1" --as "sha1" --option "sha256" --as "sha256"
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
. "$(dirname $0)/streamlet-common.sh"
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    36
	hashTypes=()
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    37
	hashCommands=()
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    38
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    39
	for (( i=0; i<${#optionNames[@]}; i++)); do
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    40
		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    41
			if type "${optionValues[$i]}sum" > /dev/null; then
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    42
				hashTypes+=("${optionValues[$i]}");
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    43
				hashCommands+=("${optionValues[$i]}sum");
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    44
			else
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    45
				echo "Unsupported attribute: ${optionValues[$i]}" >&2
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    46
			fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    47
		else
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    48
			echo "Unsupported option: ${optionNames[$i]}" >&2
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    49
		fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    50
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    51
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    52
	if [[ -z "$hashTypes" ]]; then
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    53
		hashTypes=("sha256")
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    54
		hashCommands=("sha256sum")
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    55
	fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    56
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    57
	for (( i=0; i<${#hashTypes[@]}; i++)); do
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    58
		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-${hashTypes[$i]}}"    "string"
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    59
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    60
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	send WAITING_FOR_INPUT_ATTRIBUTES
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    65
	for (( i=0; i<${#hashTypes[@]}; i++)); do
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    66
		value=$("${hashCommands[$i]}" "$currentFile" | cut -d" " -f1) 2>/dev/null;
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    67
		if [[ -z "$value" ]]; then isNull="true"; else isNull="false"; fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    68
		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    69
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    70
	
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	send WAITING_FOR_INPUT_ATTRIBUTES;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
initialize
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
processMessages