~smoser/curtin/yakkety.lp1666986

« back to all changes in this revision

Viewing changes to tools/tox-venv

  • Committer: Scott Moser
  • Date: 2016-02-12 21:54:46 UTC
  • mto: (58.1.1 pkg)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: smoser@ubuntu.com-20160212215446-0h8ka36oznelsjz1
Tags: upstream-0.1.0~bzr351
ImportĀ upstreamĀ versionĀ 0.1.0~bzr351

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# https://gist.github.com/smoser/2d4100a6a5d230ca937f
 
3
 
 
4
CR='
 
5
'
 
6
error() { echo "$@" 1>&2; }
 
7
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
 
8
get_env_dirs() {
 
9
    # read 'tox --showconfig'. return list of
 
10
    #  envname:dir
 
11
    local key="" equal="" val="" curenv="" out=""
 
12
    while read key equal val; do
 
13
        case "$key" in
 
14
           "[testenv:"*)
 
15
                curenv=${key#*:};
 
16
                curenv=${curenv%%"]"*};
 
17
                continue;;
 
18
        esac
 
19
        if [ "${key#*=}" != "$key" ]; then
 
20
            # older tox shows key=value or key=   value
 
21
            # newer tox shows: key =    value
 
22
            key=${key%%=*}
 
23
            val=${equal}
 
24
        fi
 
25
        [ "$key" = "envdir" ] || continue
 
26
        out="${out:+${out}${CR}}${curenv}:$val"
 
27
    done
 
28
    echo "$out"
 
29
}
 
30
 
 
31
load_config() {
 
32
    local tox_ini="$1" out="" envs=""
 
33
    if [ "$tox_ini" = "${CACHED_ENVS_INI}" ]; then
 
34
        _RET="$CACHED_ENVS"
 
35
        return
 
36
    fi
 
37
    out=$(tox -c "$tox_ini" --showconfig) || return 1
 
38
    envs=$(echo "$out" | get_env_dirs) || return 1
 
39
    CACHED_ENVS="$envs"
 
40
    CACHED_ENVS_INI="$tox_ini"
 
41
    _RET="$envs"
 
42
}
 
43
 
 
44
list_environments() {
 
45
    local tox_ini="$1" prefix="  " out="" envs="" oifs="$IFS"
 
46
    load_config "$tox_ini" || return 1
 
47
    envs="${_RET}"
 
48
    IFS="$CR"
 
49
    for d in ${envs}; do
 
50
        env=${d%%:*}
 
51
        dir=${d#*:}
 
52
        [ -f "$dir/bin/activate" ] && s="*" || s=""
 
53
        echo "${prefix}$env$s";
 
54
    done
 
55
    IFS="$oifs"
 
56
}
 
57
 
 
58
get_env_dir() {
 
59
    local tox_ini="$1" env="$2" oifs="$IFS" t="" d="" envs=""
 
60
    if [ "${TOX_VENV_SHORTCUT:-1}" != "0" ]; then
 
61
        local stox_d="${tox_ini%/*}/.tox/${env}"
 
62
        if [ -e "${stox_d}/bin/activate" ]; then
 
63
            _RET="${stox_d}"
 
64
            return
 
65
        fi
 
66
    fi
 
67
    load_config "$tox_ini" && envs="$_RET" || return 1
 
68
    IFS="$CR"
 
69
    for t in $envs; do
 
70
        [ "$env" = "${t%%:*}" ] && d="${t#*:}" && break
 
71
    done
 
72
    IFS=${oifs}
 
73
    [ -n "$d" ] || return 1
 
74
    _RET="$d"
 
75
}
 
76
 
 
77
Usage() {
 
78
    local tox_ini="$1"
 
79
    cat <<EOF
 
80
Usage: ${0##*/} [--no-create] tox-environment [command [args]]
 
81
   run command with provided arguments in the provided tox environment
 
82
   command defaults to \${SHELL:-/bin/sh}.
 
83
 
 
84
   run with '--list' to show available environments
 
85
EOF
 
86
    if [ -f "$tox_ini" ]; then
 
87
        local oini=${tox_ini}
 
88
        [ "${tox_ini}" -ef "$PWD/tox.ini" ] && oini="./tox.ini"
 
89
        echo
 
90
        echo "environments in $oini"
 
91
        list_environments "$tox_ini"
 
92
    fi
 
93
}
 
94
 
 
95
if [ -f tox.ini -a -d .tox ]; then
 
96
    tox_ini="$PWD/tox.ini"
 
97
else
 
98
    tox_ini="${0%/*}/../tox.ini"
 
99
fi
 
100
 
 
101
[ $# -eq 0 ] && { Usage "$tox_ini" 1>&2; exit 1; }
 
102
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage "$tox_ini"; exit 0; }
 
103
 
 
104
[ -f "$tox_ini" ] || fail "$tox_ini: did not find tox.ini"
 
105
 
 
106
if [ "$1" = "-l" -o "$1" = "--list" ]; then
 
107
    list_environments "$tox_ini"
 
108
    exit
 
109
fi
 
110
 
 
111
nocreate="false"
 
112
if [ "$1" = "--no-create" ]; then
 
113
    nocreate="true"
 
114
    shift
 
115
fi
 
116
 
 
117
env="$1"
 
118
shift
 
119
get_env_dir "$tox_ini" "$env" && activate="$_RET/bin/activate" || activate=""
 
120
 
 
121
if [ -z "$activate" -o ! -f "$activate" ]; then
 
122
    if $nocreate; then
 
123
        fail "tox env '$env' did not exist, and no-create specified"
 
124
    elif [ -n "$activate" ]; then
 
125
        error "attempting to create $env:"
 
126
        error "    tox -c $tox_ini --recreate --notest -e $env"
 
127
        tox -c "$tox_ini" --recreate --notest -e "$env" ||
 
128
            fail "failed creation of env $env"
 
129
    else
 
130
        error "$env: not a valid tox environment?"
 
131
        error "found tox_ini=$tox_ini"
 
132
        error "try one of:"
 
133
        list_environments "$tox_ini" 1>&2
 
134
        fail
 
135
    fi
 
136
fi
 
137
. "$activate"
 
138
 
 
139
[ "$#" -gt 0 ] || set -- ${SHELL:-/bin/bash}
 
140
debian_chroot="tox:$env" exec "$@"
 
141
 
 
142
# vi: ts=4 expandtab