Project

General

Profile

1
#!/bin/bash -e
2
# Imports all inputs at once
3

    
4
. "$(dirname "${BASH_SOURCE[0]}")"/../lib/runscripts/util.run "$@"
5

    
6
hidden_srcs()
7
{
8
    local hidden_srcs=1 inputs=() # with_all adds the hidden srcs to @inputs
9
    . "$selfDir/with_all" import "$@"
10
}
11

    
12
delete_logs()
13
{
14
	if test "$delete_logs"; then
15
		(set -x; find . -name "${n:+n=$n.}$version.log.sql" -delete)
16
	fi
17
}
18

    
19
main()
20
{
21
    local self="${BASH_SOURCE[0]}"
22
    local selfDir="$(dirname -- "$self")"
23
    pushd "$selfDir/.."
24
    
25
    if ! test "$SHLVL" -ge 2; then # was run without subshell
26
        # needs subshell so errexits don't close the terminal window
27
        echo 'usage: must be run in a subshell, obtained by running `$0`' >&2
28
        return 0 # not nonzero because this will close the terminal window
29
    fi
30
    if test "${BASH_LINENO[1]}" = 0; then # was run without initial "."
31
        echo "usage: (*in subshell*) [version=...] [inputs=(inputs/src/ ...)]; \
32
. $self [vars...] (note initial \".\")"|fold -s >&2
33
        return 0 # not nonzero because this will close the subshell
34
    fi
35
    : ${version=$(output_data=1 make -s version)}
36
    local by_col=${by_col-1} full_import=1
37
    export version by_col full_import
38
    
39
    if test ! "${log-1}"; then local delete_logs=1; fi #log==''
40
    trap delete_logs SIGINT SIGTERM #not EXIT b/c doesn't run until *shell* exit
41
    
42
    local log=1 # always use log files for background processes
43
    
44
    # remove any leftover TNRS lockfile. usually, the PID in it would not exist,
45
    # but sometimes it now refers to an active process which blocks tnrs.make.
46
    "rm" -f inputs/.TNRS/tnrs/tnrs.make.lock
47
    
48
    make schemas/$version/reinstall
49
    
50
    set +o errexit # don't errexit if a background process is Ctrl-C'd
51
    
52
    local hidden_srcs=1; . "$selfDir/with_all" Source/import "$@"
53
        # note that this isn't affected by $import_source
54
    wait # wait for asynchronous commands
55
    unset hidden_srcs
56
    
57
    local import_source= # Source tables have already been imported
58
    export import_source # must come after local
59
    
60
    hidden_srcs "$@" # separate function to avoid overwriting @inputs
61
    wait # wait for asynchronous commands
62
    
63
    . "$selfDir/with_all" import_scrub "$@" # uses $by_col
64
    
65
    local jobs="$(jobs -p)"
66
    echo "import_scrub PIDs: $jobs" >&2
67
    
68
    bin/after_import $jobs &
69
    echo "after_import PID: $!" >&2
70
    
71
    popd
72
    delete_logs # manually run b/c `trap EXIT` doesn't run until *shell* exit
73
}
74

    
75
on_exit
(31-31/87)