-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathyb-thirdparty-common.sh
108 lines (97 loc) · 3.21 KB
/
yb-thirdparty-common.sh
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
#!/usr/bin/env bash
# Copyright (c) YugaByte, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations
# under the License.
#
set -euo pipefail
log() {
echo >&2 "[$( date +%Y-%m-%dT%H:%M:%S )] $*"
}
fatal() {
log "$@"
exit 1
}
compute_sha256sum() {
sha256_sum=$( (
if [[ $OSTYPE =~ darwin ]]; then
local portable_arg=""
if shasum --help | grep -qE '[-][-]portable'; then
portable_arg="--portable"
fi
shasum $portable_arg --algorithm 256 "$@"
else
sha256sum "$@"
fi
) | awk '{print $1}' )
if [[ ! $sha256_sum =~ ^[0-9a-f]{64} ]]; then
log "Got an incorrect SHA256 sum: $sha256_sum. Expected 64 hex digits."
fi
}
activate_virtualenv() {
if [[ ! -d $YB_THIRDPARTY_DIR/venv ]]; then
"${PYTHON:-python3}" -m venv "$YB_THIRDPARTY_DIR/venv"
fi
set +u
# shellcheck disable=SC1090
. "$YB_THIRDPARTY_DIR/venv/bin/activate"
set -u
(
set -x
cd "$YB_THIRDPARTY_DIR"
pip3 install --quiet -r requirements_frozen.txt
)
}
# Re-executes the current script with the correct macOS architecture.
ensure_correct_mac_architecture() {
if [[ $OSTYPE != darwin* ]]; then
return
fi
if [[ -z ${YB_TARGET_ARCH:-} ]]; then
local uname_p_output
uname_p_output=$( uname -p )
if [[ $uname_p_output == arm || $uname_p_output == arm64 ]]; then
YB_TARGET_ARCH="arm64"
elif [[ $uname_p_output == i386 || $uname_p_output == x86_64 ]]; then
YB_TARGET_ARCH="x86_64"
else
fatal "Failed to determine target architecture on macOS from the output of 'uname -p':" \
"$uname_p_output"
fi
fi
if [[ $YB_TARGET_ARCH != "x86_64" && $YB_TARGET_ARCH != "arm64" ]]; then
fatal "Invalid value of YB_TARGET_ARCH on macOS (expected x86_64 or arm64): $YB_TARGET_ARCH"
fi
export YB_TARGET_ARCH
local actual_arch
actual_arch=$(arch)
if [[ $actual_arch == "i386" ]]; then
actual_arch="x86_64"
elif [[ $actual_arch != "arm64" && $actual_arch != "x86_64" ]]; then
fatal "Unexpected output from arch: $actual_arch"
fi
if [[ $actual_arch != "$YB_TARGET_ARCH" ]]; then
echo "Switching architecture to $YB_TARGET_ARCH"
set -x
exec arch "-$YB_TARGET_ARCH" "$0" "$@"
fi
}
# We ignore the previously set YB_THIRDPARTY_DIR value, because if we are executing Bash scripts
# within this third-party directory, we most likely want to work in this exact directory.
YB_THIRDPARTY_DIR=$( cd "${BASH_SOURCE[0]%/*}" && pwd )
PYTHONPATH=${PYTHONPATH:-}
if [[ -n $PYTHONPATH ]]; then
PYTHONPATH=:$PYTHONPATH
fi
# Eventually most Python scripts should move to the python directory, but right now we add both
# the "python" directory and the thirdparty root directory to PYTHONPATH.
PYTHONPATH=$YB_THIRDPARTY_DIR/python$PYTHONPATH
export PYTHONPATH
export YB_THIRDPARTY_DIR