From 49b0a92b5a48ab036656bd8a156e87481382dd71 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 1 Apr 2022 13:32:30 +0200 Subject: [PATCH] contrib: add "nm-python-black-format.sh" script This is more for completeness, to go along "nm-code-format.sh" script. Usually it's very simple to run black directly (you may still do that). However, black by default only reformats files with ".py" extension. So to get all our python files, you'd need to know and explicitly select them... or use this script. Also, `black .` scans the entire source tree, and is rather slow. This script knows which files to select and is thus faster. --- contrib/scripts/nm-python-black-format.sh | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 contrib/scripts/nm-python-black-format.sh diff --git a/contrib/scripts/nm-python-black-format.sh b/contrib/scripts/nm-python-black-format.sh new file mode 100755 index 0000000000..3a6aaa4b58 --- /dev/null +++ b/contrib/scripts/nm-python-black-format.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +set -e + +_print() { + printf '%s\n' "$*" >&2 +} + +die() { + _print "$*" + exit 1 +} + +NM_ROOT="$(git rev-parse --show-toplevel)" || die "not inside a git repository" +NM_PREFIX="$(git rev-parse --show-prefix)" || die "not inside a git repository" + +cd "$NM_ROOT" || die "failed to cd into \$NM_ROOT\"" + +if [ ! -f "./src/core/main.c" ]; then + die "Error: \"$NM_ROOT\" does not look like NetworkManager source tree" +fi + +BLACK="${BLACK:-black}" + +if ! command -v "$BLACK" &> /dev/null; then + _print "Error: black is not installed. On RHEL/Fedora/CentOS run 'dnf install black'" + exit 77 +fi + +OLD_IFS="$IFS" + +IFS=$'\n' +FILES=( $(git ls-tree --name-only -r HEAD | grep '\.py$') ) +IFS="$OLD_IFS" +FILES+=( + "examples/python/gi/nm-wg-set" +) + +usage() { + printf "Usage: %s [OPTION]...\n" "$(basename "$0")" + printf "Reformat python source files using python black.\n\n" + printf "OPTIONS:\n" + printf " -i Reformat files (this is the default)\n" + printf " -n|--dry-run|--check Only check the files (contrary to \"-i\")\n" + printf " --show-filenames Only print the filenames that would be checked/formatted\n" + printf " -h Print this help message\n" +} + +TEST_ONLY=0 +SHOW_FILENAMES=0 + +while (( $# )); do + case "$1" in + -h) + usage + exit 0 + ;; + -n|--dry-run|--check) + TEST_ONLY=1 + shift + continue + ;; + -i) + TEST_ONLY=0 + shift + continue + ;; + --show-filenames) + SHOW_FILENAMES=1 + shift + continue + ;; + *) + usage + exit 1 + ;; + esac +done + +if [ $SHOW_FILENAMES = 1 ]; then + printf '%s\n' "${FILES[@]}" + exit 0 +fi + +EXTRA_ARGS=() +if [ $TEST_ONLY = 1 ]; then + EXTRA_ARGS+=('--check') +fi + +"$BLACK" "${EXTRA_ARGS[@]}" "${FILES[@]}"