python_helper.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # Some helper functions to find the latest python bin and venv path
  18. set -ex
  19. # Find the latest Python binary in build/env/bin
  20. LATEST_PYTHON=$([ -d "$HUE_HOME_DIR/build/env/bin" ] && find "$HUE_HOME_DIR/build/env/bin" -name "python3*" -exec basename {} \; | sort -V | tail -n 1 || echo "")
  21. # Extract version from the latest python binary (e.g., python3.11 → 3.11)
  22. LATEST_PYTHON_VERSION=$(echo "$LATEST_PYTHON" | grep -oE '[0-9]+\.[0-9]+' || echo "")
  23. # Find all supported python versions from build/venvs and include latest version
  24. readarray -t SUPPORTED_VERSIONS < <(
  25. (
  26. [ -d "$HUE_HOME_DIR/build/venvs" ] && find "$HUE_HOME_DIR/build/venvs" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | grep -oE '[0-9]+\.[0-9]+'
  27. echo "$LATEST_PYTHON_VERSION"
  28. ) | sort -Vr | uniq
  29. )
  30. # Create array of supported versions
  31. SUPPORTED_PYTHON_VERSIONS=("${SUPPORTED_VERSIONS[@]}")
  32. _python_bin_path() {
  33. # Searches for the provided python version in various locations on the node
  34. PYTHON_VERSION="$1"
  35. PYTHON_VERSION_NO_DOT="${PYTHON_VERSION//./}"
  36. # First check in PATH if it exists
  37. if [ -n "$PATH" ]; then
  38. IFS=: read -ra PATH_DIRS <<< "$PATH"
  39. for DIR in "${PATH_DIRS[@]}"; do
  40. # Skip cm-agent/bin directories
  41. if [[ "$DIR" == *"cm-agent/bin"* ]]; then
  42. continue
  43. fi
  44. PY_PATH="$DIR/python$PYTHON_VERSION"
  45. if [[ -x "$PY_PATH" ]]; then
  46. echo "$PY_PATH"
  47. return 0
  48. fi
  49. done
  50. fi
  51. # Fall back to hardcoded directories
  52. SEARCH_DIRS=("/usr/local/bin" "/bin" "/usr/bin" "/opt/rh/rh-${PYTHON_VERSION_NO_DOT}/root/usr/bin")
  53. for DIR in "${SEARCH_DIRS[@]}"; do
  54. PY_PATH="$DIR/python$PYTHON_VERSION"
  55. if [[ -x "$PY_PATH" ]]; then
  56. echo "$PY_PATH"
  57. return 0
  58. fi
  59. done
  60. return 1
  61. }
  62. _choose_python_version() {
  63. # returns the latest py version, e.g., 3.11, 3.9 or 3.8
  64. # if HUE_PYTHON_VERSION is set, use it
  65. if [ -n "$HUE_PYTHON_VERSION" ]; then
  66. echo "$HUE_PYTHON_VERSION" | grep -oE '[0-9]+\.[0-9]+'
  67. return 0
  68. else
  69. for PYTHON_VERSION in "${SUPPORTED_PYTHON_VERSIONS[@]}"; do
  70. if _python_bin_path "$PYTHON_VERSION" > /dev/null; then
  71. echo "$PYTHON_VERSION"
  72. return 0
  73. fi
  74. done
  75. fi
  76. echo "No supported Python versions found in expected locations."
  77. return 1
  78. }
  79. _venv_path() {
  80. # returns the path to the env/bin of the latest python,
  81. # relative to the top-level hue directory.
  82. local version="$1"
  83. if [ -z "$version" ]; then
  84. return 1 # Return error if no version provided/found
  85. fi
  86. if [ "$version" = "$LATEST_PYTHON_VERSION" ]; then
  87. echo "build/env/bin"
  88. else
  89. echo "build/venvs/python${version}/bin"
  90. fi
  91. }
  92. export SELECTED_PYTHON_VERSION="$(_choose_python_version)"
  93. export VENV_BIN_PATH=${HUE_HOME_DIR}/$(_venv_path "$SELECTED_PYTHON_VERSION")