Skip to content

Commit 602e47c

Browse files
[lldb] Format Python files in scripts and utils (#66053)
Using: black --exclude "third_party/" ./lldb/
1 parent a1ef5a9 commit 602e47c

17 files changed

+754
-613
lines changed

lldb/scripts/analyze-project-deps.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
from use_lldb_suite import lldb_root
1111

1212
parser = argparse.ArgumentParser(
13-
description='Analyze LLDB project #include dependencies.')
14-
parser.add_argument('--show-counts', default=False, action='store_true',
15-
help='When true, show the number of dependencies from each subproject')
16-
parser.add_argument('--discover-cycles', default=False, action='store_true',
17-
help='When true, find and display all project dependency cycles. Note,'
18-
'this option is very slow')
13+
description="Analyze LLDB project #include dependencies."
14+
)
15+
parser.add_argument(
16+
"--show-counts",
17+
default=False,
18+
action="store_true",
19+
help="When true, show the number of dependencies from each subproject",
20+
)
21+
parser.add_argument(
22+
"--discover-cycles",
23+
default=False,
24+
action="store_true",
25+
help="When true, find and display all project dependency cycles. Note,"
26+
"this option is very slow",
27+
)
1928

2029
args = parser.parse_args()
2130

@@ -24,12 +33,14 @@
2433

2534
src_map = {}
2635

27-
include_regex = re.compile('#include \"((lldb|Plugins|clang)(.*/)+).*\"')
36+
include_regex = re.compile('#include "((lldb|Plugins|clang)(.*/)+).*"')
37+
2838

2939
def is_sublist(small, big):
3040
it = iter(big)
3141
return all(c in it for c in small)
3242

43+
3344
def normalize_host(str):
3445
if str.startswith("lldb/Host"):
3546
return "lldb/Host"
@@ -39,6 +50,7 @@ def normalize_host(str):
3950
return str.replace("lldb/../../source", "lldb")
4051
return str
4152

53+
4254
def scan_deps(this_dir, file):
4355
global src_map
4456
deps = {}
@@ -62,7 +74,8 @@ def scan_deps(this_dir, file):
6274
if this_dir not in src_map and len(deps) > 0:
6375
src_map[this_dir] = deps
6476

65-
for (base, dirs, files) in os.walk(inc_dir):
77+
78+
for base, dirs, files in os.walk(inc_dir):
6679
dir = os.path.basename(base)
6780
relative = os.path.relpath(base, inc_dir)
6881
inc_files = [x for x in files if os.path.splitext(x)[1] in [".h"]]
@@ -71,7 +84,7 @@ def scan_deps(this_dir, file):
7184
inc_path = os.path.join(base, inc)
7285
scan_deps(relative, inc_path)
7386

74-
for (base, dirs, files) in os.walk(src_dir):
87+
for base, dirs, files in os.walk(src_dir):
7588
dir = os.path.basename(base)
7689
relative = os.path.relpath(base, src_dir)
7790
src_files = [x for x in files if os.path.splitext(x)[1] in [".cpp", ".h", ".mm"]]
@@ -82,6 +95,7 @@ def scan_deps(this_dir, file):
8295
scan_deps(norm_base_path, src_path)
8396
pass
8497

98+
8599
def is_existing_cycle(path, cycles):
86100
# If we have a cycle like # A -> B -> C (with an implicit -> A at the end)
87101
# then we don't just want to check for an occurrence of A -> B -> C in the
@@ -90,12 +104,13 @@ def is_existing_cycle(path, cycles):
90104
# at the end), then A -> B -> C is also a cycle. This is an important
91105
# optimization which reduces the search space by multiple orders of
92106
# magnitude.
93-
for i in range(0,len(path)):
107+
for i in range(0, len(path)):
94108
if any(is_sublist(x, path) for x in cycles):
95109
return True
96110
path = [path[-1]] + path[0:-1]
97111
return False
98112

