Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2 8716 aaronmk
realpath () { readlink -f -- "$1"; }
3 8703 aaronmk
4 8716 aaronmk
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
5
6 8703 aaronmk
self_not_included () # usage: if self_not_included; then ... fi
7
{
8
	test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
9
	local include_guard="$(include_guard_var "$1")"
10 8793 aaronmk
	alias self_being_included=false
11
	test -z "${!include_guard+t}" && \
12
	{ eval "$include_guard"=1; alias self_being_included=true; }
13 8703 aaronmk
}
14
15 8793 aaronmk
# to load newly-defined aliases for use in functions in the same file:
16
## fi # load new aliases
17
## if self_being_included; then
18
# this is needed because aliases defined inside an if statement are not
19
# available inside that if statement
20
21 8704 aaronmk
if self_not_included "${BASH_SOURCE[0]}"; then
22
23 8709 aaronmk
shopt -s expand_aliases
24
25 8889 aaronmk
unalias () { builtin unalias "$@" 2>&- || true; } # no error if undefined
26
27 8907 aaronmk
function extern () { (unset -f "$1"; "$@") || exit; }
28 8905 aaronmk
29 8886 aaronmk
#### exceptions
30
31
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try
32
33
try_ () { { "$@"; e="$?";} || true; }
34 8888 aaronmk
alias try='declare e; try_ ' # trailing space alias-expands next word
35 8886 aaronmk
36
catch () { test "$e" -eq "$1"; e=0; }
37
38
ignore () { catch "$@" || true; }
39
40
alias end_try='return "$e"'
41 8903 aaronmk
alias end_try_subshell='exit "$e"'
42 8886 aaronmk
43 8899 aaronmk
#### integers
44
45
let () { builtin let "$@" || true; }
46
	# "If the last ARG evaluates to 0, let returns 1" (`help let`)
