Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 1 | #!/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. |
| 16 | import sys |
| 17 | |
| 18 | def 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 Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 28 | def select_failing_task_output(lines): |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 29 | tasks_of_interest = [] |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 30 | # first, find tasks of interest |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 31 | for line in lines: |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 32 | if line.startswith("Execution failed for task"): |
| 33 | tasks_of_interest.append(line.split("task '")[1][:-3]) |
| 34 | |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 35 | |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 36 | print("Detected these failing tasks: " + str(tasks_of_interest)) |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 37 | |
| 38 | # next, save all excerpts between start(interesting task) and end(interesting task) |
| 39 | current_interesting_tasks = [] |
| 40 | retained_lines = [] |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 41 | for line in lines: |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 42 | 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 Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 49 | 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 Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 54 | |
Jeff Gaston | e5b04c5 | 2020-06-04 12:38:05 -0400 | [diff] [blame] | 55 | def 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 Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 72 | def 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 Liutikas | 0a2579c | 2020-06-22 23:39:56 +0000 | [diff] [blame] | 77 | "See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings", |
Jeff Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 78 | |
| 79 | "Note: Some input files use or override a deprecated API.", |
Aurimas Liutikas | 0a2579c | 2020-06-22 23:39:56 +0000 | [diff] [blame] | 80 | "Note: Recompile with -Xlint:deprecation for details.", |
| 81 | "Note: Some input files use unchecked or unsafe operations.", |
Aurimas Liutikas | 4c03019 | 2020-07-22 16:24:22 +0000 | [diff] [blame^] | 82 | "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 Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 92 | } |
| 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 | |
| 112 | def 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 Gaston | 2069aae | 2020-06-10 15:42:11 -0400 | [diff] [blame] | 125 | # 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 Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 135 | def collapse_tasks_having_no_output(lines): |
Jeff Gaston | 2069aae | 2020-06-10 15:42:11 -0400 | [diff] [blame] | 136 | result = [] |
| 137 | pending_tasks = [] |
Jeff Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 138 | for line in lines: |
Jeff Gaston | 2069aae | 2020-06-10 15:42:11 -0400 | [diff] [blame] | 139 | is_task = line.startswith("> Task ") |
| 140 | if is_task: |
| 141 | pending_tasks.append(line) |
Jeff Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 142 | 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 Gaston | 2069aae | 2020-06-10 15:42:11 -0400 | [diff] [blame] | 148 | 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 Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 156 | result.append(line) |
Jeff Gaston | 2069aae | 2020-06-10 15:42:11 -0400 | [diff] [blame] | 157 | return result |
| 158 | |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 159 | try: |
| 160 | build_log_loc = sys.argv[1] |
Jeff Gaston | e5b04c5 | 2020-06-04 12:38:05 -0400 | [diff] [blame] | 161 | |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 162 | infile = open(build_log_loc) |
| 163 | lines = infile.readlines() |
| 164 | infile.close() |
Jeff Gaston | e5b04c5 | 2020-06-04 12:38:05 -0400 | [diff] [blame] | 165 | |
| 166 | lines = select_failing_task_output(lines) |
| 167 | lines = shorten_uninteresting_stack_frames(lines) |
Jeff Gaston | 9d82a8b | 2020-06-12 12:19:00 -0400 | [diff] [blame] | 168 | lines = remove_known_uninteresting_lines(lines) |
| 169 | lines = collapse_consecutive_blank_lines(lines) |
| 170 | lines = collapse_tasks_having_no_output(lines) |
Jeff Gaston | e5b04c5 | 2020-06-04 12:38:05 -0400 | [diff] [blame] | 171 | |
| 172 | print(len(lines)) |
| 173 | print(''.join(lines)) |
Owen Gray | b371f27 | 2020-04-17 18:41:03 +0000 | [diff] [blame] | 174 | except Exception as e: |
| 175 | print("An error occurred! "+str(e)) |
Jeff Gaston | 76b5a24 | 2020-06-04 12:31:13 -0400 | [diff] [blame] | 176 | usage() |