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