common_utils.sh 2.82 KB
Newer Older
1
2
#!/bin/bash
# Utilities for both OSX and Docker
3
# Python should be on the PATH
4
5
set -e

6
7
MULTIBUILD_DIR=$(dirname "${BASH_SOURCE[0]}")

8
9
10
11
function abspath {
    python -c "import os.path; print(os.path.abspath('$1'))"
}

12
13
14
15
16
function relpath {
    # Path of first input relative to second (or $PWD if not specified)
    python -c "import os.path; print(os.path.relpath('$1','${2:-$PWD}'))"
}

Matthew Brett's avatar
Matthew Brett committed
17
18
19
20
function realpath {
    python -c "import os; print(os.path.realpath('$1'))"
}

21
22
23
24
25
26
27
28
29
30
31
32
33
34
function lex_ver {
    # Echoes dot-separated version string padded with zeros
    # Thus:
    # 3.2.1 -> 003002001
    # 3     -> 003000000
    echo $1 | awk -F "." '{printf "%03d%03d%03d", $1, $2, $3}'
}

function is_function {
    set +e
    $(declare -Ff "$1") > /dev/null && echo true
    set -e
}

35
36
37
38
39
40
41
42
43
44
45
46
function gh-clone {
    git clone https://github.com/$1
}

function rm_mkdir {
    # Remove directory if present, then make directory
    local path=$1
    [ -z "$path" ] && echo "Need not-empty path" && exit 1
    [ -d "$path" ] && rm -rf $path
    mkdir $path
}

47
48
49
50
51
52
53
54
55
56
57
58
59
function clean_code {
    local repo_dir=${1:-$REPO_DIR}
    local build_commit=${2:-$BUILD_COMMIT}
    [ -z "$repo_dir" ] && echo "repo_dir not defined" && exit 1
    [ -z "$build_commit" ] && echo "build_commit not defined" && exit 1
    (cd $repo_dir \
        && git fetch origin \
        && git checkout $build_commit \
        && git clean -fxd \
        && git reset --hard \
        && git submodule update --init --recursive)
}

60
61
62
63
64
65
66
67
68
69
70
71
72
function build_wheel {
    # Builds wheel, puts into $WHEEL_SDIR
    #
    # Depends on
    #     REPO_DIR  (or via input argument)
    #     WHEEL_SDIR  (optional, default "wheelhouse")
    #     BUILD_DEPENDS (optional, default "")
    #     MANYLINUX_URL (optional, default "") (via pip_opts function)
    local repo_dir=${1:-$REPO_DIR}
    [ -z "$repo_dir" ] && echo "repo_dir not defined" && exit 1
    local wheelhouse=$(abspath ${WHEEL_SDIR:-wheelhouse})
    if [ -n $(is_function "pre_build") ]; then pre_build; fi
    if [ -n "$BUILD_DEPENDS" ]; then pip install $(pip_opts) $BUILD_DEPENDS; fi
73
    (cd $repo_dir && pip wheel $(pip_opts) -w $wheelhouse --no-deps .)
74
75
76
77
78
    repair_wheelhouse $wheelhouse
}

function pip_opts {
    [ -n "$MANYLINUX_URL" ] && echo "--find-links $MANYLINUX_URL"
79
}
80
81
82

function install_wheel {
    # Install test dependencies and built wheel
83
    #
84
    # Pass any input flags to pip install steps
85
    #
86
    # Depends on:
87
88
89
90
    #     WHEEL_SDIR  (optional, default "wheelhouse")
    #     TEST_DEPENDS  (optional, default "")
    #     MANYLINUX_URL (optional, default "") (via pip_opts function)
    local wheelhouse=$(abspath ${WHEEL_SDIR:-wheelhouse})
91
    if [ -n "$TEST_DEPENDS" ]; then
92
        pip install $(pip_opts) $@ $TEST_DEPENDS
93
94
    fi
    # Install compatible wheel
95
    pip install $(pip_opts) $@ \
96
        $(python $MULTIBUILD_DIR/supported_wheels.py $wheelhouse/*.whl)
97
}