1#!/usr/bin/env python3 2 3import argparse 4import pathlib 5import re 6 7 8def detect_misleading_indentation( 9 toml_path: str, 10 toml_lines: list[str], 11) -> bool: 12 issue_detected = False 13 previous_indentation = 0 14 for line_number, line in enumerate(toml_lines, start=1): 15 if match := re.match(r'^(\s*)\S', line): 16 line_indentation = len(match.group(1)) 17 if line_indentation < previous_indentation: 18 # Allow de-indenting when starting a new section (`[`) or 19 # terminating a multi-line list (`]`) 20 if not re.match(r'^\s*(\[|\])', line): 21 print(f'{toml_path}:{line_number}: ' 22 f'Misleading indentation found') 23 issue_detected = True 24 else: 25 line_indentation = 0 26 previous_indentation = line_indentation 27 28 return issue_detected 29 30 31def main(): 32 parser = argparse.ArgumentParser() 33 parser.add_argument( 34 'toml_files', 35 type=pathlib.Path, 36 nargs=argparse.ZERO_OR_MORE, 37 help='*.toml files to lint (default: src/**/ci/*.toml)', 38 ) 39 40 args = parser.parse_args() 41 42 if not args.toml_files: 43 args.toml_files = pathlib.Path('src').glob('**/ci/*.toml') 44 45 error = False 46 47 for path in args.toml_files: 48 with path.open('r') as toml_file: 49 toml_lines = toml_file.readlines() 50 if detect_misleading_indentation(path.as_posix(), toml_lines): 51 error = True 52 53 if error: 54 exit(1) 55 56 57if __name__ == '__main__': 58 main() 59