[go: nahoru, domu]

blob: 49afe1d6548c026ba1425a79a8d94f270adbd2b3 [file] [log] [blame]
Owen Grayb371f272020-04-17 18:41:03 +00001#!/usr/bin/python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import sys
17
18def usage():
19 print("""USAGE:
20 Simplifies a build.log from hundreds of megabytes to <100 lines. Prints output to terminal.
21 Pass this script a filepath to parse. You should be able to type "python3 build_log_simplifier.py"
22 And then drag-and-drop a log file onto the terminal window to get its path.
23
24 Sample usage: python3 development/build_log_simplifier.py Users/owengray/Desktop/build.log
25 """)
26 exit(1)
27
Jeff Gaston76b5a242020-06-04 12:31:13 -040028def select_failing_task_output(lines):
Owen Grayb371f272020-04-17 18:41:03 +000029 tasks_of_interest = []
Owen Grayb371f272020-04-17 18:41:03 +000030 # first, find tasks of interest
Jeff Gaston76b5a242020-06-04 12:31:13 -040031 for line in lines:
Owen Grayb371f272020-04-17 18:41:03 +000032 if line.startswith("Execution failed for task"):
33 tasks_of_interest.append(line.split("task '")[1][:-3])
34
Owen Grayb371f272020-04-17 18:41:03 +000035
Jeff Gaston76b5a242020-06-04 12:31:13 -040036 print("Detected these failing tasks: " + str(tasks_of_interest))
Owen Grayb371f272020-04-17 18:41:03 +000037
38 # next, save all excerpts between start(interesting task) and end(interesting task)
39 current_interesting_tasks = []
40 retained_lines = []
Jeff Gaston76b5a242020-06-04 12:31:13 -040041 for line in lines:
Owen Grayb371f272020-04-17 18:41:03 +000042 if line.startswith("Task ") and line.split(" ")[1] in tasks_of_interest:
43 if line.split(" ")[-1].strip() == "Starting":
44 current_interesting_tasks.append(line.split(" ")[1])
45 elif line.split(" ")[-1].strip() == "Finished":
46 current_interesting_tasks.remove(line.split(" ")[1])
47 retained_lines.append(line)
48 if current_interesting_tasks: retained_lines.append(line)
Jeff Gaston76b5a242020-06-04 12:31:13 -040049 if retained_lines:
50 return retained_lines
51 # if no output was created by any failing tasks, then maybe there could be useful output from
52 # somewhere else
53 return lines
Owen Grayb371f272020-04-17 18:41:03 +000054
Jeff Gastone5b04c52020-06-04 12:38:05 -040055def shorten_uninteresting_stack_frames(lines):
56 result = []
57 prev_line_is_boring = False
58 for line in lines:
59 if line.startswith("\tat org.gradle"):
60 if not prev_line_is_boring:
61 result.append("\tat org.gradle...\n")
62 prev_line_is_boring = True
63 elif line.startswith("\tat java.base"):
64 if not prev_line_is_boring:
65 result.append("\tat java.base...")
66 prev_line_is_boring = True
67 else:
68 result.append(line)
69 prev_line_is_boring = False
70 return result
71
Jeff Gaston9d82a8b2020-06-12 12:19:00 -040072def remove_known_uninteresting_lines(lines):
73 skipLines = {
74 "A fine-grained performance profile is available: use the --scan option.",
75 "* Get more help at https://help.gradle.org",
76 "Use '--warning-mode all' to show the individual deprecation warnings.",
Aurimas Liutikas0a2579c2020-06-22 23:39:56 +000077 "See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings",
Jeff Gaston9d82a8b2020-06-12 12:19:00 -040078
79 "Note: Some input files use or override a deprecated API.",
Aurimas Liutikas0a2579c2020-06-22 23:39:56 +000080 "Note: Recompile with -Xlint:deprecation for details.",
81 "Note: Some input files use unchecked or unsafe operations.",
Aurimas Liutikas4c030192020-07-22 16:24:22 +000082 "Note: Recompile with -Xlint:unchecked for details.",
83
84 "w: ATTENTION!",
85 "This build uses unsafe internal compiler arguments:",
86 "-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes",
87 "-XXLanguage:-NewInference",
88 "-XXLanguage:+InlineClasses",
89 "This mode is not recommended for production use,",
90 "as no stability/compatibility guarantees are given on",
91 "compiler or generated code. Use it at your own risk!"
Jeff Gaston9d82a8b2020-06-12 12:19:00 -040092 }
93 skipPrefixes = [
94 "See the profiling report at:",
95
96 "Deprecated Gradle features were used in this build"
97 ]
98 result = []
99 for line in lines:
100 stripped = line.strip()
101 if stripped in skipLines:
102 continue
103 include = True
104 for prefix in skipPrefixes:
105 if stripped.startswith(prefix):
106 include = False
107 break
108 if include:
109 result.append(line)
110 return result
111
112def collapse_consecutive_blank_lines(lines):
113 result = []
114 prev_blank = False
115 for line in lines:
116 if line.strip() == "":
117 if not prev_blank:
118 result.append(line)
119 prev_blank = True
120 else:
121 result.append(line)
122 prev_blank = False
123 return result
124
Jeff Gaston2069aae2020-06-10 15:42:11 -0400125# If multiple tasks have no output, this function removes all but the first and last
126# For example, turns this:
127# > Task :a
128# > Task :b
129# > Task :c
130# > Task :d
131# into this:
132# > Task :a
133# > Task ...
134# > Task :d
Jeff Gaston9d82a8b2020-06-12 12:19:00 -0400135def collapse_tasks_having_no_output(lines):
Jeff Gaston2069aae2020-06-10 15:42:11 -0400136 result = []
137 pending_tasks = []
Jeff Gaston9d82a8b2020-06-12 12:19:00 -0400138 for line in lines:
Jeff Gaston2069aae2020-06-10 15:42:11 -0400139 is_task = line.startswith("> Task ")
140 if is_task:
141 pending_tasks.append(line)
Jeff Gaston9d82a8b2020-06-12 12:19:00 -0400142 elif line.strip() == "":
143 # If only blank lines occur between tasks, skip those blank lines
144 if len(pending_tasks) > 0:
145 pending_tasks.append(line)
146 else:
147 result.append(line)
Jeff Gaston2069aae2020-06-10 15:42:11 -0400148 else:
149 if len(pending_tasks) > 0:
150 result += pending_tasks[0]
151 if len(pending_tasks) > 2:
152 result += "> Task ...\n"
153 if len(pending_tasks) > 1:
154 result += pending_tasks[-1]
155 pending_tasks = []
Jeff Gaston9d82a8b2020-06-12 12:19:00 -0400156 result.append(line)
Jeff Gaston2069aae2020-06-10 15:42:11 -0400157 return result
158
Jeff Gaston76b5a242020-06-04 12:31:13 -0400159try:
160 build_log_loc = sys.argv[1]
Jeff Gastone5b04c52020-06-04 12:38:05 -0400161
Jeff Gaston76b5a242020-06-04 12:31:13 -0400162 infile = open(build_log_loc)
163 lines = infile.readlines()
164 infile.close()
Jeff Gastone5b04c52020-06-04 12:38:05 -0400165
166 lines = select_failing_task_output(lines)
167 lines = shorten_uninteresting_stack_frames(lines)
Jeff Gaston9d82a8b2020-06-12 12:19:00 -0400168 lines = remove_known_uninteresting_lines(lines)
169 lines = collapse_consecutive_blank_lines(lines)
170 lines = collapse_tasks_having_no_output(lines)
Jeff Gastone5b04c52020-06-04 12:38:05 -0400171
172 print(len(lines))
173 print(''.join(lines))
Owen Grayb371f272020-04-17 18:41:03 +0000174except Exception as e:
175 print("An error occurred! "+str(e))
Jeff Gaston76b5a242020-06-04 12:31:13 -0400176 usage()