aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2024-08-15 03:38:35 +0200
committerMatěj Cepl <mcepl@cepl.eu>2024-08-15 03:50:40 +0200
commitf11b35f207ab6ce51e9d388580031696c9909023 (patch)
tree10757525b97efd97ce037fd0940a0b0aa086a372
parenteaa3d37c43e2657f53559d427ad91c6512f287db (diff)
downloadtmp-f11b35f207ab6ce51e9d388580031696c9909023.tar.gz
fix: conversion to the shellcheck-compatible bash script
Associative arrays of associative arrays in bash is crazy talk. It's better to use JSON string and jq.
-rwxr-xr-xtmp.py111
-rwxr-xr-xtmp.sh (renamed from tmp)0
2 files changed, 46 insertions, 65 deletions
diff --git a/tmp.py b/tmp.py
index 638f064..f94fc74 100755
--- a/tmp.py
+++ b/tmp.py
@@ -9,18 +9,14 @@
# Instructions for setting up appropriate folders on
# file.rdu.redhat.com see https://mojo.redhat.com/docs/DOC-14590
# See also variable URL_VARIANTS below for necessary configuration.
-set -eux
+set -eu
-# import os
-# import os.path
-# import subprocess
-# import sys
-# import urllib.parse
-
-DEBUG=0
+DEBUG=1
debug() {
- [ DEBUG -eq 1 ] && printf "%s\n" "$@"
+ if [ $DEBUG -eq 1 ] ; then
+ printf "%s\n" "$@"
+ fi
}
error() {
@@ -41,94 +37,79 @@ COMPRESSED_EXTENSIONS=('.jpg' '.webm' '.ogg' '.gz' '.bz2' '.zip' \
# a symlink to this script) it saves the file to the location defined in
# such section when called under the other name.
# So, `tmp filename` saves file filename to fedorapeople for me.
-declare -A URLS_barstool
-URLS_barstool['base_url']='mcepl@shell.eng.rdu.redhat.com:public_html/'
-URLS_barstool['target_url']='http://file.rdu.redhat.com/~mcepl/'
-URLS_barstool['shorten_api']='https://url.corp.redhat.com/new?'
-
-declare -A URLS_wotan
-URLS_wotan['base_url']='wotan:Export/'
-URLS_wotan['target_url']='https://w3.suse.de/~mcepl/'
-URLS_wotan['shorten_api']=''
-
-declare -A URLS_tmp
-URLS_tmp['base_url']='fedorapeople.org:public_html/tmp/'
-URLS_tmp['target_url']='https://mcepl.fedorapeople.org/tmp/'
-# URLS_tmp['shorten_api']='http://is.gd/create.php?format=simple&url='
-URLS_tmp['shorten_api']='https://da.gd/s?url='
-
-declare -A URLS
-# b=( "${a[@]}" )
-URLS['barstool']=( "${URLS_barstool[@]}" )
-URLS['wotan']=( "${URLS_wotan[@]" )
-URLS['tmp']=( "${URLS_tmp[@]" )
-
-# Requires GNU sed
-CMDNAME="$(echo "$(basename $0)"|sed 's/./\L&/g')"
-declare -A config
-
-echo ${@:1}
-
-for elem in "${URLS[$CMDNAME]}"
-do
- # echo "key : ${elem}" -- "value: ${config[${elem}]}"
- config[${elem}]=${URLS[$CMDNAME][${elem}]}
-done
-echo "${config[@]}"
-echo "${!config[@]}"
+export URL_VARIANTS="{
+ 'barstool': {
+ 'base_url': 'mcepl@shell.eng.rdu.redhat.com:public_html/',
+ 'target_url': 'http://file.rdu.redhat.com/~mcepl/',
+ 'shorten_api': 'https://url.corp.redhat.com/new?'
+ },
+ 'wotan': {
+ 'base_url': 'wotan:Export/',
+ 'target_url': 'https://w3.suse.de/~mcepl/'
+ },
+ 'tmp': {
+ 'base_url': 'fedorapeople.org:public_html/tmp/',
+ 'target_url': 'https://mcepl.fedorapeople.org/tmp/',
+ 'shorten_api': 'https://da.gd/s?url='
+ }
+}"
-exit 0
+# Requires GNU sed
+CMDNAME="$(basename "$0"|sed 's/./\L&/g')"
+CONFIG="$(echo "$URL_VARIANTS" |tr "'" '"'|jq -r ".$CMDNAME")"
+BASE_URL="$(echo "$CONFIG" | jq -r ".base_url")"
+TARGET_URL="$(echo "$CONFIG" | jq -r ".target_url")"
[ $# -lt 1 ] && exit 1
-
-
-for processed_file in ${@:1} sys.argv[1:] ; do
- st=os.stat(processed_file).st_size
- file_ext=os.path.splitext(processed_file)[1]
- debug "size of $processed_file is ${st}"
+for processed_file in "${@:1}" ; do
+ st=$(stat -c "%s" "$processed_file")
+ base_name="$(basename -- "$processed_file")"
+ file_ext="${base_name##*.}"
+ debug "size of $processed_file is ${st} bytes"
debug "extension is ${file_ext}"
- if st > 1000000 and ${file,,} not in COMPRESSED_EXTENSIONS ; then
+
+ # shellcheck disable=SC2076
+ if [ "$st" -gt 1000000 ] && [[ ! " ${COMPRESSED_EXTENSIONS[*]} " =~ " ${file_ext} " ]] ; then
debug "file ${processed_file} should be compressed (${st} b)"
- gzip --keep --best ${processed_file} || error 'Compressing the file failed!'
+ gzip --keep --best "${processed_file}" || error 'Compressing the file failed!'
fname="${processed_file}.gz"
COMPRESSED=1
- else:
+ else
debug "file ${processed_file} should not be compressed (${st} b)"
fname="${processed_file}"
COMPRESSED=0
fi
- chmod 644 fname
+ chmod 644 "$fname"
# To make scp happy
- modified_fname="$(echo ${fname}|tr ':' '_')"
- mv "${fname}" "${modified_fname}"
+ modified_fname="$(echo "${fname}"|tr ':' '_')"
+ mv -n "${fname}" "${modified_fname}"
debug "Unshorten fname=${modified_fname}"
- scp "${modified_fname}" "${config['base_url']}"
+ scp "${modified_fname}" "${BASE_URL}"
- target_URL="${config['target_url']}$(basename ${modified_fname})"
+ target_URL="${TARGET_URL}$(basename "${modified_fname}")"
# Make target_URL into a safe URL
- safe_target_URL=urllib.parse.quote(target_URL).replace('%3A', ':', 1)
-
+ safe_target_URL="${target_URL// /%20}"
echo "${safe_target_URL}"
# curl -s 'http://is.gd/create.php?format=simple&url=www.example.com'
# http://is.gd/MOgh5q
-
- if 'shorten_api' in config ; then
- shortened_URL="$(curl -s "${config['shorten_api']}""${safe_target_URL}")"
+ SHORTEN_API="$(echo "$CONFIG" | jq -r ".shorten_api")"
+ if [ "x${SHORTEN_API}" != "xnull" ] ; then
+ shortened_URL="$(curl -s "${SHORTEN_API}""${safe_target_URL}")"
echo "${shortened_URL}"
fi
# The script should have no side-effects
if [ "${COMPRESSED}" -eq 1 ] ; then
rm "${modified_fname}"
- else:
+ else
mv "${modified_fname}" "${fname}"
fi
done
diff --git a/tmp b/tmp.sh
index 67e0145..67e0145 100755
--- a/tmp
+++ b/tmp.sh