Project

General

Profile

1 183 aaronmk
#!/bin/bash
2 106 aaronmk
# Tests map on all input/*.csv
3
# Usage: env [n=<num-rows>] self
4 53 aaronmk
5
selfDir="$(dirname -- "$0")"
6
cd "$selfDir"
7
8 183 aaronmk
shopt -s nullglob
9
10 175 aaronmk
tests_n=2
11 181 aaronmk
testMode=
12
test -n "$n" || export n="$tests_n" testMode=1
13 175 aaronmk
14 252 aaronmk
test "$(hostname)" = nimoy && isNimoy=t || isNimoy=
15 274 aaronmk
test -n "$isNimoy" && . ../bin/env_password mysql_password "your MySQL"
16 252 aaronmk
17 272 aaronmk
bien_password="$(cat ../config/bien_password)"
18 131 aaronmk
19 268 aaronmk
make --directory=../mappings
20 185 aaronmk
mkdir -p output
21
22 135 aaronmk
function trace()
23 118 aaronmk
{
24 135 aaronmk
    (
25 181 aaronmk
        env echo -n "$PS4" # bug in sh's built-in echo prints the -n
26 135 aaronmk
        for arg in "$@"; do printf "%q " "$arg"; done
27
        echo "${_in+<$_in}" "${_out+>$_out}"
28
    ) >&2
29 118 aaronmk
}
30
31 319 aaronmk
function fromFile()
32
{
33
    (_in="$in"; trace)
34 322 aaronmk
    ("$@" <"$in") || exit # run in a sandbox
35 319 aaronmk
}
36
37 161 aaronmk
function map()
38 132 aaronmk
{
39 169 aaronmk
    test -e "$in" || return 1
40
    local ext="${in##*.}" # after last "."
41 319 aaronmk
    (for map in "../mappings/$src-$out_fmt."$table".csv"; do
42 169 aaronmk
        table="${map%.*}" # remove extension
43
        table="${table##*.}" # after last "."
44 252 aaronmk
        {
45 161 aaronmk
            if test "$ext" == "sh"; then
46
                trace . "$in"
47 252 aaronmk
                (
48
                    . "$in"
49 257 aaronmk
50
                    # defaults
51
                    test -n "$host" || host=localhost
52
                    if test -z "$user"; then
53
                        if test -n "$isNimoy" -a "$engine" = MySQL; then
54
                            user="$USER" password="$mysql_password"
55
                        else
56
                            user=bien password="$bien_password"
57
                        fi
58 252 aaronmk
                    fi
59 257 aaronmk
60
                    for var in engine host user password database; do
61
                        export in_$var="${!var}"
62
                    done
63 319 aaronmk
                    "$@"
64 252 aaronmk
                )
65 319 aaronmk
            else fromFile "$@"
66 161 aaronmk
            fi
67 319 aaronmk
        } || exit
68 322 aaronmk
    done) || exit # run in a sandbox
69 132 aaronmk
}
70
71 135 aaronmk
function toXml()
72
{
73 169 aaronmk
    local stem="$orig_src.$table.$out_fmt"
74
    local out="output/$stem${method+.$method}.xml"
75
    local accepted="accepted_output/$stem.xml"
76 322 aaronmk
    (set -x; ../map "$map" >"$out") || exit
77
    (set -x; ${testMode:+diff "$accepted" "$out"}) || exit
78 118 aaronmk
}
79
80 274 aaronmk
vegbienDest=../bin/vegbien_dest
81 222 aaronmk
82 135 aaronmk
function toDb()
83 118 aaronmk
{
84 222 aaronmk
    trace . "$vegbienDest"
85 322 aaronmk
    (. "$vegbienDest"; set -x; ../map ${map:+"$map"}) || exit
86 118 aaronmk
}
87
88 161 aaronmk
for in in input/*; do
89 118 aaronmk
    stem="$(basename -- "${in%.*}")" # remove extension and dir
90 169 aaronmk
    ext="${in##*.}" # after last "."
91
    src="${stem%.*}" # before last ".", if any
92
    orig_src="$src"
93
    if test "$ext" == "sh"; then table="*" # use all tables with a mapping
94
    else table="${stem##*.}" # after last "."
95
    fi
96 106 aaronmk
97 319 aaronmk
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XMLs
98
99 222 aaronmk
    out_fmt=VegBIEN
100 319 aaronmk
    # VegX to VegBIEN XML
101 169 aaronmk
    (
102
        src=VegX method=via_$src
103
        for in in "output/$orig_src."$table".$src.xml"; do
104
            table="${in#*.}" # after first "."
105
            table="${table%%.*}" # before second "."
106
            map toXml
107
        done
108 319 aaronmk
    ) || exit
109
110 222 aaronmk
    map toDb # source to VegBIEN db
111 319 aaronmk
    # VegBIEN XML to VegBIEN db
112
    for in in "output/$orig_src."$table".VegBIEN"*".xml"; do fromFile toDb; done
113 106 aaronmk
done