2019-11-22 03:24:31 +03:00
|
|
|
#!/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##
|
|
|
|
# check_indents.py: If a file has both space and tab indenting, returns an exit
|
|
|
|
# code and normalizes \t to four spaces.
|
|
|
|
##
|
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
##
|
|
|
|
|
|
|
|
import re
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
def find_whitespace(string : str) -> Tuple[str, str]:
|
|
|
|
groups = re.match(r'(\s*)([^\s].*$)?', string, flags=re.S).groups()
|
|
|
|
return groups[0], groups[1] or ''
|
|
|
|
|
|
|
|
def check_file(filename : str) -> bool:
|
|
|
|
"""
|
|
|
|
Checks a single file, returning True and writing a cleaned file if a mix
|
|
|
|
of tabs and spaces was found.
|
|
|
|
"""
|
|
|
|
problem_found = False
|
|
|
|
found_spaces = False
|
|
|
|
found_tabs = False
|
|
|
|
|
2020-01-18 04:42:31 +03:00
|
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
2019-11-22 03:24:31 +03:00
|
|
|
contents = list(f.readlines())
|
|
|
|
|
|
|
|
for line in contents:
|
|
|
|
# Find the leading whitespace.
|
|
|
|
whitespace, rest = find_whitespace(line)
|
|
|
|
found_spaces = found_spaces or (' ' in whitespace)
|
|
|
|
found_tabs = found_tabs or ('\t' in whitespace)
|
|
|
|
|
|
|
|
problem_found = found_spaces and found_tabs
|
|
|
|
|
|
|
|
if problem_found:
|
|
|
|
print(f"Found mixed spaces and tabs in {filename}.")
|
|
|
|
# Time to normalize!
|
2020-05-13 01:37:13 +03:00
|
|
|
with open(filename, 'w', encoding='utf-8') as f:
|
2019-11-22 03:24:31 +03:00
|
|
|
f.writelines(
|
|
|
|
whitespace.replace('\t', ' ') + rest
|
|
|
|
for whitespace, rest in map(find_whitespace, contents)
|
|
|
|
)
|
|
|
|
|
|
|
|
return problem_found
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
|
|
filenames = sys.argv[1:]
|
|
|
|
exit_code = 0
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
if check_file(filename):
|
|
|
|
exit_code = -1
|
|
|
|
|
|
|
|
sys.exit(exit_code)
|