From 83dd34aeeb8ee3f44dd1726b74e91c7e334d17a6 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 28 Jun 2024 17:16:51 +0200 Subject: WIP: beginning of porting barstool to a bash script. --- tmp.py | 172 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 94 insertions(+), 78 deletions(-) diff --git a/tmp.py b/tmp.py index 67e0145..9db6142 100755 --- a/tmp.py +++ b/tmp.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/bin/bash # # "THE BEER-WARE LICENSE" (Revision 42): # Matěj Cepl, wrote this file. As long as you retain @@ -9,21 +9,30 @@ # 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 -import logging -import os -import os.path -import subprocess -import sys -import urllib.parse -logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', - level=logging.INFO) - -COMPRESSED_EXTENSIONS = ['.jpg', '.webm', '.ogg', '.gz', '.bz2', '.zip', - '.xz', '.odp', '.odt', '.ods', '.mp4', '.mp3', - '.png', '.pdf', '.apk', '.rpm', '.epub', '.mkv', - '.avi', '.jar', '.opus', '.ogv', '.flac', '.msi', - '.m4a'] +# import os +# import os.path +# import subprocess +# import sys +# import urllib.parse + +DEBUG=0 + +debug() { + [ DEBUG -eq 1 ] && printf "%s\n" "$@" +} + +error() { + printf "%s\n" "$@" + exit 1 +} + +COMPRESSED_EXTENSIONS=('.jpg' '.webm' '.ogg' '.gz' '.bz2' '.zip' \ + '.xz' '.odp' '.odt' '.ods' '.mp4' '.mp3' \ + '.png' '.pdf' '.apk' '.rpm' '.epub' '.mkv' \ + '.avi' '.jar' '.opus' '.ogv' '.flac' '.msi' \ + '.m4a') # THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various # attributes to correspond to your situation. @@ -32,83 +41,90 @@ 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. -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': 'http://is.gd/create.php?format=simple&url=' - 'shorten_api': 'https://da.gd/s?url=' - } -} - -cmdname = os.path.basename(sys.argv[0]).lower() -config = URL_VARIANTS[cmdname] - -if not os.path.exists(sys.argv[1]): - sys.exit(1) - -for processed_file in sys.argv[1:]: - st = os.stat(processed_file).st_size - file_ext = os.path.splitext(processed_file)[1] - logging.debug('size of {} is {:,}.'.format(processed_file, st)) - logging.debug('extension is {}'.format(file_ext)) - if st > 1000000 and file_ext.lower() not in COMPRESSED_EXTENSIONS: - logging.debug( - "file {} should be compressed ({:,} b)".format( - processed_file, st)) - try: - ret = subprocess.check_call( - ['gzip', '--keep', '--best', processed_file]) - except subprocess.CalledProcessError: - logging.error('Compressing the file failed!') - raise - fname = processed_file + '.gz' - COMPRESSED = True +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 +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 + +for elem in "${!URLS[$CMDNAME]}" +do + # echo "key : ${elem}" -- "value: ${config[${elem}]}" + config[${elem}]=${URLS[$CMDNAME][${elem}]} +done + +echo ${config[@]} +echo ${!config[@]} + + +exit 0 + +[ $# -lt 1 ] && exit 1 + +for processed_file in 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}" + debug "extension is ${file_ext}" + if st > 1000000 and ${file,,} not in COMPRESSED_EXTENSIONS ; then + debug "file ${processed_file} should be compressed (${st} b)" + gzip --keep --best ${processed_file} || error 'Compressing the file failed!' + fname="${processed_file}.gz" + COMPRESSED=1 else: - logging.debug( - 'file {} should not be compressed ({:,} b)'.format( - processed_file, st)) - fname = processed_file - COMPRESSED = False + debug "file ${processed_file} should not be compressed (${st} b)" + fname="${processed_file}" + COMPRESSED=0 + fi - os.chmod(fname, 0o0644) + chmod 644 fname # To make scp happy - modified_fname = fname.replace(':', '_') - os.rename(fname, modified_fname) + modified_fname="$(echo ${fname}|tr ':' '_')" + mv "${fname}" "${modified_fname}" - logging.debug('Unshorten fname = {}'.format(modified_fname)) + debug "Unshorten fname=${modified_fname}" - subprocess.check_call(['scp', '{}'.format(modified_fname), - config['base_url']]) + scp "${modified_fname}" "${config['base_url']}" - target_URL = config['target_url'] + os.path.basename(modified_fname) + target_URL="${config['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=urllib.parse.quote(target_URL).replace('%3A', ':', 1) - print(safe_target_URL) + 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: - shortened_URL = subprocess.check_output( - ['curl', '-s', config['shorten_api'] + - safe_target_URL]).decode().strip() - print(shortened_URL) + if 'shorten_api' in config ; then + shortened_URL="$(curl -s "${config['shorten_api']}""${safe_target_URL}")" + echo "${shortened_URL}" + fi # The script should have no side-effects - if COMPRESSED: - os.unlink(modified_fname) + if [ "${COMPRESSED}" -eq 1 ] ; then + rm "${modified_fname}" else: - os.rename(modified_fname, fname) + mv "${modified_fname}" "${fname}" + fi +done -- cgit