From 3c9c91543b742084db149aa7b6612df7f9b4f183 Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Mon, 2 Apr 2018 07:24:40 -0700 Subject: [PATCH 1/3] [extract_log] Improve extract_log script - Some log entries has '\x00' sub-strings in front of time stamp, these sub strings are messing up with the comparator, adding a pre-handling to remove them. - Replace sort O(n x log(n)) with a look up (O(n)). Signed-off-by: Ying Xie --- ansible/library/extract_log.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ansible/library/extract_log.py b/ansible/library/extract_log.py index d415097199..7ffba97c66 100644 --- a/ansible/library/extract_log.py +++ b/ansible/library/extract_log.py @@ -90,9 +90,16 @@ def extract_line(directory, filename, target_string): file = gzip.GzipFile(path) else: file = open(path) - result = None + result = [] with file: - result = [(filename, line) for line in file if target_string in line and 'nsible' not in line] + for line in file: + # This might be a gunzip file issue, there has been '\x00's in front of the + # log entry timestamp which messes up with the comparator. + # Prehandle lines to remove these sub-strings + line = line.replace('\x00', '') + if target_string in line and 'nsible' not in line: + result.append((filename, line)) + return result @@ -159,9 +166,12 @@ def extract_latest_line_with_string(directory, filenames, start_string): for filename in filenames: target_lines.extend(extract_line(directory, filename, start_string)) - sorted_target_lines = sorted(target_lines, cmp=comparator) + target = target_lines[0] if len(target_lines) > 0 else None + for line in target_lines: + if comparator(line, target) > 0: + target = line - return sorted_target_lines[-1] + return target def calculate_files_to_copy(filenames, file_with_latest_line): From 149108684bfd0dc7efbc276a936163d052079357 Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Tue, 3 Apr 2018 05:23:44 -0700 Subject: [PATCH 2/3] Combine pre-handling code with original code --- ansible/library/extract_log.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ansible/library/extract_log.py b/ansible/library/extract_log.py index 7ffba97c66..e95af169f2 100644 --- a/ansible/library/extract_log.py +++ b/ansible/library/extract_log.py @@ -92,13 +92,11 @@ def extract_line(directory, filename, target_string): file = open(path) result = [] with file: - for line in file: - # This might be a gunzip file issue, there has been '\x00's in front of the - # log entry timestamp which messes up with the comparator. - # Prehandle lines to remove these sub-strings - line = line.replace('\x00', '') - if target_string in line and 'nsible' not in line: - result.append((filename, line)) + # This might be a gunzip file or logrotate issue, there has + # been '\x00's in front of the log entry timestamp which + # messes up with the comparator. + # Prehandle lines to remove these sub-strings + result = [(filename, line.replace('\x00', '')) for line in file if target_string in line and 'nsible' not in line] return result From 82b9c0540cdb5038cf30a3c47d9eb3d8ea0615d4 Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Tue, 3 Apr 2018 08:36:33 -0700 Subject: [PATCH 3/3] Address review issue. --- ansible/library/extract_log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/library/extract_log.py b/ansible/library/extract_log.py index e95af169f2..65828f23be 100644 --- a/ansible/library/extract_log.py +++ b/ansible/library/extract_log.py @@ -90,7 +90,7 @@ def extract_line(directory, filename, target_string): file = gzip.GzipFile(path) else: file = open(path) - result = [] + result = None with file: # This might be a gunzip file or logrotate issue, there has # been '\x00's in front of the log entry timestamp which