Last active
August 19, 2017 06:26
-
-
Save binaryphile/3cf01870516d5fffafbc84a054540160 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
app=( python3 module_design_analytics.py ) | |
root_dir=/Users/pha212/Documents/ExtFin-EFS/smoke | |
main () { | |
local dir | |
local result | |
kill_process || true | |
dir=result_$(today) | |
make_dir "$dir" | |
result=$(run_analytics "$dir") | |
copy_to_current "$result" | |
} | |
kill_process () { | |
local file=${1:-example.pid} | |
local pid | |
is_file "$file || $(raise "No $file" ) | |
pid=$(< "$file") | |
is_number "$pid" || $(raise "Invalid $file" ) | |
is_process "$pid" || $(raise "$pid - no such process" ) | |
echo "Killing $(name_of "$pid")" | |
kill_pid "$pid" && remove "$file" | |
} | |
run_analytics () { | |
local filename | |
local filepath | |
local outfile | |
filepath=$(last_in "$root_dir"/*.yaml) | |
filename=${filepath##*/} | |
echoerr "$filename" | |
outfile=$1/summary_$filename | |
"${app[@]}" "$filepath" >"$outfile" || $(raise "Error $? in app") | |
echo "$outfile" | |
} | |
copy_to_current () { | |
newer_than current.txt "$1" && cp "$1" current.txt || echo "$1 not newer" | |
} | |
last_in () { | |
local item | |
local max=$1; shift | |
for item in "$@"; do | |
[[ $item > $max ]] && max=$item | |
done | |
echo "$max" | |
} | |
echoerr () { echo "$1" >&2 ;} | |
emit () { printf 'eval eval %q\n' "$1" ;} | |
is_file () { [[ -e $1 ]] ;} | |
is_number () { [[ -n $1 && $1 != *[^[:digit:]]* ]] ;} | |
is_process () { ps "$1" >/dev/null ;} | |
kill_pid () { kill "$1" >/dev/null ;} | |
make_dir () { mkdir -p -- "$1" ;} | |
name_of () { ps -ho cmd "$1" ;} | |
newer_than () { [[ $2 -nt $1 ]] ;} | |
raise () { local rc=$?; echo "$1" >&2; emit "return ${2:-$rc}" ;} | |
remove () { rm -- "$1" ;} | |
sourced () { [[ ${FUNCNAME[1]} == 'source' ]] ;} | |
strict_mode () { set -euo pipefail ;} | |
today () { date '+%Y%m%d' ;} | |
sourced && return | |
strict_mode | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment