python-build: share platform detection (#3618)

---------

Co-authored-by: Ivan Pozdeev <vano@mail.mipt.ru>
This commit is contained in:
AyushandIvan Pozdeev authored and GitHub committed 2026-11-01 05:53:45 +03:00
1 parent 6b38999a60
commit e6669f0a84
2 files changed
+139 -1

No files matched your search

+25 -1
View File
@@ -51,9 +51,33 @@ lib() {
done
}
detect_platform() {
os="$(uname -s)" || return
arch="$(uname -m)" || return
distro=""
case "$os" in
Darwin )
distro="macos $(sw_vers -productVersion)" || return
;;
* )
if [[ -n ${_PYTHON_BUILD_FAKE_OS_RELEASE-} || \
( -z ${_PYTHON_BUILD_SKIP_OS_RELEASE-} && -r /etc/os-release ) ]]; then
distro=$(. "${_PYTHON_BUILD_FAKE_OS_RELEASE:-/etc/os-release}" && printf '%s %s' "${ID:-}" "${VERSION_ID:-}") || return
elif type -p lsb_release >/dev/null; then
distro="$(lsb_release -si) $(lsb_release -sr)" || return
else
distro="$os $(uname -r)" || return
fi
;;
esac
distro=$(printf '%s' "$distro" | tr '[:upper:]' '[:lower:]')
}
if [ "$1" == "--$FUNCNAME" ]; then
declare -f "$FUNCNAME"
echo "$FUNCNAME \"\$1\";"
echo "$FUNCNAME \"\${1:-}\";"
exit
fi
}
+114
View File
@@ -0,0 +1,114 @@
#!/usr/bin/env bats
load test_helper
read_platform() {
bash -uc 'eval "$(python-build --lib)"
detect_platform && printf "%s\n" "$os" "$arch" "$distro"'
}
@test "platform detection in macOS" {
stub uname '-s : echo Darwin' '-m : echo arm64'
stub sw_vers '-productVersion : echo 15.5.2'
run read_platform
assert_success <<!
Darwin
arm64
macos 15.5.2
!
unstub uname
unstub sw_vers
}
@test "platform detection prefers os-release in other OSes, takes the rest from uname" {
cat >"${BATS_TEST_TMPDIR}/os-release" <<!
ID=BolgenOS
VERSION_ID=99.99
!
export _PYTHON_BUILD_FAKE_OS_RELEASE="${BATS_TEST_TMPDIR}/os-release"
local expected
stub uname '-s : echo YetAnotherBSD' '-m : echo hal_9000'
stub lsb_release
run read_platform
assert_success <<!
YetAnotherBSD
hal_9000
bolgenos 99.99
!
unstub uname
unstub lsb_release
}
@test "platform detection prefers os-release (integration)" {
[[ -r /etc/os-release ]] || skip "/etc/os-release is not present"
local expected
expected="$(. /etc/os-release && printf '%s %s' "${ID:-}" "${VERSION_ID:-}")"
stub uname '-s : echo Linux' '-m : echo x86_64'
stub lsb_release
run read_platform
assert_success <<!
Linux
x86_64
$expected
!
unstub uname
unstub lsb_release
}
@test "platform detection falls back to lsb_release" {
export _PYTHON_BUILD_SKIP_OS_RELEASE=1
stub uname '-s : echo Linux' '-m : echo x86_64'
stub lsb_release '-si : echo Ubuntu' '-sr : echo 24.04'
run read_platform
assert_success <<!
Linux
x86_64
ubuntu 24.04
!
unstub uname
unstub lsb_release
}
@test "platform detection falls back to uname -r" {
export _PYTHON_BUILD_SKIP_OS_RELEASE=1
stub uname '-s : echo Linux' '-m : echo x86_64' '-r : echo 99.99-whatever'
PATH=$(path_without lsb_release) run read_platform
assert_success <<!
Linux
x86_64
linux 99.99-whatever
!
unstub uname
}
@test "platform detection propagates uname failures" {
stub uname '-s : echo "OS query failed" >&2; exit 9'
run read_platform
assert_failure "OS query failed"
assert_equal 9 "$status"
unstub uname
}
@test "platform detection propagates sw_vers failures" {
stub uname '-s : echo Darwin' '-m : echo arm64'
stub sw_vers '-productVersion : echo "release query failed" >&2; exit 9'
run read_platform
assert_failure "release query failed"
assert_equal 9 "$status"
unstub uname
unstub sw_vers
}