# HG changeset patch # User František Kučera # Date 1578777061 -3600 # Node ID 0b9e4af08cc8cca734a4306e72ced218e3810532 # Parent f9cada1d46a445fccd195d0b9cfe58f9104ec0d8 streamlet examples: hash diff -r f9cada1d46a4 -r 0b9e4af08cc8 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 . + + +# 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