|
1 | 1 | #!/bin/bash |
| 2 | +set -o pipefail |
2 | 3 |
|
3 | 4 | # Function to update the lockfile in a directory |
4 | 5 | update_lockfile() { |
5 | 6 | dir="$1" |
6 | 7 | file_path="$dir/pyproject.toml" |
7 | 8 |
|
8 | 9 | if [[ ! -f "$file_path" ]]; then |
9 | | - echo "[$dir] No pyproject.toml found in $dir" |
10 | | - return |
| 10 | + echo "[$dir] No pyproject.toml found in '$dir'" |
| 11 | + return 0 |
11 | 12 | fi |
12 | 13 |
|
13 | | - echo "[$dir] Checking $file_path for changes against origin/main..." |
| 14 | + echo "[$dir] Checking '$file_path' for changes against origin/main..." |
14 | 15 | if ! git diff --quiet origin/main -- "$file_path"; then |
15 | | - echo "[$dir] Changes detected in $file_path, updating pdm.lock for $dir ..." |
| 16 | + echo "[$dir] Changes detected in '$file_path', updating pdm.lock for '$dir' ..." |
16 | 17 |
|
17 | 18 | # Move to the directory if it's not root |
18 | 19 | if [[ "$dir" != "." ]]; then |
19 | | - cd "$dir" |
| 20 | + cd "$dir" || return 1 |
20 | 21 | fi |
21 | 22 |
|
22 | 23 | # Set up virtual environment if not exists |
23 | 24 | if [[ ! -d ".venv" ]]; then |
24 | | - echo "[$dir] Creating virtual environment in directory: $dir" |
25 | | - pdm venv create -w virtualenv --with-pip |
| 25 | + echo "[$dir] Creating virtual environment in directory: '$dir'" |
| 26 | + pdm venv create -w virtualenv --with-pip 2>&1 | sed "s|^|[$dir] |" || return 1 |
26 | 27 | else |
27 | 28 | echo "[$dir] Virtual environment already exists in $dir" |
28 | 29 | fi |
29 | 30 |
|
30 | 31 | # Activate virtual environment |
31 | | - source .venv/bin/activate |
| 32 | + source .venv/bin/activate 2>&1 | sed "s|^|[$dir] |" || return 1 |
32 | 33 |
|
33 | 34 | # HACK: https://github.com/pdm-project/pdm/issues/3199 |
34 | 35 | # Replace with checking the exit code directly once above issue is fixed |
35 | 36 | lock_output=$(pdm lock --check 2>&1) |
36 | 37 | if echo "$lock_output" | grep -q "WARNING: Lockfile is generated on an older version of PDM"; then |
37 | | - echo "[$dir] Updating pdm.lock in $dir due to outdated version..." |
38 | | - pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /" |
| 38 | + echo "[$dir] Updating pdm.lock in '$dir' due to outdated version..." |
| 39 | + pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1 |
39 | 40 | elif [[ $? -ne 0 ]]; then |
40 | | - echo "[$dir] Updating pdm.lock in $dir due to detected changes..." |
41 | | - pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /" |
| 41 | + echo "[$dir] Updating pdm.lock in '$dir' due to detected changes..." |
| 42 | + pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1 |
42 | 43 | else |
43 | | - echo "[$dir] No changes required for pdm.lock in $dir." |
| 44 | + echo "[$dir] No changes required for pdm.lock in '$dir'." |
44 | 45 | fi |
45 | 46 |
|
46 | 47 | # Go back to root if moved to a subdirectory |
47 | 48 | if [[ "$dir" != "." ]]; then |
48 | | - cd - |
| 49 | + cd - || return 1 |
49 | 50 | fi |
50 | 51 | else |
51 | | - echo "[$dir] No changes detected in $file_path" |
| 52 | + echo "[$dir] No changes detected in '$file_path'" |
52 | 53 | fi |
53 | 54 | } |
54 | 55 |
|
| 56 | +# https://unix.stackexchange.com/a/124148 |
| 57 | +# Used to list child processes to kill them in case of an error |
| 58 | +list_descendants () |
| 59 | +{ |
| 60 | + local children=$(ps -o pid= --ppid "$1") |
| 61 | + |
| 62 | + for pid in $children |
| 63 | + do |
| 64 | + list_descendants "$pid" |
| 65 | + done |
| 66 | + |
| 67 | + echo "$children" |
| 68 | +} |
55 | 69 |
|
56 | 70 | # Default directories list |
57 | 71 | directories=( |
|
75 | 89 | # To compare against main branch |
76 | 90 | git fetch origin main |
77 | 91 |
|
| 92 | +# Array to store the job PIDs and directories |
| 93 | +pids=() |
| 94 | +dirs=() |
78 | 95 |
|
79 | 96 | # Run lockfile updates in parallel |
80 | 97 | for dir in "${directories[@]}"; do |
81 | 98 | update_lockfile "$dir" & |
| 99 | + pid=$! |
| 100 | + pids+=($pid) # Add the PID of the background job to the array |
| 101 | + dirs+=("$dir") # Add the corresponding directory to the array |
82 | 102 | done |
83 | 103 |
|
84 | | -# Wait for all background processes to complete |
85 | | -wait |
| 104 | +# Wait for each background process to complete, exit on the first failure |
| 105 | +for i in "${!pids[@]}"; do |
| 106 | + pid=${pids[$i]} |
| 107 | + dir=${dirs[$i]} |
| 108 | + echo "[$dir] Waiting for child process with PID: $pid..." |
| 109 | + |
| 110 | + # # Wait for the specific process to finish |
| 111 | + if ! wait "$pid"; then |
| 112 | + echo "[$dir] Lock file generation failed. Killing other sub-processes..." |
| 113 | + kill $(list_descendants $$) 2>/dev/null || true |
| 114 | + exit 1 |
| 115 | + fi |
| 116 | +done |
0 commit comments