47
48 8854 aaronmk
#### arrays
49 8694 aaronmk
50 8833 aaronmk
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
51 8816 aaronmk
52 8691 aaronmk
reverse () # usage: array=($(reverse args...))
53
{
54
	local i
55
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
56
}
57
58 8854 aaronmk
#### verbose output
59
60 8914 aaronmk
# usage: (stdout2stderr; cmd...) || exit
61
# `|| exit` needed on Mac because of bug where -e doesn't apply to ()
62
stdout2stderr () { exec >&2; }
63
64 8713 aaronmk
: "${verbosity:=$verbose}" "${verbosity:=0}"
65 8710 aaronmk
66 8898 aaronmk
can_log () { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging
67 8895 aaronmk
68 8900 aaronmk
# usage: in func:      inc_log_level; ...
69
#        outside func: inc_log_level; ...; dec_log_level
70
alias inc_log_level='declare verbosity="$verbosity"; let verbosity--'
71
alias dec_log_level='declare verbosity="$verbosity"; let verbosity++'
72
73 8904 aaronmk
# usage: (log_stderr; cmd...) || exit
74
# `|| exit` needed on Mac because of bug where -e doesn't apply to ()
75
log_stderr () { if ! can_log; then exec 2>/dev/null; fi; }
76
77 8915 aaronmk
log_stderr_cmd () # usage: [stdout2stderr=1] log_stderr_cmd cmd...
78
{
79
	(log_stderr
80
		if test -n "$stdout2stderr"; then stdout2stderr; fi
81
		"$@"
82
	) || exit
83
}
84 8911 aaronmk
85 8897 aaronmk
echo_cmd () { if can_log; then echo "$PS4$*" >&2; fi; }
86 8272 aaronmk
87 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
88
89 8913 aaronmk
alias echo_run_extern='echo_run log_stderr_cmd "extern"'
90 8906 aaronmk
91 8907 aaronmk
alias extern="echo_run_extern" # automatically echo external commands
92
93 8908 aaronmk
# commands that are always external
94
for cmd in env; do alias "$cmd=extern $cmd"; done; unset cmd
95 8872 aaronmk
96 8646 aaronmk
canon_rel_path ()
97
{
98
	local path="$1"
99 8716 aaronmk
	path="$(realpath "$path")" # canonicalize
100 8773 aaronmk
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
101 8646 aaronmk
	echo "$path"
102
}
103
104 8909 aaronmk
fi # load new aliases
105 8884 aaronmk
if self_being_included; then
106
107 8909 aaronmk
function echo_func ()
108 8463 aaronmk
{
109 8901 aaronmk
	inc_log_level
110 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
111
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
112 8463 aaronmk
}
113 8885 aaronmk
114 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
115
{
116 8901 aaronmk
	inc_log_level
117 8897 aaronmk
	if can_log; then
118
		echo ----- >&2
119
		tee -a /dev/stderr;
120
		echo ----- >&2
121
	else cat
122
	fi
123 8702 aaronmk
}
124 8275 aaronmk
125 8856 aaronmk
echo_vars () # usage: echo_vars var...
126 8901 aaronmk
{
127
	inc_log_level
128
	if can_log; then { echo -n "$PS4"; declare -p "${@%%=*}";} >&2; fi
129
}
130 8641 aaronmk
131
echo_export ()
132
{
133 8700 aaronmk
	builtin export "$@"
134 8641 aaronmk
	echo_vars "$@"
135
}
136
137 8713 aaronmk
if test "$verbosity" -ge 2; then
138 8711 aaronmk
	alias export="echo_export" # automatically echo env vars when they are set
139
fi
140 8642 aaronmk
141 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
142
143 8873 aaronmk
fi # load new aliases
144
if self_being_included; then
145
146 8871 aaronmk
#### strings
147
148
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
149
150 8910 aaronmk
sed () { extern sed -"$sed_ere_flag" "$@";}
151 8871 aaronmk
152 8854 aaronmk
#### vars
153
154
set_var () { eval "$1"'="$2"'; }
155
156 8857 aaronmk
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
157
158 8859 aaronmk
# usage: local var=...; local_inv
159 8888 aaronmk
alias local_inv='declare "no_$var=$(test -n "${!var}" || echo 1)"'
160 8859 aaronmk
161 8863 aaronmk
# usage: local prefix=..._; import_vars
162
alias import_vars="$(cat <<'EOF'
163
: "${prefix:?}"
164
local src_var dest_var
165
for src_var in $(eval echo '${!'$prefix'*}'); do
166
	dest_var="${src_var#$prefix}"
167
	local "$dest_var=${!src_var}"; echo_vars "$dest_var"
168
done
169
EOF
170
)"
171
172 8854 aaronmk
#### commands
173
174 8272 aaronmk
top_dir="$(dirname "$0")" # outermost script
175
176 8465 aaronmk
run_args_cmd () # runs the command line args command
177 8272 aaronmk
{
178 8292 aaronmk
	test "$?" -eq 0 || return
179 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
180 8290 aaronmk
	test "$#" -ge 1 || set -- all
181 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
182 8272 aaronmk
}
183
184 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
185 8272 aaronmk
{
186 8881 aaronmk
	echo_func
187 8284 aaronmk
	: "${subdirs?}"
188
189 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
190 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
191 8272 aaronmk
	done
192
}
193
194 8854 aaronmk
#### make
195
196 8881 aaronmk
# usage: target_filename/command () { echo_func; set_make_vars; ...; }
197 8880 aaronmk
alias set_make_vars="$(cat <<'EOF'
198
local command="${FUNCNAME##*/}"; echo_vars command
199
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
200
local target="$top_dir/$target_filename"; echo_vars target
201
EOF
202
)"
203
204 8280 aaronmk
make ()
205
{
206 8881 aaronmk
	echo_func
207 8910 aaronmk
	extern make --directory="$top_dir" "$@"
208 8280 aaronmk
}
209 8272 aaronmk
210 8276 aaronmk
if false; then ## usage:
211 8652 aaronmk
inline_make 3<<'EOF'
212 8276 aaronmk
target:
213
	$(self_dir)/cmd >$@
214
EOF
215
# target will be run automatically because it's first in the makefile
216
fi ##
217
inline_make ()
218
{
219 8881 aaronmk
	echo_func
220 8640 aaronmk
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
221
	local self_dir="$(dirname "$self")"
222 8645 aaronmk
	export self self_dir
223 8640 aaronmk
224 8651 aaronmk
	make --makefile=<((
225 8652 aaronmk
		cat /dev/fd/3
226 8651 aaronmk
		echo -n "
227 8276 aaronmk
.SUFFIXES: # turn off built-in suffix rules
228
.SECONDARY: # don't automatically delete intermediate files
229
.DELETE_ON_ERROR: # delete target if recipe fails
230 8651 aaronmk
"
231
	)|echo_stdin) "$@"
232 8276 aaronmk
}
233 8658 aaronmk
234 8854 aaronmk
#### compression
235
236
### zip
237
238 8887 aaronmk
zip ()
239
{
240 8910 aaronmk
	try extern zip "$@" >&2
241 8887 aaronmk
	ignore 12 # "zip has nothing to do" (`man zip`)
242
	end_try
243
}
244
245 8910 aaronmk
unzip () { extern unzip "$@" >&2; }
246 8902 aaronmk
247 8858 aaronmk
set_inv force
248
alias zip_newer="zip${no_force:+ -u}"
249
alias unzip_newer="unzip${no_force:+ -u} -o"
250
	# -o is safe because -u only extracts newer files
