Project

General

Profile

1
#!/bin/bash
2
# Tests map on all input/*.csv
3
# Usage: env [n=<num-rows>] self
4

    
5
selfDir="$(dirname -- "$0")"
6
cd "$selfDir"
7

    
8
shopt -s nullglob
9

    
10
tests_n=2
11
testMode=
12
test -n "$n" || export n="$tests_n" testMode=1
13

    
14
test "$(hostname)" = nimoy && isNimoy=t || isNimoy=
15
test -n "$isNimoy" && . ../bin/env_password mysql_password "your MySQL"
16

    
17
bien_password="$(cat ../config/bien_password)"
18

    
19
make --directory=../mappings
20
mkdir -p output
21

    
22
function trace()
23
{
24
    (
25
        env echo -n "$PS4" # bug in sh's built-in echo prints the -n
26
        for arg in "$@"; do printf "%q " "$arg"; done
27
        echo "${_in+<$_in}" "${_out+>$_out}"
28
    ) >&2
29
}
30

    
31
function fromFile()
32
{
33
    (_in="$in"; trace)
34
    ("$@" <"$in") || exit # run in a sandbox
35
}
36

    
37
function map()
38
{
39
    test -e "$in" || return 1
40
    local ext="${in##*.}" # after last "."
41
    (for map in "../mappings/$src-$out_fmt."$table".csv"; do
42
        table="${map%.*}" # remove extension
43
        table="${table##*.}" # after last "."
44
        {
45
            if test "$ext" == "sh"; then
46
                trace . "$in"
47
                (
48
                    . "$in"
49
                    
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
                    fi
59
                    
60
                    for var in engine host user password database; do
61
                        export in_$var="${!var}"
62
                    done
63
                    "$@"
64
                )
65
            else fromFile "$@"
66
            fi
67
        } || exit
68
    done) || exit # run in a sandbox
69
}
70

    
71
function toXml()
72
{
73
    local stem="$orig_src.$table.$out_fmt"
74
    local out="output/$stem${method+.$method}.xml"
75
    local accepted="accepted_output/$stem.xml"
76
    (set -x; ../map "$map" >"$out") || exit
77
    (set -x; ${testMode:+diff "$accepted" "$out"}) || exit
78
}
79

    
80
vegbienDest=../bin/vegbien_dest
81

    
82
function toDb()
83
{
84
    trace . "$vegbienDest"
85
    (. "$vegbienDest"; set -x; ../map ${map:+"$map"}) || exit
86
}
87

    
88
for in in input/*; do
89
    stem="$(basename -- "${in%.*}")" # remove extension and dir
90
    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
    
97
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XMLs
98
    
99
    out_fmt=VegBIEN
100
    # VegX to VegBIEN XML
101
    (
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
    ) || exit
109
        
110
    map toDb # source to VegBIEN db
111
    # VegBIEN XML to VegBIEN db
112
    for in in "output/$orig_src."$table".VegBIEN"*".xml"; do fromFile toDb; done
113
done
(2-2/4)