kyonos n00b
Joined: 04 Nov 2002 Posts: 48
|
Posted: Fri Feb 14, 2003 5:26 am Post subject: extracting time elapsed from emerge.log for merged packages |
|
|
Hi,
This is an util to get the time elapsed from each merged package from emerge.log. Also this is my first python script. It "seems" to work for me.
A copy of emerge.log should be in the same dir. To execute:
> ploc emerge.log
Code: |
#!/usr/bin/python
#
# name: ploc -- process log
# description: analyze a copy of emerge.log to get the duration to
# emerge a package
#
import string
import sys
fn = sys.argv [1]
op =''
if (len(sys.argv[1:]) > 0):
op = sys.argv [1]
file=open (fn, 'r')
else:
print "need a log file name\n"
sys.exit(0)
def parse_line (il):
# only get lines start with
# ***, >>>, :::
# ignore the rest
l= string.split (il)
time = l[0][:-1]
token = l[1]
result = ''
if (token == '***' and len(l) > 3):
# some case nothing after emerge
# so len 3 filter it out
tok = l[3] #name of package
# ignore all these cases
keys= ['search','rsync','clean',
'clean', '--searchdesc',
'umerge','sync']
if (l[2] == 'emerge' and (tok not in keys)):
if (tok in ['--update', '--emptytree']):
name = l[4]
else:
name = l[3]
return [time,'*', name, 'started']
elif (l[2] == 'exiting'):
return [time,'*', 'ended']
else:
return ['ignore']
elif (l[1] == '>>>'):
if (l[2] == 'emerge'):
return [time,'>' , l[6], 'started']
else:
return ['ignore']
elif (l[1] == ':::'):
return [time, ':', 'ended']
else:
return ['ignore']
def t_xtract (l):
# get the time from a list
return l[0]
def tsec_elapse (start, stop):
# time elapsed
return (int(stop) - int(start))
def t_format (seconds):
# convert seconds into h:m:s
MIN = 60
HR = 60 * MIN
h = m = s = 0
if (seconds >= HR):
h = seconds / HR
mins = seconds % HR
if (mins > MIN):
m = mins / MIN
s = mins % MIN
return [h, m, s]
def process (s,l):
# state machine to process token
token = l[1]
l2=''
elapse =''
global state, stck
if (s == 0 and token =='*'):
stck.append(l)
state = 1
finish = 0
elif (s == 1 and token == '>'):
stck.append(l)
state = 2
finish = 0
elif (s == 2 and token == ':'):
l2=stck[-1]
stck = stck[0:-1]
state = 1
finish = 1
elapse = t_format (tsec_elapse (t_xtract(l2), t_xtract(l)))
elif (s == 1 and token == '*'):
l2=stck[-1]
stck = stck[0:-1]
state = 0
finish = 1
elapse = t_format (tsec_elapse (t_xtract(l2), t_xtract(l)))
else:
state = s
return [3, elapse, l, l2]
result = [finish, elapse, l, l2]
return result
def f_print (name, l):
print "%-40s emerged in %d:%d:%d" % (name,
tmpstr[1][0],
tmpstr[1][1],
tmpstr[1][2])
def getname (l):
if (tmpstr[2][2] == 'ended'):
name = tmpstr[3][2]
else:
name = tmpstr[2][2]
return name
# main
lines = file.readlines()
stck=[]
tmpstr=''
state = 0
for line in lines:
ml = parse_line (line)
#print "%s ---> %s" % (line, ml)
if (ml[0] != 'ignore'):
tmpstr = process (state, ml)
if (tmpstr[0] == 1):
name = getname(tmpstr)
f_print (name, tmpstr)
|
|
|