dist/tools/headerguards: fix flake8 issues
This commit is contained in:
parent
9acf7c96af
commit
cfe2885720
15
dist/tools/headerguards/headerguards.py
vendored
15
dist/tools/headerguards/headerguards.py
vendored
@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os, sys
|
import os
|
||||||
|
import sys
|
||||||
import difflib
|
import difflib
|
||||||
#from string import maketrans
|
|
||||||
from io import BytesIO, TextIOWrapper
|
from io import BytesIO, TextIOWrapper
|
||||||
|
|
||||||
_in = "/-."
|
_in = "/-."
|
||||||
@ -10,12 +10,14 @@ _out = "___"
|
|||||||
|
|
||||||
transtab = str.maketrans(_in, _out)
|
transtab = str.maketrans(_in, _out)
|
||||||
|
|
||||||
|
|
||||||
def path_to_guardname(filepath):
|
def path_to_guardname(filepath):
|
||||||
res = filepath.upper().translate(transtab)
|
res = filepath.upper().translate(transtab)
|
||||||
if res.startswith("_"):
|
if res.startswith("_"):
|
||||||
res = "PRIV" + res
|
res = "PRIV" + res
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def get_guard_name(filepath):
|
def get_guard_name(filepath):
|
||||||
parts = filepath.split(os.sep)
|
parts = filepath.split(os.sep)
|
||||||
start = 0
|
start = 0
|
||||||
@ -31,6 +33,7 @@ def get_guard_name(filepath):
|
|||||||
|
|
||||||
return path_to_guardname(os.path.join(*parts[start:]))
|
return path_to_guardname(os.path.join(*parts[start:]))
|
||||||
|
|
||||||
|
|
||||||
def fix_headerguard(filename):
|
def fix_headerguard(filename):
|
||||||
supposed = get_guard_name(filename)
|
supposed = get_guard_name(filename)
|
||||||
with open(filename, "r", encoding='utf-8', errors='ignore') as f:
|
with open(filename, "r", encoding='utf-8', errors='ignore') as f:
|
||||||
@ -42,7 +45,7 @@ def fix_headerguard(filename):
|
|||||||
guard_found = 0
|
guard_found = 0
|
||||||
guard_name = ""
|
guard_name = ""
|
||||||
ifstack = 0
|
ifstack = 0
|
||||||
for n, line in enumerate(inlines):
|
for line in inlines:
|
||||||
if guard_found == 0:
|
if guard_found == 0:
|
||||||
if line.startswith("#ifndef"):
|
if line.startswith("#ifndef"):
|
||||||
guard_found += 1
|
guard_found += 1
|
||||||
@ -68,16 +71,18 @@ def fix_headerguard(filename):
|
|||||||
|
|
||||||
tmp.seek(0)
|
tmp.seek(0)
|
||||||
if guard_found == 3:
|
if guard_found == 3:
|
||||||
for line in difflib.unified_diff(inlines, tmp.readlines(), "%s" % filename, "%s" % filename):
|
for line in difflib.unified_diff(inlines, tmp.readlines(),
|
||||||
|
"%s" % filename, "%s" % filename):
|
||||||
sys.stdout.write(line)
|
sys.stdout.write(line)
|
||||||
else:
|
else:
|
||||||
print("%s: no / broken header guard" % filename, file=sys.stderr)
|
print("%s: no / broken header guard" % filename, file=sys.stderr)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
error = False
|
error = False
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
if fix_headerguard(filename) == False:
|
if fix_headerguard(filename) is False:
|
||||||
error = True
|
error = True
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user