| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #!/bin/bash
- # Licensed to Cloudera, Inc. under one
- # or more contributor license agreements. See the NOTICE file
- # distributed with this work for additional information
- # regarding copyright ownership. Cloudera, Inc. licenses this file
- # to you 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.
- # Some helper functions to find the latest python bin and venv path
- set -ex
- # Find the latest Python binary in build/env/bin
- LATEST_PYTHON=$(find "$HUE_HOME_DIR/build/env/bin" -name "python3*" -exec basename {} \; | sort -V | tail -n 1)
- # Extract version from the latest python binary (e.g., python3.11 → 3.11)
- LATEST_PYTHON_VERSION=$(echo "$LATEST_PYTHON" | grep -oP '\d+\.\d+')
- # Find all supported python versions from build/venvs and include latest version
- readarray -t SUPPORTED_VERSIONS < <(
- (
- find "$HUE_HOME_DIR/build/venvs" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | grep -oP '\d+\.\d+';
- echo "$LATEST_PYTHON_VERSION"
- ) | sort -Vr | uniq
- )
- # Create array of supported versions
- SUPPORTED_PYTHON_VERSIONS=("${SUPPORTED_VERSIONS[@]}")
- python_bin_path() {
- # Searches for the provided python version in various locations on the node
- PYTHON_VERSION="$1"
- PYTHON_VERSION_NO_DOT="${PYTHON_VERSION//./}"
- # First check in PATH if it exists
- if [ -n "$PATH" ]; then
- IFS=: read -ra PATH_DIRS <<< "$PATH"
- for DIR in "${PATH_DIRS[@]}"; do
- # Skip cm-agent/bin directories
- if [[ "$DIR" == *"cm-agent/bin"* ]]; then
- continue
- fi
- PY_PATH="$DIR/python$PYTHON_VERSION"
- if [[ -x "$PY_PATH" ]]; then
- echo "$PY_PATH"
- return 0
- fi
- done
- fi
- # Fall back to hardcoded directories
- SEARCH_DIRS=("/usr/local/bin" "/bin" "/usr/bin" "/opt/rh/rh-${PYTHON_VERSION_NO_DOT}/root/usr/bin")
- for DIR in "${SEARCH_DIRS[@]}"; do
- PY_PATH="$DIR/python$PYTHON_VERSION"
- if [[ -x "$PY_PATH" ]]; then
- echo "$PY_PATH"
- return 0
- fi
- done
- return 1
- }
- find_latest_python() {
- # returns the latest py version, e.g., 3.11, 3.9 or 3.8
- for PYTHON_VERSION in "${SUPPORTED_PYTHON_VERSIONS[@]}"; do
- if python_bin_path "$PYTHON_VERSION" > /dev/null; then
- echo "$PYTHON_VERSION"
- return 0
- fi
- done
- echo "No supported Python versions found in expected locations."
- return 1
- }
- latest_venv_bin_path() {
- # returns the path to the env/bin of the latest python,
- # relative to the top-level hue directory.
- local version
- if [ -n "$HUE_PYTHON_VERSION" ]; then
- version=$(echo "$HUE_PYTHON_VERSION" | grep -oP '\d+\.\d+')
- else
- version="$(find_latest_python)"
- fi
- if [ -z "$version" ]; then
- return 1 # Return error if no version provided/found
- fi
- if [ "$version" = "$LATEST_PYTHON_VERSION" ]; then
- echo "build/env/bin"
- else
- echo "build/venvs/python${version}/bin"
- fi
- }
|