Project

General

Profile

1
#!/bin/bash -e
2
realpath () { readlink -f -- "$1"; }
3

    
4
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
5

    
6
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
	alias self_being_included=false
11
	test -z "${!include_guard+t}" && \
12
	{ eval "$include_guard"=1; alias self_being_included=true; }
13
}
14

    
15
# 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
if self_not_included "${BASH_SOURCE[0]}"; then
22

    
23
shopt -s expand_aliases
24

    
25
#### arrays
26

    
27
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
28

    
29
reverse () # usage: array=($(reverse args...))
30
{
31
	local i
32
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
33
}
34

    
35
#### 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
#### verbose output
42

    
43
: "${verbosity:=$verbose}" "${verbosity:=0}"
44

    
45
echo_cmd () { echo "$PS4$*" >&2; }
46

    
47
echo_run () { echo_cmd "$@"; "$@"; }
48

    
49
canon_rel_path ()
50
{
51
	local path="$1"
52
	path="$(realpath "$path")" # canonicalize
53
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
54
	echo "$path"
55
}
56

    
57
# usage: echo_func "$@"
58
echo_func ()
59
{
60
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
61
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
62
}
63

    
64
echo_stdin () # usage: input|echo_stdin|cmd
65
{
66
	echo ----- >&2
67
	tee -a /dev/stderr;
68
	echo ----- >&2
69
}
70

    
71
echo_vars () # usage: echo_vars var...
72
{ { echo -n "$PS4"; declare -p "${@%%=*}";} >&2; }
73

    
74
echo_export ()
75
{
76
	builtin export "$@"
77
	echo_vars "$@"
78
}
79

    
80
if test "$verbosity" -ge 2; then
81
	alias export="echo_export" # automatically echo env vars when they are set
82
fi
83

    
84
usage () { echo "Usage: $1" >&2; (exit 2); }
85

    
86
#### vars
87

    
88
set_var () { eval "$1"'="$2"'; }
89

    
90
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
91

    
92
# usage: local var=...; local_inv
93
alias local_inv='local "no_$var=$(test -n "${!var}" || echo 1)"'
94

    
95
# 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
#### commands
107

    
108
top_dir="$(dirname "$0")" # outermost script
109

    
110
run_args_cmd () # runs the command line args command
111
{
112
	test "$?" -eq 0 || return
113
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
114
	test "$#" -ge 1 || set -- all
115
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
116
}
117

    
118
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
119
{
120
	echo_func "$@"
121
	: "${subdirs?}"
122
	
123
	for subdir in "${subdirs[@]}"; do
124
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
125
	done
126
}
127

    
128
#### make
129

    
130
make ()
131
{
132
	echo_func "$@"
133
	echo_run env make --directory="$top_dir" "$@"
134
}
135

    
136
if false; then ## usage:
137
inline_make 3<<'EOF'
138
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
	echo_func "$@"
146
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
147
	local self_dir="$(dirname "$self")"
148
	export self self_dir
149
	
150
	make --makefile=<((
151
		cat /dev/fd/3
152
		echo -n "
153
.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
"
157
	)|echo_stdin) "$@"
158
}
159

    
160
#### compression
161

    
162
### zip
163

    
164
alias zip="echo_run zip"
165
alias unzip="echo_run unzip"
166
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

    
171
#### databases
172

    
173
# using prefixed connection vars
174
alias use_local="local prefix=local_; import_vars"
175
alias use_remote="local prefix=remote_; import_vars"
176
alias use_local_remote="use_local; use_remote"
177

    
178
quote='"'
179

    
180
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
181

    
182
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
183

    
184
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
185
alias mk_table_esc="local table_esc; mk_esc_name table"
186

    
187
fi # load new aliases
188
if self_being_included; then
189

    
190
log_sql () { test "$verbosity" -ge 2; }
191

    
192
### MySQL
193

    
194
# auto-adds connection/login opts when specified
195
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
196
{
197
	echo_func "$@"
198
	local var=ssh_dest; local_inv
199
	echo_run ${no_ssh_dest:+env }${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
200
${server:+ --host="$server" }${user+--user="$user" }\
201
--password${password+="$password"} "$@"
202
}
203

    
204
mysql ()
205
{
206
	echo_func "$@"
207
	mysql_cmd --verbose "$@"
208
}
209

    
210
mysql_ANSI ()
211
{
212
	echo_func "$@"
213
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
214
}
215

    
216
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
217
{
218
	echo_func "$@"
219
	mysql_cmd --quick --lock-tables=false --set-charset \
220
${postgres_compat:+--compatible=postgresql --add-locks=false }\
221
${schema:+--no-data }${data:+--no-create-info }"$@"
222
}
223

    
224
mysqldump_diffable ()
225
{
226
	echo_func "$@"
227
	mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
228
}
229

    
230
### PostgreSQL
231

    
232
pg_copy_to ()
233
{
234
	echo_func "$@"
235
	if test -z "$source"; then
236
		: "${table:?}"; mk_table_esc
237
		if test -z "$limit"; then local source="$table_esc"
238
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
239
		fi
240
	fi
241
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
242
	
243
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
244
}
245

    
246
pg_header ()
247
{
248
	echo_func "$@"
249
	local pg_copy_format="CSV HEADER" limit=0
250
	pg_copy_to "$@"|echo_stdin
251
}
252

    
253
pg_export_table_no_header ()
254
{
255
	echo_func "$@"
256
	local pg_copy_format="CSV"
257
	pg_copy_to "$@"
258
}
259

    
260
pg_export_table_to_dir_no_header ()
261
{
262
	echo_func "$@"
263
	local table="$1"; shift; mk_table_esc
264
	local cols="$(pg_header)"
265
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
266
}
267

    
268
fi
(45-45/50)