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