-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpython-wrapper.sh
More file actions
79 lines (60 loc) · 2.14 KB
/
Copy pathpython-wrapper.sh
File metadata and controls
79 lines (60 loc) · 2.14 KB
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
75
76
77
78
79
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024-present Scalytics, Inc. (https://www.scalytics.io)
# Python wrapper script that ensures virtual environment is activated
# before running any Python commands
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
APP_DIR="$( cd "$SCRIPT_DIR/.." &> /dev/null && pwd )"
# In production, enforce standard path
if [[ "$APP_DIR" == "/var/www/connect" ]]; then
# Force the standard path
VENV_DIR="$APP_DIR/venv"
# Override any existing environment variable
export PYTHON_VENV_DIR="$VENV_DIR"
if [ -d "$VENV_DIR" ] && [ -f "$VENV_DIR/bin/activate" ]; then
: # Found venv
else
mkdir -p "$VENV_DIR" 2>/dev/null
if command -v python3 -m venv &> /dev/null; then
python3 -m venv "$VENV_DIR" 2>/dev/null
if [ -d "$VENV_DIR" ] && [ -f "$VENV_DIR/bin/activate" ]; then
: # Successfully created
else
: # Failed to create
fi
fi
fi
else
VENV_DIR="${PYTHON_VENV_DIR:-$APP_DIR/venv}"
if [ -d "$VENV_DIR" ] && [ -f "$VENV_DIR/bin/activate" ]; then
: # Found venv
else
mkdir -p "$VENV_DIR" 2>/dev/null
if command -v python3 -m venv &> /dev/null; then
python3 -m venv "$VENV_DIR" 2>/dev/null
else
: # Failed to create
fi
fi
fi
if [ ! -d "$VENV_DIR" ] || [ ! -f "$VENV_DIR/bin/activate" ]; then
echo "Error: No valid Python virtual environment found or could be created at: $VENV_DIR" >&2
echo "Please create a Python virtual environment with: python3 -m venv $VENV_DIR" >&2
exit 1
fi
# Activate the virtual environment
source "$VENV_DIR/bin/activate"
# Explicitly capture and export LD_LIBRARY_PATH potentially set by activate
ACTIVATED_LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}" # Capture current value after source
export LD_LIBRARY_PATH="$ACTIVATED_LD_LIBRARY_PATH" # Ensure it's exported
# Debugging information
PYTHON_EXEC="$VENV_DIR/bin/python"
if [ "$1" = "pip" ] || [[ "$1" == *"/pip" ]]; then
shift
"$PYTHON_EXEC" -m pip "$@"
elif [ "$#" -eq 0 ]; then
"$PYTHON_EXEC"
else
"$PYTHON_EXEC" "$@"
fi