Skip to content

Commit

Permalink
[extract_log] Improve extract_log script
Browse files Browse the repository at this point in the history
- 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 <ying.xie@microsoft.com>
  • Loading branch information
yxieca committed Apr 2, 2018
1 parent 66d6c21 commit 3c9c915
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ansible/library/extract_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3c9c915

Please sign in to comment.