-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.sh
More file actions
executable file
·180 lines (147 loc) · 4.11 KB
/
python.sh
File metadata and controls
executable file
·180 lines (147 loc) · 4.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
#
# wrapper on some venv/bin/python executable which logs command start and exit
#
# usage:
# 1) replace existing python with this script using: ./python.sh <path to venv>
# 2) specify next environment variables:
# * PYTHON_WRAPPER_OUT_FILE: path to out file (date patterns are allowed)
# * PYTHON_WRAPPER_ERR_FILE='': path to err file (date patterns are allowed); if not set, errors will be printed only to PYTHON_WRAPPER_OUT_FILE
# * PYTHON_WRAPPER_USE_STDOUT=1: 1 means to print usual messages to /dev/stdout too
# * PYTHON_WRAPPER_USE_STDERR=1: 1 means to print error messages to /dev/stderr; otherwise will not be printed
# * also there are supplement variables PYTHON_WRAPPER_SUPPLEMENT_OUT_FILE_{1..9}, PYTHON_WRAPPER_SUPPLEMENT_ERR_FILE_{1..9}
# *
# 3) execute venv/bin/python as always
#
#region replace actual python3.* with this script
if [ $# -eq 1 ] && [ -n "$1" ] && [ -e "$1/bin/python3" ]
then
set -e
pyexes=( $1/bin/python3.* )
pyexe="${pyexes[0]}"
if [ "$pyexe" == "$1/bin/python3.*" ]
then
echo "No python3.* found in $1/bin"
exit 1
fi
src="${BASH_SOURCE[0]}"
copy=1
if [ "${#pyexes[@]}" == "1" ] # check if not already replaced
then
mv "$pyexe" "${pyexe}_"
elif [ "${#pyexes[@]}" != "2" ] || [ "${pyexes[1]}" != "${pyexe}_" ]
then
echo "Different python3.* exist but seems like it is not the result of the actual operation: ${pyexes[@]}"
exit 3
else
copy=
if [ "$(cat "$src")" != "$(cat "$pyexe")" ]
then
echo "Found old wrapper, will be replaced"
copy=1
fi
fi
if [ -n "$copy" ]
then
umask 003
cp "$src" "$pyexe"
fi
if [ ! -x "$pyexe" ]
then
echo "wrapper is applied but is not executable, perform manually: chmod +x $pyexe"
exit 3
fi
echo "wrapper is applied: $pyexe -> ${pyexe}_"
exit 0
fi
#endregion
#region check whether replacement is already performed
pyexe="$(readlink -f "$0")_"
if [ ! -e "$pyexe" ]
then
echo "wrapper is not applied"
echo "firstly u need to perform something like: ./python.sh <path to venv>"
echo "executed: $0 $*"
exit 2
fi
#endregion
set -e
outfile="$(date +"${PYTHON_WRAPPER_OUT_FILE}")"
errfile="$(date +"${PYTHON_WRAPPER_ERR_FILE}")"
outfiles=( "$outfile" )
errfiles=( "$errfile" )
for i in {1..9}
do
name="PYTHON_WRAPPER_SUPPLEMENT_OUT_FILE_$i"
value="$(date +"${!name}")"
if [ -n "$value" ]
then
outfiles+=( "$value" )
fi
name="PYTHON_WRAPPER_SUPPLEMENT_ERR_FILE_$i"
value="$(date +"${!name}")"
if [ -n "$value" ]
then
errfiles+=( "$value" )
fi
done
function _log {
local -
set -e
if [ -z "$1" ]
then
echo "saves message to log file and optionally prints to out/err descriptor"
echo "usage: _log <message> <type=out>"
return 0
fi
if [ $# -lt 1 ] || [ $# -gt 2 ]
then
echo "requires >=1,<=2 arguments, got $# ($@)!" &>/dev/stderr
_log
return 1
fi
local message="$1" kind="${2:-out}" day="$(date +"%Y-%m-%d")" time="$(date +"%T.%2N")"
local record="$day $time [$BASHPID]: $message"
local files=( ) file
files+=("${outfiles[@]}")
if [ "$kind" == "err" ] # in err print message to both err and out
then
files+=("${errfiles[@]}")
if [ "${PYTHON_WRAPPER_USE_STDERR:-1}" == "1" ]
then
echo "$record" > /dev/stderr
fi
elif [ "${PYTHON_WRAPPER_USE_STDOUT:-1}" == "1" ]
then
echo "$record"
fi
for file in "${files[@]}"
do
if [ -n "$file" ]
then
mkdir -p "$(dirname "$file")"
echo "$record" >> "$file"
fi
done
}
function _err-log {
if [ -z "$1" ]
then
echo "saves message to make err.log file"
echo "usage: _err-log <message>"
return 0
fi
_log "$1" err
}
set +e
_log "executing: $0 $*"
"$pyexe" "$@"
rc=$?
m="exited with code: $rc"
if [ $rc -ne 0 ]
then
_err-log "$m"
else
_log "$m"
fi
exit $rc