blob: bed3fbd6d71da894b1c76437fb0b975a7bf4de67 [file] [log] [blame]
Rob Mohrcacb8772019-11-22 13:14:01 -08001# Copyright 2019 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# This script must be tested on bash, zsh, and dash.
16
Rob Mohr93825562020-01-02 15:20:50 -080017function _abspath () {
18 python -c "import os.path; print(os.path.abspath('$@'))"
Rob Mohrdc70d1c2019-12-04 10:05:28 -080019}
20
Rob Mohrcacb8772019-11-22 13:14:01 -080021# Shell: bash.
22if test -n "$BASH"; then
Rob Mohr93825562020-01-02 15:20:50 -080023 PW_SETUP_SCRIPT_PATH=$(_abspath $BASH_SOURCE)
Rob Mohrcacb8772019-11-22 13:14:01 -080024# Shell: zsh.
25elif test -n "$ZSH_NAME"; then
Rob Mohr93825562020-01-02 15:20:50 -080026 PW_SETUP_SCRIPT_PATH=$(_abspath ${(%):-%N})
Rob Mohrcacb8772019-11-22 13:14:01 -080027# Shell: dash.
28elif test ${0##*/} = dash; then
Rob Mohr93825562020-01-02 15:20:50 -080029 PW_SETUP_SCRIPT_PATH=$(_abspath \
Rob Mohrcacb8772019-11-22 13:14:01 -080030 $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;'))
31# If everything else fails, try $0. It could work.
32else
Rob Mohr93825562020-01-02 15:20:50 -080033 PW_SETUP_SCRIPT_PATH=$(_abspath $0)
Rob Mohrcacb8772019-11-22 13:14:01 -080034fi
35
36PW_ENVSETUP=$(dirname $PW_SETUP_SCRIPT_PATH)
37export PW_ENVSETUP
38
39PW_ROOT=$(dirname "$PW_ENVSETUP")
40export PW_ROOT
41
Rob Mohrdc70d1c2019-12-04 10:05:28 -080042unset ABORT_PW_ENVSETUP
Rob Mohr93825562020-01-02 15:20:50 -080043unset PW_ENVSETUP_FULL
44
45if [[ $(basename $PW_SETUP_SCRIPT_PATH) == "bootstrap.sh" ]]; then
46 PW_ENVSETUP_FULL=1
47 export PW_ENVSETUP_FULL
48fi
Rob Mohrdc70d1c2019-12-04 10:05:28 -080049
Rob Mohr1e5c5832019-12-19 13:49:07 -080050# Initialize CIPD.
Rob Mohrcacb8772019-11-22 13:14:01 -080051. "$PW_ENVSETUP/cipd/init.sh"
Rob Mohrdc70d1c2019-12-04 10:05:28 -080052
Rob Mohr1e5c5832019-12-19 13:49:07 -080053# Check that Python 3 comes from CIPD.
54if [[ -z "$ABORT_PW_ENVSETUP" ]]; then
55 if [[ $(which python3) != *"cipd"* ]]; then
56 echo "Python not from CIPD--found $(which python3) instead" 1>&2
57 ABORT_PW_ENVSETUP=1
58 fi
59fi
60
61# Initialize Python 3 virtual environment.
Rob Mohrdc70d1c2019-12-04 10:05:28 -080062if [[ -z "$ABORT_PW_ENVSETUP" ]]; then
63 . "$PW_ENVSETUP/virtualenv/init.sh"
64fi
Rob Mohr1e5c5832019-12-19 13:49:07 -080065
Rob Mohr3f800992019-12-19 13:58:57 -080066if [[ -n "$ABORT_PW_ENVSETUP" ]]; then
Rob Mohr1e5c5832019-12-19 13:49:07 -080067 echo "Environment setup failed! Please see messages above." 1>&2
68fi
Rob Mohr93825562020-01-02 15:20:50 -080069
70# Unset this at the end so users running setup scripts directly get consistent
71# behavior.
72unset PW_ENVSETUP_FULL