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