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