Skip to content

Commit 250598a

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26906: test: add an easy way to run linters locally
b68e5a7 lint: specify the right commit range when running locally (James O'Beirne) dff7ed5 test: add an easy way to run linters locally (James O'Beirne) Pull request description: Adds a Dockerfile configuration ~~(originally written mostly by fanquake)~~ that allows straightforward running of linters with compatible versions locally. This removes a ton of annoyance when trying to appease CI, because many of the linter versions are quite old and difficult to maintain locally. I realize that people may not be thrilled to add more ancillary tooling to the repo, but I think this makes a lot of sense given the linter versions listed in this container configuration are dictated by this repo (within the CI configuration), so having these things live in two separate places is a recipe for version mismatches. Eventually we can likely just use this container on CI directly to avoid any chance of inconsistencies between local dev experience and CI. ACKs for top commit: aureleoules: ACK b68e5a7 stickies-v: ACK b68e5a7 john-moffett: ACK b68e5a7 Tree-SHA512: 7ef7a5dae023d81fdb6296d5d92dfa074ee321c7993e607c9f014d0f21c91558611aa00fc3ce1edc7b5e68371aea0d27fa1931291a79bb867a6c783bb536775f
2 parents b5c88a5 + b68e5a7 commit 250598a

File tree

5 files changed

+85
-19
lines changed

5 files changed

+85
-19
lines changed

ci/lint/04_install.sh

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@ ${CI_RETRY_EXE} apt-get update
1313
# - gpg (used by verify-commits)
1414
${CI_RETRY_EXE} apt-get install -y curl xz-utils git gpg
1515

16-
PYTHON_PATH=/tmp/python
17-
if [ ! -d "${PYTHON_PATH}/bin" ]; then
18-
(
19-
git clone https://github.com/pyenv/pyenv.git
20-
cd pyenv/plugins/python-build || exit 1
21-
./install.sh
22-
)
23-
# For dependencies see https://github.com/pyenv/pyenv/wiki#suggested-build-environment
24-
${CI_RETRY_EXE} apt-get install -y build-essential libssl-dev zlib1g-dev \
25-
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
26-
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
27-
clang
28-
env CC=clang python-build "$(cat "${BASE_ROOT_DIR}/.python-version")" "${PYTHON_PATH}"
16+
if [ -z "${SKIP_PYTHON_INSTALL}" ]; then
17+
PYTHON_PATH=/tmp/python
18+
if [ ! -d "${PYTHON_PATH}/bin" ]; then
19+
(
20+
git clone https://github.com/pyenv/pyenv.git
21+
cd pyenv/plugins/python-build || exit 1
22+
./install.sh
23+
)
24+
# For dependencies see https://github.com/pyenv/pyenv/wiki#suggested-build-environment
25+
${CI_RETRY_EXE} apt-get install -y build-essential libssl-dev zlib1g-dev \
26+
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
27+
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
28+
clang
29+
env CC=clang python-build "$(cat "${BASE_ROOT_DIR}/.python-version")" "${PYTHON_PATH}"
30+
fi
31+
export PATH="${PYTHON_PATH}/bin:${PATH}"
32+
command -v python3
33+
python3 --version
2934
fi
30-
export PATH="${PYTHON_PATH}/bin:${PATH}"
31-
command -v python3
32-
python3 --version
3335

3436
${CI_RETRY_EXE} pip3 install codespell==2.2.1
3537
${CI_RETRY_EXE} pip3 install flake8==5.0.4
@@ -38,5 +40,6 @@ ${CI_RETRY_EXE} pip3 install pyzmq==24.0.1
3840
${CI_RETRY_EXE} pip3 install vulture==2.6
3941

4042
SHELLCHECK_VERSION=v0.8.0
41-
curl -sL "https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VERSION}/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz" | tar --xz -xf - --directory /tmp/
42-
export PATH="/tmp/shellcheck-${SHELLCHECK_VERSION}:${PATH}"
43+
curl -sL "https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VERSION}/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz" | \
44+
tar --xz -xf - --directory /tmp/
45+
mv "/tmp/shellcheck-${SHELLCHECK_VERSION}/shellcheck" /usr/bin/

ci/lint/06_script.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
export LC_ALL=C
88

9-
if [ -n "$CIRRUS_PR" ]; then
9+
if [ -n "$LOCAL_BRANCH" ]; then
10+
# To faithfully recreate CI linting locally, specify all commits on the current
11+
# branch.
12+
COMMIT_RANGE="$(git merge-base HEAD master)..HEAD"
13+
elif [ -n "$CIRRUS_PR" ]; then
1014
COMMIT_RANGE="HEAD~..HEAD"
1115
echo
1216
git log --no-merges --oneline "$COMMIT_RANGE"

ci/lint/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See test/lint/README.md for usage.
2+
#
3+
# This container basically has to live in this directory in order to pull in the CI
4+
# install scripts. If it lived in the root directory, it would have to pull in the
5+
# entire repo as docker context during build; if it lived elsewhere, it wouldn't be
6+
# able to make back-references to pull in the install scripts. So here it lives.
7+
8+
FROM python:3.7-buster
9+
10+
ENV DEBIAN_FRONTEND=noninteractive
11+
ENV LC_ALL=C.UTF-8
12+
13+
# This is used by the 04_install.sh script; we can't read the Python version from
14+
# .python-version for the same reasons as above, and it's more efficient to pull a
15+
# preexisting Python image than it is to build from source.
16+
ENV SKIP_PYTHON_INSTALL=1
17+
18+
# Must be built from ./ci/lint/ for these paths to work.
19+
COPY ./docker-entrypoint.sh /entrypoint.sh
20+
COPY ./04_install.sh /install.sh
21+
22+
RUN /install.sh && \
23+
echo 'alias lint="./ci/lint/06_script.sh"' >> ~/.bashrc && \
24+
chmod 755 /entrypoint.sh && \
25+
rm -rf /var/lib/apt/lists/*
26+
27+
28+
WORKDIR /bitcoin
29+
ENTRYPOINT ["/entrypoint.sh"]

ci/lint/docker-entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
export LC_ALL=C
3+
4+
# Fixes permission issues when there is a container UID/GID mismatch with the owner
5+
# of the mounted bitcoin src dir.
6+
git config --global --add safe.directory /bitcoin
7+
8+
if [ -z "$1" ]; then
9+
LOCAL_BRANCH=1 bash -ic "./ci/lint/06_script.sh"
10+
else
11+
exec "$@"
12+
fi

test/lint/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
This folder contains lint scripts.
22

3+
Running locally
4+
===============
5+
6+
To run linters locally with the same versions as the CI environment, use the included
7+
Dockerfile:
8+
9+
```sh
10+
cd ./ci/lint
11+
docker build -t bitcoin-linter .
12+
13+
cd /root/of/bitcoin/repo
14+
docker run --rm -v $(pwd):/bitcoin -it bitcoin-linter
15+
```
16+
17+
After building the container once, you can simply run the last command any time you
18+
want to lint.
19+
20+
321
check-doc.py
422
============
523
Check for missing documentation of command line options.

0 commit comments

Comments
 (0)