Skip to content

Commit a1fa874

Browse files
rodrigo-sobraletejedor
authored andcommitted
swancustomenvironments: Add support for pyproject.toml
Some constraints need to be taken into account: - `pyproject.toml` does not use `-r` for installing its requirements. Instead, needs to be specified by its folder path. - For this reason, the `REQ_PATH` must be changed right before it is used for installation. Since it is also used for checking the presence of `nxcals` in it. - The precedence must follow the following order: - `requirements.txt` -> `pyproject.toml` -> `requirements.in` - One of the packages included in the environment is the project's package itself, which will point to its path during the installation process. However, as we later on move the project to `SWAN_projects`, the reference will still point to `/tmp/<project_name>`. - This represents a problem because after the first clone of the project, if the user generates a `requirements.txt` file, it will still be pointing to `/tmp`. And since the project is no longer there, the next creation of the environment will continuously fail. - For fixing this, the simplest way is to reinstall the requirements right after moving the project out of `/tmp`. Since all its dependencies are installed already, they will be automatically skipped and only the project package's reference will get updated, which makes this reinstallation process way faster - Example of a well referenced package `acc-py-example-package @ file:///eos/user/r/rhenriqu/SWAN_projects/python-package`
1 parent a99e31a commit a1fa874

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

SwanCustomEnvironments/swancustomenvironments/scripts/builders/accpy.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ eval "${ACTIVATE_ENV_CMD}"
1919
# Use uv for better performance if environment is fully resolved;
2020
# Otherwise, use pip for resolution (more reliable long-term).
2121
_log "Installing packages from ${REQ_PATH}..."
22+
23+
# If REQ_PATH points to a pyproject.toml, change it to the its parent folder
24+
# so pip doesn't get called with "-r pyproject.toml", which is invalid.
25+
if [[ "${REQ_PATH}" == *pyproject.toml ]]; then
26+
REQ_PATH="$(dirname "${REQ_PATH}")"
27+
fi
28+
2229
if [ "${RESOLVED_REQ}" = true ]; then
2330
# Use the same pip configuration as the Acc-Py default pip
2431
ACCPY_PIP_CONF="-i $(pip config get global.index-url) --allow-insecure-host $(pip config get global.trusted-host)"
25-
uv pip install ${ACCPY_PIP_CONF} -r "${REQ_PATH}" 2>&1
32+
uv pip install ${ACCPY_PIP_CONF} ${R_FLAG} "${REQ_PATH}" 2>&1
2633
if [ $? -ne 0 ]; then
2734
return 1
2835
fi
2936
# Enforce installation of our version of ipykernel
3037
uv pip install ${ACCPY_PIP_CONF} ${IPYKERNEL} 2>&1
3138
else
32-
pip install -r "${REQ_PATH}" 2>&1
39+
pip install ${R_FLAG} "${REQ_PATH}" 2>&1
3340
if [ $? -ne 0 ]; then
3441
return 1
3542
fi

SwanCustomEnvironments/swancustomenvironments/scripts/builders/venv.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ eval "${ACTIVATE_ENV_CMD}"
1515
# Use uv for better performance if environment is fully resolved;
1616
# Otherwise, use pip for resolution (more reliable long-term).
1717
_log "Installing packages from ${REQ_PATH}..."
18+
19+
# If REQ_PATH points to a pyproject.toml, change it to the its parent folder
20+
# so pip doesn't get called with "-r pyproject.toml", which is invalid.
21+
if [[ "${REQ_PATH}" == *pyproject.toml ]]; then
22+
REQ_PATH="$(dirname "${REQ_PATH}")"
23+
fi
1824
if [ "${RESOLVED_REQ}" = true ]; then
19-
uv pip install -r "${REQ_PATH}" 2>&1
25+
uv pip install ${R_FLAG} "${REQ_PATH}" 2>&1
2026
if [ $? -ne 0 ]; then
2127
return 1
2228
fi
2329
# Enforce installation of our version of ipykernel and its dependencies
2430
uv pip install ${IPYKERNEL} 2>&1
2531
else
26-
pip install -r "${REQ_PATH}" 2>&1
32+
pip install ${R_FLAG} "${REQ_PATH}" 2>&1
2733
if [ $? -ne 0 ]; then
2834
return 1
2935
fi

SwanCustomEnvironments/swancustomenvironments/scripts/makenv.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,27 @@ fi
175175
# --------------------------------------------------------------------------------------------
176176
# Create and set up the environment
177177

178+
R_FLAG=""
178179
ENV_PATH="/home/$USER/${ENV_NAME}"
179180
IPYKERNEL="ipykernel==$(python -c 'import ipykernel; print(ipykernel.__version__)')"
180181

181182
if [ -f "${TMP_REPO_PATH}/requirements.txt" ]; then
182183
# Fully resolved requirements (requirements.txt) take precedence
183184
RESOLVED_REQ=true
185+
R_FLAG=-r
184186
REQ_PATH="${TMP_REPO_PATH}/requirements.txt"
187+
elif [ -f "${TMP_REPO_PATH}/pyproject.toml" ]; then
188+
# Else if pyproject.toml is present, proceed with high-level requirements
189+
RESOLVED_REQ=false
190+
REQ_PATH="${TMP_REPO_PATH}/pyproject.toml"
185191
elif [ -f "${TMP_REPO_PATH}/requirements.in" ]; then
186192
# If only requirements.in is present, proceed with high-level requirements
187193
RESOLVED_REQ=false
194+
R_FLAG=-r
188195
REQ_PATH="${TMP_REPO_PATH}/requirements.in"
189196
else
190-
# There are no requirements files (neither requirements.txt nor requirements.in) in the repository
191-
_log "ERROR: No requirements file found. You must provide a requirements.in or requirements.txt file." && exit 1
197+
# There are no requirements files (neither requirements.txt, pyproject.toml nor requirements.in) in the repository
198+
_log "ERROR: No requirements file found. You must provide a requirements.in, pyproject.toml, or requirements.txt file." && exit 1
192199
fi
193200

194201
# Check if the requirements file contains the nxcals package, if the user activated the nxcals option
@@ -249,6 +256,13 @@ mv -f ${TMP_KERNEL} ${KERNEL_JSON} 2>&1
249256
if [ ! -d "${GIT_REPO_PATH}" ]; then
250257
mkdir -p ${GIT_HOME}
251258
mv ${TMP_REPO_PATH} ${GIT_HOME}
259+
# If pyproject.toml was used, reinstall the package reference, because it was
260+
# initially installed under /tmp (only if the repo was not previously cloned)
261+
# So, when requirements.txt is generated, it will point to the actual path and not /tmp
262+
if [ "${R_FLAG}" != "-r" ]; then
263+
REQ_PATH="${GIT_REPO_PATH}"
264+
pip install "${REQ_PATH}" 2>&1 # Use installed env pip
265+
fi
252266
fi
253267

254268
_log "REPO_PATH:${GIT_REPO_PATH#$HOME}"

0 commit comments

Comments
 (0)