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