|
| 1 | +#/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -o pipefail |
| 5 | + |
| 6 | +PROG=$(basename $0) |
| 7 | + |
| 8 | +script_dir=$(cd $(dirname $0) || exit 1; pwd) |
| 9 | +root_dir=$(realpath ${script_dir}/..) |
| 10 | + |
| 11 | +declare -A hashcmds=() |
| 12 | +hashcmds["MD5"]="md5sum" |
| 13 | +hashcmds["SHA224"]="sha224sum" |
| 14 | +hashcmds["SHA256"]="sha256sum" |
| 15 | +hashcmds["SHA384"]="sha384sum" |
| 16 | +hashcmds["SHA512"]="sha512sum" |
| 17 | + |
| 18 | +supported_hashalgos=${!hashcmds[@]} |
| 19 | + |
| 20 | +#------------------------------------------------------------------------------ |
| 21 | +if ! command -v github-release &> /dev/null; then |
| 22 | + echo >&2 'error: "github-release" not found!' |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +#------------------------------------------------------------------------------ |
| 27 | +err() { echo -e >&2 ERROR: $@\\n; } |
| 28 | +die() { err $@; exit 1; } |
| 29 | +help() { |
| 30 | + cat >&2 <<ENDHELP |
| 31 | +Usage: $PROG HASHALGO [HASHALGO ...] |
| 32 | +
|
| 33 | +Upload incoming files associated them with HASHALGO release. |
| 34 | +Available HASHALGO are ${supported_hashalgos} |
| 35 | +
|
| 36 | +ENDHELP |
| 37 | +} |
| 38 | + |
| 39 | +if [[ "$@" == "" ]]; then |
| 40 | + err Missing HASHALGO option |
| 41 | + help |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | +for hashalgo in "$@"; do |
| 45 | + if [[ ! "${supported_hashalgos}" =~ "${hashalgo}" ]]; then |
| 46 | + err Unknown HASHALGO [${hashalgo}] option |
| 47 | + help |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +done |
| 51 | +hashalgos="$@" |
| 52 | + |
| 53 | +for hashalgo in ${hashalgos}; do |
| 54 | + hashcmd=${hashcmds[${hashalgo}]} |
| 55 | + if ! command -v ${hashcmd} &> /dev/null; then |
| 56 | + echo >&2 'error: "${hashcmd}" not found!' |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +done |
| 60 | + |
| 61 | +#------------------------------------------------------------------------------ |
| 62 | +for hashalgo in ${hashalgos}; do |
| 63 | + |
| 64 | + hashcmd=${hashcmds[${hashalgo}]} |
| 65 | + |
| 66 | + mkdir -p ${root_dir}/${hashalgo} |
| 67 | + |
| 68 | + if [[ ! -f ${root_dir}/${hashalgo}.csv ]]; then |
| 69 | + touch ${root_dir}/${hashalgo}.csv |
| 70 | + fi |
| 71 | + |
| 72 | + #---- |
| 73 | + echo "${hashalgo}: checking for incoming files" |
| 74 | + pushd ${root_dir}/INCOMING > /dev/null |
| 75 | + for filename in $(ls -1 | sort); do |
| 76 | + echo "${hashalgo}: found ${filename}" |
| 77 | + checksum=$(${hashcmd} ${filename} | awk '{ print $1 }') |
| 78 | + cp ${filename} ${root_dir}/${hashalgo}/${checksum} |
| 79 | + referenced_assets=$(cat ${root_dir}/${hashalgo}.csv | cut -d";" -f1) |
| 80 | + if [[ ! "${referenced_assets}" =~ "${checksum}" ]]; then |
| 81 | + echo "${checksum};${filename}" >> ${root_dir}/${hashalgo}.csv |
| 82 | + fi |
| 83 | + done |
| 84 | + popd > /dev/null |
| 85 | + echo |
| 86 | + |
| 87 | + #---- |
| 88 | + echo "${hashalgo}: retrieving list of uploaded assets" |
| 89 | + set +o pipefail |
| 90 | + uploaded_assets=$( |
| 91 | + githubrelease asset Slicer/SlicerTestingData list ${hashalgo} | tr -d " " | grep name -A1 | sed -e "s/name://" | sed -e "s/state://" | grep -v -E "\-\-" | while read checksum |
| 92 | + do |
| 93 | + read upload_state |
| 94 | + if [[ ${upload_state} == "uploaded" ]]; then |
| 95 | + echo ${checksum} |
| 96 | + else |
| 97 | + # Remove asset partially uploaded |
| 98 | + githubrelease asset Slicer/SlicerTestingData delete ${hashalgo} ${checksum} |
| 99 | + fi |
| 100 | + done |
| 101 | + ) |
| 102 | + set -o pipefail |
| 103 | + echo |
| 104 | + |
| 105 | + rm -f ${root_dir}/${hashalgo}.md |
| 106 | + |
| 107 | + #---- |
| 108 | + echo "${hashalgo}: uploading release assets" |
| 109 | + for line in $(cat ${root_dir}/${hashalgo}.csv); do |
| 110 | + checksum=$(echo ${line} | cut -d";" -f1) |
| 111 | + filename=$(echo ${line} | cut -d";" -f2) |
| 112 | + echo "checksum [${checksum}]" |
| 113 | + if [[ "${uploaded_assets}" =~ "${checksum}" ]]; then |
| 114 | + echo "${hashalgo}: skipping ${checksum}" |
| 115 | + else |
| 116 | + githubrelease asset Slicer/SlicerTestingData upload ${hashalgo} ${root_dir}/${hashalgo}/${checksum} |
| 117 | + fi |
| 118 | + echo "* [$filename](https://github.com/Slicer/SlicerTestingData/releases/download/${hashalgo}/${checksum})" >> ${root_dir}/${hashalgo}.md |
| 119 | + done |
| 120 | + echo |
| 121 | + |
| 122 | + #---- |
| 123 | + echo "${hashalgo}: updating ${hashalgo}.csv and ${hashalgo}.md" |
| 124 | + githubrelease asset Slicer/SlicerTestingData delete ${hashalgo} "${hashalgo}.csv ${hashalgo}.md" |
| 125 | + githubrelease asset Slicer/SlicerTestingData upload ${hashalgo} ${root_dir}/${hashalgo}.csv ${root_dir}/${hashalgo}.md |
| 126 | + echo |
| 127 | + |
| 128 | + #---- |
| 129 | + echo "${hashalgo}: updating release notes" |
| 130 | + githubrelease release Slicer/SlicerTestingData edit --body "$(cat ${root_dir}/${hashalgo}.md)" ${hashalgo} |
| 131 | + |
| 132 | +done |
0 commit comments