-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindsrc.py
executable file
·129 lines (96 loc) · 3.71 KB
/
findsrc.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
import sys
import argparse
from pathlib import Path
from collections.abc import Generator, Sequence
class HeaderNotFoundError (Exception):
""" Exception to warn about header not found """
def __init__ (self, header: str, fname: Path, line: int) -> None:
""" Constructs HeaderNotFound exception """
super().__init__(f"Header {header} not found at {fname}@{line}.")
def fetch_headers (fname: Path) -> Generator[Path, None]:
""" Fetch all headers from a file """
# Gets file directory
fdir = fname.parent
lineno = 0
with fname.open("rt") as file:
# Iterates over trimmed lines
for line in map(str.lstrip, file):
lineno += 1
if not line.startswith("#include"):
continue
# Removes #include
line = line[ 8 : ].lstrip()
# Removes comments
# NOTE only single line comments suported
comm = line.find("//")
if comm != -1:
line = line[ : comm ]
line = line.rstrip()
# Gets only headers between quotes (assumes that they are local)
if line[0] == "\"" and line[-1] == "\"":
# Removes quotes
hname = line[ 1 : -1 ]
# Appends file directory to header location
header = fdir / hname
if not header.is_file():
raise HeaderNotFoundError(hname, fname, lineno)
yield header
# Build argument parser
argparser = argparse.ArgumentParser(prog=Path(__file__).name)
argparser.add_argument("files", type=Path, nargs="+", help="Starting files.")
argparser.add_argument(
"-he", "--headers", default=None, nargs="+", help="Supported header extensions."
)
argparser.add_argument(
"-se", "--sources", default=[ "cc", "cpp" ], nargs="+", help="Supported source extensions."
)
argparser.add_argument("-i", "--ignore", type=Path, default=[], nargs="+", help="Files to ignore.")
argparser.add_argument(
"-b", "--basepath", type=Path, default=Path.cwd(), help="Base directory for files."
)
argparser.add_argument(
"-p", "--print-headers", action="store_true", help="Also print headers alongsize results."
)
def main (argv: Sequence[str]) -> None:
""" Main function """
# Parse arguments
args = argparser.parse_args(argv)
headers: frozenset[str] = frozenset(args.headers) if args.headers else None
sources: frozenset[str] = frozenset(ext.lstrip(".") for ext in args.sources)
result: list[Path] = []
files: list[Path] = list(args.files)
found: set[Path] = { ign.relative_to(args.basepath) for ign in args.ignore }
# Iterate over files
while files:
fname = files.pop().resolve().relative_to(args.basepath)
# Ignore duplicate files
if fname in found:
continue
# Test if file is valid
found.add(fname)
ext = fname.suffix.lstrip(".")
# Add file to found sources
if ext in sources:
result.append(fname)
# Skips unsupported headers
elif headers is not None and ext not in headers:
continue
else:
if args.print_headers:
result.append(fname)
# Fetch sources by name
for ext in sources:
sname = fname.with_suffix(f".{ext}")
if sname.is_file():
files.append(sname)
try:
# Fetch headers from file
files.extend(fetch_headers(fname))
except HeaderNotFoundError as e:
print(e, file=sys.stderr)
exit(1)
# Print results
print(" ".join(map(str, result)))
if __name__ == "__main__":
main(sys.argv[ 1 : ])