Skip to content

Commit 244d186

Browse files
Merge pull request #680 from paulovmr/RHOAIENG-11090
RHOAIENG-11090: Create a script to automate multiple library upgrades
2 parents f04b978 + 3eb1bf9 commit 244d186

File tree

2 files changed

+69
-63
lines changed

2 files changed

+69
-63
lines changed

scripts/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
## update_library_version.sh
44

5-
This script updates the version of a specified library in Pipfile and requirements-elyra.txt files, only if the new version is higher. It can optionally run `pipenv lock` after updating the version.
5+
This script updates the version of one or more libraries in Pipfile and requirements-elyra.txt files, only if the new version is higher. It can optionally run `pipenv lock` after updating the version.
66

77
### Examples
88

9-
Update the `numpy` library to version `2.0.1` in all files under `./myproject`, and run `pipenv lock`:
9+
Update the `numpy` library to version `2.0.1` and the `pandas` library to version `2.2.2` in all files under the current folder (`.`), and run `pipenv lock`:
1010

1111
```sh
12-
./update_library_version.sh ./myproject numpy 2.0.1 '' '' true
12+
./scripts/update_library_version.sh . '[{"name":"numpy","version":"2.0.1"},{"name":"pandas","version":"2.2.2"}]' '' '' true
1313
```
1414

15-
Update the `pandas` library to version `2.2.2` in all files under `./myproject` where the directory contains `include` or `this`, excluding directories containing `exclude` or `that`, and do not run `pipenv lock`:
15+
Update the `pandas` library to version `2.2.2` in all files under the current folder (`.`), where the directory contains `include` or `this`, excluding directories containing `exclude` or `that`, and do not run `pipenv lock`:
1616

1717
```sh
18-
./update_library_version.sh ./myproject pandas 2.2.2 'include|this' 'exclude|that' false
18+
./scripts/update_library_version.sh . '[{"name":"pandas","version":"2.2.2"}]' 'include|this' 'exclude|that' false
1919
```
2020

2121
## new_python_based_image.py

scripts/update_library_version.sh

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,42 @@ fi
88

99
# Detailed help message
1010
print_help() {
11-
echo "Usage: $0 <directory> <library_name> <new_version> <include_paths_with> <exclude_paths_with> <lock_files>"
11+
echo "Usage: $0 <directory> <libraries> <include_paths_with> <exclude_paths_with> <lock_files>"
1212
echo
1313
echo "Parameters:"
1414
echo " directory The root directory to start searching for Pipfile and requirements-elyra.txt files."
15-
echo " library_name The name of the library to update."
16-
echo " new_version The new version to set for the library."
15+
echo " libraries JSON formatted array of objects with 'name' and 'version' fields to update."
1716
echo " include_paths_with A pipe-separated list of substrings; only files in directories containing at least one of these substrings will be processed."
1817
echo " exclude_paths_with A pipe-separated list of substrings; files in directories containing any of these substrings will be excluded."
1918
echo " lock_files Whether to run 'pipenv lock' after updating the library version (true or false)."
2019
echo
2120
echo "Examples:"
22-
echo " $0 ./myproject numpy 2.0.1 '' '' true"
23-
echo " $0 ./myproject pandas 2.2.2 'include|this' 'exclude|that' false"
21+
echo " $0 ./myproject '[{\"name\": \"numpy\", \"version\": \"2.0.1\"}, {\"name\": \"pandas\", \"version\": \"2.2.2\"}]' '' '' true"
2422
}
2523

2624
# Check if the correct number of arguments are passed
27-
if [ "$#" -ne 6 ]; then
25+
if [ "$#" -ne 5 ]; then
2826
print_help
2927
exit 1
3028
fi
3129

3230
# Arguments
3331
directory=$1
34-
library_name=$2
35-
new_version=$3
36-
include_paths_with=$4
37-
exclude_paths_with=$5
38-
lock_files=$6
32+
libraries_json=$2
33+
include_paths_with=$3
34+
exclude_paths_with=$4
35+
lock_files=$5
36+
37+
# Storage for the directories that need to be locked at the end
38+
files_to_lock_temp_file=$(mktemp)
3939

