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