Skip to content

Commit 8865080

Browse files
committed
Add utility for trailing whitespaces
1 parent 570ba59 commit 8865080

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

.github/workflows/website-checks.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ on:
77
workflow_dispatch:
88

99
jobs:
10+
trailing-whitespaces-check:
11+
name: Trailing whitespaces check
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v6
15+
- name: Analysing the trailing whitespaces
16+
run: ./bin/trailing_spaces --Werror $(git ls-files 'content/*.md')
17+
1018
links-check:
1119
name: Links check using linkchecker
1220
runs-on: ubuntu-latest

bin/trailing_spaces

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

0 commit comments

Comments
 (0)