1# Copyright 2022 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Extracts a concise error from a bazel log.""" 15 16from pathlib import Path 17import re 18import sys 19 20 21def parse_bazel_stdout(bazel_stdout: Path) -> str: 22 """Extracts a concise error from a bazel log.""" 23 seen_error = False 24 error_lines = [] 25 26 with bazel_stdout.open() as ins: 27 for line in ins: 28 # Trailing whitespace isn't significant, as it doesn't affect the 29 # way the line shows up in the logs. However, leading whitespace may 30 # be significant, especially for compiler error messages. 31 line = line.rstrip() 32 33 if re.match(r'^ERROR:', line): 34 seen_error = True 35 36 if seen_error: 37 # Ignore long lines that just show the environment. 38 if re.search(r' +[\w_]*(PATH|PWD)=.* \\', line): 39 continue 40 41 # Ignore lines that only show bazel sandboxing. 42 if line.strip().startswith(('(cd /', 'exec env')): 43 continue 44 45 if line.strip().startswith('Use --sandbox_debug to see'): 46 continue 47 48 if line.strip().startswith('# Configuration'): 49 continue 50 51 # An "<ALLCAPS>:" line usually means compiler output is done 52 # and useful compiler errors are complete. 53 if re.match(r'^(?!ERROR)[A-Z]+:', line): 54 break 55 56 error_lines.append(line) 57 58 result = '\n'.join(error_lines) 59 return re.sub(r'\n+', '\n', result) 60 61 62if __name__ == '__main__': 63 print(parse_bazel_stdout(Path(sys.argv[1]))) 64