1#!/bin/sh 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright(c) 2022 University of New Hampshire 4# Copyright(c) 2023 PANTHEON.tech s.r.o. 5 6usage() { 7 echo "Usage: $(basename $0) [options] [directory]" 8 echo 'Options:' 9 # Get source code comments after getopts arguments and print them both 10 grep -E '[a-zA-Z]+\) +#' "$0" | tr -d '#' 11} 12 13format=true 14lint=true 15typecheck=true 16 17# Comments after args serve as documentation; must be present 18while getopts "hflt" arg; do 19 case $arg in 20 h) # Display this message 21 echo 'Run formatting and linting programs for DTS.' 22 usage 23 exit 0 24 ;; 25 f) # Don't run formatters 26 format=false 27 ;; 28 l) # Don't run linter 29 lint=false 30 ;; 31 t) # Don't run type checker 32 typecheck=false 33 ;; 34 ?) 35 usage 36 exit 1 37 esac 38done 39shift $(($OPTIND - 1)) 40 41directory=$(realpath --relative-base=$(pwd) ${1:-$(dirname $0)/../dts}) 42cd $directory || exit 1 43 44heading() { 45 echo $* 46 echo $* | sed 's/./-/g' # underline 47} 48 49errors=0 50 51if $format; then 52 if command -v git > /dev/null; then 53 if git rev-parse --is-inside-work-tree >&-; then 54 heading "Formatting in $directory/" 55 if command -v black > /dev/null; then 56 echo "Formatting code with black:" 57 black . 58 else 59 echo "black is not installed, not formatting" 60 errors=$((errors + 1)) 61 fi 62 if command -v isort > /dev/null; then 63 echo "Sorting imports with isort:" 64 isort . 65 else 66 echo "isort is not installed, not sorting imports" 67 errors=$((errors + 1)) 68 fi 69 70 git update-index --refresh 71 retval=$? 72 if [ $retval -ne 0 ]; then 73 echo 'The "needs update" files have been reformatted.' 74 echo 'Please update your commit.' 75 fi 76 errors=$((errors + retval)) 77 else 78 echo ".git directory not found, not formatting code" 79 errors=$((errors + 1)) 80 fi 81 else 82 echo "git command not found, not formatting code" 83 errors=$((errors + 1)) 84 fi 85fi 86 87if $lint; then 88 if $format; then 89 echo 90 fi 91 heading "Linting in $directory/" 92 if command -v pylama > /dev/null; then 93 pylama . 94 errors=$((errors + $?)) 95 else 96 echo "pylama not found, unable to run linter" 97 errors=$((errors + 1)) 98 fi 99fi 100 101if $typecheck; then 102 if $format || $lint; then 103 echo 104 fi 105 heading "Checking types in $directory/" 106 if command -v mypy > /dev/null; then 107 mypy . 108 errors=$((errors + $?)) 109 else 110 echo "mypy not found, unable to check types" 111 errors=$((errors + 1)) 112 fi 113fi 114 115echo 116heading "Summary for $directory/" 117echo "Found $errors errors" 118exit $errors 119