Skip to content

Commit bf46638

Browse files
committed
♻️ Refactor autoversioning script
1 parent 7bcf7ee commit bf46638

34 files changed

+197
-90
lines changed

.github/scripts/autoversioning.sh

Lines changed: 159 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

3-
# Copyright (C) 2024 C-PAC Developers
3+
# Copyright (C) 2024-2025 C-PAC Developers
44

55
# This file is part of C-PAC.
66

@@ -17,54 +17,170 @@
1717
# You should have received a copy of the GNU Lesser General Public
1818
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
1919

20-
# Update version comment strings
21-
function wait_for_git_lock() {
22-
while [ -f "./.git/index.lock" ]; do
23-
echo "Waiting for the git lock file to be removed..."
24-
sleep 1
25-
done
20+
21+
set -euo pipefail
22+
trap 'echo "❌ Script failed at line $LINENO with exit code $?"' ERR
23+
24+
# -------------------------------------------------------------------------
25+
# Helpers
26+
# -------------------------------------------------------------------------
27+
28+
git_add_with_retry() {
29+
local file=$1
30+
local attempts=0
31+
local max_attempts=10
32+
while ! git add "$file"; do
33+
attempts=$((attempts+1))
34+
echo "Git add failed for $file (attempt $attempts), retrying..."
35+
sleep 1
36+
if [[ $attempts -ge $max_attempts ]]; then
37+
echo "❌ Failed to git add $file after $max_attempts attempts"
38+
exit 1
39+
fi
40+
done
41+
}
42+
43+
update_file_if_changed() {
44+
# Run a regex replacement or copy on a file and stage it if it changed
45+
local expr=$1
46+
local src=$2
47+
local dest=${3:-$src}
48+
49+
local changed=0
50+
if [[ -n "$expr" ]]; then
51+
tmp=$(mktemp)
52+
sed -E "$expr" "$src" > "$tmp"
53+
if ! cmp -s "$tmp" "$dest"; then
54+
mv "$tmp" "$dest"
55+
git_add_with_retry "$dest"
56+
changed=1
57+
else
58+
rm "$tmp"
59+
fi
60+
else
61+
if [[ ! -f "$dest" ]] || ! cmp -s "$src" "$dest"; then
62+
cp "$src" "$dest"
63+
git_add_with_retry "$dest"
64+
changed=1
65+
fi
66+
fi
67+
return $changed
2668
}
2769

28-
cd CPAC || exit 1
29-
VERSION=$(python -c "from info import __version__; print(('.'.join(('.'.join(__version__[::-1].split('-')[1].split('.')[1:])[::-1], __version__.split('-')[1])) if '-' in __version__ else __version__).split('+', 1)[0])")
30-
cd ..
31-
echo "v${VERSION}" > version
32-
export _SED_COMMAND="s/^(# [Vv]ersion ).*$/# Version ${VERSION}/g"
33-
if [[ "$OSTYPE" == "darwin"* ]]; then
34-
# Mac OSX
35-
find ./CPAC/resources/configs -name "*.yml" -exec sed -i '' -E "${_SED_COMMAND}" {} \;
70+
log_info() {
71+
echo "=== $* ==="
72+
}
73+
74+
# -------------------------------------------------------------------------
75+
# Main
76+
# -------------------------------------------------------------------------
77+
78+
START_DIR=$(pwd)
79+
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
80+
REPO_ROOT="$(realpath "$SCRIPT_DIR/../..")"
81+
82+
# -------------------------------------------------------------------------
83+
# Fetch version
84+
# -------------------------------------------------------------------------
85+
log_info "Fetching version"
86+
VERSION=$(python -c "import sys; sys.path.insert(0, '$REPO_ROOT/CPAC'); from info import __version__; print(__version__.split('+', 1)[0])")
87+
VERSION_FILE="$REPO_ROOT/version"
88+
if [[ -f "$VERSION_FILE" ]]; then
89+
OLD_VERSION=$(<"$VERSION_FILE")
3690
else
37-
# Linux and others
38-
find ./CPAC/resources/configs -name "*.yml" -exec sed -i'' -r "${_SED_COMMAND}" {} \;
91+
OLD_VERSION="<none>"
92+
fi
93+
echo "v${VERSION}" > "$VERSION_FILE"
94+
95+
# -------------------------------------------------------------------------
96+
# Write version file and stage it
97+
# -------------------------------------------------------------------------
98+
log_info "Updating version file"
99+
if update_file_if_changed "" <(echo "v${VERSION}") "$VERSION_FILE"; then
100+
git_add_with_retry "$VERSION_FILE"
39101
fi
40-
wait_for_git_lock && git add version
41-
VERSIONS=( `git show $(git log --pretty=format:'%h' -n 1 version | tail -n 1):version` `cat version` )
42-
export PATTERN="(declare|typeset) -a"
43-
if [[ "$(declare -p VERSIONS)" =~ $PATTERN ]]
44-
then
45-
for DOCKERFILE in $(find ./.github/Dockerfiles -name "*.Dockerfile")
46-
do
47-
export IFS=""
48-
for LINE in $(grep "FROM ghcr\.io/fcp\-indi/c\-pac/.*\-${VERSIONS[0]}" ${DOCKERFILE})
49-
do
50-
echo "Updating stage tags in ${DOCKERFILE}"
102+
103+
# -------------------------------------------------------------------------
104+
# Update YAML config files
105+
# -------------------------------------------------------------------------
106+
log_info "Updating YAML config files"
107+
VERSION_EXPR="s/^(# [Vv]ersion ).*$/# Version ${VERSION}/g"
108+
for YAML_FILE in "$REPO_ROOT"/CPAC/resources/configs/*.yml; do
109+
echo "Processing ${YAML_FILE}"
110+
echo "Applying regex: ${VERSION_EXPR}"
111+
112+
# Run sed safely
113+
tmp=$(mktemp)
114+
if ! sed -E "$VERSION_EXPR" "$YAML_FILE" > "$tmp"; then
115+
echo "❌ sed failed on $YAML_FILE"
116+
rm "$tmp"
117+
exit 1
118+
fi
119+
120+
if ! cmp -s "$tmp" "$YAML_FILE"; then
121+
mv "$tmp" "$YAML_FILE"
122+
echo "Updated $YAML_FILE"
123+
git_add_with_retry "$YAML_FILE"
124+
else
125+
rm "$tmp"
126+
echo "No changes needed for $YAML_FILE"
127+
fi
128+
done
129+
130+
# -------------------------------------------------------------------------
131+
# Update Dockerfiles (only C-PAC tags)
132+
# -------------------------------------------------------------------------
133+
log_info "Updating Dockerfiles"
134+
NEW_VERSION=$(<"$VERSION_FILE")
135+
136+
if [[ "$OLD_VERSION" != "$NEW_VERSION" ]]; then
137+
for DOCKERFILE in "$REPO_ROOT"/.github/Dockerfiles/*.Dockerfile; do
138+
if grep -q "FROM ghcr\.io/fcp-indi/c-pac/.*-${OLD_VERSION}" "$DOCKERFILE"; then
139+
echo "Updating C-PAC version in ${DOCKERFILE} from ${OLD_VERSION} to ${NEW_VERSION}"
140+
51141
if [[ "$OSTYPE" == "darwin"* ]]; then
52-
# Mac OSX
53-
sed -i "" "s/\-${VERSIONS[0]}/\-${VERSIONS[1]}/g" ${DOCKERFILE}
142+
# macOS sed
143+
sed -i "" "s/-${OLD_VERSION}/-${NEW_VERSION}/g" "$DOCKERFILE"
54144
else
55-
# Linux and others
56-
sed -i "s/\-${VERSIONS[0]}/\-${VERSIONS[1]}/g" ${DOCKERFILE}
145+
# Linux sed
146+
sed -i -E "s/-${OLD_VERSION}/-${NEW_VERSION}/g" "$DOCKERFILE"
57147
fi
58-
done
148+
149+
git_add_with_retry "$DOCKERFILE"
150+
fi
59151
done
60-
unset IFS
61152
fi
62-
wait_for_git_lock && git add CPAC/resources/configs .github/Dockerfiles
63-
64-
# Overwrite top-level Dockerfiles with the CI Dockerfiles
65-
wait_for_git_lock && cp .github/Dockerfiles/C-PAC.develop-jammy.Dockerfile Dockerfile
66-
wait_for_git_lock && cp .github/Dockerfiles/C-PAC.develop-lite-jammy.Dockerfile variant-lite.Dockerfile
67-
for DOCKERFILE in $(ls *Dockerfile)
68-
do
69-
wait_for_git_lock && git add $DOCKERFILE
153+
154+
# -------------------------------------------------------------------------
155+
# Overwrite top-level Dockerfiles
156+
# -------------------------------------------------------------------------
157+
log_info "Updating top-level Dockerfiles"
158+
TOP_DOCKERFILES=(
159+
".github/Dockerfiles/C-PAC.develop-jammy.Dockerfile:Dockerfile"
160+
".github/Dockerfiles/C-PAC.develop-lite-jammy.Dockerfile:variant-lite.Dockerfile"
161+
)
162+
for SRC_DST in "${TOP_DOCKERFILES[@]}"; do
163+
# Split SRC_DST by colon safely
164+
SRC="${SRC_DST%%:*}"
165+
DST="${SRC_DST##*:}"
166+
167+
FULL_SRC="$REPO_ROOT/$SRC"
168+
FULL_DST="$REPO_ROOT/$DST"
169+
170+
if [[ ! -f "$FULL_SRC" ]]; then
171+
echo "⚠️ Source Dockerfile does not exist: $FULL_SRC"
172+
continue
173+
fi
174+
echo "Updating top-level Dockerfile: $FULL_DST from $FULL_SRC"
175+
cp "$FULL_SRC" "$FULL_DST" && git_add_with_retry "$FULL_DST"
70176
done
177+
178+
# Return to original directory
179+
cd "$START_DIR"
180+
181+
# -------------------------------------------------------------------------
182+
# Summary
183+
# -------------------------------------------------------------------------
184+
echo
185+
echo "Version changed: (from ${OLD_VERSION} to ${NEW_VERSION})"
186+
echo "======================"

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ repos:
6969
name: Update Dockerfiles and version comments
7070
entry: .github/scripts/autoversioning.sh
7171
language: script
72-
files: '.*Dockerfile$|.*\.yaml$|^CPAC/info\.py$'
72+
files: '(^CPAC/info\.py$|.*Dockerfile$|.*\.ya?ml$)'
7373
- id: update-yaml-comments
7474
name: Update YAML comments
7575
entry: CPAC/utils/configuration/yaml_template.py

CPAC/resources/configs/data_config_S3-BIDS-ABIDE.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Configuration File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/data_config_S3-BIDS-ADHD200.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Configuration File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/data_config_S3-BIDS-ADHD200_only2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Configuration File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/data_config_S3-BIDS-NKI-RocklandSample.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Configuration File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/data_config_cpac_benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Configuration File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/data_settings_template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Data Settings File
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/group_config_template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CPAC Group-Level Analysis Configuration YAML file
2-
# Version
2+
# Version 1.8.8.dev1
33
#
44
# http://fcp-indi.github.io for more info.
55
#

CPAC/resources/configs/pipeline_config_abcd-options.yml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%YAML 1.1
22
---
33
# CPAC Pipeline Configuration YAML file
4-
# Version
4+
# Version 1.8.8.dev1
55
#
66
# http://fcp-indi.github.io for more info.
77
#
@@ -217,20 +217,17 @@ registration_workflows:
217217
# Mask the sbref created by coregistration input prep nodeblocks above before registration
218218
mask_sbref: Off
219219

220-
# Choose coregistration interpolation
221-
interpolation: spline
222-
223-
# Choose coregistration degree of freedom
224-
dof: 12
225220
boundary_based_registration:
226221

227222
# this is a fork point
228223
# run: [On, Off] - this will run both and fork the pipeline
229224
run: [On]
230225

231-
# reference for boundary based registration
232-
# options: 'whole-head' or 'brain'
233-
reference: whole-head
226+
# Choose coregistration interpolation
227+
interpolation: spline
228+
229+
# Choose coregistration degree of freedom
230+
dof: 12
234231

235232
func_registration_to_template:
236233

@@ -269,12 +266,6 @@ registration_workflows:
269266
# Interpolation method for writing out transformed functional images.
270267
# Possible values: Linear, BSpline, LanczosWindowedSinc
271268
interpolation: Linear
272-
EPI_registration:
273-
274-
# directly register the mean functional to an EPI template
275-
# instead of applying the anatomical T1-to-template transform to the functional data that has been
276-
# coregistered to anatomical/T1 space
277-
run: off
278269

279270
functional_preproc:
280271
run: On

0 commit comments

Comments
 (0)