File: pre-commit-install.sh

package info (click to toggle)
cp2k 2025.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 366,832 kB
  • sloc: fortran: 955,049; f90: 21,676; ansic: 18,058; python: 13,378; sh: 12,179; xml: 2,173; makefile: 964; pascal: 845; perl: 492; lisp: 272; cpp: 137; csh: 16
file content (99 lines) | stat: -rwxr-xr-x 2,791 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh -l
# author: Tiziano Müller
# SPDX-License-Identifier: MIT

set -o errexit
set -o nounset

PYTHON=$(command -v python3 || true)

if [ -z "${PYTHON}" ]; then
  echo "ERROR: the python3 executable could not be found, but is required for the complete build process"
  exit 1
fi

GOLANG=$(command -v go || true)

if [ -z "${GOLANG}" ]; then
  # TODO: bootstrap go as well, maybe use https://github.com/stefanmaric/g
  echo "ERROR: did not find a 'go' executable, required for some of the pre-commit hooks"
  exit 1
fi

SCRIPTDIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)

# here we assume that this script is located in the tools/ subfolder
ENVDIR="${SCRIPTDIR}/../.pre-commit-env"

if [ -f "${ENVDIR}/bootstrap_done" ]; then
  echo "The environment in <CP2K-SRC-DIR>/.pre-commit-env/ is ready to use..."
  exit 0
fi

echo "Installing all required tools to run pre-commit in <CP2K-SRC-DIR>/.pre-commit-env/"

echo "Creating the virtualenv for pre-commit..."
if [ ! -d "${ENVDIR}" ]; then
  "${PYTHON}" -m venv "${ENVDIR}"
fi

# resolve the relative path now that we have it:
ENVDIR=$(CDPATH='' cd -- "${ENVDIR}" && pwd)

# shellcheck source=/dev/null
. "${ENVDIR}/bin/activate"

# change the command to the prefixed one for the rest of the script:
PYTHON=$(command -v python)
PIP=$(command -v pip || true)

if [ -z "${PIP}" ]; then
  echo "Bootstrapping the pip command inside the virtual environment..."

  FETCHER="$(command -v curl || true)"
  FETCHER_OPTS=""

  if [ -z "${FETCHER}" ]; then
    FETCHER="$(command -v wget || true)"
    FETCHER_OPTS="-O-"
  fi

  if [ -z "${FETCHER}" ]; then
    echo "ERROR: neither wget nor curl seem to be available, please download"
    echo "    https://bootstrap.pypa.io/get-pip.py"
    echo "manually and run:"
    echo "    python3 get-pip.py"
    echo "to install the pip command, then rerun this script."
    rm -rf "${ENVDIR}" # cleaning up to avoid picking up the unprefixed pip on the rerun
    exit 1
  fi

  "${FETCHER}" ${FETCHER_OPTS} "https://bootstrap.pypa.io/get-pip.py" | "${PYTHON}"
fi

PIP=$(command -v pip)

echo "Installing pre-commit..."
"${PIP}" install pre-commit
# this will also install things like shellcheck and a pre-built node if necessary

PRE_COMMIT=$(command -v pre-commit)

# Enable Go 11 style modules for shfmt
export GO111MODULE=on

echo "Letting pre-commit setup the rest..."
"${PRE_COMMIT}" install --install-hooks

echo "successfully bootstrapped at $(date) by $0" > "${ENVDIR}/bootstrap_done"

echo ""
echo "All done: pre-commit will now run on each 'git commit' on the files you are committing!"

echo ""
echo "Should you need to run it manually, use"
echo ""
echo "    ${ENVDIR}/bin/pre-commit"
echo ""
echo "or add the directory ${ENVDIR}/bin to your \$PATH variable."
echo "Happy hacking! :)"