Project

General

Profile

1 11823 aaronmk
#!/bin/bash -e
2 1541 aaronmk
# Imports all inputs at once
3 14903 aaronmk
4
. "$(dirname "${BASH_SOURCE[0]}")"/../lib/runscripts/util.run "$@"
5
6 11823 aaronmk
set -o errexit -o pipefail # errexit since may be called from shell
7 1541 aaronmk
8 11422 aaronmk
if test "$1" = .; then set --; fi # was .-included without args, so $@ is wrong
9
10 13978 aaronmk
hidden_srcs()
11
{
12
    local hidden_srcs=1 inputs=() # with_all adds the hidden srcs to @inputs
13 14099 aaronmk
    . "$selfDir/with_all" import "$@"
14 13978 aaronmk
}
15
16 14070 aaronmk
delete_logs()
17
{
18
	if test "$delete_logs"; then
19 14072 aaronmk
		(set -x; find . -name "${n:+n=$n.}$version.log.sql" -delete)
20 14070 aaronmk
	fi
21
}
22 13985 aaronmk
23 11416 aaronmk
main()
24 1952 aaronmk
{
25
    local self="${BASH_SOURCE[0]}"
26
    local selfDir="$(dirname -- "$self")"
27 11430 aaronmk
    pushd "$selfDir/.."
28 1952 aaronmk
29 13982 aaronmk
    if ! test "$SHLVL" -ge 2; then # was run without subshell
30
        # needs subshell so errexits don't close the terminal window
31
        echo 'usage: must be run in a subshell, obtained by running `$0`' >&2
32
        return 0 # not nonzero because this will close the terminal window
33
    fi
34 1953 aaronmk
    if test "${BASH_LINENO[1]}" = 0; then # was run without initial "."
35 13981 aaronmk
        echo "usage: (*in subshell*) [version=...] [inputs=(inputs/src/ ...)]; \
36
. $self [vars...] (note initial \".\")"|fold -s >&2
37 13983 aaronmk
        return 0 # not nonzero because this will close the subshell
38 1952 aaronmk
    fi
39 14903 aaronmk
    : ${version=$(output_data=1 make -s version)}
40 10579 aaronmk
    local by_col=${by_col-1} full_import=1
41
    export version by_col full_import
42 1952 aaronmk
43 14070 aaronmk
    if test ! "${log-1}"; then local delete_logs=1; fi #log==''
44 14073 aaronmk
    trap delete_logs SIGINT SIGTERM #not EXIT b/c doesn't run until *shell* exit
45 13985 aaronmk
46 13980 aaronmk
    local log=1 # always use log files for background processes
47
48 10847 aaronmk
    # remove any leftover TNRS lockfile. usually, the PID in it would not exist,
49
    # but sometimes it now refers to an active process which blocks tnrs.make.
50 11419 aaronmk
    "rm" -f inputs/.TNRS/tnrs/tnrs.make.lock
51 10847 aaronmk
52 11421 aaronmk
    make schemas/$version/reinstall
53 6946 aaronmk
54 13984 aaronmk
    set +o errexit # don't errexit if a background process is Ctrl-C'd
55
56 13979 aaronmk
    local hidden_srcs=1; . "$selfDir/with_all" Source/import "$@"
57 6897 aaronmk
        # note that this isn't affected by $import_source
58 6593 aaronmk
    wait # wait for asynchronous commands
59 11374 aaronmk
    unset hidden_srcs
60 6593 aaronmk
61 7095 aaronmk
    local import_source= # Source tables have already been imported
62
    export import_source # must come after local
63 6897 aaronmk
64 13978 aaronmk
    hidden_srcs "$@" # separate function to avoid overwriting @inputs
65 6594 aaronmk
    wait # wait for asynchronous commands
66 5503 aaronmk
67 11286 aaronmk
    . "$selfDir/with_all" import_scrub "$@" # uses $by_col
68 6210 aaronmk
69 7418 aaronmk
    local jobs="$(jobs -p)"
70
    echo "import_scrub PIDs: $jobs" >&2
71
72 10586 aaronmk
    bin/after_import $jobs &
73 7276 aaronmk
    echo "after_import PID: $!" >&2
74 7228 aaronmk
75 11430 aaronmk
    popd
76 14073 aaronmk
    delete_logs # manually run b/c `trap EXIT` doesn't run until *shell* exit
77 1952 aaronmk
}
78 14903 aaronmk
79
on_exit