|
| 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 | + |
0 commit comments