251 8704 aaronmk
252 8775 aaronmk
#### databases
253
254 8868 aaronmk
# using prefixed connection vars
255 8888 aaronmk
alias use_local="declare prefix=local_; import_vars"
256
alias use_remote="declare prefix=remote_; import_vars"
257 8868 aaronmk
alias use_local_remote="use_local; use_remote"
258 8864 aaronmk
259 8775 aaronmk
quote='"'
260
261
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
262
263
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
264
265 8888 aaronmk
alias mk_schema_esc="declare schema_esc; mk_esc_name schema"
266
alias mk_table_esc="declare table_esc; mk_esc_name table"
267 8796 aaronmk
268
fi # load new aliases
269
if self_being_included; then
270
271
log_sql () { test "$verbosity" -ge 2; }
272
273
### MySQL
274
275 8862 aaronmk
# auto-adds connection/login opts when specified
276 8865 aaronmk
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
277 8860 aaronmk
{
278 8881 aaronmk
	echo_func
279 8870 aaronmk
	if test _"$ssh_server" = _"$(hostname -f)"; then local ssh_server=; fi
280 8869 aaronmk
	if test -n "$ssh_server"; then
281
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
282
	fi
283 8875 aaronmk
	if test -n "$schema"; then local database="${database-$schema}"; fi
284 8869 aaronmk
285 8860 aaronmk
	local var=ssh_dest; local_inv
286 8910 aaronmk
	extern ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
287 8879 aaronmk
${server:+ --host="$server" }${user:+--user="$user" } --password\
288
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
289 8860 aaronmk
}
290
291 8775 aaronmk
mysql ()
292
{
293 8881 aaronmk
	echo_func
294 8865 aaronmk
	mysql_cmd --verbose "$@"
295 8775 aaronmk
}
296
297
mysql_ANSI ()
298
{
299 8881 aaronmk
	echo_func
300 8775 aaronmk
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
301
}
302
303 8866 aaronmk
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
304
{
305 8881 aaronmk
	echo_func
306 8866 aaronmk
	mysql_cmd --quick --lock-tables=false --set-charset \
307
${postgres_compat:+--compatible=postgresql --add-locks=false }\
308
${schema:+--no-data }${data:+--no-create-info }"$@"
309
}
310
311 8867 aaronmk
mysqldump_diffable ()
312
{
313 8881 aaronmk
	echo_func
314 8867 aaronmk
	mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
315
}
316
317 8796 aaronmk
### PostgreSQL
318
319
pg_copy_to ()
320
{
321 8881 aaronmk
	echo_func
322 8805 aaronmk
	if test -z "$source"; then
323
		: "${table:?}"; mk_table_esc
324 8834 aaronmk
		if test -z "$limit"; then local source="$table_esc"
325
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
326 8805 aaronmk
		fi
327
	fi
328 8834 aaronmk
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
329 8805 aaronmk
330 8807 aaronmk
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
331 8796 aaronmk
}
332
333
pg_header ()
334
{
335 8881 aaronmk
	echo_func
336 8806 aaronmk
	local pg_copy_format="CSV HEADER" limit=0
337 8807 aaronmk
	pg_copy_to "$@"|echo_stdin
338 8796 aaronmk
}
339
340
pg_export_table_no_header ()
341
{
342 8881 aaronmk
	echo_func
343 8796 aaronmk
	local pg_copy_format="CSV"
344 8807 aaronmk
	pg_copy_to "$@"
345 8796 aaronmk
}
346
347
pg_export_table_to_dir_no_header ()
348
{
349 8881 aaronmk
	echo_func
350 8796 aaronmk
	local table="$1"; shift; mk_table_esc
351 8807 aaronmk
	local cols="$(pg_header)"
352 8796 aaronmk
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
353
}
354
355 8704 aaronmk
fi