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 |
8873
|
aaronmk
|
fi # load new aliases
|
85 |
|
|
if self_being_included; then
|
86 |
|
|
|
87 |
8871
|
aaronmk
|
#### strings
|
88 |
|
|
|
89 |
|
|
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
|
90 |
|
|
|
91 |
8873
|
aaronmk
|
sed () { env sed -"$sed_ere_flag" "$@";}
|
92 |
8871
|
aaronmk
|
|
93 |
8854
|
aaronmk
|
#### vars
|
94 |
|
|
|
95 |
|
|
set_var () { eval "$1"'="$2"'; }
|
96 |
|
|
|
97 |
8857
|
aaronmk
|
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
|
98 |
|
|
|
99 |
8859
|
aaronmk
|
# usage: local var=...; local_inv
|
100 |
|
|
alias local_inv='local "no_$var=$(test -n "${!var}" || echo 1)"'
|
101 |
|
|
|
102 |
8863
|
aaronmk
|
# usage: local prefix=..._; import_vars
|
103 |
|
|
alias import_vars="$(cat <<'EOF'
|
104 |
|
|
: "${prefix:?}"
|
105 |
|
|
local src_var dest_var
|
106 |
|
|
for src_var in $(eval echo '${!'$prefix'*}'); do
|
107 |
|
|
dest_var="${src_var#$prefix}"
|
108 |
|
|
local "$dest_var=${!src_var}"; echo_vars "$dest_var"
|
109 |
|
|
done
|
110 |
|
|
EOF
|
111 |
|
|
)"
|
112 |
|
|
|
113 |
8854
|
aaronmk
|
#### commands
|
114 |
|
|
|
115 |
8272
|
aaronmk
|
top_dir="$(dirname "$0")" # outermost script
|
116 |
|
|
|
117 |
8465
|
aaronmk
|
run_args_cmd () # runs the command line args command
|
118 |
8272
|
aaronmk
|
{
|
119 |
8292
|
aaronmk
|
test "$?" -eq 0 || return
|
120 |
8693
|
aaronmk
|
eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
121 |
8290
|
aaronmk
|
test "$#" -ge 1 || set -- all
|
122 |
8648
|
aaronmk
|
echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
|
123 |
8272
|
aaronmk
|
}
|
124 |
|
|
|
125 |
8284
|
aaronmk
|
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
126 |
8272
|
aaronmk
|
{
|
127 |
8463
|
aaronmk
|
echo_func "$@"
|
128 |
8284
|
aaronmk
|
: "${subdirs?}"
|
129 |
|
|
|
130 |
8272
|
aaronmk
|
for subdir in "${subdirs[@]}"; do
|
131 |
8280
|
aaronmk
|
"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
|
132 |
8272
|
aaronmk
|
done
|
133 |
|
|
}
|
134 |
|
|
|
135 |
8854
|
aaronmk
|
#### make
|
136 |
|
|
|
137 |
8280
|
aaronmk
|
make ()
|
138 |
|
|
{
|
139 |
8463
|
aaronmk
|
echo_func "$@"
|
140 |
8873
|
aaronmk
|
env make --directory="$top_dir" "$@"
|
141 |
8280
|
aaronmk
|
}
|
142 |
8272
|
aaronmk
|
|
143 |
8276
|
aaronmk
|
if false; then ## usage:
|
144 |
8652
|
aaronmk
|
inline_make 3<<'EOF'
|
145 |
8276
|
aaronmk
|
target:
|
146 |
|
|
$(self_dir)/cmd >$@
|
147 |
|
|
EOF
|
148 |
|
|
# target will be run automatically because it's first in the makefile
|
149 |
|
|
fi ##
|
150 |
|
|
inline_make ()
|
151 |
|
|
{
|
152 |
8653
|
aaronmk
|
echo_func "$@"
|
153 |
8640
|
aaronmk
|
local self="$(readlink -f "${BASH_SOURCE[1]}")"
|
154 |
|
|
local self_dir="$(dirname "$self")"
|
155 |
8645
|
aaronmk
|
export self self_dir
|
156 |
8640
|
aaronmk
|
|
157 |
8651
|
aaronmk
|
make --makefile=<((
|
158 |
8652
|
aaronmk
|
cat /dev/fd/3
|
159 |
8651
|
aaronmk
|
echo -n "
|
160 |
8276
|
aaronmk
|
.SUFFIXES: # turn off built-in suffix rules
|
161 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
162 |
|
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
163 |
8651
|
aaronmk
|
"
|
164 |
|
|
)|echo_stdin) "$@"
|
165 |
8276
|
aaronmk
|
}
|
166 |
8658
|
aaronmk
|
|
167 |
8854
|
aaronmk
|
#### compression
|
168 |
|
|
|
169 |
|
|
### zip
|
170 |
|
|
|
171 |
8658
|
aaronmk
|
alias zip="echo_run zip"
|
172 |
|
|
alias unzip="echo_run unzip"
|
173 |
8858
|
aaronmk
|
set_inv force
|
174 |
|
|
alias zip_newer="zip${no_force:+ -u}"
|
175 |
|
|
alias unzip_newer="unzip${no_force:+ -u} -o"
|
176 |
|
|
# -o is safe because -u only extracts newer files
|
177 |
8704
|
aaronmk
|
|
178 |
8775
|
aaronmk
|
#### databases
|
179 |
|
|
|
180 |
8868
|
aaronmk
|
# using prefixed connection vars
|
181 |
|
|
alias use_local="local prefix=local_; import_vars"
|
182 |
8864
|
aaronmk
|
alias use_remote="local prefix=remote_; import_vars"
|
183 |
8868
|
aaronmk
|
alias use_local_remote="use_local; use_remote"
|
184 |
8864
|
aaronmk
|
|
185 |
8775
|
aaronmk
|
quote='"'
|
186 |
|
|
|
187 |
|
|
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
188 |
|
|
|
189 |
|
|
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
190 |
|
|
|
191 |
8796
|
aaronmk
|
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
|
192 |
|
|
alias mk_table_esc="local table_esc; mk_esc_name table"
|
193 |
|
|
|
194 |
|
|
fi # load new aliases
|
195 |
|
|
if self_being_included; then
|
196 |
|
|
|
197 |
|
|
log_sql () { test "$verbosity" -ge 2; }
|
198 |
|
|
|
199 |
|
|
### MySQL
|
200 |
|
|
|
201 |
8862
|
aaronmk
|
# auto-adds connection/login opts when specified
|
202 |
8865
|
aaronmk
|
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
|
203 |
8860
|
aaronmk
|
{
|
204 |
|
|
echo_func "$@"
|
205 |
8870
|
aaronmk
|
if test _"$ssh_server" = _"$(hostname -f)"; then local ssh_server=; fi
|
206 |
8869
|
aaronmk
|
if test -n "$ssh_server"; then
|
207 |
|
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
208 |
|
|
fi
|
209 |
8875
|
aaronmk
|
if test -n "$schema"; then local database="${database-$schema}"; fi
|
210 |
8869
|
aaronmk
|
|
211 |
8860
|
aaronmk
|
local var=ssh_dest; local_inv
|
212 |
8865
|
aaronmk
|
echo_run ${no_ssh_dest:+env }${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
|
213 |
8878
|
aaronmk
|
${server:+ --host="$server" }${user+--user="$user" } --password\
|
214 |
|
|
${password+="$password"} ${database+--databases "$database" --tables } "$@"
|
215 |
8860
|
aaronmk
|
}
|
216 |
|
|
|
217 |
8775
|
aaronmk
|
mysql ()
|
218 |
|
|
{
|
219 |
|
|
echo_func "$@"
|
220 |
8865
|
aaronmk
|
mysql_cmd --verbose "$@"
|
221 |
8775
|
aaronmk
|
}
|
222 |
|
|
|
223 |
|
|
mysql_ANSI ()
|
224 |
|
|
{
|
225 |
|
|
echo_func "$@"
|
226 |
|
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
227 |
|
|
}
|
228 |
|
|
|
229 |
8866
|
aaronmk
|
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
|
230 |
|
|
{
|
231 |
|
|
echo_func "$@"
|
232 |
|
|
mysql_cmd --quick --lock-tables=false --set-charset \
|
233 |
|
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
234 |
|
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
235 |
|
|
}
|
236 |
|
|
|
237 |
8867
|
aaronmk
|
mysqldump_diffable ()
|
238 |
|
|
{
|
239 |
|
|
echo_func "$@"
|
240 |
|
|
mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
|
241 |
|
|
}
|
242 |
|
|
|
243 |
8796
|
aaronmk
|
### PostgreSQL
|
244 |
|
|
|
245 |
|
|
pg_copy_to ()
|
246 |
|
|
{
|
247 |
|
|
echo_func "$@"
|
248 |
8805
|
aaronmk
|
if test -z "$source"; then
|
249 |
|
|
: "${table:?}"; mk_table_esc
|
250 |
8834
|
aaronmk
|
if test -z "$limit"; then local source="$table_esc"
|
251 |
|
|
else local source="(SELECT * FROM $table_esc LIMIT $limit)"
|
252 |
8805
|
aaronmk
|
fi
|
253 |
|
|
fi
|
254 |
8834
|
aaronmk
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
255 |
8805
|
aaronmk
|
|
256 |
8807
|
aaronmk
|
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
|
257 |
8796
|
aaronmk
|
}
|
258 |
|
|
|
259 |
|
|
pg_header ()
|
260 |
|
|
{
|
261 |
|
|
echo_func "$@"
|
262 |
8806
|
aaronmk
|
local pg_copy_format="CSV HEADER" limit=0
|
263 |
8807
|
aaronmk
|
pg_copy_to "$@"|echo_stdin
|
264 |
8796
|
aaronmk
|
}
|
265 |
|
|
|
266 |
|
|
pg_export_table_no_header ()
|
267 |
|
|
{
|
268 |
|
|
echo_func "$@"
|
269 |
|
|
local pg_copy_format="CSV"
|
270 |
8807
|
aaronmk
|
pg_copy_to "$@"
|
271 |
8796
|
aaronmk
|
}
|
272 |
|
|
|
273 |
|
|
pg_export_table_to_dir_no_header ()
|
274 |
|
|
{
|
275 |
|
|
echo_func "$@"
|
276 |
|
|
local table="$1"; shift; mk_table_esc
|
277 |
8807
|
aaronmk
|
local cols="$(pg_header)"
|
278 |
8796
|
aaronmk
|
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
|
279 |
|
|
}
|
280 |
|
|
|
281 |
8704
|
aaronmk
|
fi
|