Skip to content

Commit b054a0d

Browse files
committed
merge bitcoin#24840: port lint-shell.sh to python
1 parent 973ca7b commit b054a0d

File tree

3 files changed

+96
-41
lines changed

3 files changed

+96
-41
lines changed

test/README.md

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

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

test/lint/lint-shell.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
'SC2046', # Quote this to prevent word splitting.
18+
'SC2086', # Double quote to prevent globbing and word splitting.
19+
'SC2162', # read without -r will mangle backslashes.
20+
]
21+
22+
def check_shellcheck_install():
23+
try:
24+
subprocess.run(['shellcheck', '--version'], stdout=subprocess.DEVNULL, check=True)
25+
except FileNotFoundError:
26+
print('Skipping shell linting since shellcheck is not installed.')
27+
sys.exit(0)
28+
29+
def get_files(command):
30+
output = subprocess.run(command, stdout=subprocess.PIPE, universal_newlines=True)
31+
files = output.stdout.split('\n')
32+
33+
# remove whitespace element
34+
files = list(filter(None, files))
35+
return files
36+
37+
def main():
38+
check_shellcheck_install()
39+
40+
# build the `exclude` flag
41+
exclude = '--exclude=' + ','.join(DISABLED)
42+
43+
# build the `sourced files` list
44+
sourced_files_cmd = [
45+
'git',
46+
'grep',
47+
'-El',
48+
r'^# shellcheck shell=',
49+
]
50+
sourced_files = get_files(sourced_files_cmd)
51+
52+
# build the `guix files` list
53+
guix_files_cmd = [
54+
'git',
55+
'grep',
56+
'-El',
57+
r'^#!\/usr\/bin\/env bash',
58+
'--',
59+
'contrib/guix',
60+
'contrib/shell',
61+
]
62+
guix_files = get_files(guix_files_cmd)
63+
64+
# build the other script files list
65+
files_cmd = [
66+
'git',
67+
'ls-files',
68+
'--',
69+
'*.sh',
70+
]
71+
files = get_files(files_cmd)
72+
# remove everything that doesn't match this regex
73+
reg = re.compile(r'src/[dashbls,immer,leveldb,secp256k1,minisketch,univalue]')
74+
files[:] = [file for file in files if not reg.match(file)]
75+
76+
# build the `shellcheck` command
77+
shellcheck_cmd = [
78+
'shellcheck',
79+
'--external-sources',
80+
'--check-sourced',
81+
'--source-path=SCRIPTDIR',
82+
]
83+
shellcheck_cmd.append(exclude)
84+
shellcheck_cmd.extend(sourced_files)
85+
shellcheck_cmd.extend(guix_files)
86+
shellcheck_cmd.extend(files)
87+
88+
# run the `shellcheck` command
89+
try:
90+
subprocess.check_call(shellcheck_cmd)
91+
except subprocess.CalledProcessError:
92+
sys.exit(1)
93+
94+
if __name__ == '__main__':
95+
main()

test/lint/lint-shell.sh

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

0 commit comments

Comments
 (0)