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