forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
74 lines (60 loc) · 2.11 KB
/
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
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
# shellcheck shell=bash
COLOR_BLUE="\x1b[34m"
COLOR_RED="\x1b[31m"
COLOR_GREEN="\x1b[32m"
COLOR_RESET="\x1b[0m"
function log() {
echo -e "$@" 1>&2
}
function die() {
(($# > 0)) && log "\n${COLOR_RED}$*${COLOR_RESET}"
exit 1
}
function green() {
(($# > 0)) && log "\n${COLOR_GREEN}$*${COLOR_RESET}"
}
# Initialization for elapsed()
: "${elapsed_start_time:=$(date +'%s')}"
export elapsed_start_time
function elapsed() {
now=$(date '+%s')
elapsed_secs=$((now - elapsed_start_time))
echo $elapsed_secs | awk '{printf "%02d:%02d\n",int($1/60), int($1%60)}'
}
function banner() {
echo -e "${COLOR_BLUE}[=== $(elapsed) $* ===]${COLOR_RESET}"
}
function fingerprint_data() {
git hash-object --stdin
}
function git_merge_base() {
# This prints the tracking branch if set and otherwise falls back to the commit before HEAD.
# We fall back to the commit before HEAD to attempt to account for situations without a tracking
# branch, which might include `main` builds, but can also include branch-PR builds, where
# Travis checks out a specially crafted Github `+refs/pull/11516/merge` branch.
git rev-parse --symbolic-full-name --abbrev-ref HEAD@\{upstream\} 2> /dev/null || git rev-parse HEAD^
}
function determine_python() {
if [[ -n "${PY:-}" ]]; then
which "${PY}" && return 0
fi
version='3.9'
interpreter_path="$(command -v "python${version}")"
if [[ -z "${interpreter_path}" ]]; then
echo "pants: Failed to find a Python ${version} interpreter" && return 1
fi
# Check if the Python version is installed via Pyenv but not activated.
if [[ "$("${interpreter_path}" --version 2>&1 > /dev/null)" == "pyenv: python${version}"* ]]; then
echo "pants: The Python ${version} interpreter at ${interpreter_path} is an inactive pyenv interpreter" && return 1
fi
echo "${interpreter_path}"
return 0
}
function is_macos_arm() {
[[ $(uname -sm) == "Darwin arm64" ]]
}
function is_macos_big_sur() {
[[ $(uname) == "Darwin" && $(sw_vers -productVersion) = 11\.* ]]
}