Skip to main content

python/pyfmt

1#!/usr/bin/env bash
2# Lint and format Python files with `ruff`.
3#
4# This will format files in the working directory by default, or you
5# can supply one or more paths for it to check.
7set -o errexit
8set -o nounset
11# Work out which directory to run in:
13# * if no argument is specified, use the current working directory
14# * if one or more directories are specified, use those
16# TODO: What if somebody passes a custom directory and `--fix`?
17if (( $# == 0 ))
18then
19 ROOT="$(pwd)"
20elif [[ "$#" = "1" && "$1" = "--fix" ]]
21then
22 ROOT="$(pwd)"
23else
24 ROOT="$@"
25fi
29# Work out which instance of ruff to use
31# If we're in a virtualenv which has ruff installed, we use the locally
32# installed copy.
34# If not, we use the `ruff` from my ~/repos/scripts virtualenv.
35if which ruff >/dev/null
36then
37 RUFF="$(which ruff)"
38else
39 RUFF=~/repos/scripts/.venv/bin/ruff
40fi
43print_info "-> ruff format"
44bash -c "$RUFF format $ROOT"
46echo ""
48print_info "-> ruff check --fix"
49bash -c "$RUFF check $ROOT --fix"