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