From 2c79fad96f7c8103ec982d0af3aa06b277ac7ca1 Mon Sep 17 00:00:00 2001 From: Jiri Kucera Date: Mon, 13 Jan 2020 11:57:13 +0100 Subject: [PATCH] Add black wrapper Some system roles want not to run black on their code. This wrapper decides whether to run black or not depending on content of config.sh. This wrapper also allows to set patterns of files and directories via config.sh to be processed or skipped by black. --- .travis/config.sh | 9 ++++++ .travis/runblack.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 5 +++- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100755 .travis/runblack.sh diff --git a/.travis/config.sh b/.travis/config.sh index 0c1b4b5..95a61a2 100644 --- a/.travis/config.sh +++ b/.travis/config.sh @@ -7,5 +7,14 @@ # # - LSR_EXTRA_PACKAGES # +# Environment variables that not start with LSR_* but have influence on CI +# process: +# +# * .travis/runblack.sh: +# +# - RUN_BLACK_INCLUDE +# - RUN_BLACK_EXCLUDE +# - RUN_BLACK_DISABLED +# export LSR_MOLECULE_DEPS='-rmolecule_requirements.txt' diff --git a/.travis/runblack.sh b/.travis/runblack.sh new file mode 100755 index 0000000..2b82832 --- /dev/null +++ b/.travis/runblack.sh @@ -0,0 +1,70 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +# A shell wrapper around black (Python formatter). The purpose of this wrapper +# is to get a user the opportunity to control black from config.sh via setting +# environment variables. + +# The first script argument is a path to Python interpreter, the rest of +# arguments are passed to black. + +# Environment variables: +# +# RUN_BLACK_INCLUDE +# a regular expression specifying files to be included; can be overridden +# from command line by --include; +# +# RUN_BLACK_EXCLUDE +# a regular expression specifying files to be excluded; can be overridden +# from command line by --exclude; +# +# RUN_BLACK_DISABLED +# if set to an arbitrary non-empty value, black will be not executed + +set -e + +ME=$(basename $0) +SCRIPTDIR=$(readlink -f $(dirname $0)) + +. ${SCRIPTDIR}/utils.sh +. ${SCRIPTDIR}/config.sh + +if [[ "${RUN_BLACK_DISABLED}" ]]; then + lsr_info "${ME}: black is disabled. Skipping." + exit 0 +fi + +# Sanitize path in case if running within tox (see +# https://github.com/tox-dev/tox/issues/1463): +ENVPYTHON=$(readlink -f $1) +shift + +DEFAULT_INCLUDE='^[^.].*\.py$' +DEFAULT_EXCLUDE='/(\.[^.].*|tests/roles)/' + +INCLUDE_ARG="" +EXCLUDE_ARG="" +OTHER_ARGS=() + +while [[ $# -gt 0 ]]; do + case "$1" in + --include) + shift + INCLUDE_ARG="$1" + ;; + --exclude) + shift + EXCLUDE_ARG="$1" + ;; + *) + OTHER_ARGS+=( "$1" ) + ;; + esac + shift +done + +set -x +${ENVPYTHON} -m black \ + --include "${INCLUDE_ARG:-${RUN_BLACK_INCLUDE:-${DEFAULT_INCLUDE}}}" \ + --exclude "${EXCLUDE_ARG:-${RUN_BLACK_EXCLUDE:-${DEFAULT_EXCLUDE}}}" \ + "${OTHER_ARGS[@]}" diff --git a/tox.ini b/tox.ini index 6a0364b..6b782a3 100644 --- a/tox.ini +++ b/tox.ini @@ -126,10 +126,13 @@ commands = [testenv:black] envdir = {toxworkdir}/env-{env:TRAVIS_PYTHON_VERSION:3.6} basepython = python3.6 +passenv = RUN_BLACK_* deps = black +whitelist_externals = + {[base]whitelist_externals} commands = - black --check --diff --include "^[^.].*\.py$" --exclude "/(\.[^.].*|tests/roles)/" . + bash {toxinidir}/.travis/runblack.sh {envpython} --check --diff . [testenv:pylint] envdir = {toxworkdir}/env-{env:TRAVIS_PYTHON_VERSION:2.7}