Project

General

Profile

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