aboutsummaryrefslogtreecommitdiff
path: root/scripts/cbc_monolith
blob: a55798c8f191482194e2b8037248d884c658e5d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
EXEC_PATH=$(dirname "${BASH_SOURCE[0]}")
source "$EXEC_PATH/cbc_functions.inc"
# declare $OS and $ARCH (don't touch)
get_os_info

LOGFILE="monolith.log"
LOGFILE_PREV="$LOGFILE.prev"
TMPDIR=/tmp
start_pos=

PYTHON_VERSION="3.4"
NUMPY_VERSION="1.9"
BRANCH=""
BRANCH_MANIFEST=""


function usage {
    echo "usage: $(basename $0) {manifest} [-pnco]
    manifest                List of recipes to build (in order)
    --python            -p  Version to pass to conda-build
    --numpy             -n  Version to pass to conda-build
    --branch            -b  Build from a branch (or tag)
    --branch-manifest       List containing git_url patterns
    --cbc-recipes       -c  Path to CBC recipes directory
    --cbc-output-dir    -o  Path to CONDA recipes
    "
}

function build_restart {
    if [ -f "$LOGFILE" ]; then
        echo "Build restarting..."
        cp -av "$LOGFILE" $LOGFILE.prev
        sync
        export start_pos=$(tail -n 1 "$LOGFILE_PREV" | tr -d ' ')
    fi
}

function build_prepare {
    echo "Translating CBC recipes..."
    cbc_build --no-build $(echo $CBC_RECIPES/*/*.ini)
    _OK=`success`
    if [ $_OK -ne 0 ]; then
        echo "Something went wrong with cbc_build..."
        exit 1
    fi

    if [ -n "$BRANCH" ]; then
        build_prepare_branch
    fi

}

function build_prepare_branch()
{
    echo Building from branch: $BRANCH
    CBC_HOME_ORIG="$CBC_HOME"

    mktemp_args=
    case "$OS" in
        osx)
        export mktemp_args="-d -u -t $(basename $0)"
        ;;
        linux)
        export mktemp_args="-u -d"
        ;;
        *)
        ;;
    esac

    export CBC_HOME="$(mktemp $mktemp_args)"
    mkdir -pv "$CBC_HOME"

    echo "CBC_HOME=$CBC_HOME"
    rsync -a "$CBC_HOME_ORIG/" "$CBC_HOME"

    _OK=`success`
    if [ $_OK -ne 0 ]; then
        echo "Failed to transfer: $CBC_HOME_ORIG -> $CBC_HOME"
        echo "Aborting..."
        exit 1
    fi


    # Redundant code alert... ugh (WILL FIX)

    is_osx=
    if [[ $OS == "osx" ]]; then
        export is_osx=".orig"
    fi

    if [ -n "$BRANCH_MANIFEST" ]; then
        while read pattern
        do
            for f in `find $CBC_HOME -type f -name "*.yaml"`
            do
                grep "$pattern" "$f" >/dev/null
                _OK=`success`
                if [ $_OK -eq 0 ]; then
                    echo "Applying branch: $BRANCH -> $(dirname $f)"
                    sed -i$is_osx 's|git_tag|#git_tag|g' "$f" >/dev/null
                    sed -i$is_osx "/git_url/ a \ \ \ \ git_tag: '$BRANCH'" "$f" >/dev/null
                fi
            done
        done < "$BRANCH_MANIFEST"
    else
        for f in `find $CBC_HOME -type f -name "*.yaml"`
        do
            grep "git_url" "$f" >/dev/null
            _OK=`success`
            if [ $_OK -eq 0 ]; then
                echo "Applying branch $BRANCH -> $(dirname $f)"
                sed -i$is_osx 's|git_tag|#git_tag|g' "$f" >/dev/null
                sed -i$is_osx "/git_url/ a \ \ \ \ git_tag: '$BRANCH'" "$f" >/dev/null
            fi
        done
    fi
}

function build_cleanup {
    if [ -n "$BRANCH" ]; then
        if [[ $CBC_RECIPES == *$TMPDIR* ]]; then
            echo "Removing temporary branch data..."
            rm -rf "$CBC_RECIPES"
        fi
    fi
}

function build() {
    CWD=`pwd`
    while read recipe
    do
        if [ -n "$start_pos" ]; then
            if [[ $recipe != $start_pos ]]; then
                echo "Skipping: $recipe"
                continue 
            else
                # Iterate through remaining recipes
                unset start_pos
            fi
        fi
    
        cd "$CBC_HOME"
    
        conda build \
            --no-binstar-upload \
            --python $PYTHON_VERSION \
            --numpy $NUMPY_VERSION \
            --override-channels -c defaults "$recipe"
    
        _OK=`success`
        if [ $_OK -ne 0 ]; then
            echo
            echo "Monolithic build failure..."
            echo "Cause:"
            echo "    $recipe"
    
            cd "$CWD"
            exit $_OK
        fi
    done < "$MANIFEST"
    cd "$CWD"
}

function bad_arg {
    usage
    echo "Bad argument: $@"
    exit 1
}

ARGC="${#}"
if [[ ${ARGC} < 1 ]]; then
    usage
    exit 1
fi

MANIFEST="$1"
if [ ! -f "$MANIFEST" ]; then
    echo "\"$MANIFEST\" does not exist."
    exit 1
fi


# MAIN
shift
while [ $# -gt 0 ];
do
    case "$1" in
        '')
            usage
            exit 1
        ;;
        --python|-p)
            PYTHON_VERSION="$2"
            if [ -z "$PYTHON_VERSION" ]; then
                bad_arg "Missing python version."
            fi
            export PYTHON_VERSION
            shift
        ;;
        --numpy|-n)
            NUMPY_VERSION="$2"
            if [ -z "$NUMPY_VERSION" ]; then
                bad_arg "Missing numpy version."
            fi
            export NUMPY_VERSION
            shift
        ;;
        --branch|-b)
            BRANCH="$2"
            if [ -z "$BRANCH" ]; then
                bad_arg "Missing branch name."
            fi
            export BRANCH
            shift
        ;;
        --branch-manifest)
            BRANCH_MANIFEST="$2"
            if [ -z "$BRANCH_MANIFEST" ]; then
                bad_arg "Missing branch manifest filename."
            fi
            export BRANCH_MANIFEST
            shift
        ;;
        --cbc-recipes|-c)
            CBC_RECIPES="$2"
            if [ -z "$CBC_RECIPES" ]; then
                bad_arg "Missing recipe directory."
            fi
            export CBC_RECIPES
            shift
        ;;
        --cbc-output-dir|-o)
            CBC_HOME="$2"
            if [ -z "$CBC_HOME" ]; then
                bad_arg "Missing conda recipe directory."
            fi
            export CBC_HOME
            shift
        ;;
        *)
            echo "Unknown option: $1"
            usage
            exit 1
        ;;
    esac
    shift
done


# DO NOT LOG THIS...
build_prepare
build_restart

# LOG THIS
( build ) 2>&1 | tee "$LOGFILE"

build_cleanup