Skip to content

Commit

Permalink
Merge pull request #109 from opensafely/iaindillingham/update-tooling
Browse files Browse the repository at this point in the history
Update tooling
  • Loading branch information
iaindillingham authored Dec 12, 2023
2 parents 28e0e90 + ac22b5e commit 82ec201
Show file tree
Hide file tree
Showing 4 changed files with 712 additions and 512 deletions.
63 changes: 28 additions & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,35 @@ default_language_version:
python: python3.8

repos:
- repo: https://github.com/psf/black
rev: 23.3.0
- repo: local
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- "flake8-builtins"
- "flake8-implicit-str-concat"
- "flake8-no-pep420"

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/sqlfluff/sqlfluff
rev: 2.0.3
# This hook filters sql files, so if there aren't sql files in the staging
# area, then `pre-commit run` will skip it. Indeed, `pre-commit run
# --all-files` will skip it too, as the `--all-files` argument doesn't work
# as documented.
hooks:
- id: sqlfluff-fix
- id: black
name: black
entry: just black
language: system
types: [python]
require_serial: true
- id: ruff
name: ruff
entry: just ruff
language: system
types: [python]
require_serial: true
- id: sqlfluff
name: sqlfluff
entry: just sqlfluff
language: system
types: [python]
require_serial: true

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
- id: check-ast
- id: check-json
- id: check-toml
- id: check-yaml
- id: detect-private-key
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
- id: check-ast
- id: check-json
- id: check-toml
- id: check-yaml
- id: detect-private-key
45 changes: 30 additions & 15 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# just has no idiom for setting a default value for an environment variable
# so we shell out, as we need VIRTUAL_ENV in the justfile environment
export VIRTUAL_ENV := `echo ${VIRTUAL_ENV:-.venv}`
export VIRTUAL_ENV := env_var_or_default("VIRTUAL_ENV", ".venv")

export BIN := VIRTUAL_ENV + if os_family() == "unix" { "/bin" } else { "/Scripts" }
export PIP := BIN + if os_family() == "unix" { "/python -m pip" } else { "/python.exe -m pip" }
Expand All @@ -21,6 +19,8 @@ clean:
# ensure valid virtualenv
virtualenv:
#!/usr/bin/env bash
set -euo pipefail
# allow users to specify python version in .env
PYTHON_VERSION=${PYTHON_VERSION:-$DEFAULT_PYTHON}

Expand All @@ -33,6 +33,8 @@ virtualenv:

_compile src dst *args: virtualenv
#!/usr/bin/env bash
set -euo pipefail
# exit if src file is older than dst file (-nt = 'newer than', but we negate with || to avoid error exit code)
test "${FORCE:-}" = "true" -o {{ src }} -nt {{ dst }} || exit 0
$BIN/pip-compile --allow-unsafe --generate-hashes --output-file={{ dst }} {{ src }} {{ args }}
Expand All @@ -51,6 +53,8 @@ requirements-dev *args: requirements-prod
# ensure prod requirements installed and up to date
prodenv: requirements-prod
#!/usr/bin/env bash
set -euo pipefail
# exit if .txt file has not changed since we installed them (-nt == "newer than', but we negate with || to avoid error exit code)
test requirements.prod.txt -nt $VIRTUAL_ENV/.prod || exit 0

Expand All @@ -64,6 +68,8 @@ prodenv: requirements-prod
# ensure dev requirements installed and up to date
devenv: prodenv requirements-dev && install-precommit
#!/usr/bin/env bash
set -euo pipefail
# exit if .txt file has not changed since we installed them (-nt == "newer than', but we negate with || to avoid error exit code)
test requirements.dev.txt -nt $VIRTUAL_ENV/.dev || exit 0

Expand All @@ -74,39 +80,48 @@ devenv: prodenv requirements-dev && install-precommit
# ensure precommit is installed
install-precommit:
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR=$(git rev-parse --show-toplevel)
test -f $BASE_DIR/.git/hooks/pre-commit || $BIN/pre-commit install


# upgrade dev or prod dependencies (specify package to upgrade single package, all by default)
upgrade env package="": virtualenv
#!/usr/bin/env bash
set -euo pipefail
opts="--upgrade"
test -z "{{ package }}" || opts="--upgrade-package {{ package }}"
FORCE=true "{{ just_executable() }}" requirements-{{ env }} $opts


# *ARGS is variadic, 0 or more. This allows us to do `just test -k match`, for example.
# *args is variadic, 0 or more. This allows us to do `just test -k match`, for example.
# Run the tests
test *ARGS: devenv
$BIN/opensafely exec python -m pytest --disable-warnings {{ ARGS }}
test *args: devenv
$BIN/opensafely exec python -m pytest --disable-warnings {{ args }}


black *args=".": devenv
$BIN/black --check {{ args }}

ruff *args=".": devenv
$BIN/ruff check {{ args }}

sqlfluff *args=".": devenv
$BIN/sqlfluff lint {{ args }}

# runs the format (black), sort (isort) and lint (flake8) check but does not change any files
check: devenv
$BIN/black --check .
$BIN/isort --check-only --diff .
$BIN/flake8
$BIN/sqlfluff lint .
# run the various dev checks but does not change any files
check: black ruff sqlfluff


# fix formatting and import sort ordering
fix: devenv
$BIN/black .
$BIN/isort .
$BIN/ruff --fix .
$BIN/sqlfluff fix .


# Run JupyterLab
run *ARGS: devenv
opensafely jupyter {{ ARGS }}
run *args: devenv
opensafely jupyter {{ args }}
6 changes: 1 addition & 5 deletions requirements.dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
# pip-compile --generate-hashes --output-file=requirements.dev.txt requirements.dev.in

black
flake8
flake8-builtins
flake8-implicit-str-concat
flake8-no-pep420
isort
opensafely
pip-tools
pre-commit
ruff
sqlfluff
Loading

0 comments on commit 82ec201

Please sign in to comment.