-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_python_packages.sh
More file actions
98 lines (81 loc) · 3.27 KB
/
update_python_packages.sh
File metadata and controls
98 lines (81 loc) · 3.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
set -e
NEW_PYTHON_VERSION="3.12"
OLD_PYTHON_VERSION=""
LAMBDA_FOLDER_PATH=""
REQUIREMENTS_PATH=""
FILES=()
usage() {
echo "Usage: $0 -p <lambda_folder_path> -f <file_paths> [-v <NEW_python_version>]"
echo " -p Path to lambda code files (required)"
echo " -f Comma-separated list of file paths to include (e.g., common.py)"
echo " -n New python version to update to (default: ${NEW_PYTHON_VERSION})"
echo " -o Old python version to find and replace (required)"
echo " -r Path to the requirements file (required)"
echo ""
echo "Example: $0 ./update_python_packages.sh -p /terraform-app-google-device-management/google_management_lambda -f dynamo.py,common.py,oomnitza.py -n 3.12 -o 3.9 -r /terraform-app-google-device-management/google_management_lambda/packages/requirements.txt"
exit 1
}
while getopts ":p:n:o:r:f:" opt; do
case $opt in
p) LAMBDA_FOLDER_PATH="$OPTARG" ;;
n) NEW_PYTHON_VERSION="$OPTARG" ;;
o) OLD_PYTHON_VERSION="$OPTARG" ;;
r) REQUIREMENTS_PATH="$OPTARG" ;;
f) IFS=',' read -r -a FILES <<< "$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2; usage ;;
:) echo "Option -$OPTARG requires an argument" >&2; usage ;;
esac
done
# if pyenv is not installed print error
if ! command -v pyenv 1>/dev/null 2>&1; then
echo "pyenv is not installed. Please install pyenv and run the script again."
exit 1
fi
# use pyenv to install the new python version, then find the path
pyenv install --skip-existing $NEW_PYTHON_VERSION
pyenv global $NEW_PYTHON_VERSION
PYTHONCMD=$(pyenv which python3 --skip-advice)
if [ ! -d "venv" ]; then
echo "Creating virtual env"
$PYTHONCMD -m venv venv
source venv/bin/activate
fi
if [ -z "$LAMBDA_FOLDER_PATH" ]; then
echo "Lambda code path (-p) is required"
usage
fi
if [ -z "$REQUIREMENTS_PATH" ]; then
echo "Requirements path (-r) is required"
usage
fi
if [ -z "$OLD_PYTHON_VERSION" ]; then
echo "Old python version to find and replace (-o) is required"
usage
fi
# Make a directory to store all of the output from this script
UPDATE_OUTPUT_PATH="${LAMBDA_FOLDER_PATH}/latest-python-version-packages"
NEW_SITE_PACKAGES_PATH="${UPDATE_OUTPUT_PATH}/python/lib/python${NEW_PYTHON_VERSION}/site-packages"
/bin/mkdir -p "$NEW_SITE_PACKAGES_PATH"
for FILE in "${FILES[@]}"; do
FILE_PATH="${LAMBDA_FOLDER_PATH}/packages/python/lib/python${OLD_PYTHON_VERSION}/site-packages/${FILE}"
COPY_PATH="${NEW_SITE_PACKAGES_PATH}/${FILE}"
if [ -f "$FILE_PATH" ]; then
# Copy files to the output directory if it is not a symlink
if [ ! -L "$COPY_PATH" ]; then
echo "Copying ${FILE}"
/bin/cp "$FILE_PATH" "$COPY_PATH"
else
echo "${FILE} already exists"
fi
else
echo "${FILE} not found in the specified lambda path"
fi
done
echo "Installing Python requirements into the output directory"
./venv/bin/python3 -m pip install --upgrade wheel
# set up for aws lambda
rm -rf "${LAMBDA_FOLDER_PATH}/site-packages"
./venv/bin/python3 -m pip install --target "${NEW_SITE_PACKAGES_PATH}" --platform manylinux2014_x86_64 --python-version "${NEW_PYTHON_VERSION}" --only-binary=:all: -r "${REQUIREMENTS_PATH}"
echo "Generating requirements.txt in the output directory"
./venv/bin/python3 -m pip freeze --path "${NEW_SITE_PACKAGES_PATH}" > "${UPDATE_OUTPUT_PATH}/requirements.txt"