aboutsummaryrefslogtreecommitdiff
path: root/test/lint
diff options
context:
space:
mode:
authorMacroFake <falke.marco@gmail.com>2022-05-05 17:06:19 +0200
committerMacroFake <falke.marco@gmail.com>2022-05-05 17:07:15 +0200
commitc367736f85ac7f63a63cbfa0bcc05a42277f04bc (patch)
treeaa517322043675c9d99db9ae3d59ef445db9a965 /test/lint
parent7cc1860b12a51d6ff180da945d63d6aa03e3a4ca (diff)
parentbd6ceb4049602aa556a93a0ac8954802dc0bf456 (diff)
downloadbitcoin-c367736f85ac7f63a63cbfa0bcc05a42277f04bc.tar.xz
Merge bitcoin/bitcoin#24840: test: port 'lint-shell.sh' to python
bd6ceb4049602aa556a93a0ac8954802dc0bf456 test: port 'lint-shell.sh' to python (whiteh0rse) Pull request description: Converts `test/lint/lint-shell.sh` to Python and updates the docs accordingly. In order for the linter to run, it requires `git` and the `shellcheck` linter to be installed on the system. The script will fail gracefully with a help message if `shellcheck` is not installed. Top commit has no ACKs. Tree-SHA512: edc3f1af582b736a0b46f32bd7448e859201dc43f5dd086f16aab49037a1ab936f5376c29fc1006a932b9e98b4f2423d83d98e9666304781a06eb4d2a16f54e3
Diffstat (limited to 'test/lint')
-rwxr-xr-xtest/lint/lint-shell.py93
-rwxr-xr-xtest/lint/lint-shell.sh33
2 files changed, 93 insertions, 33 deletions
diff --git a/test/lint/lint-shell.py b/test/lint/lint-shell.py
new file mode 100755
index 0000000000..f1e4494350
--- /dev/null
+++ b/test/lint/lint-shell.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) 2018-2022 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+"""
+Check for shellcheck warnings in shell scripts.
+"""
+
+import subprocess
+import re
+import sys
+
+# Disabled warnings:
+DISABLED = [
+ 'SC2162', # read without -r will mangle backslashes.
+]
+
+def check_shellcheck_install():
+ try:
+ subprocess.run(['shellcheck', '--version'], stdout=subprocess.DEVNULL, check=True)
+ except FileNotFoundError:
+ print('Skipping shell linting since shellcheck is not installed.')
+ sys.exit(0)
+
+def get_files(command):
+ output = subprocess.run(command, stdout=subprocess.PIPE, universal_newlines=True)
+ files = output.stdout.split('\n')
+
+ # remove whitespace element
+ files = list(filter(None, files))
+ return files
+
+def main():
+ check_shellcheck_install()
+
+ # build the `exclude` flag
+ exclude = '--exclude=' + ','.join(DISABLED)
+
+ # build the `sourced files` list
+ sourced_files_cmd = [
+ 'git',
+ 'grep',
+ '-El',
+ r'^# shellcheck shell=',
+ ]
+ sourced_files = get_files(sourced_files_cmd)
+
+ # build the `guix files` list
+ guix_files_cmd = [
+ 'git',
+ 'grep',
+ '-El',
+ r'^#!\/usr\/bin\/env bash',
+ '--',
+ 'contrib/guix',
+ 'contrib/shell',
+ ]
+ guix_files = get_files(guix_files_cmd)
+
+ # build the other script files list
+ files_cmd = [
+ 'git',
+ 'ls-files',
+ '--',
+ '*.sh',
+ ]
+ files = get_files(files_cmd)
+ # remove everything that doesn't match this regex
+ reg = re.compile(r'src/[leveldb,secp256k1,minisketch,univalue]')
+ files[:] = [file for file in files if not reg.match(file)]
+
+ # build the `shellcheck` command
+ shellcheck_cmd = [
+ 'shellcheck',
+ '--external-sources',
+ '--check-sourced',
+ '--source-path=SCRIPTDIR',
+ ]
+ shellcheck_cmd.append(exclude)
+ shellcheck_cmd.extend(sourced_files)
+ shellcheck_cmd.extend(guix_files)
+ shellcheck_cmd.extend(files)
+
+ # run the `shellcheck` command
+ try:
+ subprocess.check_call(shellcheck_cmd)
+ except subprocess.CalledProcessError:
+ sys.exit(1)
+
+if __name__ == '__main__':
+ main()
diff --git a/test/lint/lint-shell.sh b/test/lint/lint-shell.sh
deleted file mode 100755
index 5fa104fce6..0000000000
--- a/test/lint/lint-shell.sh
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright (c) 2018-2021 The Bitcoin Core developers
-# Distributed under the MIT software license, see the accompanying
-# file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#
-# Check for shellcheck warnings in shell scripts.
-
-export LC_ALL=C
-
-# Disabled warnings:
-disabled=(
- SC2162 # read without -r will mangle backslashes.
-)
-
-EXIT_CODE=0
-
-if ! command -v shellcheck > /dev/null; then
- echo "Skipping shell linting since shellcheck is not installed."
- exit $EXIT_CODE
-fi
-
-SHELLCHECK_CMD=(shellcheck --external-sources --check-sourced --source-path=SCRIPTDIR)
-EXCLUDE="--exclude=$(IFS=','; echo "${disabled[*]}")"
-# Check shellcheck directive used for sourced files
-mapfile -t SOURCED_FILES < <(git ls-files | xargs gawk '/^# shellcheck shell=/ {print FILENAME} {nextfile}')
-mapfile -t GUIX_FILES < <(git ls-files contrib/guix contrib/shell | xargs gawk '/^#!\/usr\/bin\/env bash/ {print FILENAME} {nextfile}')
-mapfile -t FILES < <(git ls-files -- '*.sh' | grep -vE 'src/(leveldb|secp256k1|minisketch|univalue)/')
-if ! "${SHELLCHECK_CMD[@]}" "$EXCLUDE" "${SOURCED_FILES[@]}" "${GUIX_FILES[@]}" "${FILES[@]}"; then
- EXIT_CODE=1
-fi
-
-exit $EXIT_CODE