Skip to content

Commit

Permalink
fix: Lock file script changes to fail fast (#774)
Browse files Browse the repository at this point in the history
* Lock file script changes to fail fast

* Single quoted dir names in lock file script's print statements

* Updated delimiter for sed in lock file script to support / in dir path

* Fix to lock file automation workflow - actionlint-docker related involving shellcheck

---------

Co-authored-by: Ritwik G <[email protected]>
  • Loading branch information
chandrasekharan-zipstack and ritwik-g authored Oct 7, 2024
1 parent 5c4f9bc commit 887dfde
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pdm-lock-automation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
IFS=',' read -r -a dir_array <<< "$dirs"
# Print directories being processed
echo "Processing specified directories: ${dir_array[@]}"
echo "Processing specified directories: ${dir_array[*]}"
# Pass directories as command-line arguments to the script
./scripts/pdm-lock.sh "${dir_array[@]}"
Expand Down
65 changes: 48 additions & 17 deletions scripts/pdm-lock.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
#!/bin/bash
set -o pipefail

# Function to update the lockfile in a directory
update_lockfile() {
dir="$1"
file_path="$dir/pyproject.toml"

if [[ ! -f "$file_path" ]]; then
echo "[$dir] No pyproject.toml found in $dir"
return
echo "[$dir] No pyproject.toml found in '$dir'"
return 0
fi

echo "[$dir] Checking $file_path for changes against origin/main..."
echo "[$dir] Checking '$file_path' for changes against origin/main..."
if ! git diff --quiet origin/main -- "$file_path"; then
echo "[$dir] Changes detected in $file_path, updating pdm.lock for $dir ..."
echo "[$dir] Changes detected in '$file_path', updating pdm.lock for '$dir' ..."

# Move to the directory if it's not root
if [[ "$dir" != "." ]]; then
cd "$dir"
cd "$dir" || return 1
fi

# Set up virtual environment if not exists
if [[ ! -d ".venv" ]]; then
echo "[$dir] Creating virtual environment in directory: $dir"
pdm venv create -w virtualenv --with-pip
echo "[$dir] Creating virtual environment in directory: '$dir'"
pdm venv create -w virtualenv --with-pip 2>&1 | sed "s|^|[$dir] |" || return 1
else
echo "[$dir] Virtual environment already exists in $dir"
fi

# Activate virtual environment
source .venv/bin/activate
source .venv/bin/activate 2>&1 | sed "s|^|[$dir] |" || return 1

# HACK: https://github.com/pdm-project/pdm/issues/3199
# Replace with checking the exit code directly once above issue is fixed
lock_output=$(pdm lock --check 2>&1)
if echo "$lock_output" | grep -q "WARNING: Lockfile is generated on an older version of PDM"; then
echo "[$dir] Updating pdm.lock in $dir due to outdated version..."
pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /"
echo "[$dir] Updating pdm.lock in '$dir' due to outdated version..."
pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1
elif [[ $? -ne 0 ]]; then
echo "[$dir] Updating pdm.lock in $dir due to detected changes..."
pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /"
echo "[$dir] Updating pdm.lock in '$dir' due to detected changes..."
pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1
else
echo "[$dir] No changes required for pdm.lock in $dir."
echo "[$dir] No changes required for pdm.lock in '$dir'."
fi

# Go back to root if moved to a subdirectory
if [[ "$dir" != "." ]]; then
cd -
cd - || return 1
fi
else
echo "[$dir] No changes detected in $file_path"
echo "[$dir] No changes detected in '$file_path'"
fi
}

# https://unix.stackexchange.com/a/124148
# Used to list child processes to kill them in case of an error
list_descendants ()
{
local children=$(ps -o pid= --ppid "$1")

for pid in $children
do
list_descendants "$pid"
done

echo "$children"
}

# Default directories list
directories=(
Expand All @@ -75,11 +89,28 @@ fi
# To compare against main branch
git fetch origin main

# Array to store the job PIDs and directories
pids=()
dirs=()

# Run lockfile updates in parallel
for dir in "${directories[@]}"; do
update_lockfile "$dir" &
pid=$!
pids+=($pid) # Add the PID of the background job to the array
dirs+=("$dir") # Add the corresponding directory to the array
done

# Wait for all background processes to complete
wait
# Wait for each background process to complete, exit on the first failure
for i in "${!pids[@]}"; do
pid=${pids[$i]}
dir=${dirs[$i]}
echo "[$dir] Waiting for child process with PID: $pid..."

# # Wait for the specific process to finish
if ! wait "$pid"; then
echo "[$dir] Lock file generation failed. Killing other sub-processes..."
kill $(list_descendants $$) 2>/dev/null || true
exit 1
fi
done

0 comments on commit 887dfde

Please sign in to comment.