Skip to content

Instantly share code, notes, and snippets.

@nikhilo
Created July 19, 2017 03:23
Show Gist options
  • Save nikhilo/c69071cd871e8a2c393f8d60c324f99a to your computer and use it in GitHub Desktop.
Save nikhilo/c69071cd871e8a2c393f8d60c324f99a to your computer and use it in GitHub Desktop.
Travis log analyzer
import re, json, datetime
from sys import argv,exit
def parse_log(filename):
try:
fh = open(filename)
except Exception as e:
print "Error in opening file: %s" % str(e)
lines = fh.readlines()
fh.close()
# Sanitize travis log
lines = [ re.sub(r'\[0K', '', line) for line in lines ]
tasks = {}
for i in range(0, len(lines)-1):
startpat = re.compile("travis_time:start:([a-z\d]+)")
matchObj = re.match(startpat, lines[i])
if matchObj:
# We found a task being started, create a dict in task array
tasks[matchObj.group(1)] = {}
tasks[matchObj.group(1)]['command'] = lines[i+1]
endpat = re.compile("travis_time:end:([a-z\d]+).*duration=(\d+)")
matchObj = re.match(endpat, lines[i])
if matchObj:
# We found a task being completed, add completion stats in the dict
seconds = long(matchObj.group(2)) / 10**9
# Only if the task took more than 5s
if (seconds < 5 ):
del tasks[matchObj.group(1)]
next
else:
tasks[matchObj.group(1)]['duration'] = str(datetime.timedelta(seconds=int(seconds)))
print json.dumps(tasks)
if __name__ == "__main__":
if (len(argv) != 2):
print "Usage: python travis_log.py <filename>"
exit(1)
parse_log(str(argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment