-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtergraph.py
executable file
·418 lines (354 loc) · 13.7 KB
/
filtergraph.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
"""Filter a TGF graph generated by includegraph.py.
There are two kinds of rules you can use to query the dependency graph.
1. (--filter) You can filter out specific subtrees that match a given glob
2. (--keep-only) You can filter out everything _except_ subtrees that match a given glob
These two options can be given multiple times. If both --keep-only and --filter globs are given, the
--filter globs are applied after the --keep-only globs.
"""
import argparse
import collections
import fnmatch
import functools
import itertools
import logging
import sys
from pathlib import Path, PurePath
from typing import Callable, Dict, Iterable, List, Set, Tuple
# This is kind of hacky, but there's two other options:
# 1. duplicate the shared stuff and hope they stay in sync
# 2. add an includegraph library, and require it gets installed in order to use the scripts
# I don't like the first option because it's a maintenance nightmare, but I also don't like the
# second option because it increases the friction to use these tools.
repo_root = Path(__file__).resolve().parent
repo_root = str(repo_root)
sys.path.insert(0, repo_root)
try:
from includegraph import output_dep_graph_tgf
from tgf2graphviz import IncludeGraph, IncludeGraphNode, parse_tgf_graph
except ImportError:
logging.critical("Failed to import types from includegraph.py.")
raise
LOG_LEVELS = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
}
DEFAULT_LEVEL = "INFO"
def parse_args():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"--input",
"-i",
type=argparse.FileType("r"),
default=sys.stdin,
help="The path to the input graph. Defaults to stdin.",
)
parser.add_argument(
"--output",
"-o",
type=argparse.FileType("w"),
default=sys.stdout,
help="The file to save the output to. Defaults to stdout.",
)
parser.add_argument(
"--log-level",
"-l",
type=str,
default=DEFAULT_LEVEL,
choices=LOG_LEVELS.keys(),
help=f"Set the logging output level. Defaults to {DEFAULT_LEVEL}.",
)
parser.add_argument(
"--shorten-file-paths",
"-s",
action="store_true",
default=False,
help="Shorten absolute node file paths",
)
parser.add_argument(
"--filter-transitive-system-headers",
action="store_true",
default=False,
help="Remove system headers included by another system header.",
)
parser.add_argument(
"--filter-system-headers",
action="store_true",
default=False,
help="Remove all system headers from the graph.",
)
parser.add_argument(
"--filter",
"-f",
type=str,
action="append",
default=[],
help="Remove subtrees where the root nodes match the given filepath glob(s). Applied after any --keep-only globs, if any are present.",
)
parser.add_argument(
"--keep-only",
"-k",
type=str,
action="append",
default=[],
help="Keep only subtrees where the root node matches the given filepath glob(s)",
)
return parser.parse_args()
def map_basenames_to_absolute(paths: Iterable[str]) -> Dict[str, Set[str]]:
"""Map the file basenames to their absolute paths.
Example input:
/a/b/c.h
/a/b/d.h
/a/c/c.h
Example output:
c.h -> {/a/b/c.h, /a/c/c.h}
d.h -> {/a/b/d.h, }
A helper for shorten_absolute_paths.
"""
mapping = collections.defaultdict(set)
for path in paths:
path = PurePath(path)
mapping[path.name].add(str(path))
return mapping
def all_equal(s: Iterable) -> bool:
"""Determine if every element of the given iterable are equal."""
g = itertools.groupby(s)
return next(g, True) and not next(g, False)
def shortest_unique_suffixes(paths: Set[str]) -> Dict[str, str]:
"""Find the shortest unique suffix for each of the given strings.
Example input:
{/a/b/c.h, /a/c/c.h}
Example output:
/a/b/c.h -> b/c.h
/a/c/c.h -> c/c.h
"""
paths = list(paths) # need deterministic ordering, so no set for you.
path_parts = (PurePath(p) for p in paths)
path_parts = (reversed(p.parts) for p in path_parts)
path_parts = zip(*path_parts)
# Start at the end:
# 0. (c.h, c.h) # Equal, continue
# 1. (b/, c/) # Not equal, break
# This results in:
# [(c.h, c.h), (b/, c/)]
suffixes = []
for level in path_parts:
# create the suffix for each path
suffixes.append(level)
if len(level) == 1 or not all_equal(level):
break
# Then we take
# [(c.h, c.h), (b/, c/)]
# and prepend the levels to generate
# (b/c.h, c/c.h)
def prepend_levels(level1: Tuple[str], level2: Tuple[str]) -> Tuple[str]:
return tuple(PurePath(l2) / l1 for l1, l2 in zip(level1, level2))
suffixes = functools.reduce(prepend_levels, suffixes)
suffixes = (str(s) for s in suffixes)
suffixes = dict(zip(paths, suffixes))
return suffixes
def shorten_absolute_paths(paths: Iterable[str]) -> Dict[str, str]:
"""Shorten the given absolute paths into the shortest unique suffix.
Example input:
/a/b/c.h
/a/b/d.h
/a/c/c.h
Example output:
/a/b/c.h -> b/c.h
/a/b/d.h -> d.h
/a/c/c.h -> c/c.h
That is, the returned dictionary maps the absolute paths to their shortened form.
"""
suffixes = {}
# Determine if there are multiple occurrences of the same header
multiple_occurrences = map_basenames_to_absolute(paths)
for basename, occurrences in multiple_occurrences.items():
# Nominal case. There's no need to find the shortest suffix.
if len(occurrences) == 1:
absolute = occurrences.pop()
suffix = basename
suffixes[absolute] = suffix
else:
suffixes.update(shortest_unique_suffixes(occurrences))
return suffixes
def matches_globs(s: str, patterns: List[str]) -> bool:
for pattern in patterns:
if fnmatch.fnmatch(s, pattern):
return True
return False
def bfs(
graph: IncludeGraph,
source: IncludeGraphNode,
visitor: Callable[IncludeGraphNode, Set[IncludeGraphNode]],
):
"""Perform a breadth first search of the given graph starting at 'node'.
Calls 'visitor' on each node visited. The visitor returns the node's children. This is useful as
a mechanism to influence the graph traversal (e.g., early exit)
"""
visited = set()
queue = collections.deque([source])
while queue:
current = queue.popleft()
visited.add(current)
children = visitor(graph, current)
for child in children:
if child not in visited:
queue.append(child)
visited.add(child)
def dfs(
graph: IncludeGraph,
source: IncludeGraphNode,
visitor: Callable[IncludeGraphNode, Set[IncludeGraphNode]],
):
"""Perform a depth first search of the given graph starting at 'node'.
Calls 'visitor' on each node visited. The visitor returns the node's children. This is useful as
a mechanism to influence the graph traversal (e.g., early exit)
"""
visited = set()
# With the iterative algorithm, the only difference between DFS and BFS is it uses a stack
# instead of a queue.
stack = [source]
while stack:
current = stack.pop()
visited.add(current)
children = visitor(graph, current)
for child in children:
if child not in visited:
stack.append(child)
visited.add(child)
def filter_graph(
graph: IncludeGraph,
filter_globs: List[str],
filter_system_headers=False,
filter_transitive_system_headers=False,
) -> IncludeGraph:
"""Filter the given graph by a list of filter globs.
* Add metadata to each node when parsing the graph
* number of in-edges
* Build a set of unvisited nodes
* Search for nodes matching the filter pattern
* Pick an unvisited node
* optimization - pick a node with zero in-edges
* BFS, look for nodes that match the filter pattern
* If a matching node was found
* BFS
* Remove any node with less than 2 in-edges
* Early return (one stack frame) if all adjacent edges have at least 2 in-edges
"""
unvisited_nodes = set(graph.keys())
nodes_to_delete = set()
def remove_if_not_included_by_something_else(
graph: IncludeGraph, node: IncludeGraphNode
) -> bool:
# If multiple nodes include this one, we can't remove it, or any of its children
# Additionally, since we're iterating over the graph as we're removing nodes, we should skip
# anything that's already been removed.
if (
not matches_globs(node.filename, filter_globs) and node.num_in_edges > 1
) or node not in graph:
logging.debug("\t\t\tcan't remove %s", node)
return set()
logging.debug("\t\t\tRemoving %s", node)
children = graph[node]
nodes_to_delete.add(node)
return children
def remove_subtree_of_matching_node(node: IncludeGraphNode):
logging.debug("\t\tRemoving subtree for node %s", node)
# Do another BFS search starting from this node, removing each visited node, if it wasn't
# included by another node.
bfs(graph, node, remove_if_not_included_by_something_else)
while nodes_to_delete:
node = nodes_to_delete.pop()
for child in graph[node]:
child.num_in_edges -= 1
del graph[node]
def remove_nodes_matching_glob(
graph: IncludeGraph, node: IncludeGraphNode
) -> Set[IncludeGraphNode]:
# Mark this node as visited
unvisited_nodes.discard(node)
matches_glob = matches_globs(node.filename, filter_globs)
system_header = filter_system_headers and node.is_system_header
transitive_system_header = (
filter_transitive_system_headers
and node.is_system_header
# TODO: Make this is_transitive_system_header
and not node.is_first_level_system_header
)
if matches_glob or system_header or transitive_system_header:
remove_subtree_of_matching_node(node)
return graph.get(node, set())
while unvisited_nodes:
# This is how you have to iterate over a set that changes size. Unfortunately though, it
# introduces randomness.
root = unvisited_nodes.pop()
unvisited_nodes.add(root)
# From this root, look for nodes that match any of our filters
bfs(graph, root, remove_nodes_matching_glob)
for source, targets in graph.items():
graph[source] = set(t for t in targets if t in graph)
return graph
def recalculate_in_edges(graph: IncludeGraph) -> IncludeGraph:
"""Recalculate the number of in-edges for each node."""
nodes = {}
for node in graph.keys():
node.num_in_edges = 0
nodes[node.filename] = node
for node in graph:
for target in graph[node]:
nodes[target.filename].num_in_edges += 1
return graph
def filter_all_except(graph: IncludeGraph, exclusion_globs: List[str]) -> IncludeGraph:
"""Filter everything except subtrees where the root matches some exclusion pattern."""
nodes_to_keep = set()
unvisited_nodes = set(graph.keys())
def mark_as_keep(graph: IncludeGraph, node: IncludeGraphNode) -> Set[IncludeGraphNode]:
unvisited_nodes.discard(node)
nodes_to_keep.add(node)
return graph[node]
while unvisited_nodes:
node = unvisited_nodes.pop()
if matches_globs(node.filename, exclusion_globs):
bfs(graph, node, mark_as_keep)
graph = {n: t for n, t in graph.items() if n in nodes_to_keep}
for source, targets in graph.items():
graph[source] = set(t for t in targets if t in graph)
# The number of in-edges is calculated during graph parsing, but it's necessary to be correct
# for filter_graph() to work, so if we modify the graph, we need to update the edge count.
graph = recalculate_in_edges(graph)
return graph
def main(args):
graph: IncludeGraph = parse_tgf_graph(args.input)
initial_size = len(graph)
logging.info("Parsed %d nodes", initial_size)
if args.keep_only:
logging.info("Filtering everything except the given globs...")
graph = filter_all_except(graph, args.keep_only)
logging.info("Removed %d nodes", initial_size - len(graph))
if args.filter or args.filter_system_headers or args.filter_transitive_system_headers:
logging.info("Filtering graph...")
graph = filter_graph(
graph, args.filter, args.filter_system_headers, args.filter_transitive_system_headers
)
logging.info("Removed %d nodes", initial_size - len(graph))
if args.shorten_file_paths:
logging.info("Shortening absolute file paths...")
paths = [node.filename for node in graph]
shortened_filenames = shorten_absolute_paths(paths)
for node in graph:
shortened_filename = shortened_filenames.get(node.filename, node.filename)
node.filename = shortened_filename
output_dep_graph_tgf(graph, args.output)
if __name__ == "__main__":
args = parse_args()
logging.basicConfig(
format="%(asctime)s - %(module)s - %(levelname)s - %(message)s",
level=LOG_LEVELS.get(args.log_level),
stream=sys.stderr,
)
main(args)