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