-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrailing_spaces
executable file
·74 lines (58 loc) · 2.56 KB
/
trailing_spaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# Copyright (C) The DDC development team, see COPYRIGHT.md file
#
# SPDX-License-Identifier: MIT
import argparse
import os
import re
import sys
def find_trailing_whitespaces(files):
"""
Find all lines with trailing whitespaces in C++ and CMake files within the given directory.
Args:
directory (str): The directory to search for files.
Returns:
list: A list of tuples containing the filename, line number, and line content with trailing whitespaces.
"""
files_with_trailing_whitespaces = []
# Check each file for trailing whitespaces
for filename in files:
with open(filename, 'r', encoding='utf-8') as file:
for line_number, line in enumerate(file, 1):
if re.search(r'[ \t]+$', line):
files_with_trailing_whitespaces.append((filename, line_number, line.rstrip()))
return files_with_trailing_whitespaces
def remove_trailing_whitespaces(files_with_trailing_whitespaces):
"""
Remove trailing whitespaces from the specified files.
Args:
files_with_trailing_whitespaces (list): A list of tuples containing the filename, line number,
and line content with trailing whitespaces.
"""
for filename, _, _ in files_with_trailing_whitespaces:
with open(filename, 'r', encoding='utf-8') as file:
lines = file.readlines()
with open(filename, 'w', encoding='utf-8') as file:
for line in lines:
file.write(re.sub(r'[ \t]+$', '', line))
# print(f"Removed trailing whitespaces from: {filename}")
def main():
"""
The main function to parse arguments and find/remove trailing whitespaces.
"""
parser = argparse.ArgumentParser(description="Find and optionally remove trailing whitespaces in given files.")
parser.add_argument('file', type=str, nargs='+', help="File to analyze.")
parser.add_argument('-i', action='store_true', help="Remove trailing whitespaces.")
parser.add_argument('--Werror', action='store_true', help="If set, treat warnings as errors")
args = parser.parse_args()
files_with_trailing_whitespaces = find_trailing_whitespaces(args.file)
if args.i:
remove_trailing_whitespaces(files_with_trailing_whitespaces)
else:
if files_with_trailing_whitespaces:
for file, line_number, line in files_with_trailing_whitespaces:
print(f"{file}:{line_number}:\n{line}")
if args.Werror:
sys.exit(1)
if __name__ == "__main__":
main()