-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp.sh
executable file
·363 lines (306 loc) · 7.67 KB
/
wp.sh
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
#
# Copyright 2014 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd
#
# User facing executables for webpipe.
#
# Usage:
# ./wp.sh <function name>
set -o nounset
set -o pipefail
#
# Path stuff
#
# cross platform readlink -f
realpath() {
local path=$0
local ostype=${OSTYPE:-}
# test if ostype begins with "darwin". ignore stdout of expr.
if expr $ostype : darwin >/dev/null; then
python -S -c 'import os,sys; print os.path.realpath(sys.argv[1])' $path
else
readlink -f $path
fi
}
# dereference symlinks in $0
readonly THIS_DIR=$(dirname $(realpath $0))
webpipe_dev=${WEBPIPE_DEV:-}
if test -z "$webpipe_dev"; then
export PYTHONPATH=$THIS_DIR
fi
#
# Utilities
#
log() {
echo 1>&2 "$@"
}
die() {
log "$@"
exit 1
}
readonly INPUT_DIR=~/webpipe/input
# is watched necessary anymore? no reason most people can't shell out to nc.
# but it is still possible.
readonly WATCH_DIR=~/webpipe/watched
# 'nc' is required for wp show and in the R client. Right now we check at 'wp
# init' time.
check-tools() {
local err="'nc' not found. On Ubuntu/Debian, run 'sudo apt-get install netcat'"
which nc >/dev/null || die "$err"
}
#
# Public
#
# Set up the default dir to watch.
init() {
# Where files from the input dirs are moved and renamed to, so they are HTML
# and shell safe.
mkdir --verbose -p \
~/webpipe/renamed \
$INPUT_DIR/sink \
$WATCH_DIR \
~/webpipe/plugins
# Where user can install their own plugins
# Named pipe that receives paths relative to the sink dir. We remove and
# create the pipe to reset it?
# NOTE: We want at least two ways of showing files:
# - put something in the 'watched' dir
# - wp show <filename>
#
# A named pipe can't handle both. You would need a Unix socket.
#rm --verbose ~/webpipe/input
#mkfifo ~/webpipe/input
#local exit_code=$?
#if test $exit_code -eq 0; then
# log "Created ~/webpipe/input"
#else
# log "mkfifo error"
#fi
# Do this last, since it dies.
check-tools
log "wp: init done"
}
# People can run print-events | xrender to directly to render on a different
# host. For now we keep them separate, so we have an explicit and flexible
# pipeline.
print-events() {
local input_dir=${1:-$WATCH_DIR}
# --quiet: only print events
# --monitor: loop forever
# --format %f: print out the filename in the directory we're watching
# close_write: when a file is closed after writing
# create: creating a symlink (ln -sf of a dir alway does DELETE then CREATE)
log "wp: Watching $input_dir"
inotifywait --monitor --quiet -e close_write,create $input_dir --format '%f'
}
# render files to HTML.
xrender() {
$THIS_DIR/webpipe/xrender.py "$@"
}
# serve HTML and static files.
serve() {
$THIS_DIR/webpipe/serve.py "$@"
}
# Better/more portable server than nc. Should only be used when we don't care
# about running on Mac.
socat-listen() {
local port=$1
# -u: unidirection (</dev/null would be the same)
# fork: fork a child process for each connection; gives us the "loop"
# behavior.
socat -u TCP4-LISTEN:$port,fork -
}
# Run the whole pipeline.
#
# TODO:
# - Add flags that are common: --user-dir, --port (for server), etc.
# - What about rendering flags?
#
# $ webpipe run --port 8888
run() {
local sessionName=${1:-}
local stamp=$(date +%Y-%m-%d)
if test -z "$sessionName"; then
sessionName=$stamp
else
# Special syntax: + prepends the current date.
# run +foo ==> session name is 2014-03-30-foo
sessionName=$(echo $sessionName | sed s/+/$stamp-/)
fi
local session=~/webpipe/s/$sessionName
mkdir -p $session
export PYTHONUNBUFFERED=1
# TODO: Add xrender --listen-port 8988. If port is specified, then instead of
# reading from stdin, we listen on a port.
#
# nc servers don't reliably work the same way on all machines. socat isn't
# installed.
#
# We might want recv --listen-port too, but maybe later. And even server
# --listen-port.
xrender -p 8988 $INPUT_DIR $session \
| serve serve $session
}
# Like run, but just test latency.
noop() {
local sessionName=${1:-}
local stamp=$(date +%Y-%m-%d)
if test -z "$sessionName"; then
sessionName=$stamp
else
# Special syntax: + prepends the current date.
# run +foo ==> session name is 2014-03-30-foo
sessionName=$(echo $sessionName | sed s/+/$stamp-/)
fi
export PYTHONUNBUFFERED=1
# make sure it succeeds
echo foo >/tmp/foo.txt
echo foo.txt \
| xrender /tmp /tmp \
| serve noop
}
# NOTE: Use nc for a CLIENT only. Anything else isn't portable.
nc-send() {
local port=$1
nc localhost $port
}
# Show a file, specifying file type first.
#
# $ wp show-as txt NOTES
# $ gen-html | wp show-as html
#
# wp as could be an alias.
show-as() {
local ext=$1
shift
# no args; read from stdin
if test $# -eq 0; then
if test -z "$ext"; then
ext=txt
fi
local tempfile=$INPUT_DIR/sink/$$.$ext
cat > $tempfile
echo $tempfile | nc-send 8988
fi
# TODO: respect $ext here. Need to send it as a TNET message I suppose.
for filename in "$@"; do
if test ${filename:0:1} = /; then
echo "$filename" | nc-send 8988
else
# relative path, make it absolute.
echo "$PWD/$filename" | nc-send 8988
fi
done
}
as() {
show-as "$@"
}
# Show a file (webpipe client).
# If no filename is given, then it reads from stdin. File type is inferred
# from extension.
#
# $ wp show foo.csv
# $ ls -l | wp show
show() {
show-as '' "$@"
}
publish() {
$THIS_DIR/webpipe/publish.py "$@"
}
# set up reverse tunnel for receiving files.
wp-ssh() {
log "webpipe: Setting up SSH reverse tunnel from remote port 8987 to localhost port 8987."
ssh -R 8987:localhost:8987 "$@"
}
# Other actions:
# - sink (move from the stub?)
# - show <files...>
# - watch -- start the inotify daemon on watched
#
# Individual actions (for advanced users):
# - xrender
# - serve
help() {
local topic=${1:-}
case "$topic" in
advanced)
cat $THIS_DIR/doc/wp-help-advanced.txt
;;
*)
cat $THIS_DIR/doc/wp-help.txt
;;
esac
}
recv() {
export PYTHONUNBUFFERED=1
$THIS_DIR/webpipe/recv.py "$@"
}
# TODO: This should be documented. This is in conjunction with wp ssh, and
# remove wp-stub.sh send, I think.
# NOTE: We're using socat here because this is for Linux people? Could also
# add recv -p (factor the code out of xrender and put it in util/common).
run-recv() {
socat-listen 8987 \
| recv ~/webpipe/input \
| while read line; do echo $line | nc-send 8988; done
}
#
# Introspection
#
# So people can do scp $(webpipe stub-path) [email protected]:bin
stub-path() {
local path=$THIS_DIR/wp-stub.sh
if test -f $path; then
echo $path
else
die "Invalid installation; $path doesn't exist"
fi
}
scp-stub() {
local path=$(stub-path)
scp $path "$@"
}
version() {
# TODO: Show the actual version? For now just show the package-dir.
# assuming that has the version.
package-dir
}
# Use this to find stub path?
# TODO: Should there also be a user-dir thing? I think that should always be
# ~/webpipe.
package-dir() {
echo $THIS_DIR
}
if test $# -eq 0; then
help
exit 0
fi
case $1 in
# generally public ones
help|init|run|noop|run-recv|package-dir|publish|show|show-as|as|stub-path|scp-stub|version)
"$@"
;;
ssh)
# need to special case this to avoid recursion
shift
wp-ssh "$@"
;;
# advanced ones
recv|serve|xrender)
"$@"
;;
# demo
sendrecv-demo)
"$@"
;;
--help|-h)
help
;;
*)
# uncomment to run internal functions
#"$@"
die "wp: Invalid action '$1'"
;;
esac