-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrit-docker
executable file
·223 lines (202 loc) · 6.56 KB
/
writ-docker
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
DOCKER=$(command -v docker)
if [ -z "${DOCKER}" ] ; then
echo "Docker was not found in your PATH. Aborting."
exit 1
fi
usage() {
cat <<EOF
Usage: $0 [OPTIONS] WASMFILE FUNCNAME [ARG...]
WASI Reactor Interface Tester (container mode)
Arguments:
WASMFILE Specifies the path to the Wasm module (.wasm file)
FUNCNAME Specifies the Wasm function name to run
ARG... Specifies 0 or more arguments to pass into the Wasm function.
Complex arguments may be expressed in JSON format. May not be
used with the -b option
Options:
-a, --debug-args <ARGS> Passes the given string as additional arguments into
GDB (only valid with -d)
-b, --batch <BATCHFILE> Specifies a path to a file containing one or more
JSON-formatted inputs to use in place of in-line
arguments (see "Batch File Format", below)
-d, --debug Runs the Wasm module in GDB
-e, --expect <EXPECTSTR> Specifies an expected result in JSON form. If not
matched, the program exits with the error code 2. May
not be used with -b.
-q, --quiet Suppress result output
-s, --source <SRCDIR> Specifies a source code directory; may be used
multiple times (only valid with -d)
-v, --verbose Enable debug output
-w, --wit <WITFILE> Specifies the path to the WIT (.wit) file
Batch File Format:
A JSON-formatted file may be passed in lieu of in-line arguments. This file
must consist of either a list of lists or a list of single values. For
example, either of the following forms will work:
[ [
"John Lennon", OR [ "John Lennon", "Guitar", 1940 ],
"Paul McCartney", [ "Paul McCartney", "Bass", 1942 ],
... ...
] ]
Each entry in the outer-most list represents the arguments for a single call
into FUNCNAME.
EOF
exit 1
}
get-full-path()
{
echo $(cd $(dirname $1) && pwd)/$(basename $1)
}
DEBUG=0
SRC_DIRS=()
WIT_PATH=
BATCH_PATH=
USER_GDB_OPTS=
QUIET=0
VERBOSE=0
WRIT_EXPECT_OPT=
WRIT_EXPECT_VAL=
while [ 1 ] ; do
case "$1" in
-d|--debug)
if [ $DEBUG -eq 1 ] ; then
echo "ERROR: -d/--debug may only be specified once."
echo
usage
fi
DEBUG=1
;;
-a|--debug-args)
shift
[ -z "$1" ] && usage
USER_GDB_OPTS="${USER_GDB_OPTS} $1"
;;
-s|--source)
shift
[ -z "$1" ] && usage
if [ ! -d "$1" ] ; then
echo "ERROR: The source directory '$1' does not exist. Aborting."
exit 1
fi
SRC_DIRS+=($(get-full-path $1))
;;
-w|--wit)
shift
[ -z "$1" ] && usage
if [ -n "$WIT_PATH" ] ; then
echo "ERROR: WIT path may only be specified once."
echo
usage
fi
if [ ! -f "$1" ] ; then
echo "ERROR: The WIT file '$1' does not exist. Aborting."
exit 1
fi
WIT_PATH=$(get-full-path $1)
;;
-b|--batch)
shift
[ -z "$1" ] && usage
if [ -n "$BATCH_PATH" ] ; then
echo "ERROR: Batch path may only be specified once."
echo
usage
fi
if [ ! -f "$1" ] ; then
echo "ERROR: The file '$1' does not exist. Aborting."
exit 1
fi
BATCH_PATH=$(get-full-path $1)
;;
-e|--expect)
shift
[ -z "$1" ] && usage
if [ -n "$EXPECT" ] ; then
echo "ERROR: Expected value may only be specified once."
echo
usage
fi
WRIT_EXPECT_OPT="--expect"
WRIT_EXPECT_VAL="$1"
;;
-q|--quiet)
QUIET=1
;;
-v|--verbose)
VERBOSE=1
;;
-*)
usage
;;
*)
break
;;
esac
shift
done
[ $# -lt 2 ] && usage
if [ ${#SRC_DIRS[@]} -gt 0 -a $DEBUG -eq 0 ] ; then
echo "ERROR: The --source option is only valid with --debug."
echo
usage
fi
WASM_PATH=$(get-full-path $1)
shift
WASM_FUNC=$1
shift
if [ ! -f "${WASM_PATH}" ] ; then
echo "ERROR: The Wasm file '${WASM_PATH}' does not exist. Aborting."
exit 1
fi
WASM_DIR=$(dirname ${WASM_PATH})
WASM_NAME=$(basename ${WASM_PATH})
WRIT_CACHE_DIR=/tmp/writ-bind-cache-$(id -un)
DOCKER_RUN_ARGS="-it --rm -v ${WRIT_CACHE_DIR}:/writ-cache -v ${WASM_DIR}:/wasm-dir"
GDB_OPTS=
WRIT_OPTS=
for SRC_DIR_IDX in "${!SRC_DIRS[@]}" ; do
SRC_DIR="${SRC_DIRS[$SRC_DIR_IDX]}"
MAPPED_SRC_DIR="/src-dirs/src-${SRC_DIR_IDX}"
DOCKER_RUN_ARGS="${DOCKER_RUN_ARGS} -v ${SRC_DIR}:${MAPPED_SRC_DIR}"
GDB_OPTS="${GDB_OPTS} -d ${MAPPED_SRC_DIR}"
done
WRIT_OPTS="${WRIT_OPTS} --cache /writ-cache"
if [ -n "${BATCH_PATH}" ] ; then
DOCKER_RUN_ARGS="${DOCKER_RUN_ARGS} -v ${BATCH_PATH}:/tmp/batch.json"
WRIT_OPTS="${WRIT_OPTS} --batch /tmp/batch.json"
fi
if [ -n "${WIT_PATH}" ] ; then
WIT_NAME=$(basename ${WIT_PATH})
DOCKER_RUN_ARGS="${DOCKER_RUN_ARGS} -v ${WIT_PATH}:/tmp/${WIT_NAME}"
WRIT_OPTS="${WRIT_OPTS} --wit /tmp/${WIT_NAME}"
fi
if [ ${VERBOSE} -eq 1 ] ; then
WRIT_OPTS="${WRIT_OPTS} --verbose"
fi
if [ ${QUIET} -eq 1 ] ; then
WRIT_OPTS="${WRIT_OPTS} --quiet"
fi
if [ ${DEBUG} -eq 1 ] ; then
echo "Entering debug mode ..."
DOCKER_RUN_ARGS="${DOCKER_RUN_ARGS} --entrypoint /writ/docker/docker-run-gdb"
GDB_OPTS="${GDB_OPTS} ${USER_GDB_OPTS} -q --args /usr/local/bin/python /writ/src/writ"
WRIT_OPTS="${WRIT_OPTS} --debug-info"
fi
if [ -z "${WRIT_EXPECT_OPT}" ] ; then
"${DOCKER}" run ${DOCKER_RUN_ARGS} \
ghcr.io/singlestore-labs/writ \
${GDB_OPTS} \
${WRIT_OPTS} \
"/wasm-dir/${WASM_NAME}" \
"${WASM_FUNC}" \
"$@"
else
"${DOCKER}" run ${DOCKER_RUN_ARGS} \
ghcr.io/singlestore-labs/writ \
${GDB_OPTS} \
${WRIT_OPTS} \
${WRIT_EXPECT_OPT} "${WRIT_EXPECT_VAL}" \
"/wasm-dir/${WASM_NAME}" \
"${WASM_FUNC}" \
"$@"
fi