113+
99114
def expand(path_queue, path_lengths, cycles, src_map):
100115
# We do a breadth first search, to make sure we visit all paths in order
101116
# of ascending length. This is an important optimization to make sure that
@@ -127,54 +142,57 @@ def expand(path_queue, path_lengths, cycles, src_map):
127142
path_queue.append(cur_path + [item])
128143
pass
129144

145+
130146
cycles = []
131147

132148
path_queue = [[x] for x in iter(src_map)]
133149
path_lens = [1] * len(path_queue)
134150

135151
items = list(src_map.items())
136-
items.sort(key = lambda A : A[0])
152+
items.sort(key=lambda A: A[0])
137153

138-
for (path, deps) in items:
154+
for path, deps in items:
139155
print(path + ":")
140156
sorted_deps = list(deps.items())
141157
if args.show_counts:
142-
sorted_deps.sort(key = lambda A: (A[1], A[0]))
158+
sorted_deps.sort(key=lambda A: (A[1], A[0]))
143159
for dep in sorted_deps:
144160
print("\t{} [{}]".format(dep[0], dep[1]))
145161
else:
146-
sorted_deps.sort(key = lambda A: A[0])
162+
sorted_deps.sort(key=lambda A: A[0])
147163
for dep in sorted_deps:
148164
print("\t{}".format(dep[0]))
149165

166+
150167
def iter_cycles(cycles):
151168
global src_map
152169
for cycle in cycles:
153170
cycle.append(cycle[0])
154171
zipper = list(zip(cycle[0:-1], cycle[1:]))
155-
result = [(x, src_map[x][y], y) for (x,y) in zipper]
172+
result = [(x, src_map[x][y], y) for (x, y) in zipper]
156173
total = 0
157174
smallest = result[0][1]
158-
for (first, value, last) in result:
175+
for first, value, last in result:
159176
total += value
160177
smallest = min(smallest, value)
161178
yield (total, smallest, result)
162179

180+
163181
if args.discover_cycles:
164182
print("Analyzing cycles...")
165183

166184
expand(path_queue, path_lens, cycles, src_map)
167185

168-
average = sum([len(x)+1 for x in cycles]) / len(cycles)
186+
average = sum([len(x) + 1 for x in cycles]) / len(cycles)
169187

170188
print("Found {} cycles. Average cycle length = {}.".format(len(cycles), average))
171189
counted = list(iter_cycles(cycles))
172190
if args.show_counts:
173-
counted.sort(key = lambda A: A[0])
174-
for (total, smallest, cycle) in counted:
191+
counted.sort(key=lambda A: A[0])
192+
for total, smallest, cycle in counted:
175193
sys.stdout.write("{} deps to break: ".format(total))
176194
sys.stdout.write(cycle[0][0])
177-
for (first, count, last) in cycle:
195+
for first, count, last in cycle:
178196
sys.stdout.write(" [{}->] {}".format(count, last))
179197
sys.stdout.write("\n")
180198
else:
@@ -186,8 +204,8 @@ def iter_cycles(cycles):
186204
islands = []
187205
outgoing_counts = defaultdict(int)
188206
incoming_counts = defaultdict(int)
189-
for (total, smallest, cycle) in counted:
190-
for (first, count, last) in cycle:
207+
for total, smallest, cycle in counted:
208+
for first, count, last in cycle:
191209
outgoing_counts[first] += count
192210
incoming_counts[last] += count
193211
for cycle in cycles:
@@ -201,8 +219,8 @@ def iter_cycles(cycles):
201219
sorted = []
202220
for node in island:
203221
sorted.append((node, incoming_counts[node], outgoing_counts[node]))
204-
sorted.sort(key = lambda x: x[1]+x[2])
205-
for (node, inc, outg) in sorted:
222+
sorted.sort(key=lambda x: x[1] + x[2])
223+
for node, inc, outg in sorted:
206224
print(" {} [{} in, {} out]".format(node, inc, outg))
207225
sys.stdout.flush()
208226
pass

0 commit comments

Comments
 (0)