-
Notifications
You must be signed in to change notification settings - Fork 0
/
deplist.py
executable file
·621 lines (532 loc) · 20.3 KB
/
deplist.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/bin/sh
# Copyright (c) 2018-2024, MPI-M
#
# Author: Sergey Kosukhin <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
""":"
for cmd in python3 python; do
if command -v > /dev/null "${cmd}"; then
exec "${cmd}" "$0" "$@"
fi
done
echo "Error: could not find a python interpreter!" >&2
exit 1
":"""
import argparse
import collections
import fnmatch
import itertools
import os
import re
import sys
_re_rule = re.compile(
r"^[ ]*(?P<targets>[^:|#\s]+(?:[ ]+[^:|#\s]+)*)[ ]*"
r":[ ]*(?P<normal>[^:|#\s]+(?:[ ]+[^:|#\s]+)*)?[ ]*"
r"\|?[ ]*(?P<order_only>[^:|#\s]+(?:[ ]+[^:|#\s]+)*)?[ ]*"
r"(?:#-hint)?[ ]*(?P<hint>[^:|#\s]+(?:[ ]+[^:|#\s]+)*)?[ ]*"
)
_meta_root = 0
_term_colors = {
"black": 90,
"red": 91,
"green": 92,
"yellow": 93,
"blue": 94,
"magenta": 95,
"cyan": 96,
"white": 97,
}
def parse_args():
class ArgumentParser(argparse.ArgumentParser):
def convert_arg_line_to_args(self, arg_line):
try:
# Drop everything after the first occurrence of #:
arg_line = arg_line[: arg_line.index("#")]
except ValueError:
pass
result = []
# Do not regard consecutive whitespaces as a single separator:
for arg in arg_line.split(" "):
if arg:
result.append(arg)
elif result:
# The previous argument has a significant space:
result[-1] += " "
return result
parser = ArgumentParser(
fromfile_prefix_chars="@",
description="Reads a set of MAKEFILEs and prints a topologically "
"sorted list of TARGETs (PREREQuisites) together with their "
"dependencies (dependents).",
)
parser.add_argument(
"-d", "--debug-file", help="dump debug information to DEBUG_FILE"
)
parser.add_argument(
"-t",
"--target",
nargs="*",
help="names of the makefile targets to be printed together with their "
"dependencies; mutually exclusive with the argument '-p/--prereq'; if "
"neither of the arguments is specified, all targets and prerequisites "
"found in the makefiles are sent to the output",
)
parser.add_argument(
"-p",
"--prereq",
nargs="*",
help="names of the makefile prerequisites to be printed together with "
"their dependents; mutually exclusive with the argument '-t/--target'; "
"if neither of the arguments is specified, all targets and "
"prerequisites found in the makefiles are sent to the output",
)
parser.add_argument(
"--max-depth",
metavar="MAX_DEPTH",
type=int,
help="print dependencies (dependents) that are at most MAX_DEPTH "
"levels from the requested targets (prerequisites)",
)
parser.add_argument(
"--ignore-order-only",
"--no-oo",
action="store_true",
help="ignore order-only prerequisites",
)
parser.add_argument(
"--ignore-hints",
"--no-hints",
action="store_true",
help="ignore #-hint prerequisites",
)
parser.add_argument(
"-r",
"--reverse",
action="store_true",
help="print the output list in the reversed order",
)
parser.add_argument(
"--check-unique-prereq",
action="append",
# Unfortunately, we cannot set nargs to 'two or more', therefore we
# set nargs to 'one or more':
nargs="+",
metavar="PATTERN",
help="list of two or more shell-like wildcards; the option enables "
"additional consistency checks of the dependency graph: each target "
"that matches the first pattern of the list is checked whether it has "
"no more than one prerequisite matching any of the rest of the "
"patterns; if the check fails, a warning message is emitted to the "
"standard error stream",
)
parser.add_argument(
"--check-unique-basename",
action="append",
nargs="+",
metavar="PATTERN",
help="list of shell-like wildcards; the option enables additional "
"consistency checks of the dependency graph; all targets that match at "
"least one of the patterns are checked whether none of them have the "
"same basename; if the check fails, a warning message is emitted to "
"the standard error stream",
)
parser.add_argument(
"--check-exists-prereq",
action="append",
# Unfortunately, we cannot set nargs to 'two or more', therefore we
# set nargs to 'one or more':
nargs="+",
metavar="PATTERN",
help="list of two or more shell-like wildcards; the option enables "
"additional consistency checks of the dependency graph: each target "
"that matches the first pattern of the list is checked whether it has "
"at least one prerequisite matching any of the rest of the patterns; "
"if the check fails, a warning message is emitted to the standard "
"error stream",
)
parser.add_argument(
"--check-cycles",
action="store_true",
help="check whether the dependency graph is acyclic, e.g. there is no "
"circular dependencies; if a cycle is found, a warning message is "
"emitted to the standard output",
)
parser.add_argument(
"--check-colour",
choices=_term_colors.keys(),
help="colour the message output of the checks using ANSI escape "
"sequences; the argument is ignored if the standard error stream is "
"not associated with a terminal device",
)
parser.add_argument(
"-f",
"--makefile",
nargs="*",
help="paths to makefiles; a single dash (-) triggers reading from the "
"standard input stream",
)
args = parser.parse_args()
if args.target is not None and args.prereq is not None:
parser.error(
"arguments -t/--target and -p/--prereq are mutually " "exclusive"
)
if args.max_depth is not None and args.max_depth < 0:
args.max_depth = None
if args.check_unique_prereq:
for pattern_list in args.check_unique_prereq:
if len(pattern_list) < 2:
parser.error(
"argument --check-unique-prereq: expected 2 or "
"more arguments"
)
if args.check_exists_prereq:
for pattern_list in args.check_exists_prereq:
if len(pattern_list) < 2:
parser.error(
"argument --check-exists-prereq: expected 2 or "
"more arguments"
)
if not sys.stderr.isatty():
args.check_colour = None
return args
def read_makefiles(makefiles, ignore_order_only, ignore_hints):
dep_graph = collections.defaultdict(list)
extra_edges = collections.defaultdict(list)
for mkf in makefiles:
if mkf == "-":
stream = sys.stdin
elif not os.path.isfile(mkf):
continue
else:
stream = open(mkf, "r")
it = iter(stream)
for line in it:
while line.endswith("\\\n"):
line = line[:-2]
try:
line += next(it)
except StopIteration:
break
match = _re_rule.match(line)
if match:
targets = set(match.group("targets").split())
prereqs = []
prereqs_string = match.group("normal")
if prereqs_string:
prereqs.extend(prereqs_string.split())
if not ignore_order_only:
prereqs_string = match.group("order_only")
if prereqs_string:
prereqs.extend(prereqs_string.split())
for target in targets:
dep_graph[target].extend(prereqs)
if not ignore_hints:
prereqs_string = match.group("hint")
if prereqs_string:
for target in targets:
extra_edges[target].extend(prereqs_string.split())
stream.close()
return dep_graph, extra_edges
def visit_dfs(
dep_graph,
vertex,
current_depth=0,
max_depth=None,
visited=None,
start_visit_cb_list=None,
finish_visit_cb_list=None,
skip_visit_cb_list=None,
):
if max_depth is not None and current_depth > max_depth:
return
if visited is None:
visited = dict()
if vertex in visited:
if skip_visit_cb_list:
for skip_visit_cb in skip_visit_cb_list:
skip_visit_cb(vertex)
return
if start_visit_cb_list:
for start_visit_cb in start_visit_cb_list:
start_visit_cb(vertex)
visited[vertex] = current_depth
if vertex in dep_graph:
for child in dep_graph[vertex]:
visit_dfs(
dep_graph,
child,
current_depth + 1,
max_depth,
visited,
start_visit_cb_list,
finish_visit_cb_list,
skip_visit_cb_list,
)
if finish_visit_cb_list:
for finish_visit_cb in finish_visit_cb_list:
finish_visit_cb(vertex)
def dedupe(sequence):
seen = set()
for x in sequence:
if x not in seen:
yield x
seen.add(x)
def sanitize_graph(graph):
# Remove duplicates (we do not use sets as values of the dictionary to keep
# the order of prerequisites):
for target in graph.keys():
graph[target] = graph.default_factory(
dedupe(prereq for prereq in graph[target])
)
# Make leaves (i.e. prerequisites without any prerequisites) explicit nodes
# of the graph:
leaves = set(
prereq
for prereqs in graph.values()
for prereq in prereqs
if prereq not in graph
)
graph.update((prereq, graph.default_factory()) for prereq in leaves)
def flip_edges(graph):
result = collections.defaultdict(list)
for parent, children in graph.items():
for child in children:
result[child].append(parent)
else:
_ = result[parent]
return result
def warn(msg, colour=None):
sys.stderr.write(
"{0}{1}: WARNING: {2}{3}\n".format(
("\033[{0}m".format(_term_colors[colour])) if colour else "",
os.path.basename(__file__),
msg,
"\033[0m" if colour else "",
)
)
def main():
args = parse_args()
if args.debug_file:
with open(args.debug_file, "w") as debug_file:
debug_file.writelines(
[
"# Python version: ",
sys.version.replace("\n", " "),
"\n",
"#\n",
"# Command:\n",
"# ",
" ".join(sys.argv),
"\n",
"#\n",
"# Parsed arguments:\n",
"# ",
"\n# ".join(
[k + "=" + str(v) for k, v in vars(args).items()]
),
"\n",
]
)
if args.makefile is None:
return
dep_graph, extra_edges = read_makefiles(
args.makefile, args.ignore_order_only, args.ignore_hints
)
if not dep_graph:
return
sanitize_graph(dep_graph)
sanitize_graph(extra_edges)
if args.prereq is None:
traversed_graph = dep_graph
start_nodes = args.target
else:
traversed_graph = flip_edges(dep_graph)
start_nodes = args.prereq
extra_edges = flip_edges(extra_edges)
# Insert _meta_root, which will be the starting-point for the dependency
# graph traverse:
if start_nodes is None:
traversed_graph[_meta_root] = sorted(traversed_graph.keys())
else:
traversed_graph[_meta_root] = [
t for t in start_nodes if t in traversed_graph
]
# Visitor callbacks:
start_visit_cb_list = []
finish_visit_cb_list = []
skip_visit_cb_list = []
# Callbacks that are called once the graph is traversed:
postprocess_cb_list = []
if args.check_unique_prereq:
def check_unique_prereq_start_visit_cb(vertex):
# Skip if the vertex is _meta_root or does not have descendants:
if vertex == _meta_root:
return
for pattern_list in args.check_unique_prereq:
if fnmatch.fnmatch(vertex, pattern_list[0]):
vertex_prereqs = dep_graph[vertex]
prereq_patterns = pattern_list[1:]
matching_prereqs = [
prereq
for prereq_pattern in prereq_patterns
for prereq in fnmatch.filter(
vertex_prereqs, prereq_pattern
)
]
if len(matching_prereqs) > 1:
warn(
"target '{0}' has more than one immediate "
"prerequisite matching any of the patterns: "
"'{1}':\n\t{2}".format(
vertex,
"', '".join(prereq_patterns),
"\n\t".join(matching_prereqs),
),
args.check_colour,
)
start_visit_cb_list.append(check_unique_prereq_start_visit_cb)
if args.check_unique_basename:
basenames = [
collections.defaultdict(set)
for _ in range(len(args.check_unique_basename))
]
def check_unique_basename_start_visit_cb(vertex):
# Skip if the vertex is _meta_root:
if vertex == _meta_root:
return
for i, pattern_list in enumerate(args.check_unique_basename):
for pattern in pattern_list:
if fnmatch.fnmatch(vertex, pattern):
basenames[i][os.path.basename(vertex)].add(vertex)
start_visit_cb_list.append(check_unique_basename_start_visit_cb)
def check_unique_basename_postprocess_cb():
for basename_group in basenames:
for basename, paths in basename_group.items():
if len(paths) > 1 and basename:
warn(
"the dependency graph contains more than one "
"target with basename '{0}':\n\t{1}".format(
basename, "\n\t".join(paths)
),
args.check_colour,
)
postprocess_cb_list.append(check_unique_basename_postprocess_cb)
if args.check_exists_prereq:
def check_exists_prereq_start_visit_cb(vertex):
# Skip if the vertex is _meta_root:
if vertex == _meta_root:
return
for pattern_list in args.check_exists_prereq:
if fnmatch.fnmatch(vertex, pattern_list[0]):
vertex_prereqs = dep_graph.get(vertex, set())
prereq_patterns = pattern_list[1:]
if not any(
fnmatch.filter(vertex_prereqs, prereq_pattern)
for prereq_pattern in prereq_patterns
):
warn(
"target '{0}' does not have an immediate "
"prerequisite matching any of the patterns: "
"'{1}'".format(
vertex, "', '".join(prereq_patterns)
),
args.check_colour,
)
start_visit_cb_list.append(check_exists_prereq_start_visit_cb)
if args.check_cycles:
path = []
def check_cycles_start_visit_cb(vertex):
path.append(vertex)
def check_cycles_skip_visit_cb(vertex):
if vertex in path:
start_cycle_idx = path.index(vertex)
if args.prereq is None:
msg_lines = (
path[1:start_cycle_idx]
+ [path[start_cycle_idx] + " <- start of cycle"]
+ path[start_cycle_idx + 1 :]
+ [vertex + " <- end of cycle"]
)
else:
msg_lines = (
[vertex + " <- start of cycle"]
+ path[-1:start_cycle_idx:-1]
+ [path[start_cycle_idx] + " <- end of cycle"]
+ path[start_cycle_idx - 1 : 0 : -1]
)
warn(
"the dependency graph has a cycle:\n"
"\t{0}".format("\n\t".join(msg_lines)),
args.check_colour,
)
def check_cycles_finish_visit_cb(_):
path.pop()
start_visit_cb_list.append(check_cycles_start_visit_cb)
skip_visit_cb_list.append(check_cycles_skip_visit_cb)
finish_visit_cb_list.append(check_cycles_finish_visit_cb)
toposort = []
def toposort_finish_visit_cb(vertex):
toposort.append(vertex)
def toposort_postprocess_cb():
# The last element of toposort is _meta_root:
toposort.pop()
finish_visit_cb_list.append(toposort_finish_visit_cb)
postprocess_cb_list.append(toposort_postprocess_cb)
visited_vertices = dict()
def traverse(start_depth=-1):
visit_dfs(
traversed_graph,
_meta_root,
current_depth=start_depth,
max_depth=args.max_depth,
visited=visited_vertices,
start_visit_cb_list=start_visit_cb_list,
finish_visit_cb_list=finish_visit_cb_list,
skip_visit_cb_list=skip_visit_cb_list,
)
for postprocess_cb in postprocess_cb_list:
postprocess_cb()
traverse()
# Add the extra prerequisites to the graph:
for target, prereqs in extra_edges.items():
traversed_graph[target] = traversed_graph.default_factory(
dedupe(itertools.chain(traversed_graph[target], prereqs))
)
for target, prereqs in extra_edges.items():
target_depth = visited_vertices.get(target, None)
if target_depth is None:
continue
# Reset the _meta_root and traverse the graph:
visited_vertices.pop(_meta_root)
traversed_graph[_meta_root] = prereqs
traverse(target_depth)
if args.reverse ^ (args.prereq is not None):
toposort.reverse()
print("\n".join(toposort))
if __name__ == "__main__":
main()