Skip to content

Commit a20626c

Browse files
authored
pre-commit hook + ruff usage (#2013)
1 parent 67c9fc4 commit a20626c

File tree

573 files changed

+2887
-3216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

573 files changed

+2887
-3216
lines changed

.github/workflows/ci.yml

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ jobs:
1111
fail-fast: false
1212
matrix:
1313
env: [environment_a, environment_b, conda-forge]
14+
1415
steps:
1516
- uses: actions/checkout@v4
1617
- uses: actions/cache@v4
1718
id: cache
1819
with:
1920
path: /usr/share/miniconda/envs/im${{ matrix.env }}
2021
key: ${{ format('{0}-conda-improver-{1}-{2}', runner.os, matrix.env, hashFiles(format('envs/{0}.yml', matrix.env))) }}
22+
2123
- name: conda env update
2224
if: steps.cache.outputs.cache-hit != 'true'
2325
run: |
@@ -26,29 +28,34 @@ jobs:
2628
conda install -q -n base -c conda-forge -c nodefaults mamba
2729
rm -f /usr/share/miniconda/pkgs/cache/*.json # workaround for mamba-org/mamba#488
2830
mamba env create -q --file envs/${{ matrix.env }}.yml --name im${{ matrix.env }}
31+
2932
- name: conda info
3033
run: |
3134
source '/usr/share/miniconda/etc/profile.d/conda.sh'
3235
conda activate im${{ matrix.env }}
3336
conda info
3437
conda list
38+
3539
- name: sphinx documentation
3640
run: |
3741
source '/usr/share/miniconda/etc/profile.d/conda.sh'
3842
conda activate im${{ matrix.env }}
3943
make -C doc html SPHINXOPTS="-W --keep-going"
44+
4045
- name: pytest without coverage
4146
if: matrix.env == 'conda-forge'
4247
run: |
4348
source '/usr/share/miniconda/etc/profile.d/conda.sh'
4449
conda activate im${{ matrix.env }}
4550
pytest
51+
4652
- name: pytest with coverage
4753
if: matrix.env != 'conda-forge'
4854
run: |
4955
source '/usr/share/miniconda/etc/profile.d/conda.sh'
5056
conda activate im${{ matrix.env }}
5157
pytest --cov=improver --cov-report xml:coverage.xml
58+
5259
- name: codacy upload
5360
if: env.CODACY_PROJECT_TOKEN && matrix.env == 'environment_a'
5461
run: |
@@ -57,76 +64,81 @@ jobs:
5764
python-codacy-coverage -v -r coverage.xml
5865
env:
5966
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
67+
6068
- name: codecov upload
6169
uses: codecov/codecov-action@v4
6270
with:
6371
name: ${{ matrix.env }}
6472
if: matrix.env != 'conda_forge'
65-
Codestyle-and-flake8:
73+
74+
pre-commit-checks:
6675
runs-on: ubuntu-22.04
6776
strategy:
6877
fail-fast: false
69-
matrix:
70-
env: [environment_a]
7178
steps:
72-
- uses: actions/checkout@v4
73-
- uses: actions/cache@v4
74-
id: cache
79+
80+
- name: improver checkout
81+
uses: actions/checkout@v4
7582
with:
76-
path: /usr/share/miniconda/envs/im${{ matrix.env }}
77-
key: ${{ format('{0}-conda-improver-{1}-{2}', runner.os, matrix.env, hashFiles(format('envs/{0}.yml', matrix.env))) }}
78-
- name: conda env update
79-
if: steps.cache.outputs.cache-hit != 'true'
80-
run: |
81-
source '/usr/share/miniconda/etc/profile.d/conda.sh'
82-
conda update -q -n base -c defaults conda
83-
conda install -q -n base -c conda-forge -c nodefaults mamba
84-
rm -f /usr/share/miniconda/pkgs/cache/*.json # workaround for mamba-org/mamba#488
85-
mamba env create -q --file envs/${{ matrix.env }}.yml --name im${{ matrix.env }}
86-
- name: conda info
87-
run: |
88-
source '/usr/share/miniconda/etc/profile.d/conda.sh'
89-
conda activate im${{ matrix.env }}
90-
conda info
91-
conda list
92-
- name: isort
83+
fetch-depth: 0
84+
85+
- name: Set up Python
86+
uses: actions/setup-python@v5
87+
with:
88+
python-version: 3.7
89+
cache: 'pip'
90+
91+
- name: pip install pre-commit
9392
run: |
94-
source '/usr/share/miniconda/etc/profile.d/conda.sh'
95-
conda activate im${{ matrix.env }}
96-
isort --check-only .
97-
- name: black
93+
pip install pre-commit
94+
95+
- name: Python interpreter version sha (PYSHA)
96+
run: echo "PYSHA=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
97+
98+
- name: Cache pre-commit
99+
uses: actions/cache@v3
100+
id: pre-commit-cache
101+
with:
102+
path: ~/.cache/pre-commit
103+
key: pre-commit|${{ env.PYSHA }}|${{ hashFiles('.pre-commit-config.yaml') }}
104+
105+
- name: pre-commit install
98106
run: |
99-
source '/usr/share/miniconda/etc/profile.d/conda.sh'
100-
conda activate im${{ matrix.env }}
101-
black --check .
102-
- name: flake8
107+
pre-commit install
108+
109+
- name: pre-commit run
103110
run: |
104-
source '/usr/share/miniconda/etc/profile.d/conda.sh'
105-
conda activate im${{ matrix.env }}
106-
flake8 improver improver_tests
111+
pre-commit run --verbose --color=always --all-files
112+
107113
PR-standards:
108114
runs-on: ubuntu-22.04
109115
timeout-minutes: 10
110116
steps:
117+
111118
- name: Checkout
112119
uses: actions/checkout@v4
113120
with:
114121
fetch-depth: 0
122+
115123
- name: Check CONTRIBUTING.md
116124
uses: cylc/release-actions/check-shortlog@v1
125+
117126
Safety-Bandit:
118127
runs-on: ubuntu-22.04
119128
strategy:
120129
fail-fast: false
121130
matrix:
122131
env: [environment_a, environment_b, conda-forge]
123132
steps:
133+
124134
- uses: actions/checkout@v4
135+
125136
- uses: actions/cache@v4
126137
id: cache
127138
with:
128139
path: /usr/share/miniconda/envs/im${{ matrix.env }}
129140
key: ${{ format('{0}-conda-improver-{1}-{2}', runner.os, matrix.env, hashFiles(format('envs/{0}.yml', matrix.env))) }}
141+
130142
- name: conda env update
131143
if: steps.cache.outputs.cache-hit != 'true'
132144
run: |
@@ -135,17 +147,20 @@ jobs:
135147
conda install -q -n base -c conda-forge mamba
136148
rm -f /usr/share/miniconda/pkgs/cache/*.json # workaround for mamba-org/mamba#488
137149
mamba env create -q --file envs/${{ matrix.env }}.yml --name im${{ matrix.env }}
150+
138151
- name: conda info
139152
run: |
140153
source '/usr/share/miniconda/etc/profile.d/conda.sh'
141154
conda activate im${{ matrix.env }}
142155
conda info
143156
conda list
157+
144158
- name: safety
145159
run: |
146160
source '/usr/share/miniconda/etc/profile.d/conda.sh'
147161
conda activate im${{ matrix.env }}
148162
safety check || true
163+
149164
- name: bandit
150165
run: |
151166
source '/usr/share/miniconda/etc/profile.d/conda.sh'

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.4.8
5+
hooks:
6+
# Run the linter (must come before any formatter, black/ruff when --fix).
7+
- id: ruff
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format
11+
- repo: local
12+
hooks:
13+
- id: copyright_check
14+
name: Check copyright header
15+
types: [python]
16+
args: [ --fix ]
17+
language: system
18+
entry: ./copyright_check
19+
- id: init_check
20+
name: Check for missing __init__.py files
21+
types: [python]
22+
language: system
23+
entry: ./init_check
24+
files: ^(improver/|improver_tests/)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ activate your new improver environment
4242
```
4343
conda activate improver
4444
```
45+
46+
## Pre-commit Hook
47+
OPTIONAL: A pre-commit hook can be added to facilitate the development of this code base.
48+
Ensure that you have python available on the path, then install the pre-commit hook by running `pre-commit install` from within your working copy.
49+
pre-commit checks will run against modified files when you commit from then on.
50+
51+
These pre-commit hooks will run as part of continuous integration to maintain code quality standards in the project.

bin/improver

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
2-
# (C) Crown copyright, Met Office. All rights reserved.
2+
# (C) Crown Copyright, Met Office. All rights reserved.
33
#
4-
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
4+
# This file is part of 'IMPROVER' and is released under the BSD 3-Clause license.
55
# See LICENSE in the root of the repository for full licensing details.
66
#------------------------------------------------------------------------------
77
# NAME

bin/improver-tests

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
2-
# (C) Crown copyright, Met Office. All rights reserved.
2+
# (C) Crown Copyright, Met Office. All rights reserved.
33
#
4-
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
4+
# This file is part of 'IMPROVER' and is released under the BSD 3-Clause license.
55
# See LICENSE in the root of the repository for full licensing details.
66
#------------------------------------------------------------------------------
77
# NAME
@@ -31,19 +31,9 @@ function get_python_files {
3131
! -name '*~' \)`
3232
}
3333

34-
function improver_test_black {
35-
${BLACK:-black} $FILES_TO_TEST
36-
echo_ok "black"
37-
}
38-
39-
function improver_test_isort {
40-
${ISORT:-isort} $FILES_TO_TEST
41-
echo_ok "isort"
42-
}
43-
44-
function improver_test_flake8 {
45-
${FLAKE8:-flake8} $FILES_TO_TEST
46-
echo_ok "flake8"
34+
function improver_test_pre_commit {
35+
pre-commit run --all-files
36+
echo_ok "pre-commit"
4737
}
4838

4939
function improver_test_doc {
@@ -105,19 +95,19 @@ function print_usage {
10595
cat <<'__USAGE__'
10696
improver tests [OPTIONS] [SUBTEST...] [SUBCLI...]
10797
108-
Run black, isort, flake8, documentation, unit and CLI acceptance tests.
98+
Run pre-commit, documentation, unit and CLI acceptance tests.
10999
110100
Optional arguments:
111101
--debug Run in verbose mode (may take longer for CLI)
112102
-h, --help Show this message and exit
113103
114104
Arguments:
115105
SUBTEST Name(s) of a subtest to run without running the rest.
116-
Valid names are: black, isort, flake8, doc, unit, cli,
117-
and recreate_checksums. The default tests are black,
118-
isort, flake8, doc, unit, and cli. Using
119-
recreate_checksums will regenerate the test data
120-
checksum file which is used as part of the cli tests.
106+
Valid names are: pre_commit, doc, unit, cli,
107+
and recreate_checksums. The default tests are pre_commit,
108+
doc, unit, and cli. Using recreate_checksums will
109+
regenerate the test data checksum file which is used as
110+
part of the cli tests.
121111
__USAGE__
122112
}
123113

@@ -158,7 +148,7 @@ for arg in "$@"; do
158148
print_usage
159149
exit 0
160150
;;
161-
black|isort|flake8|doc|unit|cli|recreate_checksums)
151+
pre_commit|doc|unit|cli|recreate_checksums)
162152
SUBTESTS="$SUBTESTS $arg"
163153
;;
164154
$cli_tasks)
@@ -176,7 +166,7 @@ if [[ -n "$SUBTESTS" ]]; then
176166
TESTS="$SUBTESTS"
177167
else
178168
# Default tests.
179-
TESTS="isort black flake8 doc unit cli"
169+
TESTS="pre_commit doc unit cli"
180170
fi
181171

182172
# If cli sub test is not specified by user, do all cli tests.

0 commit comments

Comments
 (0)