Skip to content

Commit

Permalink
Add a conda-wrapper for the pre-commit script
Browse files Browse the repository at this point in the history
pre-commit recently required Python 3.7+ so can no
longer run on stock CentoOS 7
  • Loading branch information
martyngigg committed Nov 16, 2022
1 parent 44c5bcf commit 05a04f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
42 changes: 42 additions & 0 deletions buildconfig/Jenkins/Conda/conda-pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash -ex

# This script will set up a conda environment before running the pre-commit shell script.
#
# Script usage:
# conda-pre-commit <path-to-workspace> <job-type>
#
# Example command to run a PR build on ubuntu:
# conda-pre-commit pull_requests-cppcheck ${WORKSPACE}
#
# Expected args:
# 1. WORKSPACE: path to the workspace/source code that this should run inside, Windows Caveat: Only use / for
# this argument do not use \\ or \ in the path.
# 2. GIT_COMMIT_SHA: SHA of the commit to compare with the base when checking diffs
# 2. JOB_TYPE: An optional job type. If it contains 'pull_requests', then pre-commit only runs in he diff

# Check arguments passed are 2, and aren't optional flags.
if [[ $# -lt 2 ]]; then
echo "Usage: conda-pre-commit <path-to-workspace> <job-name>"
exit 1
fi

# SCRIPT_DIR discovery from https://stackoverflow.com/a/9107028 by https://stackoverflow.com/users/1184238/andrew-norrie
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR=$1
GIT_SHA=$2
JOB_TYPE=$3

EXPECTED_MAMBAFORGE_PATH=$SOURCE_DIR/mambaforge # Install into the WORKSPACE_DIR
EXPECTED_CONDA_PATH=$EXPECTED_MAMBAFORGE_PATH/bin/mamba
CONDA_ENV_NAME="pre-commit"
CLEAN_BUILD=false

# Setup Mambaforge
$SCRIPT_DIR/download-and-install-mambaforge $EXPECTED_MAMBAFORGE_PATH $EXPECTED_CONDA_PATH $CLEAN_BUILD
source $EXPECTED_MAMBAFORGE_PATH/etc/profile.d/conda.sh
source $EXPECTED_MAMBAFORGE_PATH/etc/profile.d/mamba.sh
mamba create -n $CONDA_ENV_NAME pre-commit
mamba activate $CONDA_ENV_NAME

# Run the script that handles the actual building
$SCRIPT_DIR/pre-commit ${SOURCE_DIR} ${GIT_SHA} ${JOB_TYPE}
2 changes: 1 addition & 1 deletion buildconfig/Jenkins/Conda/download-and-install-mambaforge
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if [[ $CLEAN_BUILD == true ]]; then
fi

# Ensure conda is installed
if [[ ! -f $EXPECTED_CONDA_PATH ]]; then
if [[ ! -d $EXPECTED_MAMBAFORGE_PATH ]]; then
if [[ ! -f $MAMBAFORGE_SCRIPT_NAME ]]; then
# Download mambaforge
if [ -x "$(which curl)" ]; then
Expand Down
20 changes: 9 additions & 11 deletions buildconfig/Jenkins/Conda/pre-commit
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/bin/bash
# Check that the source conforms to the pre-commit framework
# Args are expected as GIT_SHA1 as $1, PROJECT_SOURCE as $2, JOB_TYPE as $3
# Check that the source conforms to the pre-commit framework. It assumes pre-commit
# has already been installed in the Python environment available
# Args are expected as PROJECT_SOURCE as $1, GIT_SHA1 as $2, optional JOB_TYPE as $3

if [ $# -ne 3 ]; then
echo "Usage: ./pre-commit branch-sha1 project-root job-type"
echo " job_type=master|pull-request "
if [ $# -lt 2 ]; then
echo "Usage: ./pre-commit project-root branch-sha1 [job-type]"
echo " job_type=main|pull-request "
exit 1
fi

GIT_SHA=$1
PROJECT_SOURCE=$2
PROJECT_SOURCE=$1
GIT_SHA=$2
JOB_TYPE=$3

cd "$PROJECT_SOURCE" || exit 127
Expand All @@ -26,11 +27,8 @@ if [[ $JOB_TYPE == "pull-request" ]]; then
else
pre-commit run --from-ref "${BASE_SHA}" --to-ref "${GIT_SHA}" --show-diff-on-failure --verbose
fi
elif [[ $JOB_TYPE == "main" ]]; then
pre-commit run --all-files
else
echo "JOB_TYPE not set, add a 3rd argument (e.g. master or pull-request)"
exit 1
pre-commit run --all-files
fi

# Define exit code so can be copied later
Expand Down

0 comments on commit 05a04f0

Please sign in to comment.