Skip to content

Commit 2544523

Browse files
authored
Fix/check s3 (#2282)
2 parents c72d516 + 855e83a commit 2544523

34 files changed

+518
-379
lines changed

.github/scripts/autoversioning.sh

Lines changed: 163 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,174 @@
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+
cd "$REPO_ROOT"
90+
OLD_VERSION=$(git show "$(git log --pretty=format:'%h' -n 1 -- version | tail -n 1)":version)
91+
cd "$START_DIR"
3692
else
37-
# Linux and others
38-
find ./CPAC/resources/configs -name "*.yml" -exec sed -i'' -r "${_SED_COMMAND}" {} \;
93+
OLD_VERSION="<none>"
3994
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}"
95+
echo "v${VERSION}" > "$VERSION_FILE"
96+
97+
# -------------------------------------------------------------------------
98+
# Write version file and stage it
99+
# -------------------------------------------------------------------------
100+
log_info "Updating version file"
101+
if update_file_if_changed "" <(echo "v${VERSION}") "$VERSION_FILE"; then
102+
git_add_with_retry "$VERSION_FILE"
103+
fi
104+
105+
# -------------------------------------------------------------------------
106+
# Update YAML config files
107+
# -------------------------------------------------------------------------
108+
log_info "Updating YAML config files"
109+
VERSION_EXPR="s/^(# [Vv]ersion ).*$/# Version ${VERSION}/g"
110+
for YAML_FILE in "$REPO_ROOT"/CPAC/resources/configs/{*.yml,*.yaml,test_configs/*.yml,test_configs/*.yaml}; do
111+
[[ -e "$YAML_FILE" ]] || continue
112+
113+
echo "Processing ${YAML_FILE}"
114+
echo "Applying regex: ${VERSION_EXPR}"
115+
116+
# Run sed safely
117+
tmp=$(mktemp)
118+
if ! sed -E "$VERSION_EXPR" "$YAML_FILE" > "$tmp"; then
119+
echo "❌ sed failed on $YAML_FILE"
120+
rm "$tmp"
121+
exit 1
122+
fi
123+
124+
if ! cmp -s "$tmp" "$YAML_FILE"; then
125+
mv "$tmp" "$YAML_FILE"
126+
echo "Updated $YAML_FILE"
127+
git_add_with_retry "$YAML_FILE"
128+
else
129+
rm "$tmp"
130+
echo "No changes needed for $YAML_FILE"
131+
fi
132+
done
133+
134+
# -------------------------------------------------------------------------
135+
# Update Dockerfiles (only C-PAC tags)
136+
# -------------------------------------------------------------------------
137+
log_info "Updating Dockerfiles"
138+
NEW_VERSION=$(<"$VERSION_FILE")
139+
140+
if [[ "$OLD_VERSION" != "$NEW_VERSION" ]]; then
141+
for DOCKERFILE in "$REPO_ROOT"/.github/Dockerfiles/*.Dockerfile; do
142+
if grep -q "FROM ghcr\.io/fcp-indi/c-pac/.*-${OLD_VERSION}" "$DOCKERFILE"; then
143+
echo "Updating C-PAC version in ${DOCKERFILE} from ${OLD_VERSION} to ${NEW_VERSION}"
144+
51145
if [[ "$OSTYPE" == "darwin"* ]]; then
52-
# Mac OSX
53-
sed -i "" "s/\-${VERSIONS[0]}/\-${VERSIONS[1]}/g" ${DOCKERFILE}
146+
# macOS sed
147+
sed -i "" "s/-${OLD_VERSION}/-${NEW_VERSION}/g" "$DOCKERFILE"
54148
else
55-
# Linux and others
56-
sed -i "s/\-${VERSIONS[0]}/\-${VERSIONS[1]}/g" ${DOCKERFILE}
149+
# Linux sed
150+
sed -i -E "s/-${OLD_VERSION}/-${NEW_VERSION}/g" "$DOCKERFILE"
57151
fi
58-
done
152+
153+
git_add_with_retry "$DOCKERFILE"
154+
fi
59155
done
60-
unset IFS
61156
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
157+
158+
# -------------------------------------------------------------------------
159+
# Overwrite top-level Dockerfiles
160+
# -------------------------------------------------------------------------
161+
log_info "Updating top-level Dockerfiles"
162+
TOP_DOCKERFILES=(
163+
".github/Dockerfiles/C-PAC.develop-jammy.Dockerfile:Dockerfile"
164+
".github/Dockerfiles/C-PAC.develop-lite-jammy.Dockerfile:variant-lite.Dockerfile"
165+
)
166+
for SRC_DST in "${TOP_DOCKERFILES[@]}"; do
167+
# Split SRC_DST by colon safely
168+
SRC="${SRC_DST%%:*}"
169+
DST="${SRC_DST##*:}"
170+
171+
FULL_SRC="$REPO_ROOT/$SRC"
172+
FULL_DST="$REPO_ROOT/$DST"
173+
174+
if [[ ! -f "$FULL_SRC" ]]; then
175+
echo "⚠️ Source Dockerfile does not exist: $FULL_SRC"
176+
continue
177+
fi
178+
echo "Updating top-level Dockerfile: $FULL_DST from $FULL_SRC"
179+
cp "$FULL_SRC" "$FULL_DST" && git_add_with_retry "$FULL_DST"
70180
done
181+
182+
# Return to original directory
183+
cd "$START_DIR"
184+
185+
# -------------------------------------------------------------------------
186+
# Summary
187+
# -------------------------------------------------------------------------
188+
echo
189+
echo "Version changed: (from ${OLD_VERSION} to ${NEW_VERSION})"
190+
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

.ruff.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ external = ["T20"] # Don't autoremove 'noqa` comments for these rules
2020
"nibabel" = "nib"
2121
"nipype.interfaces.io" = "nio"
2222
"networkx" = "nx"
23-
"pkg_resources" = "p"
2423
"CPAC.pipeline.nipype_pipeline_engine" = "pe"
2524

2625
[lint.isort]

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5959
- Removed broken support for native-space masking.
6060
- Introduced a new `template_space_func_masking` section in the pipeline config for template-space-only methods.
6161
- Moved `Anatomical_Resampled` masking method from `func_masking` to the `template_space_func_masking`.
62+
- Upgraded resource retrieval to `importlib.resources`.
6263
- Turned `On` boundary_based_registration for abcd-options preconfig.
6364
- Refactored `transform_timeseries_to_T1template_abcd` nodeblock removing unnecessary nodes, changing `desc-preproc_T1w` inputs as reference to `desc-head_T1w`.
6465
- Appended `T1w to Template` FOV match transform to the XFM.

CPAC/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ def _docs_prefix() -> str:
3535
return DOCS_URL_PREFIX
3636

3737

38-
license_notice = f"""Copyright (C) 2022-2024 C-PAC Developers.
38+
def license_notice() -> str:
39+
"""Get the license notice for this version."""
40+
return f"""Copyright (C) 2022-2024 C-PAC Developers.
3941
4042
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
4143
and you are welcome to redistribute it under certain conditions. For
4244
details, see {_docs_prefix()}/license or the COPYING and
4345
COPYING.LESSER files included in the source code."""
46+
47+
4448
__all__ = ["license_notice", "version", "__version__"]

0 commit comments

Comments
 (0)