Skip to content

Commit 777b89b

Browse files
committed
Merge bitcoin/bitcoin#24929: lint: convert shell locale linter test to Python
2c838cc lint: convert shell locale linter test to Python (Eunoia) Pull request description: Refs #24783 ACKs for top commit: laanwj: Code review ACK 2c838cc Tree-SHA512: 3cb5e7c7cd2acbdf0dc45096055b33cbfa0ec9e47ea567452d23a49a7441b3b62a8416879f234459c86fa892c42205c91d8a575115346c023ab0152cf713e20c
2 parents 8b68677 + 2c838cc commit 777b89b

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

test/lint/lint-shell-locale.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
Make sure all shell scripts are:
9+
a.) explicitly opt out of locale dependence using
10+
"export LC_ALL=C" or "export LC_ALL=C.UTF-8", or
11+
b.) explicitly opt in to locale dependence using the annotation below.
12+
"""
13+
14+
import subprocess
15+
import sys
16+
import re
17+
18+
OPT_IN_LINE = '# This script is intentionally locale dependent by not setting \"export LC_ALL=C\"'
19+
20+
OPT_OUT_LINES = [
21+
'export LC_ALL=C',
22+
'export LC_ALL=C.UTF-8',
23+
]
24+
25+
def get_shell_files_list():
26+
command = [
27+
'git',
28+
'ls-files',
29+
'--',
30+
'*.sh',
31+
]
32+
try:
33+
return subprocess.check_output(command, stderr = subprocess.STDOUT).decode('utf-8').splitlines()
34+
except subprocess.CalledProcessError as e:
35+
if e.returncode > 1: # return code is 1 when match is empty
36+
print(e.output.decode('utf-8'), end='')
37+
sys.exit(1)
38+
return []
39+
40+
def main():
41+
exit_code = 0
42+
shell_files = get_shell_files_list()
43+
for file_path in shell_files:
44+
if re.search('src/(secp256k1|minisketch|univalue)/', file_path):
45+
continue
46+
47+
with open(file_path, 'r', encoding='utf-8') as file_obj:
48+
contents = file_obj.read()
49+
50+
if OPT_IN_LINE in contents:
51+
continue
52+
53+
non_comment_pattern = re.compile(r'^\s*((?!#).+)$', re.MULTILINE)
54+
non_comment_lines = re.findall(non_comment_pattern, contents)
55+
if not non_comment_lines:
56+
continue
57+
58+
first_non_comment_line = non_comment_lines[0]
59+
if first_non_comment_line not in OPT_OUT_LINES:
60+
print(f'Missing "export LC_ALL=C" (to avoid locale dependence) as first non-comment non-empty line in {file_path}')
61+
exit_code = 1
62+
63+
return sys.exit(exit_code)
64+
65+
if __name__ == '__main__':
66+
main()
67+

test/lint/lint-shell-locale.sh

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

0 commit comments

Comments
 (0)