-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathrm_whitespace.py
executable file
·70 lines (62 loc) · 1.91 KB
/
rm_whitespace.py
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
#!/usr/bin/env python3
# Copyright (c) 2018, Niklas Hauser
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import fnmatch
import subprocess
import multiprocessing
from pathlib import Path
def repopath(path):
return (Path(os.path.abspath(__file__)).parents[2] / path)
def run(where, command, stdin=None):
print(command)
result = subprocess.run(command, shell=True, cwd=where, input=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return (result.returncode,
result.stdout.decode("utf-8").strip(" \n"),
result.stderr.decode("utf-8").strip(" \n"))
ignored = [
".git*",
"*.png",
"*.svg",
"*.css",
"*.dot",
"*.pbm",
"*.font",
"ext/arm/cmsis/CMSIS",
"ext/dlr/scons-build-tools",
"ext/fatfs*",
"ext/aws/freertos*",
"ext/adamgreen/crashcatcher",
"ext/gcc/libstdc++",
"ext/modm-devices",
"ext/nxp*",
"ext/ros/ros-lib",
"ext/st/stm32*",
"ext/tlsf*",
]
all_files = run(repopath("."), "git ls-files")[1].splitlines()
filtered = []
markdowns = []
for ffile in all_files:
if fnmatch.fnmatch(ffile, "*.md*"):
markdowns.append(ffile)
continue
if any(fnmatch.fnmatch(ffile, i) for i in ignored):
continue
filtered.append(ffile)
print("#Files: ", len(filtered))
# Remove all trailing whitespace for regular files
run(repopath("."), r"xargs -0 -n1 sed -i 's/\s*$//'", stdin="\0".join(filtered).encode("utf-8"))
# But for markdown files, only remove non-double space trailing whitespace
run(repopath("."), r"xargs -0 -n1 sed -i 's/(?! $)\s*$//'", stdin="\0".join(markdowns).encode("utf-8"))
differences = run(repopath("."), r"git diff")[1]
if len(differences):
subprocess.run("git --no-pager diff", shell=True, cwd=repopath("."))
print("\n\nPlease remove all trailing whitespace!\n\n")
exit(1)
exit(0)