diff options
author | Neil <nyamatongwe@gmail.com> | 2021-02-01 17:31:58 +1100 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2021-02-01 17:31:58 +1100 |
commit | 20bdc47bf1ece6f32b104e4ecc64abd88f5bd8ab (patch) | |
tree | d8a7ed2264cc6ef3ca6ed1efbf4bbd2afed9caf6 /scripts/HeaderCheck.py | |
parent | 219092af51fa15d813a0374227ce9baf8d53d239 (diff) | |
download | scintilla-mirror-20bdc47bf1ece6f32b104e4ecc64abd88f5bd8ab.tar.gz |
Move the patterns to check and exclude to HeaderOrder.txt so one script can be
used for Scintilla, Lexilla, and SciTE.
Update HeaderOrder.txt to match files moved out to Lexilla.
Diffstat (limited to 'scripts/HeaderCheck.py')
-rw-r--r-- | scripts/HeaderCheck.py | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/scripts/HeaderCheck.py b/scripts/HeaderCheck.py index 29dc4fba3..1349c3b20 100644 --- a/scripts/HeaderCheck.py +++ b/scripts/HeaderCheck.py @@ -1,23 +1,9 @@ #!/usr/bin/env python3 # Script to check that headers are in a consistent order -# Canonical header order is defined in scripts/HeaderOrder.txt +# Canonical header order is defined in a file, normally scripts/HeaderOrder.txt # Requires Python 3.6 or later -import pathlib - -patterns = [ - "include/*.h", - "src/*.cxx", - "lexlib/*.cxx", - "lexers/*.cxx", - "win32/*.cxx", - "gtk/*.cxx", - "cocoa/*.mm", - "cocoa/*.h", - "test/unit/*.cxx", - "lexilla/src/*.cxx", - "lexilla/test/*.cxx", -] +import pathlib, sys def IsHeader(x): return x.strip().startswith("#") and \ @@ -28,32 +14,41 @@ def HeaderFromIncludeLine(s): #\s*#\s*(include|import)\s+\S+\s* return s.strip()[1:].strip()[7:].strip() -def ExtractHeaders(filename): - with filename.open(encoding="cp437") as infile: +def ExtractHeaders(file): + with file.open(encoding="cp437") as infile: return [HeaderFromIncludeLine(l) for l in infile if IsHeader(l)] -def ExcludeName(name): - # LexCaml adds system headers in #if to be an external lexer - # moc_ files are generated by Qt and follow its rules - return "LexCaml" in name or "moc_" in name +def ExtractWithPrefix(file, prefix): + with file.open(encoding="cp437") as infile: + return [l.strip()[len(prefix):] for l in infile if l.startswith(prefix)] + +def ExcludeName(name, excludes): + return any(exclude in name for exclude in excludes) def SortLike(incs, order): return sorted(incs, key = lambda i: order.index(i)) -def CheckFiles(root): - # Find all the lexer source code files +basePrefix = "//base:" +sourcePrefix = "//source:" +excludePrefix = "//exclude:" + +def CheckFiles(headerOrderTxt): + headerOrderFile = pathlib.Path(headerOrderTxt).resolve() + bases = ExtractWithPrefix(headerOrderFile, basePrefix) + base = bases[0] if len(bases) > 0 else ".." + orderDirectory = headerOrderFile.parent + root = (orderDirectory / base).resolve() + + # Find all the source code files + patterns = ExtractWithPrefix(headerOrderFile, sourcePrefix) + excludes = ExtractWithPrefix(headerOrderFile, excludePrefix) + filePaths = [] for p in patterns: filePaths += root.glob(p) - # The Qt platform code interleaves system and Scintilla headers - #~ filePaths += root.glob("qt/ScintillaEditBase/*.cpp") - #~ filePaths += root.glob("qt/ScintillaEdit/*.cpp") - #~ print(filePaths) - scriptDirectory = root / "scripts" - headerOrderFile = scriptDirectory / "HeaderOrder.txt" headerOrder = ExtractHeaders(headerOrderFile) originalOrder = headerOrder[:] - orderedPaths = [p for p in sorted(filePaths) if not ExcludeName(str(p))] + orderedPaths = [p for p in sorted(filePaths) if not ExcludeName(str(p), excludes)] allIncs = set() for f in orderedPaths: print(" File ", f.relative_to(root)) @@ -105,7 +100,7 @@ def CheckFiles(root): if headerOrder != originalOrder: newIncludes = set(headerOrder) - set(originalOrder) - headerOrderNew = scriptDirectory / "NewOrder.txt" + headerOrderNew = orderDirectory / "NewOrder.txt" print(f"{headerOrderFile}:1: changed to {headerOrderNew}") print(f" Added {', '.join(newIncludes)}.") with headerOrderNew.open("w") as headerOut: @@ -117,4 +112,7 @@ def CheckFiles(root): print("In HeaderOrder.txt but not used") print("\n".join(unused)) -CheckFiles(pathlib.Path(__file__).resolve().parent.parent) +if len(sys.argv) > 1: + CheckFiles(sys.argv[1]) +else: + CheckFiles("HeaderOrder.txt") |