4040
# Print arguments
4141
echo "Arguments:"
42-
echo " directory = $directory"
43-
echo " library_name = $library_name"
44-
echo " new_version = $new_version"
45-
echo " include_paths_with = $include_paths_with"
46-
echo " exclude_paths_with = $exclude_paths_with"
47-
echo " lock_files = $lock_files"
42+
echo " directory = $directory"
43+
echo " libraries = $libraries_json"
44+
echo " include_paths_with = $include_paths_with"
45+
echo " exclude_paths_with = $exclude_paths_with"
46+
echo " lock_files = $lock_files"
4847

4948
# Function to check if one version is higher than the other
5049
# Returns 0 if the first version is greater and 1 if the second is greater or equal
@@ -64,7 +63,7 @@ is_version_higher() {
6463
# Use 0 if a part is missing
6564
v1=${ver1_parts[i]:-0}
6665
v2=${ver2_parts[i]:-0}
67-
66+
6867
# Compare the parts
6968
if ((v1 > v2)); then
7069
return 0
@@ -82,23 +81,11 @@ update_library_version_pipfile() {
8281
local include_paths_with=$4
8382
local exclude_paths_with=$5
8483
local lock_files=$6
84+
local files_to_lock_temp_file=$7
8585
local directory=$(dirname "$file")
8686
local filename=$(basename "$file")
8787

88-
# Determine if this is an architecture-specific Pipfile (with the "gpu" or "cpu" suffixes) and determine the corresponding lock file name
89-
local is_specific=false
90-
local lockfile=""
91-
if [[ "$filename" == Pipfile.gpu ]]; then
92-
is_specific=true
93-
lockfile="Pipfile.lock.gpu"
94-
elif [[ "$filename" == Pipfile.cpu ]]; then
95-
is_specific=true
96-
lockfile="Pipfile.lock.cpu"
97-
else
98-
lockfile="Pipfile.lock"
99-
fi
100-
101-
# Check if the file directory has at least one of the substrings (separated by "|") in $include_paths_with
88+
# Check if the file directory has at least one of the substrings (separated by "|") in $include_paths_with
10289
# and does not contain any of the substrings (separated by "|") in $exclude_paths_with
10390
if { [[ -z "$include_paths_with" ]] || [[ "$directory" =~ $include_paths_with ]]; } && { [[ -z "$exclude_paths_with" ]] || [[ ! "$directory" =~ $exclude_paths_with ]]; }; then
10491
echo "Processing $file (directory matches the pattern)"
@@ -136,18 +123,7 @@ update_library_version_pipfile() {
136123
fi
137124
echo "Updated $lib in $file to version ${current_qualifier}${new_ver}"
138125

139-
# Handle renaming and pipenv lock, if necessary
140-
if [ "$lock_files" == "true" ]; then
141-
if [ "$is_specific" = true ]; then
142-
mv "$file" "${directory}/Pipfile"
143-
mv "${directory}/$lockfile" "${directory}/Pipfile.lock"
144-
(cd "$directory" && pipenv lock)
145-
mv "${directory}/Pipfile" "$file"
146-
mv "${directory}/Pipfile.lock" "${directory}/$lockfile"
147-
else
148-
(cd "$directory" && pipenv lock)
149-
fi
150-
fi
126+
echo "$file" >> "$files_to_lock_temp_file"
151127
else
152128
echo "$lib in $file is already up-to-date or has a higher version ($current_ver)."
153129
fi
@@ -161,8 +137,8 @@ update_library_version_requirements() {
161137
local include_paths_with=$4
162138
local exclude_paths_with=$5
163139
local directory=$(dirname "$file")
164-
165-
# Check if the file directory has at least one of the substrings (separated by "|") in $include_paths_with
140+
141+
# Check if the file directory has at least one of the substrings (separated by "|") in $include_paths_with
166142
# and does not contain any of the substrings (separated by "|") in $exclude_paths_with
167143
if { [[ -z "$include_paths_with" ]] || [[ "$directory" =~ $include_paths_with ]]; } && { [[ -z "$exclude_paths_with" ]] || [[ ! "$directory" =~ $exclude_paths_with ]]; }; then
168144
echo "Processing $file (directory matches the pattern)"
@@ -198,18 +174,48 @@ export -f is_version_higher
198174
export -f update_library_version_pipfile
199175
export -f update_library_version_requirements
200176

177+
# Skip double quotes that were not skipped in the libraries parameter
178+
libraries_json=$(echo "$libraries_json" | sed -E 's/(^|[^\\])"/\1\\"/g')
179+
201180
# Find and update Pipfile files and requirements-elyra.txt files
202181
find "$directory" -type f \( -name "Pipfile" -o -name "Pipfile.gpu" -o -name "Pipfile.cpu" -o -name "requirements-elyra.txt" \) -exec bash -c '
203182
file=$0
204-
lib=$1
205-
new_ver=$2
206-
include_paths_with=$3
207-
exclude_paths_with=$4
208-
lock_files=$5
209-
210-
case "$file" in
211-
*Pipfile* ) update_library_version_pipfile "$file" "$lib" "$new_ver" "$include_paths_with" "$exclude_paths_with" "$lock_files" ;;
212-
*requirements-elyra.txt* ) update_library_version_requirements "$file" "$lib" "$new_ver" "$include_paths_with" "$exclude_paths_with" ;;
213-
esac
214-
' {} "$library_name" "$new_version" "$include_paths_with" "$exclude_paths_with" "$lock_files" \;
215-
183+
libraries_json=$1
184+
include_paths_with=$2
185+
exclude_paths_with=$3
186+
lock_files=$4
187+
files_to_lock_temp_file=$5
188+
189+
echo "'$libraries_json'" | jq -c ".[]" | while IFS= read -r lib_info; do
190+
lib_name=$(echo "$lib_info" | jq -r ".name")
191+
lib_version=$(echo "$lib_info" | jq -r ".version")
192+
193+
case "$file" in
194+
*Pipfile* ) update_library_version_pipfile "$file" "$lib_name" "$lib_version" "$include_paths_with" "$exclude_paths_with" "$lock_files" "$files_to_lock_temp_file" ;;
195+
*requirements-elyra.txt* ) update_library_version_requirements "$file" "$lib_name" "$lib_version" "$include_paths_with" "$exclude_paths_with" ;;
196+
esac
197+
done
198+
' {} "$libraries_json" "$include_paths_with" "$exclude_paths_with" "$lock_files" "$files_to_lock_temp_file" \;
199+
200+
# Lock the modified files if needed
201+
modified_files=($(cat "$files_to_lock_temp_file"))
202+
rm "$files_to_lock_temp_file"
203+
204+
if [ "$lock_files" == "true" ]; then
205+
for file in "${modified_files[@]}"; do
206+
directory=$(dirname "$file")
207+
filename=$(basename "$file")
208+
echo "Locking dependencies for $directory..."
209+
if [[ "$filename" == Pipfile* ]]; then
210+
if [[ "$filename" == Pipfile.gpu || "$filename" == Pipfile.cpu ]]; then
211+
mv "$file" "${directory}/Pipfile"
212+
mv "${directory}/Pipfile.lock.${filename##*.}" "${directory}/Pipfile.lock"
213+
(cd "$directory" && pipenv lock)
214+
mv "${directory}/Pipfile" "$file"
215+
mv "${directory}/Pipfile.lock" "${directory}/Pipfile.lock.${filename##*.}"
216+
else
217+
(cd "$directory" && pipenv lock)
218+
fi
219+
fi
220+
done
221+
fi

0 commit comments

Comments
 (0)