Skip to content

Commit

Permalink
pre-commit isort and black --check only for cirrus-ci (#4235)
Browse files Browse the repository at this point in the history
* pre-commit isort and black --check only for cirrus-ci

* run discovered hooks, with exclusion

* review actions

* match on pre-commit hook id

* add clarifying comment to nox

* support pre-commit excluded hooks

* rebrand lint to precommit
  • Loading branch information
bjlittle authored Jul 9, 2021
1 parent 7b6634a commit 9a611ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
10 changes: 5 additions & 5 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ env:
# Increment the build number to force new pip cache upload.
PIP_CACHE_BUILD: "0"
# Pip packages to be upgraded/installed.
PIP_CACHE_PACKAGES: "nox pip setuptools wheel"
PIP_CACHE_PACKAGES: "nox pip pyyaml setuptools wheel"
# Conda packages to be installed.
CONDA_CACHE_PACKAGES: "nox pip"
# Git commit hash for iris test data.
Expand Down Expand Up @@ -105,25 +105,25 @@ iris_test_data_template: &IRIS_TEST_DATA_TEMPLATE
#
# Linting
#
lint_task:
precommit_task:
only_if: ${SKIP_LINT_TASK} == ""
<< : *CREDITS_TEMPLATE
auto_cancellation: true
container:
image: python:3.8
cpu: 2
memory: 4G
name: "${CIRRUS_OS}: linting"
name: "${CIRRUS_OS}: pre-commit hooks"
pip_cache:
folder: ~/.cache/pip
fingerprint_script:
- echo "${CIRRUS_TASK_NAME} py${PYTHON_VERSION}"
- echo "$(date +%Y).$(expr $(date +%U) / ${CACHE_PERIOD}):${PIP_CACHE_BUILD} ${PIP_CACHE_PACKAGES}"
lint_script:
precommit_script:
- pip list
- python -m pip install --retries 3 --upgrade ${PIP_CACHE_PACKAGES}
- pip list
- nox --session lint
- nox --session precommit


#
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
hooks:
- id: black
pass_filenames: false
args: [--config=./pyproject.toml, --check, .]
args: [--config=./pyproject.toml, .]

- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
Expand All @@ -46,7 +46,7 @@ repos:
hooks:
- id: isort
types: [file, python]
args: [--filter-files, --check]
args: [--filter-files]

- repo: https://github.com/asottile/blacken-docs
rev: v1.10.0
Expand Down
29 changes: 22 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,38 @@ def prepare_venv(session: nox.sessions.Session) -> None:


@nox.session
def lint(session: nox.sessions.Session):
def precommit(session: nox.sessions.Session):
"""
Perform pre-commit linting of iris codebase.
Perform pre-commit hooks of iris codebase.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
import yaml

# Pip install the session requirements.
session.install("pre-commit")
# Execute the pre-commit linting tasks.
cmd = ["pre-commit", "run", "--all-files"]
hooks = ["black", "blacken-docs", "flake8", "isort"]
for hook in hooks:
session.run(*cmd, hook)

# Load the pre-commit configuration YAML file.
with open(".pre-commit-config.yaml", "r") as fi:
config = yaml.load(fi, Loader=yaml.FullLoader)

# List of pre-commit hook ids that we don't want to run.
excluded = ["no-commit-to-branch"]

# Enumerate the ids of pre-commit hooks we do want to run.
ids = [
hook["id"]
for entry in config["repos"]
for hook in entry["hooks"]
if hook["id"] not in excluded
]

# Execute the pre-commit hooks.
[session.run("pre-commit", "run", "--all-files", id) for id in ids]


@nox.session(python=PY_VER, venv_backend="conda")
Expand Down

0 comments on commit 9a611ef

Please sign in to comment.