k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/regex.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 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 17 import re 18 19 20 # Match a specific word 21 def wordRE(word): 22 return re.compile(r'\b(%s)\b' % word, re.IGNORECASE) 23 24 # Match lines with error messages 25 # HACK: match ANSI colored lines by allowing preceding "m", 26 # as in"\x1b[0;31mFAILED\x1b[0m" 27 28 default_words = [ 29 "build timed out", 30 "error", 31 "fail", 32 "failed", 33 "fatal", 34 "undefined", 35 "panic:", 36 ] 37 38 error_re = re.compile( 39 r'(?:\b|(?<=m))(%s)\b' % '|'.join(default_words), re.IGNORECASE) 40 41 # Match the dictionary string in the given line 42 def objref(line): 43 return re.search(r'api\.ObjectReference(\{.*?"\})', line) 44 45 # Combine a list of words into one regex that match any of them 46 def combine_wordsRE(words_list): 47 return re.compile(r'\b(%s)\b' % '|'.join(words_list), re.IGNORECASE) 48 49 # Match the file name of a log given a filepath to the log 50 log_re = re.compile(r'[^/]+\.log$') 51 52 # Match the container id given a line containing the pod name 53 def containerID(line): 54 return re.search(r'ContainerID:([0-9A-Fa-f]*)', line) 55 56 def timestamp(line): 57 return re.search(r'(\d\d-?\d\d[T\s]\d\d:\d\d:\d\d\.\d+)', line) 58 59 def sub_timestamp(line): 60 return re.sub(r'(-|T|\s)', "", timestamp(line).group(0))