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