|
| 1 | +#!/bin/bash |
| 2 | +# Licensed to Cloudera, Inc. under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. Cloudera, Inc. licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# Some helper functions to find the latest python bin and venv path |
| 19 | +set -ex |
| 20 | + |
| 21 | +# Find the latest Python binary in build/env/bin |
| 22 | +LATEST_PYTHON=$(find "$HUE_HOME_DIR/build/env/bin" -name "python3*" -exec basename {} \; | sort -V | tail -n 1) |
| 23 | + |
| 24 | +# Extract version from the latest python binary (e.g., python3.11 → 3.11) |
| 25 | +LATEST_PYTHON_VERSION=$(echo "$LATEST_PYTHON" | grep -oP '\d+\.\d+') |
| 26 | + |
| 27 | +# Find all supported python versions from build/venvs and include latest version |
| 28 | +readarray -t SUPPORTED_VERSIONS < <( |
| 29 | + ( |
| 30 | + find "$HUE_HOME_DIR/build/venvs" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | grep -oP '\d+\.\d+'; |
| 31 | + echo "$LATEST_PYTHON_VERSION" |
| 32 | + ) | sort -Vr | uniq |
| 33 | +) |
| 34 | + |
| 35 | +# Create array of supported versions |
| 36 | +SUPPORTED_PYTHON_VERSIONS=("${SUPPORTED_VERSIONS[@]}") |
| 37 | + |
| 38 | +python_bin_path() { |
| 39 | + # Searches for the provided python version in various locations on the node |
| 40 | + PYTHON_VERSION="$1" |
| 41 | + PYTHON_VERSION_NO_DOT="${PYTHON_VERSION//./}" |
| 42 | + |
| 43 | + # First check in PATH if it exists |
| 44 | + if [ -n "$PATH" ]; then |
| 45 | + IFS=: read -ra PATH_DIRS <<< "$PATH" |
| 46 | + for DIR in "${PATH_DIRS[@]}"; do |
| 47 | + # Skip cm-agent/bin directories |
| 48 | + if [[ "$DIR" == *"cm-agent/bin"* ]]; then |
| 49 | + continue |
| 50 | + fi |
| 51 | + PY_PATH="$DIR/python$PYTHON_VERSION" |
| 52 | + if [[ -x "$PY_PATH" ]]; then |
| 53 | + echo "$PY_PATH" |
| 54 | + return 0 |
| 55 | + fi |
| 56 | + done |
| 57 | + fi |
| 58 | + |
| 59 | + # Fall back to hardcoded directories |
| 60 | + SEARCH_DIRS=("/usr/local/bin" "/bin" "/usr/bin" "/opt/rh/rh-${PYTHON_VERSION_NO_DOT}/root/usr/bin") |
| 61 | + for DIR in "${SEARCH_DIRS[@]}"; do |
| 62 | + PY_PATH="$DIR/python$PYTHON_VERSION" |
| 63 | + if [[ -x "$PY_PATH" ]]; then |
| 64 | + echo "$PY_PATH" |
| 65 | + return 0 |
| 66 | + fi |
| 67 | + done |
| 68 | + return 1 |
| 69 | +} |
| 70 | + |
| 71 | +find_latest_python() { |
| 72 | + # returns the latest py version, e.g., 3.11, 3.9 or 3.8 |
| 73 | + for PYTHON_VERSION in "${SUPPORTED_PYTHON_VERSIONS[@]}"; do |
| 74 | + if python_bin_path "$PYTHON_VERSION" > /dev/null; then |
| 75 | + echo "$PYTHON_VERSION" |
| 76 | + return 0 |
| 77 | + fi |
| 78 | + done |
| 79 | + |
| 80 | + echo "No supported Python versions found in expected locations." |
| 81 | + return 1 |
| 82 | +} |
| 83 | + |
| 84 | +latest_venv_bin_path() { |
| 85 | + # returns the path to the env/bin of the latest python, |
| 86 | + # relative to the top-level hue directory. |
| 87 | + local version |
| 88 | + if [ -n "$HUE_PYTHON_VERSION" ]; then |
| 89 | + version=$(echo "$HUE_PYTHON_VERSION" | grep -oP '\d+\.\d+') |
| 90 | + else |
| 91 | + version="$(find_latest_python)" |
| 92 | + fi |
| 93 | + |
| 94 | + if [ -z "$version" ]; then |
| 95 | + return 1 # Return error if no version provided/found |
| 96 | + fi |
| 97 | + |
| 98 | + if [ "$version" = "$LATEST_PYTHON_VERSION" ]; then |
| 99 | + echo "build/env/bin" |
| 100 | + else |
| 101 | + echo "build/venvs/python${version}/bin" |
| 102 | + fi |
| 103 | +} |
0 commit comments