-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_run
More file actions
executable file
·84 lines (70 loc) · 1.72 KB
/
cache_run
File metadata and controls
executable file
·84 lines (70 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
#
# Run a command and cache the output and return code. If output from a previous
# duplicate execution exists from more recent than <lifetime> seconds ago,
# re-use the output.
#
# <lifetime> can be -1 to specify inifinate expiration
#
# usage: cache_run <lifetime> <cmd> [ <extra key component> ]
#
set -o errexit
set -o nounset
set -o pipefail
cachedir="$HOME/.generic_cache"
TIMEOUT=2
if [ ! -d "$cachedir" ]
then
mkdir "$cachedir"
fi
cache_life=$1
cmd=$2
key_extra="${3:-}" # optional
if [ ! "$cmd" ]
then
echo "usage: $0 <data_lifetime> <cmd> [ <extra key component> ]"
exit 2
fi
if which md5 &>/dev/null
then
md5=md5
elif which md5sum &> /dev/null
then
md5=md5sum
else
# don't use checksum for cache file
#
md5=cat
fi
cachefile="$cachedir"/`echo "$key_extra$cmd" | sed 's/[^0-9a-zA-Z_-]/_/g' | $md5 | tr -d \ -`
log() {
LOG="/tmp/cache_run.log"
# date >> $LOG
# echo "$*" >> $LOG
}
log "cache_life: $cache_life"
log "cachefile: $cachefile"
log "cmd: $cmd"
if [ -e "$cachefile.out" -a -e "$cachefile.err" -a -e "$cachefile.exitcode" ]
then
if [ "$cache_life" == "-1" ]
then
cat "$cachefile.out" >&1
cat "$cachefile.err" >&2
exit $(cat "$cachefile.exitcode")
fi
# check if content has expired using stdout file
#
file_mtime=`perl -MFile::stat -e 'print stat("'$cachefile.out'")->mtime'`
current_time=`date +%s`
if [ `expr $current_time - $file_mtime` -lt $cache_life ]
then
cat "$cachefile.out" >&1
cat "$cachefile.err" >&2
exit $(cat "$cachefile.exitcode")
fi
fi
trap "rm \"$cachefile.out\" \"$cachefile.err\"" INT
(eval $cmd ; echo $? > "$cachefile.exitcode" ) 2>"$cachefile.err" | tee "$cachefile.out" || true
cat "$cachefile.err" >&2
exit $(cat "$cachefile.exitcode")