Skip to content

Commit c367736

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#24840: test: port 'lint-shell.sh' to python
bd6ceb4 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
2 parents 7cc1860 + bd6ceb4 commit c367736

File tree

4 files changed

+95
-35
lines changed

4 files changed

+95
-35
lines changed

REVIEWERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
/src/dbwrapper.* @jamesob
113113

114114
# Linter
115-
/test/lint/lint-shell.sh @hebasto
115+
/test/lint/lint-shell.py @hebasto
116116

117117
# Bech32
118118
/src/bech32.* @sipa

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Use the `-v` option for verbose output.
309309
| [`lint-python.py`](lint/lint-python.py) | [mypy](https://github.com/python/mypy)
310310
| [`lint-python.py`](lint/lint-python.py) | [pyzmq](https://github.com/zeromq/pyzmq)
311311
| [`lint-python-dead-code.py`](lint/lint-python-dead-code.py) | [vulture](https://github.com/jendrikseipp/vulture)
312-
| [`lint-shell.sh`](lint/lint-shell.sh) | [ShellCheck](https://github.com/koalaman/shellcheck)
312+
| [`lint-shell.py`](lint/lint-shell.py) | [ShellCheck](https://github.com/koalaman/shellcheck)
313313
| [`lint-spelling.py`](lint/lint-spelling.py) | [codespell](https://github.com/codespell-project/codespell)
314314

315315
In use versions and install instructions are available in the [CI setup](../ci/lint/04_install.sh).

test/lint/lint-shell.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2018-2022 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
7+
"""
8+
Check for shellcheck warnings in shell scripts.
9+
"""
10+
11+
import subprocess
12+
import re
13+
import sys
14+
15+
# Disabled warnings:
16+
DISABLED = [
17+
'SC2162', # read without -r will mangle backslashes.
18+
]
19+
20+
def check_shellcheck_install():
21+
try:
22+
subprocess.run(['shellcheck', '--version'], stdout=subprocess.DEVNULL, check=True)
23+
except FileNotFoundError:
24+
print('Skipping shell linting since shellcheck is not installed.')
25+
sys.exit(0)
26+
27+
def get_files(command):
28+
output = subprocess.run(command, stdout=subprocess.PIPE, universal_newlines=True)
29+
files = output.stdout.split('\n')
30+
31+
# remove whitespace element
32+
files = list(filter(None, files))
33+
return files
34+
35+
def main():
36+
check_shellcheck_install()
37+
38+
# build the `exclude` flag
39+
exclude = '--exclude=' + ','.join(DISABLED)
40+
41+
# build the `sourced files` list
42+
sourced_files_cmd = [
43+
'git',
44+
'grep',
45+
'-El',
46+
r'^# shellcheck shell=',
47+
]
48+
sourced_files = get_files(sourced_files_cmd)
49+
50+
# build the `guix files` list
51+
guix_files_cmd = [
52+
'git',
53+
'grep',
54+
'-El',
55+
r'^#!\/usr\/bin\/env bash',
56+
'--',
57+
'contrib/guix',
58+
'contrib/shell',
59+
]
60+
guix_files = get_files(guix_files_cmd)
61+
62+
# build the other script files list
63+
files_cmd = [
64+
'git',
65+
'ls-files',
66+
'--',
67+
'*.sh',
68+
]
69+
files = get_files(files_cmd)
70+
# remove everything that doesn't match this regex
71+
reg = re.compile(r'src/[leveldb,secp256k1,minisketch,univalue]')
72+
files[:] = [file for file in files if not reg.match(file)]
73+
74+
# build the `shellcheck` command
75+
shellcheck_cmd = [
76+
'shellcheck',
77+
'--external-sources',
78+
'--check-sourced',
79+
'--source-path=SCRIPTDIR',
80+
]
81+
shellcheck_cmd.append(exclude)
82+
shellcheck_cmd.extend(sourced_files)
83+
shellcheck_cmd.extend(guix_files)
84+
shellcheck_cmd.extend(files)
85+
86+
# run the `shellcheck` command
87+
try:
88+
subprocess.check_call(shellcheck_cmd)
89+
except subprocess.CalledProcessError:
90+
sys.exit(1)
91+
92+
if __name__ == '__main__':
93+
main()

test/lint/lint-shell.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)